I will be taking a look at this soon (though may be weekend as
real life is interfering :))...
-Tom
On Wed, 15 Feb 2006, Tim Azzopardi defenestrated me:
> If anybody is interested...
>
> The attached patch is a first stab at a pluggable ENV implementation with
> reasonable default implementations.
>
> The Java5 System.getenv method is tried first by reflection and then if its
> unavailable it falls through to the "set/env > ~/.jrubyenv" method. (Only
> jruby.bat and jruby.sh have been modified for this)
>
> I've tested with Java5 and Java 1.4.2 on Windows XP sp2 and on linux with
> Java 1.4.2 and it works 100% in those environments.
>
> I've tested the pluggable implementation mechanism to force a specific
> method. (E.g. -Djruby.env.method=org.jruby.OSEnvironmentFromFile) which
> seems to work ok too.
>
> I haven't implemented a Runtime.exec solution because Neil's point put me
> right off. Maybe this could be the last resort fallback. (TODO Its easy
> enough to write a thin wrapper around the ant Execute.getProcEnvironment()
> (which is a a Runtime.exec solution under the covers) to see how well this
> works.)
>
> Unresolved issues:
> What to do about the ENV['HOME'] which is set from
> System.getProperty("user.home")
>
> Error handling as ever is a bit of a pain. How verbose should it be. I've
> erred on the side of failing silently.
>
> How to unit test this stuff is not obvious as it is all fundamentally
> environment dependent.
>
> On holiday to for 5 days so communication may be slow.
>
> Cheers
> Tim
>
>
>
>
>
>
>
>
>
>
>
>
> Index: src/org/jruby/OSEnvironmentFromFile.java
> ===================================================================
> RCS file: src/org/jruby/OSEnvironmentFromFile.java
> diff -N src/org/jruby/OSEnvironmentFromFile.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ src/org/jruby/OSEnvironmentFromFile.java 1 Jan 1970 00:00:00 -0000
> @@ -0,0 +1,105 @@
> +/***** BEGIN LICENSE BLOCK *****
> + * Version: CPL 1.0/GPL 2.0/LGPL 2.1
> + *
> + * The contents of this file are subject to the Common Public
> + * License Version 1.0 (the "License"); you may not use this file
> + * except in compliance with the License. You may obtain a copy of
> + * the License at http://www.eclipse.org/legal/cpl-v10.html
> + *
> + * Software distributed under the License is distributed on an "AS
> + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
> + * implied. See the License for the specific language governing
> + * rights and limitations under the License.
> + *
> + * Copyright (C) 2006 Tim Azzopardi <[EMAIL PROTECTED]>
> + *
> + * Alternatively, the contents of this file may be used under the terms of
> + * either of the GNU General Public License Version 2 or later (the "GPL"),
> + * or the GNU Lesser General Public License Version 2.1 or later (the
> "LGPL"),
> + * in which case the provisions of the GPL or the LGPL are applicable instead
> + * of those above. If you wish to allow use of your version of this file only
> + * under the terms of either the GPL or the LGPL, and not to allow others to
> + * use your version of this file under the terms of the CPL, indicate your
> + * decision by deleting the provisions above and replace them with the notice
> + * and other provisions required by the GPL or the LGPL. If you do not delete
> + * the provisions above, a recipient may use your version of this file under
> + * the terms of any one of the CPL, the GPL or the LGPL.
> + ***** END LICENSE BLOCK *****/
> +
> +package org.jruby;
> +
> +import java.io.BufferedReader;
> +import java.io.File;
> +import java.io.FileNotFoundException;
> +import java.io.FileReader;
> +import java.io.IOException;
> +import java.util.HashMap;
> +import java.util.Map;
> +
> +public class OSEnvironmentFromFile implements IOSEnvironment {
> +
> + private static final String JRUBY_ENVFILE = "jruby.envfile";
> +
> + protected void warn(String warning) {
> + throw new EnvironmentAccessExcepton(warning);
> + }
> +
> + /* (non-Javadoc)
> + * @see org.jruby.IOSEnvironment#isAccessible()
> + */
> + public boolean isAccessible() {
> + String jrubyEnvFilename = System.getProperty(JRUBY_ENVFILE);
> +
> + if (jrubyEnvFilename == null || jrubyEnvFilename.length() < 1) {
> + return false;
> + }
> +
> + File jrubyEnvFile = new File(jrubyEnvFilename);
> +
> + return (jrubyEnvFile.exists() && !jrubyEnvFile.isDirectory() &&
> jrubyEnvFile.canRead());
> + }
> +
> + /* (non-Javadoc)
> + * @see org.jruby.IOSEnvironment#getVariables()
> + */
> + public Map getVariables() {
> + String jrubyEnvFilename = System.getProperty(JRUBY_ENVFILE);
> +
> + HashMap envs = new HashMap();
> +
> + if (jrubyEnvFilename == null || jrubyEnvFilename.length() < 1) {
> + warn("Property " + JRUBY_ENVFILE + " not defined.");
> + } else {
> + BufferedReader jrubyEnvFile = null;
> + try {
> + jrubyEnvFile = new BufferedReader(new
> FileReader(jrubyEnvFilename));
> + try {
> + String line, envVarName, envVarValue;
> + int equalsPos;
> + while ((line = jrubyEnvFile.readLine()) != null) {
> + equalsPos = line.indexOf('=');
> + if (equalsPos >= 1) {
> + envVarName = line.substring(0, equalsPos);
> + envVarValue = line.substring(equalsPos + 1);
> + envs.put(envVarName, envVarValue);
> + }
> + }
> + } catch (IOException e) {
> + envs = null;
> + warn(e.getMessage() + " reading " + jrubyEnvFilename);
> + } finally {
> + try {
> + jrubyEnvFile.close();
> + } catch (IOException e) {
> + envs = null;
> + warn(e.getMessage() + " closing " +
> jrubyEnvFilename);
> + }
> + }
> + } catch (FileNotFoundException e) {
> + envs = null;
> + warn("File " + jrubyEnvFilename + " (jruby.envfile) does not
> exist. ");
> + }
> + }
> + return envs;
> + }
> +}
> Index: src/org/jruby/OSEnvironmentFromJava5SystemGetenv.java
> ===================================================================
> RCS file: src/org/jruby/OSEnvironmentFromJava5SystemGetenv.java
> diff -N src/org/jruby/OSEnvironmentFromJava5SystemGetenv.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ src/org/jruby/OSEnvironmentFromJava5SystemGetenv.java 1 Jan 1970
> 00:00:00 -0000
> @@ -0,0 +1,95 @@
> +/***** BEGIN LICENSE BLOCK *****
> + * Version: CPL 1.0/GPL 2.0/LGPL 2.1
> + *
> + * The contents of this file are subject to the Common Public
> + * License Version 1.0 (the "License"); you may not use this file
> + * except in compliance with the License. You may obtain a copy of
> + * the License at http://www.eclipse.org/legal/cpl-v10.html
> + *
> + * Software distributed under the License is distributed on an "AS
> + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
> + * implied. See the License for the specific language governing
> + * rights and limitations under the License.
> + *
> + * Copyright (C) 2006 Tim Azzopardi <[EMAIL PROTECTED]>
> + *
> + * Alternatively, the contents of this file may be used under the terms of
> + * either of the GNU General Public License Version 2 or later (the "GPL"),
> + * or the GNU Lesser General Public License Version 2.1 or later (the
> "LGPL"),
> + * in which case the provisions of the GPL or the LGPL are applicable instead
> + * of those above. If you wish to allow use of your version of this file only
> + * under the terms of either the GPL or the LGPL, and not to allow others to
> + * use your version of this file under the terms of the CPL, indicate your
> + * decision by deleting the provisions above and replace them with the notice
> + * and other provisions required by the GPL or the LGPL. If you do not delete
> + * the provisions above, a recipient may use your version of this file under
> + * the terms of any one of the CPL, the GPL or the LGPL.
> + ***** END LICENSE BLOCK *****/
> +
> +package org.jruby;
> +
> +import java.lang.reflect.Method;
> +import java.util.Iterator;
> +import java.util.Map;
> +
> +public class OSEnvironmentFromJava5SystemGetenv implements IOSEnvironment {
> +
> + protected void warn(String warning, Exception e) {
> + throw new EnvironmentAccessExcepton(warning + " (" +
> e.getClass().getName() + " " + e.getMessage() + ")");
> + }
> +
> + /**
> + * Gets the new Java 5 Sytem.getenv method via reflection.
> + * @return
> + */
> + protected Method getSystemGetenvMethod() {
> + Method getenvMethod = null;
> + try {
> + getenvMethod = java.lang.System.class.getMethod("getenv",
> (Class[]) null);
> + if (!getenvMethod.getReturnType().equals(java.util.Map.class)) {
> + // wrong return type
> + getenvMethod = null;
> + }
> + } catch (NoSuchMethodException e) {
> + // This is the error you get if using Java 1.4.2
> + getenvMethod = null;
> + } catch (Exception e) {
> + // Some other e.g. security problem
> + getenvMethod = null;
> + }
> + return getenvMethod;
> + }
> +
> + /* (non-Javadoc)
> + * @see org.jruby.IOSEnvironment#isAccessible()
> + */
> + public boolean isAccessible() {
> + return getSystemGetenvMethod() != null;
> + }
> +
> + /* (non-Javadoc)
> + * @see org.jruby.IOSEnvironment#getVariables()
> + */
> + public Map getVariables() {
> + Map returnMap = null;
> + Method getenvMethod = getSystemGetenvMethod();
> + try {
> + if (getenvMethod != null) {
> + returnMap = (Map) getenvMethod.invoke(null, (Object[]) null);
> + }
> + } catch (Exception e) {
> + warn("Unable to call System.getenv", e);
> + }
> + return returnMap;
> + }
> +
> + public static void main(String[] args) {
> + OSEnvironmentFromJava5SystemGetenv getenv = new
> OSEnvironmentFromJava5SystemGetenv();
> + Map envs = getenv.getVariables();
> + for (Iterator i = envs.entrySet().iterator(); i.hasNext();) {
> + Map.Entry entry = (Map.Entry) i.next();
> + System.out.println(entry.getKey() + ":" + entry.getValue());
> + }
> + System.out.println();
> + }
> +}
> Index: src/org/jruby/IOSEnvironment.java
> ===================================================================
> RCS file: src/org/jruby/IOSEnvironment.java
> diff -N src/org/jruby/IOSEnvironment.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ src/org/jruby/IOSEnvironment.java 1 Jan 1970 00:00:00 -0000
> @@ -0,0 +1,60 @@
> +/***** BEGIN LICENSE BLOCK *****
> + * Version: CPL 1.0/GPL 2.0/LGPL 2.1
> + *
> + * The contents of this file are subject to the Common Public
> + * License Version 1.0 (the "License"); you may not use this file
> + * except in compliance with the License. You may obtain a copy of
> + * the License at http://www.eclipse.org/legal/cpl-v10.html
> + *
> + * Software distributed under the License is distributed on an "AS
> + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
> + * implied. See the License for the specific language governing
> + * rights and limitations under the License.
> + *
> + * Copyright (C) 2006 Tim Azzopardi <[EMAIL PROTECTED]>
> + *
> + * Alternatively, the contents of this file may be used under the terms of
> + * either of the GNU General Public License Version 2 or later (the "GPL"),
> + * or the GNU Lesser General Public License Version 2.1 or later (the
> "LGPL"),
> + * in which case the provisions of the GPL or the LGPL are applicable instead
> + * of those above. If you wish to allow use of your version of this file only
> + * under the terms of either the GPL or the LGPL, and not to allow others to
> + * use your version of this file under the terms of the CPL, indicate your
> + * decision by deleting the provisions above and replace them with the notice
> + * and other provisions required by the GPL or the LGPL. If you do not delete
> + * the provisions above, a recipient may use your version of this file under
> + * the terms of any one of the CPL, the GPL or the LGPL.
> + ***** END LICENSE BLOCK *****/
> +package org.jruby;
> +
> +import java.util.Map;
> +
> +public interface IOSEnvironment {
> +
> + /**
> + * Get the operating system environment variables.
> + *
> + * Returns an unmodifiable string map view of the current system
> environment.
> + * If environment variables,are not supported, then null is returned.
> (isEnvironmentAccessible() returns false).
> + * If there are no variables defined, then an empty Map is returned.
> + *
> + * @return the operating system environment variables in a
> Map<String,String>.
> + * @throws EnvironmentAccessExcepton if there is a problem accessing the
> environment variables.
> + */
> + Map getVariables();
> +
> + /**
> + * Determines whether the OS environment variables are accessible.
> + *
> + * @return whether the OS environment variables are accessible.
> + */
> + boolean isAccessible();
> +
> +
> + class EnvironmentAccessExcepton extends RuntimeException {
> + public EnvironmentAccessExcepton(String message) {
> + super(message);
> + }
> + };
> +
> +}
--
+ http://www.tc.umn.edu/~enebo +---- mailto:[EMAIL PROTECTED] ----+
| Thomas E Enebo, Protagonist | "Luck favors the prepared |
| | mind." -Louis Pasteur |
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Jruby-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jruby-devel