You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by br...@apache.org on 2005/08/31 12:48:28 UTC
svn commit: r265009 - in /jakarta/commons/sandbox/exec/trunk: ./
src/main/java/org/apache/commons/exec/Execute.java
src/main/java/org/apache/commons/exec/environment/Environment.java
src/test/java/org/apache/commons/exec/environment/EnvironmentTest.java
Author: brett
Date: Wed Aug 31 03:48:16 2005
New Revision: 265009
URL: http://svn.apache.org/viewcvs?rev=265009&view=rev
Log:
PR: 36222
Submitted by: Niklas Gustavsson
Reviewed by: Brett Porter
use Java 1.5 System.getenv() for environment when available
Modified:
jakarta/commons/sandbox/exec/trunk/ (props changed)
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/Environment.java
jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentTest.java
Propchange: jakarta/commons/sandbox/exec/trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Aug 31 03:48:16 2005
@@ -1,2 +1,6 @@
target
build.properties
+*.iml
+*.ipr
+*.iws
+
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java?rev=265009&r1=265008&r2=265009&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java Wed Aug 31 03:48:16 2005
@@ -158,8 +158,10 @@
* Returns the environment used to create a subprocess.
*
* @return the environment used to create a subprocess
+ * @throws IOException If the environment can not be
+ * retrived.
*/
- public Environment getEnvironment() {
+ public Environment getEnvironment() throws IOException {
if (environment == null || newEnvironment) {
return environment;
}
@@ -396,8 +398,9 @@
* Patch the current environment with the new values from the user.
*
* @return the patched environment
+ * @throws IOException if the procssing environment can not be retrived
*/
- private Environment patchEnvironment() {
+ private Environment patchEnvironment() throws IOException {
// On OpenVMS Runtime#exec() doesn't support the environment array,
// so we only return the new values which then will be set in
// the generated DCL script, inheriting the parent process environment
@@ -405,8 +408,8 @@
return environment;
}
- Environment osEnv = (Environment) Environment.getProcEnvironment()
- .clone();
+ Environment procEnv = Environment.getProcEnvironment();
+ Environment osEnv = (Environment) procEnv.clone();
osEnv.putAll(environment);
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/Environment.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/Environment.java?rev=265009&r1=265008&r2=265009&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/Environment.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/Environment.java Wed Aug 31 03:48:16 2005
@@ -22,21 +22,29 @@
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
+import java.util.Set;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.CommandLineImpl;
import org.apache.commons.exec.Execute;
import org.apache.commons.exec.OS;
import org.apache.commons.exec.PumpStreamHandler;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
/**
* Wrapper for environment variables.
+ * @todo change from inheritence to delegation, implement map
*/
public class Environment extends HashMap {
+ private static Log LOG = LogFactory.getLog(Environment.class);
+
public static Environment createEnvironment() {
if (OS.isFamilyOpenVms()) {
return new OpenVmsEnvironment();
@@ -62,21 +70,34 @@
*/
private static final long serialVersionUID = 3256443594801165364L;
- /**
- * a vector of type EnvirommentVariable
- *
- * @see EnvironmentVariable
- */
-
private static Environment procEnvironment;
/**
- * constructor
+ * Default constructor, creates an empty environment
*/
protected Environment() {
}
/**
+ * Creates an environment from a @link Map of @link String
+ * keys and values.
+ *
+ * @param env A map containg the environment variable name
+ * as @link String key and the variable value as @link String
+ * value
+ */
+ protected Environment(Map env) {
+ this();
+
+ Set entries = env.entrySet();
+ for (Iterator iter = entries.iterator(); iter.hasNext();) {
+ Map.Entry entry = (Map.Entry) iter.next();
+ addVariable((String) entry.getKey(),
+ (String) entry.getValue());
+ }
+ }
+
+ /**
* add a variable. Validity checking is <i>not</i> performed at this point.
* Duplicates are not caught either.
*
@@ -116,14 +137,32 @@
*
* @return a vector containing the environment variables the vector elements
* are strings formatted like variable = value
+ * @throws IOException
*/
- public static synchronized Environment getProcEnvironment() {
- if (procEnvironment != null) {
- return procEnvironment;
+ public static synchronized Environment getProcEnvironment() throws IOException {
+ if (procEnvironment == null) {
+ try
+ {
+ Method getenvs = System.class.getMethod( "getenv", null );
+ Map env = (Map) getenvs.invoke( null, null );
+ procEnvironment = new Environment( env );
+ }
+ catch ( NoSuchMethodException e )
+ {
+ // ok, just not on JDK 1.5
+ }
+ catch ( IllegalAccessException e )
+ {
+ LOG.warn( "Unexpected error obtaining environment - using JDK 1.4 method" );
+ }
+ catch ( InvocationTargetException e )
+ {
+ LOG.warn( "Unexpected error obtaining environment - using JDK 1.4 method" );
+ }
}
- procEnvironment = new Environment();
- try {
+ if(procEnvironment == null) {
+ procEnvironment = new Environment();
BufferedReader in = runProcEnvCommand();
String var = null;
@@ -151,9 +190,6 @@
procEnvironment.addVariable(EnvironmentVariable
.createEnvironmentVariable(var));
}
- } catch (java.io.IOException exc) {
- exc.printStackTrace();
- // Just try to see how much we got
}
return procEnvironment;
}
Modified: jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentTest.java?rev=265009&r1=265008&r2=265009&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentTest.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentTest.java Wed Aug 31 03:48:16 2005
@@ -17,6 +17,7 @@
package org.apache.commons.exec.environment;
+import java.io.IOException;
import java.util.Iterator;
import junit.framework.TestCase;
@@ -63,7 +64,7 @@
.createEnvironmentVariable("dum", "my")));
}
- public void testGetEnvironment() {
+ public void testGetEnvironment() throws IOException {
Environment env = Environment.getProcEnvironment();
for (Iterator iter = env.keySet().iterator(); iter.hasNext();) {
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org