From 65a74dfc9e1accb6b5074973840aaea3c16c9f6d Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sat, 5 Jan 2002 03:09:24 +0000 Subject: [PATCH] Made sure that setting of environment variables for the native exec calls occurs accoridng to epectations. If no properties are specified then null is passed to underlying exec call. if some properties are set and environment is additive then the native environment is added to environment object git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270518 13f79535-47bb-0310-9956-ffa450edef68 --- .../launchers/DefaultCommandLauncher.java | 38 +++++++++++++++++-- .../exec/launchers/Resources.properties | 1 + 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 proposal/myrmidon/src/java/org/apache/myrmidon/framework/exec/launchers/Resources.properties diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/framework/exec/launchers/DefaultCommandLauncher.java b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/exec/launchers/DefaultCommandLauncher.java index fcbdd2ef9..1b3e00d6a 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/framework/exec/launchers/DefaultCommandLauncher.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/exec/launchers/DefaultCommandLauncher.java @@ -11,7 +11,11 @@ import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Properties; +import org.apache.avalon.excalibur.i18n.ResourceManager; +import org.apache.avalon.excalibur.i18n.Resources; import org.apache.myrmidon.framework.exec.CommandLauncher; +import org.apache.myrmidon.framework.exec.Environment; import org.apache.myrmidon.framework.exec.ExecException; import org.apache.myrmidon.framework.exec.ExecMetaData; @@ -27,6 +31,9 @@ import org.apache.myrmidon.framework.exec.ExecMetaData; public class DefaultCommandLauncher implements CommandLauncher { + private static final Resources REZ = + ResourceManager.getPackageResources( DefaultCommandLauncher.class ); + private static final Method c_execWithCWD; static @@ -64,13 +71,12 @@ public class DefaultCommandLauncher { if( ExecUtil.isCwd( metaData.getWorkingDirectory() ) ) { - final String[] env = ExecUtil.toNativeEnvironment( metaData.getEnvironment() ); + final String[] env = getEnvironmentSpec( metaData ); return Runtime.getRuntime().exec( metaData.getCommand(), env ); } else if( null == c_execWithCWD ) { - final String message = "Unable to launch native command in a " + - "working directory other than \".\""; + final String message = REZ.getString( "default.bad-dir.error" ); throw new ExecException( message ); } else @@ -79,6 +85,30 @@ public class DefaultCommandLauncher } } + private String[] getEnvironmentSpec( final ExecMetaData metaData ) + throws ExecException, IOException + { + final Properties environment = metaData.getEnvironment(); + if( 0 == environment.size() ) + { + return null; + } + else + { + if( metaData.isEnvironmentAdditive() ) + { + final Properties newEnvironment = new Properties(); + newEnvironment.putAll( environment ); + newEnvironment.putAll( Environment.getNativeEnvironment() ); + return ExecUtil.toNativeEnvironment( newEnvironment ); + } + else + { + return ExecUtil.toNativeEnvironment( environment ); + } + } + } + /** * Execute the Java1.3 Runtime.exec() 3 parame method that sets working * directory. This needs to be done via reflection so that it can compile @@ -87,7 +117,7 @@ public class DefaultCommandLauncher private Process execJava13( final ExecMetaData metaData ) throws IOException, ExecException { - final String[] env = ExecUtil.toNativeEnvironment( metaData.getEnvironment() ); + final String[] env = getEnvironmentSpec( metaData ); final Object[] args = {metaData.getCommand(), env, diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/framework/exec/launchers/Resources.properties b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/exec/launchers/Resources.properties new file mode 100644 index 000000000..52007c3a1 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/exec/launchers/Resources.properties @@ -0,0 +1 @@ +default.bad-dir.error=Unable to launch native command in a working directory other than "." on Java 1.2 JVMs. \ No newline at end of file