I have your old patch in my tree right now. I will reapply this version.
Thanks for working on this.
-Tom
On Wed, 29 Mar 2006, Tim Azzopardi defenestrated me:
> Congratulations on the 0.8.3 release. Outstanding.
>
> Hopefully you'll like this patch a little better as there are no changes to
> jruby.bat or jruby.sh.
>
> First, if the Java 5 Systen.getenv is available, then that is used to get
> the environment. If not then the Runtime.getRuntime() technique is used. (I
> made RubyKernel.runInShell public to do this without duplicating code)
>
> There are two other implementations which can be selected setting the
> System property jruby.env.method to specify the name of the class to use:
> OSEnvironmentReaderFromFile gets the environment from a file (e.g. created
> by env or set). OSEnvironmentReaderFromApacheAnt gets the environment from
> tha apache ant Execute.getProcEnvironment method reflectively to avoid a
> dependecy on ant jars.
>
> System.getProperties values are put into a new global ENV_JAVA. This has
> meant a change to RubyDir which used to use ENV['HOME'].
>
> I put 3 runtime.getWarnings().warn messages is OSEnvironment.java so that
> you can see which implementation is being used, suring startup. These can
> obviously be removed later.
>
>
>
>
>
> Index: src/org/jruby/RubyDir.java
> ===================================================================
> RCS file: /cvsroot/jruby/jruby/src/org/jruby/RubyDir.java,v
> retrieving revision 1.34
> diff -u -r1.34 RubyDir.java
> --- src/org/jruby/RubyDir.java 28 Mar 2006 16:35:30 -0000 1.34
> +++ src/org/jruby/RubyDir.java 29 Mar 2006 08:07:26 -0000
> @@ -444,15 +444,15 @@
> }
>
> public static RubyString getHomeDirectoryPath(IRubyObject recv) {
> - RubyHash hash = (RubyHash)
> recv.getRuntime().getObject().getConstant("ENV");
> - IRubyObject home =
> hash.aref(recv.getRuntime().newString("HOME"));
> + RubyHash hash = (RubyHash)
> recv.getRuntime().getObject().getConstant("ENV_JAVA");
> + IRubyObject home =
> hash.aref(recv.getRuntime().newString("user.home"));
>
> if (home == null || home.isNil()) {
> home = hash.aref(recv.getRuntime().newString("LOGDIR"));
> }
>
> if (home == null || home.isNil()) {
> - throw recv.getRuntime().newArgumentError("HOME/LOGDIR
> not set");
> + throw
> recv.getRuntime().newArgumentError("user.home/LOGDIR not set");
> }
>
> return (RubyString) home;
> Index: src/org/jruby/RubyGlobal.java
> ===================================================================
> RCS file: /cvsroot/jruby/jruby/src/org/jruby/RubyGlobal.java,v
> retrieving revision 1.24
> diff -u -r1.24 RubyGlobal.java
> --- src/org/jruby/RubyGlobal.java 25 Mar 2006 06:25:36 -0000 1.24
> +++ src/org/jruby/RubyGlobal.java 29 Mar 2006 08:07:27 -0000
> @@ -17,6 +17,7 @@
> * Copyright (C) 2004 Charles O Nutter <[EMAIL PROTECTED]>
> * Copyright (C) 2004 Thomas E Enebo <[EMAIL PROTECTED]>
> * Copyright (C) 2004 Stefan Matthias Aust <[EMAIL PROTECTED]>
> + * 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"),
> @@ -34,7 +35,10 @@
>
> import java.util.ArrayList;
> import java.util.HashMap;
> +import java.util.Map;
>
> +import org.jruby.environment.OSEnvironmentReaderExcepton;
> +import org.jruby.environment.OSEnvironment;
> import org.jruby.runtime.Constants;
> import org.jruby.runtime.GlobalVariable;
> import org.jruby.runtime.ReadonlyGlobalVariable;
> @@ -60,12 +64,6 @@
> runtime.defineGlobalConstant("RELEASE_DATE", release);
> runtime.defineGlobalConstant("PLATFORM", platform);
>
> - // FIXME: ENV Not really a RubyHash (but close)
> - // FIXME: ENV Should not copy system properties, but should
> reference them instead
> - HashMap envs = new HashMap();
> - envs.put(runtime.newString("HOME"),
> runtime.newString(System.getProperty("user.home")));
> - runtime.defineGlobalConstant("ENV", RubyHash.newHash(runtime, envs,
> runtime.getNil()));
> -
> runtime.defineVariable(new StringGlobalVariable(runtime, "$KCODE",
> runtime.newString("UTF8")));
> runtime.defineVariable(new StringGlobalVariable(runtime, "$/",
> runtime.newString("\n")));
> runtime.defineVariable(new StringGlobalVariable(runtime, "$\\",
> runtime.getNil()));
> @@ -102,11 +100,39 @@
> runtime.defineVariable(new LoadPath(runtime, "$-I"));
> runtime.defineVariable(new LoadPath(runtime, "$LOAD_PATH"));
>
> + // after defn of $stderr as the call may produce warnings
> + defineGlobalEnvConstants(runtime);
> +
> // ARGF, $< object
> RubyArgsFile argsFile = new RubyArgsFile(runtime);
> argsFile.initArgsFile();
> }
>
> + private static void defineGlobalEnvConstants(IRuby runtime) {
> +
> + Map environmentVariableMap = null;
> + OSEnvironment environment = new OSEnvironment();
> + try {
> + environmentVariableMap =
> environment.getEnvironmentVariableMap(runtime);
> + } catch (OSEnvironmentReaderExcepton e) {
> + // If the environment variables are not accessible shouldn't
> terminate
> + runtime.getWarnings().warn(e.getMessage());
> + }
> +
> + if (environmentVariableMap == null) {
> + // if the environment variables can't be obtainded, define an
> empty ENV
> + environmentVariableMap = new HashMap();
> + }
> + runtime.defineGlobalConstant("ENV", RubyHash.newHash(runtime,
> environmentVariableMap, runtime.getNil()));
> +
> + // Define System.getProperties() in ENV_JAVA
> + Map systemProps = environment.getSystemPropertiesMap(runtime);
> + runtime.defineGlobalConstant("ENV_JAVA", RubyHash.newHash(runtime,
> systemProps, runtime.getNil()));
> +
> + }
> +
> +
> +
> // Accessor methods.
>
> private static class LineNumberGlobalVariable extends GlobalVariable {
> Index: src/org/jruby/RubyKernel.java
> ===================================================================
> RCS file: /cvsroot/jruby/jruby/src/org/jruby/RubyKernel.java,v
> retrieving revision 1.45
> diff -u -r1.45 RubyKernel.java
> --- src/org/jruby/RubyKernel.java 28 Mar 2006 03:06:16 -0000 1.45
> +++ src/org/jruby/RubyKernel.java 29 Mar 2006 08:07:28 -0000
> @@ -745,7 +745,7 @@
> return PATH_SEPARATORS.matcher(executable).replaceAll(replacement) +
> remainder;
> }
>
> - private static int runInShell(IRuby runtime, String command,
> StringBuffer output) {
> + public static int runInShell(IRuby runtime, String command, StringBuffer
> output) {
> try {
> String shell = System.getProperty("jruby.shell");
> Process aProcess;
> Index: src/org/jruby/environment/OSEnvironmentReaderFromJava5SystemGetenv.java
> ===================================================================
> RCS file:
> src/org/jruby/environment/OSEnvironmentReaderFromJava5SystemGetenv.java
> diff -N
> src/org/jruby/environment/OSEnvironmentReaderFromJava5SystemGetenv.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ src/org/jruby/environment/OSEnvironmentReaderFromJava5SystemGetenv.java
> 1 Jan 1970 00:00:00 -0000
> @@ -0,0 +1,94 @@
> +/***** 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.environment;
> +
> +import java.lang.reflect.Method;
> +import java.util.Iterator;
> +import java.util.Map;
> +
> +import org.jruby.IRuby;
> +
> +class OSEnvironmentReaderFromJava5SystemGetenv implements
> IOSEnvironmentReader {
> +
> +
> + /**
> + * 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(IRuby runtime) {
> + return getSystemGetenvMethod() != null;
> + }
> +
> + /* (non-Javadoc)
> + * @see org.jruby.IOSEnvironment#getVariables()
> + */
> + public Map getVariables(IRuby runtime) {
> + Map returnMap = null;
> + Method getenvMethod = getSystemGetenvMethod();
> + try {
> + if (getenvMethod != null) {
> + returnMap = (Map) getenvMethod.invoke(null, (Object[]) null);
> + }
> + } catch (Exception e) {
> + new OSEnvironment().handleException(e);
> + }
> + return returnMap;
> + }
> +
> + public static void main(String[] args) {
> + OSEnvironmentReaderFromJava5SystemGetenv getenv = new
> OSEnvironmentReaderFromJava5SystemGetenv();
> + Map envs = getenv.getVariables(null);
> + 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/environment/OSEnvironmentReaderFromFile.java
> ===================================================================
> RCS file: src/org/jruby/environment/OSEnvironmentReaderFromFile.java
> diff -N src/org/jruby/environment/OSEnvironmentReaderFromFile.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ src/org/jruby/environment/OSEnvironmentReaderFromFile.java 1 Jan
> 1970 00:00:00 -0000
> @@ -0,0 +1,91 @@
> +/***** 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.environment;
> +
> +import java.io.BufferedReader;
> +import java.io.File;
> +import java.io.FileNotFoundException;
> +import java.io.FileReader;
> +import java.util.Map;
> +
> +import org.jruby.IRuby;
> +
> +
> +/**
> + * Get the os environment from a file.
> + * Requires a java system property "jruby.envfile" to give the location of
> a file from which
> + * the environment variables can be loaded.
> + *
> + */
> +class OSEnvironmentReaderFromFile implements IOSEnvironmentReader {
> +
> + private static final String JRUBY_ENVFILE = "jruby.envfile";
> +
> + private final OSEnvironment environmentReader = new OSEnvironment();
> +
> +
> + /* (non-Javadoc)
> + * @see org.jruby.IOSEnvironment#isAccessible()
> + */
> + public boolean isAccessible(IRuby runtime) {
> + 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(IRuby runtime) {
> + String jrubyEnvFilename = System.getProperty(JRUBY_ENVFILE);
> +
> + Map envs = null;
> +
> + if (jrubyEnvFilename == null || jrubyEnvFilename.length() < 1) {
> + environmentReader.handleException(new
> OSEnvironmentReaderExcepton("Property " + JRUBY_ENVFILE + " not defined."));
> + } else {
> + BufferedReader reader = null;
> + try {
> + reader = new BufferedReader(new
> FileReader(jrubyEnvFilename));
> + } catch (FileNotFoundException e) {
> + envs = null;
> + // Very unlikely to happen as isAccessible() should be used
> first
> + environmentReader.handleException(e);
> + }
> + envs = environmentReader.getVariablesFrom(reader);
> + }
> + return envs;
> + }
> +}
> Index: src/org/jruby/environment/OSEnvironmentReaderExcepton.java
> ===================================================================
> RCS file: src/org/jruby/environment/OSEnvironmentReaderExcepton.java
> diff -N src/org/jruby/environment/OSEnvironmentReaderExcepton.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ src/org/jruby/environment/OSEnvironmentReaderExcepton.java 1 Jan
> 1970 00:00:00 -0000
> @@ -0,0 +1,42 @@
> +/***** 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.environment;
> +
> +public class OSEnvironmentReaderExcepton extends RuntimeException {
> +
> + private static final long serialVersionUID = 8101107415664066073L;
> +
> + public OSEnvironmentReaderExcepton() {
> + super();
> + }
> +
> + public OSEnvironmentReaderExcepton(String message) {
> + super(message);
> + }
> +}
> Index: src/org/jruby/environment/OSEnvironmentReaderFromRuntimeExec.java
> ===================================================================
> RCS file: src/org/jruby/environment/OSEnvironmentReaderFromRuntimeExec.java
> diff -N src/org/jruby/environment/OSEnvironmentReaderFromRuntimeExec.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ src/org/jruby/environment/OSEnvironmentReaderFromRuntimeExec.java 1 Jan
> 1970 00:00:00 -0000
> @@ -0,0 +1,91 @@
> +/***** 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.environment;
> +
> +import java.io.BufferedReader;
> +import java.io.StringReader;
> +import java.util.Map;
> +
> +import org.jruby.IRuby;
> +import org.jruby.RubyKernel;
> +import org.jruby.exceptions.RaiseException;
> +
> +class OSEnvironmentReaderFromRuntimeExec implements IOSEnvironmentReader {
> +
> + private final OSEnvironment environmentReader = new OSEnvironment();
> +
> + /* (non-Javadoc)
> + * @see org.jruby.IOSEnvironment#isAccessible()
> + */
> + public boolean isAccessible(IRuby runtime) {
> + return true;
> + }
> +
> +
> + private String getEnvCommand() {
> + String command;
> + String osname = System.getProperty("os.name").toLowerCase();
> +
> + // TODO merge this logic into RubyKernel.runInShell? / possible
> conflict if jruby.shell System property is set
> +
> + if (osname.indexOf("windows 9") > -1) {
> + command = "command.com /c set";
> + } else if ((osname.indexOf("nt") > -1)
> + || (osname.indexOf("windows 20") > -1)
> + || (osname.indexOf("windows xp") > -1)) {
> + command = "cmd.exe /c set";
> + } else {
> + // Assume some unix variant with an env command
> + command = "env";
> + }
> + return command;
> + }
> +
> +
> + /*
> + * (non-Javadoc)
> + *
> + * @see org.jruby.IOSEnvironment#getVariables()
> + */
> + public Map getVariables(IRuby runtime) {
> +
> + StringBuffer output = new StringBuffer();
> +
> + try {
> + RubyKernel.runInShell(runtime, getEnvCommand(), output);
> + } catch (RaiseException e) {
> + environmentReader.handleException(e);
> + }
> +
> + BufferedReader reader = new BufferedReader(new
> StringReader(output.toString()));
> +
> + return environmentReader.getVariablesFrom(reader);
> + }
> +
> +}
> Index: src/org/jruby/environment/OSEnvironmentReaderFromApacheAnt.java
> ===================================================================
> RCS file: src/org/jruby/environment/OSEnvironmentReaderFromApacheAnt.java
> diff -N src/org/jruby/environment/OSEnvironmentReaderFromApacheAnt.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ src/org/jruby/environment/OSEnvironmentReaderFromApacheAnt.java 1 Jan
> 1970 00:00:00 -0000
> @@ -0,0 +1,129 @@
> +/***** 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.environment;
> +
> +import java.lang.reflect.InvocationTargetException;
> +import java.lang.reflect.Method;
> +import java.util.HashMap;
> +import java.util.Iterator;
> +import java.util.Map;
> +import java.util.Vector;
> +
> +import org.jruby.IRuby;
> +
> +class OSEnvironmentReaderFromApacheAnt implements IOSEnvironmentReader {
> +
> +
> + protected Map getProcEnvironmentMethodV() {
> +
> + Method getProcEnvironment = getProcEnvironmentMethod();
> + Vector v = new Vector();
> +
> + HashMap map = new HashMap();
> +
> +
> + if (getProcEnvironment != null) {
> + try {
> + v = (Vector) getProcEnvironment.invoke(null,
> (Object[]) null);
> + } catch (IllegalArgumentException e) {
> + return map;
> + } catch (IllegalAccessException e) {
> + return map;
> + } catch (InvocationTargetException e) {
> + return map;
> + }
> + }
> +
> + String line;
> + int equalsPos;
> +
> + for (java.util.Enumeration e = v.elements(); e.hasMoreElements();) {
> + line = (String) e.nextElement();
> + equalsPos = line.indexOf('=');
> + if (equalsPos >= 1) {
> + map.put(line.substring(0, equalsPos),
> line.substring(equalsPos + 1));
> + }
> + }
> +
> +
> + return map;
> +
> + }
> +
> +
> + protected Method getProcEnvironmentMethod() {
> +
> + Method getProcEnvironment;
> + Class c;
> +
> + try {
> + c = Class.forName("org.apache.tools.ant.taskdefs.Execute");
> + } catch (ClassNotFoundException e) {
> + return null;
> + }
> +
> + try {
> + getProcEnvironment = c.getMethod("getProcEnvironment",
> (Class[]) null);
> + } catch (SecurityException e) {
> + return null;
> + } catch (NoSuchMethodException e) {
> + return null;
> + }
> +
> + return getProcEnvironment;
> +
> + }
> +
> +
> +
> +
> + /* (non-Javadoc)
> + * @see org.jruby.IOSEnvironment#isAccessible()
> + */
> + public boolean isAccessible(IRuby runtime) {
> + return getProcEnvironmentMethod() != null;
> + }
> +
> + /* (non-Javadoc)
> + * @see org.jruby.IOSEnvironment#getVariables()
> + */
> + public Map getVariables(IRuby runtime) {
> + return getProcEnvironmentMethodV();
> + }
> +
> + public static void main(String[] args) {
> + OSEnvironmentReaderFromApacheAnt getenv = new
> OSEnvironmentReaderFromApacheAnt();
> + Map envs = getenv.getVariables(null);
> + 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/environment/OSEnvironment.java
> ===================================================================
> RCS file: src/org/jruby/environment/OSEnvironment.java
> diff -N src/org/jruby/environment/OSEnvironment.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ src/org/jruby/environment/OSEnvironment.java 1 Jan 1970 00:00:00
> -0000
> @@ -0,0 +1,170 @@
> +/***** 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.environment;
> +
> +import java.io.BufferedReader;
> +import java.io.IOException;
> +import java.util.HashMap;
> +import java.util.Iterator;
> +import java.util.Map;
> +import java.util.Set;
> +
> +import org.jruby.IRuby;
> +
> +public class OSEnvironment {
> +
> +
> + /**
> + * Handles exceptions from implementors of [EMAIL PROTECTED]
> IOSEnvironmentReader},
> + * converting the exception to a [EMAIL PROTECTED]
> OSEnvironmentReaderExcepton}
> + * @param e
> + */
> + void handleException(Exception e) {
> + throw (OSEnvironmentReaderExcepton)
> + new OSEnvironmentReaderExcepton().initCause(e);
> + }
> +
> + /**
> + * Returns the OS environment variables as a Map<RubyString,RubyString>.
> + * If the Java system property "jruby.env.method" is set then
> + * the value is used as the classname of a class than implements the
> IOSEnvironmentReader
> + * interface and the environment is obtained via this class.
> + * If the "jruby.env.method" is
> "org.jruby.environment.OSEnvironmentReaderFromFile" then
> + * the java system property "jruby.envfile" should give the location of
> a file from which
> + * the environment variables can be loaded.
> + * Otherwise, other default implementations of IOSEnvironmentReader are
> tried
> + * to obtain the os environment variables.
> + * @param runtime
> + * @param System.getProperty("jruby.env.method")
> + * @throws OSEnvironmentReaderExcepton
> + */
> + public Map getEnvironmentVariableMap(IRuby runtime) {
> + String jrubyEnvMethod = System.getProperty("jruby.env.method");
> +
> + IOSEnvironmentReader reader;
> +
> + if (jrubyEnvMethod == null || jrubyEnvMethod.length() < 1) {
> + // Try to get environment from Java5 System.getenv()
> + reader = getAccessibleOSEnvironment(runtime,
> OSEnvironmentReaderFromJava5SystemGetenv.class.getName());
> + // not Java5 so try getting environment using Runtime exec
> + if (reader == null) {
> + reader = getAccessibleOSEnvironment(runtime,
> OSEnvironmentReaderFromRuntimeExec.class.getName());
> + runtime.getWarnings().warn("Getting environment variables
> using Runtime Exec");
> + } else {
> + runtime.getWarnings().warn("Getting environment variables
> using Java5 System.getenv()");
> + }
> + } else {
> + // get environment from jruby command line property supplied
> class
> + runtime.getWarnings().warn("Getting environment variables using
> command line defined method: " + jrubyEnvMethod);
> + reader = getAccessibleOSEnvironment(runtime, jrubyEnvMethod);
>
> + }
> +
> + Map envs = null;
> +
> + if (reader != null) {
> + Map variables = null;
> + variables = reader.getVariables(runtime);
> + envs = getAsMapOfRubyStrings(runtime,
> variables.entrySet());
> + }
> +
> + return envs;
> +
> + }
> +
> + /**
> + * Returns java system properties as a Map<RubyString,RubyString>.
> + * @param runtime
> + * @return the java system properties as a Map<RubyString,RubyString>.
> + */
> + public Map getSystemPropertiesMap(IRuby runtime) {
> + return getAsMapOfRubyStrings(runtime,
> System.getProperties().entrySet());
> + }
> +
> +
> + private static IOSEnvironmentReader getAccessibleOSEnvironment(IRuby
> runtime, String classname) {
> + IOSEnvironmentReader osenvironment = null;
> + try {
> + osenvironment =
> (IOSEnvironmentReader)Class.forName(classname).newInstance();
> + } catch (Exception e) {
> + // This should only happen for a command line supplied
> IOSEnvironmentReader implementation
> + runtime.getWarnings().warn(e.getMessage());
> + }
> +
> + if (osenvironment != null & osenvironment.isAccessible(runtime)) {
> + return osenvironment;
> + }
> + return null;
> + }
> +
> +
> +
> + private static Map getAsMapOfRubyStrings(IRuby runtime, Set entrySet) {
> + Map envs = new HashMap();
> + for (Iterator iter = entrySet.iterator(); iter.hasNext();) {
> + Map.Entry entry = (Map.Entry) iter.next();
> +
> envs.put(runtime.newString((String)entry.getKey()),runtime.newString((String)entry.getValue()));
> + }
> + return envs;
> + }
> +
> +
> + /**
> + * Returns a Map of the variables found in the reader of form var=value
> + * @param reader
> + * @return Map<String,String> of variables found by reading lines from
> reader.
> + */
> + Map getVariablesFrom(BufferedReader reader) {
> + Map envs = new HashMap();
> + try {
> + String line, envVarName, envVarValue;
> + int equalsPos;
> + while ((line = reader.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;
> + handleException(e);
> + } finally {
> + try {
> + reader.close();
> + } catch (IOException e) {
> + envs = null;
> + handleException(e);
> + }
> + }
> + return envs;
> + }
> +
> +
> +}
> Index: src/org/jruby/environment/IOSEnvironmentReader.java
> ===================================================================
> RCS file: src/org/jruby/environment/IOSEnvironmentReader.java
> diff -N src/org/jruby/environment/IOSEnvironmentReader.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ src/org/jruby/environment/IOSEnvironmentReader.java 1 Jan 1970
> 00:00:00 -0000
> @@ -0,0 +1,55 @@
> +/***** 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.environment;
> +
> +import java.util.Map;
> +
> +import org.jruby.IRuby;
> +
> +public interface IOSEnvironmentReader {
> +
> + /**
> + * Get the operating system environment variables.
> + *
> + * Returns an 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 OSEnvironmentReaderExcepton if there is a problem accessing
> the environment variables.
> + */
> + Map getVariables(IRuby runtime);
> +
> + /**
> + * Determines whether the OS environment variables are accessible using
> a given implementation.
> + *
> + * @return whether the OS environment variables are accessible.
> + */
> + boolean isAccessible(IRuby runtime);;
> +
> +}
--
+ 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 xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Jruby-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jruby-devel