You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by do...@apache.org on 2002/02/02 07:38:07 UTC
cvs commit: jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl Environment.java DefaultExecManager.java
donaldp 02/02/01 22:38:07
Modified: proposal/myrmidon/src/java/org/apache/antlib/nativelib
LoadEnvironment.java
proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/launchers
ExecUtil.java
proposal/myrmidon/src/java/org/apache/aut/nativelib
ExecManager.java
proposal/myrmidon/src/java/org/apache/aut/nativelib/impl
Environment.java DefaultExecManager.java
Log:
Rework Environment implementation so that it is part of the ExecManager service. This makes it less coupled to bad practices and also merges all the native execution code into a central place.
Detected by JDepend
Revision Changes Path
1.5 +12 -8 jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/nativelib/LoadEnvironment.java
Index: LoadEnvironment.java
===================================================================
RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/nativelib/LoadEnvironment.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- LoadEnvironment.java 2 Feb 2002 06:03:12 -0000 1.4
+++ LoadEnvironment.java 2 Feb 2002 06:38:07 -0000 1.5
@@ -7,15 +7,16 @@
*/
package org.apache.antlib.nativelib;
-import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
+import org.apache.aut.nativelib.ExecException;
+import org.apache.aut.nativelib.ExecManager;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.myrmidon.api.AbstractTask;
import org.apache.myrmidon.api.TaskException;
-import org.apache.aut.nativelib.impl.Environment;
-import org.apache.aut.nativelib.ExecException;
+import org.apache.myrmidon.framework.factorys.ExecManagerFactory;
+import org.apache.myrmidon.services.ServiceException;
/**
* This task is responsible for loading that OS-specific environment
@@ -88,15 +89,18 @@
{
try
{
- return Environment.getNativeEnvironment();
+ final ExecManagerFactory factory = new ExecManagerFactory();
+ final ExecManager manager = (ExecManager)factory.createService();
+
+ return manager.getNativeEnvironment();
}
- catch( final ExecException ee )
+ catch( final ServiceException se )
{
- throw new TaskException( ee.getMessage(), ee );
+ throw new TaskException( se.getMessage(), se );
}
- catch( final IOException ioe )
+ catch( final ExecException ee )
{
- throw new TaskException( ioe.getMessage(), ioe );
+ throw new TaskException( ee.getMessage(), ee );
}
}
}
1.3 +3 -14 jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/launchers/ExecUtil.java
Index: ExecUtil.java
===================================================================
RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/launchers/ExecUtil.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ExecUtil.java 2 Feb 2002 06:03:12 -0000 1.2
+++ ExecUtil.java 2 Feb 2002 06:38:07 -0000 1.3
@@ -12,7 +12,6 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
-import org.apache.aut.nativelib.impl.Environment;
import org.apache.aut.nativelib.ExecException;
import org.apache.aut.nativelib.ExecMetaData;
@@ -20,7 +19,7 @@
* A set of utility functions useful when writing CommandLaunchers.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
- * @version $Revision: 1.2 $ $Date: 2002/02/02 06:03:12 $
+ * @version $Revision: 1.3 $ $Date: 2002/02/02 06:38:07 $
*/
class ExecUtil
{
@@ -121,23 +120,13 @@
throws ExecException, IOException
{
final Properties environment = metaData.getEnvironment();
- if( 0 == environment.size() )
+ if( null == environment || 0 == environment.size() )
{
return null;
}
else
{
- if( metaData.isEnvironmentAdditive() )
- {
- final Properties newEnvironment = new Properties();
- newEnvironment.putAll( Environment.getNativeEnvironment() );
- newEnvironment.putAll( environment );
- return toNativeEnvironment( newEnvironment );
- }
- else
- {
- return toNativeEnvironment( environment );
- }
+ return toNativeEnvironment( environment );
}
}
}
1.2 +9 -1 jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/ExecManager.java
Index: ExecManager.java
===================================================================
RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/ExecManager.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ExecManager.java 12 Jan 2002 04:51:07 -0000 1.1
+++ ExecManager.java 2 Feb 2002 06:38:07 -0000 1.2
@@ -10,6 +10,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.Properties;
/**
* Interface via which clients can request that a native
@@ -26,10 +27,17 @@
* </ul>
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
- * @version $Revision: 1.1 $ $Date: 2002/01/12 04:51:07 $
+ * @version $Revision: 1.2 $ $Date: 2002/02/02 06:38:07 $
*/
public interface ExecManager
{
+ /**
+ * Retrieve a properties object that contains a list of
+ * all the native environment variables.
+ */
+ Properties getNativeEnvironment()
+ throws ExecException;
+
/**
* Execute a process and wait for it to finish before
* returning.
1.2 +31 -55 jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/Environment.java
Index: Environment.java
===================================================================
RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/Environment.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Environment.java 2 Feb 2002 06:03:12 -0000 1.1
+++ Environment.java 2 Feb 2002 06:38:07 -0000 1.2
@@ -9,16 +9,16 @@
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
+import java.io.File;
import java.io.IOException;
import java.io.StringReader;
-import java.util.Iterator;
import java.util.Locale;
import java.util.Properties;
-import org.apache.avalon.excalibur.util.StringUtil;
-import org.apache.myrmidon.api.TaskException;
-import org.apache.tools.ant.taskdefs.exec.Execute;
import org.apache.aut.nativelib.ExecException;
+import org.apache.aut.nativelib.ExecManager;
+import org.apache.aut.nativelib.ExecMetaData;
import org.apache.aut.nativelib.Os;
+import org.apache.avalon.excalibur.util.StringUtil;
/**
* This is the class that can be used to retrieve the environment
@@ -26,9 +26,9 @@
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
* @author <a href="mailto:thomas.haas@softwired-inc.com">Thomas Haas</a>
- * @version $Revision: 1.1 $ $Date: 2002/02/02 06:03:12 $
+ * @version $Revision: 1.2 $ $Date: 2002/02/02 06:38:07 $
*/
-public final class Environment
+final class Environment
{
private final static String[] COMMAND_COM = new String[]{"command.com", "/c", "set"};
private final static String[] CMD_EXE = new String[]{"cmd", "/c", "set"};
@@ -37,39 +37,27 @@
private final static String[] ENV_CMD = new String[]{"/usr/bin/env"};
private final static String[] ENV_RAW = new String[]{"env"};
- private static Properties c_procEnvironment;
+ /**
+ * This is a cached version of the native environment variables.
+ */
+ private Properties m_procEnvironment;
/**
- * Private constructor to block instantiation.
+ * This is the class that is used to invoke the native process
+ * to retrieve then environment variables.
*/
- private Environment()
- {
- }
+ private final ExecManager m_execManager;
- public static void addNativeEnvironment( final Properties environment )
- throws ExecException, IOException
+ public Environment( final ExecManager execManager )
{
- final Properties nativeEnvironment = getEnvironmentVariables();
- final Iterator nativeKeys = nativeEnvironment.keySet().iterator();
- while( nativeKeys.hasNext() )
- {
- final String key = (String)nativeKeys.next();
- if( environment.contains( key ) )
- {
- //Skip environment variables that are overidden
- continue;
- }
-
- final String value = nativeEnvironment.getProperty( key );
- environment.setProperty( key, value );
- }
+ m_execManager = execManager;
}
/**
* Retrieve a Properties object that contains the list of all
* native EnvironmentData Variables for the current process.
*/
- public static Properties getNativeEnvironment()
+ public Properties getNativeEnvironment()
throws IOException, ExecException
{
final Properties properties = new Properties();
@@ -81,21 +69,21 @@
* Get the Property object with all environment variables and
* attempt to load it if it has not already been loaded.
*/
- private static synchronized Properties getEnvironmentVariables()
+ private synchronized Properties getEnvironmentVariables()
throws IOException, ExecException
{
- if( null == c_procEnvironment )
+ if( null == m_procEnvironment )
{
- c_procEnvironment = retrieveEnvironmentVariables();
+ m_procEnvironment = retrieveEnvironmentVariables();
}
- return c_procEnvironment;
+ return m_procEnvironment;
}
/**
* Retrieve a last of environment variables from the native OS.
*/
- private static synchronized Properties retrieveEnvironmentVariables()
+ private synchronized Properties retrieveEnvironmentVariables()
throws IOException, ExecException
{
final String data = getEnvironmentText();
@@ -137,8 +125,8 @@
* '=' character then generate an exception. After parsed data place
* the key-value pair into the specified Properties object.
*/
- private static void addProperty( final Properties properties,
- final String data )
+ private void addProperty( final Properties properties,
+ final String data )
throws ExecException
{
final int index = data.indexOf( '=' );
@@ -161,30 +149,18 @@
* Retrieve the text of data that is the result of
* running the environment command.
*/
- private static String getEnvironmentText()
+ private String getEnvironmentText()
throws IOException, ExecException
{
- final ByteArrayOutputStream output = new ByteArrayOutputStream();
- final Execute exe = new Execute();
- exe.setOutput( output );
- exe.setError( output );
-
- exe.setCommandline( getEnvCommand() );
+ final String[] command = getEnvCommand();
+ final File workingDirectory = new File( "." );
+ final ExecMetaData metaData = new ExecMetaData( command, null, workingDirectory, false );
- // Make sure we do not recurse forever
- exe.setNewenvironment( true );
-
- try
- {
- final int retval = exe.execute();
- if( retval != 0 )
- {
- // Just try to use what we got
- }
- }
- catch( final TaskException te )
+ final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ final int retval = m_execManager.execute( metaData, null, output, output, 0 );
+ if( retval != 0 )
{
- throw new ExecException( te.getMessage(), te );
+ // Just try to use what we got
}
return output.toString();
1.7 +54 -3 jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/DefaultExecManager.java
Index: DefaultExecManager.java
===================================================================
RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/DefaultExecManager.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- DefaultExecManager.java 2 Feb 2002 05:10:36 -0000 1.6
+++ DefaultExecManager.java 2 Feb 2002 06:38:07 -0000 1.7
@@ -12,16 +12,17 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
+import java.util.Properties;
import org.apache.aut.nativelib.ExecException;
import org.apache.aut.nativelib.ExecManager;
import org.apache.aut.nativelib.ExecMetaData;
import org.apache.aut.nativelib.ExecOutputHandler;
import org.apache.aut.nativelib.Os;
+import org.apache.aut.nativelib.impl.launchers.CommandLauncher;
import org.apache.aut.nativelib.impl.launchers.DefaultCommandLauncher;
import org.apache.aut.nativelib.impl.launchers.MacCommandLauncher;
import org.apache.aut.nativelib.impl.launchers.ScriptCommandLauncher;
import org.apache.aut.nativelib.impl.launchers.WinNTCommandLauncher;
-import org.apache.aut.nativelib.impl.launchers.CommandLauncher;
import org.apache.avalon.excalibur.io.FileUtil;
import org.apache.avalon.excalibur.io.IOUtil;
@@ -31,9 +32,10 @@
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
* @author <a href="mailto:thomas.haas@softwired-inc.com">Thomas Haas</a>
- * @version $Revision: 1.6 $ $Date: 2002/02/02 05:10:36 $
+ * @version $Revision: 1.7 $ $Date: 2002/02/02 06:38:07 $
* @see ExecManager
* @see ExecMetaData
+ * @see Environment
*/
public class DefaultExecManager
implements ExecManager
@@ -46,11 +48,35 @@
private final CommandLauncher m_launcher;
private final CommandLauncher m_shellLauncher;
+ /**
+ * Utility class that is used to load and parse the native
+ * environment variables.
+ */
+ private final Environment m_environment;
+
public DefaultExecManager( final File homeDir )
throws ExecException
{
m_launcher = new DefaultCommandLauncher();
m_shellLauncher = createShellLauncher( homeDir );
+ m_environment = new Environment( this );
+ }
+
+ /**
+ * Retrieve a properties object that contains a list of
+ * all the native environment variables.
+ */
+ public Properties getNativeEnvironment()
+ throws ExecException
+ {
+ try
+ {
+ return m_environment.getNativeEnvironment();
+ }
+ catch( final IOException ioe )
+ {
+ throw new ExecException( ioe.getMessage(), ioe );
+ }
}
/**
@@ -79,13 +105,14 @@
* Execute a process and wait for it to finish before
* returning.
*/
- public int execute( final ExecMetaData metaData,
+ public int execute( final ExecMetaData command,
final InputStream input,
final OutputStream output,
final OutputStream error,
final long timeout )
throws IOException, ExecException
{
+ final ExecMetaData metaData = prepareExecMetaData( command );
final CommandLauncher launcher = getLauncher( metaData );
final Process process = launcher.exec( metaData );
final ProcessMonitor monitor =
@@ -130,6 +157,30 @@
catch( final InterruptedException ie )
{
//should never happen
+ }
+ }
+
+ /**
+ * Utility method to preapre a metaData object.
+ * This involves adding the native environment to the metaData if the
+ * metaData is specified as being additive.
+ */
+ private ExecMetaData prepareExecMetaData( final ExecMetaData metaData )
+ throws ExecException
+ {
+ if( !metaData.isEnvironmentAdditive() )
+ {
+ return metaData;
+ }
+ else
+ {
+ final Properties newEnvironment = new Properties();
+ newEnvironment.putAll( getNativeEnvironment() );
+ newEnvironment.putAll( metaData.getEnvironment() );
+ return new ExecMetaData( metaData.getCommand(),
+ newEnvironment,
+ metaData.getWorkingDirectory(),
+ false );
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>