You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sg...@apache.org on 2007/12/06 23:32:34 UTC

svn commit: r601884 - in /commons/sandbox/exec/trunk/src: main/java/org/apache/commons/exec/environment/ main/java/org/apache/commons/exec/launcher/ test/java/org/apache/commons/exec/environment/

Author: sgoeschl
Date: Thu Dec  6 14:32:34 2007
New Revision: 601884

URL: http://svn.apache.org/viewvc?rev=601884&view=rev
Log:
SANDBOX-204 Cleaning up some more code

Modified:
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/EnvironmentUtil.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java
    commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java?rev=601884&r1=601883&r2=601884&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java Thu Dec  6 14:32:34 2007
@@ -26,34 +26,64 @@
 import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Iterator;
 
 import org.apache.commons.exec.CommandLine;
 import org.apache.commons.exec.DefaultExecutor;
 import org.apache.commons.exec.Executor;
 import org.apache.commons.exec.OS;
 import org.apache.commons.exec.PumpStreamHandler;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 
+/**
+ * Helper class to determine the environment variable
+ * for the OS. Depending on the JDK the environment
+ * variables can be either retrieved directly from the
+ * JVM or requires starting a process to get them running
+ * an OS command line. 
+ */
 public class DefaultProcessingEnvironment {
 
-	private static Log LOG = LogFactory.getLog(DefaultProcessingEnvironment.class);
-	
-    /**
-     * TODO move this and other final static / constants into a constants class ?
-     */
+    /** the line seperator of the system */
     private static final String LINE_SEPARATOR = System.getProperty("line.separator");
-	
-	protected Map procEnvironment;
-	
+
+    /** the environment variables of the process */
+    protected Map procEnvironment;
+
     /**
      * Find the list of environment variables for this process.
      *
-     * @return a vector containing the environment variables the vector elements
-     *         are strings formatted like variable = value
-     * @throws IOException
+     * @return a map containing the environment variables
+     * @throws IOException obtaining the environment variables failed
      */
     public synchronized Map getProcEnvironment() throws IOException {
+
+        HashMap result = new HashMap();
+
+        if(procEnvironment == null) {
+            procEnvironment = this.createProcEnvironment();
+        }
+
+        // create a clone of the map just in case that
+        // anyone is going to modifiy it, e.g. removing
+        // or setting an evironment variable
+
+        Iterator iter = procEnvironment.keySet().iterator();
+        while(iter.hasNext()) {
+            Object key = iter.next();
+            Object value = procEnvironment.get(key);
+            result.put(key, value);
+        }
+
+        return result;
+    }
+
+    /**
+     * Find the list of environment variables for this process.
+     *
+     * @return a amp containing the environment variables
+     * @throws IOException the operation failed 
+     */
+    protected Map createProcEnvironment() throws IOException {
         if (procEnvironment == null) {
             try {
                 Method getenvs = System.class.getMethod( "getenv", null );
@@ -62,9 +92,9 @@
             } catch ( NoSuchMethodException e ) {
                 // ok, just not on JDK 1.5
             } catch ( IllegalAccessException e ) {
-                LOG.warn( "Unexpected error obtaining environment - using JDK 1.4 method" );
+                // Unexpected error obtaining environment - using JDK 1.4 method
             } catch ( InvocationTargetException e ) {
-                LOG.warn( "Unexpected error obtaining environment - using JDK 1.4 method" );
+                // Unexpected error obtaining environment - using JDK 1.4 method
             }
         }
 
@@ -100,8 +130,10 @@
     }
 
     /**
-     * @return
-     * @throws IOException
+     * Start a process to list the environment variables.
+     *
+     * @return a reader containing the output of the process 
+     * @throws IOException starting the process failed
      */
     protected BufferedReader runProcEnvCommand() throws IOException {
         ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -115,6 +147,12 @@
         return new BufferedReader(new StringReader(toString(out)));
     }
 
+    /**
+     * Determine the OS specific command line to get a list of environment
+     * variables.
+     *
+     * @return the command line
+     */
     protected CommandLine getProcEnvCommand() {
         String executable;
         String[] arguments = null;

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/EnvironmentUtil.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/EnvironmentUtil.java?rev=601884&r1=601883&r2=601884&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/EnvironmentUtil.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/EnvironmentUtil.java Thu Dec  6 14:32:34 2007
@@ -40,15 +40,16 @@
 	}
 	
     /**
-     * Disable constructor
+     * Disable constructor.
      */
     private EnvironmentUtil() {
 
     }
 
     /**
-     * get the variable list as an array
+     * Get the variable list as an array.
      *
+     * @param environment the environment to use
      * @return array of key=value assignment strings
      */
     public static String[] toStrings(Map environment) {
@@ -66,6 +67,28 @@
         return result;
     }
 
+    /**
+     * Find the list of environment variables for this process.
+     *
+     * @return a vector containing the environment variables the vector elements
+     *         are strings formatted like variable = value
+     * @throws IOException the operation failed
+     */
+    public static synchronized Map getProcEnvironment() throws IOException {
+    	return procEnvironment.getProcEnvironment();
+    }
+
+    /**
+     * Add a key/value pair to the given environment.
+     *
+     * @param environment the current environment
+     * @param keyAndValue the key/value pair 
+     */
+    public static void addVariableToEnvironment(Map environment, String keyAndValue) {
+		String[] parsedVarible = parseEnvironmentVariable(keyAndValue);		
+		environment.put(parsedVarible[0], parsedVarible[1]);
+	}
+
     private static String[] parseEnvironmentVariable(final String keyAndValue) {
         int index = keyAndValue.indexOf('=');
         if (index == -1) {
@@ -77,24 +100,8 @@
         String[] result = new String[2];
         result[0] = keyAndValue.substring(0, index);
         result[1] = keyAndValue.substring(index + 1);
-        
+
         return result;
     }
     
-    /**
-     * Find the list of environment variables for this process.
-     *
-     * @return a vector containing the environment variables the vector elements
-     *         are strings formatted like variable = value
-     * @throws IOException
-     */
-    public static synchronized Map getProcEnvironment() throws IOException {
-    	return procEnvironment.getProcEnvironment();
-    }
-
-	public static void addVariableToEnvironment(Map environment, String keyAndValue) {
-		String[] parsedVarible = parseEnvironmentVariable(keyAndValue);
-		
-		environment.put(parsedVarible[0], parsedVarible[1]);
-	}
 }

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java?rev=601884&r1=601883&r2=601884&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java Thu Dec  6 14:32:34 2007
@@ -25,9 +25,19 @@
 
 import org.apache.commons.exec.CommandLine;
 
+/**
+ * Helper class to determine the environment variable
+ * for VMS.
+ */
 public class OpenVmsProcessingEnvironment extends DefaultProcessingEnvironment {
 
-    public synchronized Map getProcEnvironment() throws IOException {
+    /**
+     * Find the list of environment variables for this process.
+     *
+     * @return a amp containing the environment variables
+     * @throws IOException the operation failed
+     */    
+    protected synchronized Map createProcEnvironment() throws IOException {
         if (procEnvironment == null) {
             procEnvironment = new HashMap();
 
@@ -40,6 +50,12 @@
         return procEnvironment;
     }
 
+    /**
+     * Determine the OS specific command line to get a list of environment
+     * variables.
+     *
+     * @return the command line
+     */    
     protected CommandLine getProcEnvCommand() {
         CommandLine commandLine = new CommandLine("show");
         commandLine.addArgument("logical");
@@ -54,12 +70,17 @@
      * that a logical defined in multiple tables only gets added from the
      * highest order table. Logicals with multiple equivalence names are mapped
      * to a variable with multiple values separated by a comma (,).
+     *
+     * @param environment the current environment
+     * @param in the reader from the process to determine VMS env variables
+     * @return the updated environment
+     * @throws IOException operation failed
      */
     private Map addVMSLogicals(final Map environment,
             final BufferedReader in) throws IOException {
+        String line;
         HashMap logicals = new HashMap();
         String logName = null, logValue = null, newLogName;
-        String line = null;
         while ((line = in.readLine()) != null) {
             // parse the VMS logicals into required format ("VAR=VAL[,VAL2]")
             if (line.startsWith("\t=")) {
@@ -94,5 +115,4 @@
         }
         return environment;
     }
-
 }

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java?rev=601884&r1=601883&r2=601884&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java Thu Dec  6 14:32:34 2007
@@ -34,6 +34,8 @@
      * @param env
      *            The environment for the new process. If null, the environment
      *            of the current process is used.
+     * 
+     * @return the newly created process
      * @throws IOException
      *             if attempting to run a command in a specific directory
      */
@@ -52,6 +54,8 @@
      * @param workingDir
      *            The directory to start the command in. If null, the current
      *            directory is used
+     *
+     * @return the newly created process
      * @throws IOException
      *             if trying to change directory
      */

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java?rev=601884&r1=601883&r2=601884&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java Thu Dec  6 14:32:34 2007
@@ -24,17 +24,17 @@
 
 import org.apache.commons.exec.CommandLine;
 import org.apache.commons.exec.environment.EnvironmentUtil;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 
 /**
  * A command launcher for JDK/JRE 1.3 (and higher). Uses the built-in
  * Runtime.exec() command
  */
 public class Java13CommandLauncher extends CommandLauncherImpl {
-	private static Log log = LogFactory.getLog(Java13CommandLauncher.class);
 
-	public Java13CommandLauncher() {
+    /**
+     * Constructor
+     */
+    public Java13CommandLauncher() {
 	}
 
 	/**
@@ -52,7 +52,6 @@
 	 */
 	public Process exec(final CommandLine cmd, final Map env,
 			final File workingDir) throws IOException {
-		log.debug("Execute:Java13CommandLauncher: " + cmd);
 
 		String[] envVars = null;
 		if(env != null) {

Modified: commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java?rev=601884&r1=601883&r2=601884&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java (original)
+++ commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java Thu Dec  6 14:32:34 2007
@@ -48,11 +48,14 @@
      */
     public void testGetProcEnvironment() throws IOException {
         Map procEnvironment = EnvironmentUtil.getProcEnvironment();
+        // we assume that there is at least one environment variable
+        // for this process
         assertTrue(procEnvironment.size() > 0);
         String[] envArgs = EnvironmentUtil.toStrings(procEnvironment);
         for(int i=0; i<envArgs.length; i++) {
             assertNotNull(envArgs[i]);
             assertTrue(envArgs[i].length() > 0);
+            System.out.println(envArgs[i]);
         }
     }
 }