From d8bff4d35a6dc5fed4a19e92fdfc1e7b3e4ddadf Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 12 Oct 2000 15:59:18 +0000 Subject: [PATCH] Added a nested sysproperty element to . This allows the user to set system properties even in non-fork mode. Submitted by: Jose Alberto Fernandez git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268080 13f79535-47bb-0310-9956-ffa450edef68 --- docs/index.html | 8 ++ .../tools/ant/taskdefs/ExecuteJava.java | 17 +++- .../org/apache/tools/ant/taskdefs/Java.java | 14 ++- .../tools/ant/types/CommandlineJava.java | 94 ++++++++++++++++++- .../apache/tools/ant/types/Environment.java | 10 +- 5 files changed, 132 insertions(+), 11 deletions(-) diff --git a/docs/index.html b/docs/index.html index fbe8c9b8e..c19a7e30d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -2487,6 +2487,13 @@ the one that is currently running Ant.

Use nested <arg> and <jvmarg> elements to specify arguments for the or the forked VM. See Command line arguments.

+

sysproperty

+

Use nested <sysproperty> +elements to specify system properties required by the class. +These properties will be made available to the VM during the execution +of the class (either ANT's VM or the forked VM). The attributes +for this element are the same as for environment +variables.

classpath

Java's classpath attribute is a PATH like structure and can also be set via a nested @@ -2505,6 +2512,7 @@ href="#path">PATH like structure and can also be set via a nested

  <java classname="test.Main" />
  <java classname="test.Main"
         fork="yes" >
+    <sysproperty key="DEBUG" value="true" /> 
     <arg value="-h" /> 
     <jvmarg value="-Xrunhprof:cpu=samples,file=log.txt,depth=3" /> 
   </java>
diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java b/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java
index fbe7be5f7..33dba5feb 100644
--- a/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java
+++ b/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java
@@ -59,6 +59,7 @@ import org.apache.tools.ant.AntClassLoader;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.types.Commandline;
+import org.apache.tools.ant.types.CommandlineJava;
 import org.apache.tools.ant.types.Path;
 
 import java.lang.reflect.InvocationTargetException;
@@ -72,6 +73,7 @@ public class ExecuteJava {
 
     private Commandline javaCommand = null;
     private Path classpath = null;
+    private CommandlineJava.SysProperties sysProperties = null;
 
     public void setJavaCommand(Commandline javaCommand) {
         this.javaCommand = javaCommand;
@@ -81,10 +83,18 @@ public class ExecuteJava {
         classpath = p;
     }
 
+    public void setSystemProperties(CommandlineJava.SysProperties s) {
+        sysProperties = s;
+    }
+
     public void execute(Project project) throws BuildException{
         final String classname = javaCommand.getExecutable();
         final Object[] argument = { javaCommand.getArguments() };
         try {
+            if (sysProperties != null) {
+                sysProperties.restoreSystem();
+            }
+
             final Class[] param = { Class.forName("[Ljava.lang.String;") };
             Class target = null;
             if (classpath == null) {
@@ -95,6 +105,7 @@ public class ExecuteJava {
             }
             final Method main = target.getMethod("main", param);
             main.invoke(null, argument);
+
         } catch (NullPointerException e) {
             throw new BuildException("Could not find main() method in " + classname);
         } catch (ClassNotFoundException e) {
@@ -108,6 +119,10 @@ public class ExecuteJava {
             // if the invoked application tried to call System.exit()
         } catch (Exception e) {
             throw new BuildException(e);
+        } finally {
+            if (sysProperties != null) {
+                sysProperties.restoreSystem();
+            }
         }
-   }
+    }
 }
diff --git a/src/main/org/apache/tools/ant/taskdefs/Java.java b/src/main/org/apache/tools/ant/taskdefs/Java.java
index 6fd699bdf..f928bc561 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Java.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Java.java
@@ -58,10 +58,7 @@ import org.apache.tools.ant.AntClassLoader;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.Task;
-import org.apache.tools.ant.types.Commandline;
-import org.apache.tools.ant.types.CommandlineJava;
-import org.apache.tools.ant.types.Path;
-import org.apache.tools.ant.types.Reference;
+import org.apache.tools.ant.types.*;
 
 import java.io.File;
 import java.io.IOException;
@@ -203,6 +200,13 @@ public class Java extends Task {
         cmdl.setVm(s);
     }
         
+    /**
+     * Add a nested sysproperty element.
+     */
+    public void addSysproperty(Environment.Variable sysp) {
+        cmdl.addSysproperty(sysp);
+    }
+
     /**
      * Throw a BuildException if process returns non 0.
      */
@@ -236,6 +240,8 @@ public class Java extends Task {
         ExecuteJava exe = new ExecuteJava();
         exe.setJavaCommand(command.getJavaCommand());
         exe.setClasspath(command.getClasspath());
+        exe.setSystemProperties(command.getSystemProperties());
+        
         exe.execute(project);
     }
 
diff --git a/src/main/org/apache/tools/ant/types/CommandlineJava.java b/src/main/org/apache/tools/ant/types/CommandlineJava.java
index 3b5e11d2f..025bb321a 100644
--- a/src/main/org/apache/tools/ant/types/CommandlineJava.java
+++ b/src/main/org/apache/tools/ant/types/CommandlineJava.java
@@ -54,7 +54,9 @@
 
 package org.apache.tools.ant.types;
 
+import java.util.*;
 import org.apache.tools.ant.Project;
+import org.apache.tools.ant.BuildException;
 
 /*
  *
@@ -64,9 +66,66 @@ public class CommandlineJava implements Cloneable {
 
     private Commandline vmCommand = new Commandline();
     private Commandline javaCommand = new Commandline();
+    private SysProperties sysProperties = new SysProperties();
     private Path classpath = null;
     private String vmVersion;
 
+    /**
+     * Specialized Environment class for System properties
+     */
+    public static class SysProperties extends Environment implements Cloneable {
+        Properties sys = null;
+
+        public String[] getVariables() throws BuildException {
+            String props[] = super.getVariables();
+      
+            if (props == null) return null;
+
+            for (int i = 0; i < props.length; i++) {
+                props[i] = "-D" + props[i];
+            }
+            return props;
+        }
+
+        public int size() {
+            return variables.size();
+        }
+
+        public void setSystem() throws BuildException {
+            try {
+                Properties p = new Properties(sys = System.getProperties());
+        
+                for (Enumeration e = variables.elements(); e.hasMoreElements(); ) {
+                    Environment.Variable v = (Environment.Variable) e.nextElement();
+                    p.put(v.getKey(), v.getValue());
+                }
+                System.setProperties(p);
+            } catch (SecurityException e) {
+                throw new BuildException("Cannot modify system properties", e);
+            }
+        }
+
+        public void restoreSystem() throws BuildException {
+            if (sys == null)
+                throw new BuildException("Unbalanced nesting of SysProperties");
+
+            try {
+                System.setProperties(sys);
+                sys = null;
+            } catch (SecurityException e) {
+                throw new BuildException("Cannot modify system properties", e);
+            }
+        }
+
+        public Object clone() {
+            try {
+                SysProperties c = (SysProperties) super.clone();
+                c.variables = (Vector) variables.clone();
+                return c;
+            } catch(CloneNotSupportedException e){return null;}
+        }
+
+    }
 
     public CommandlineJava() {
         setVm("java");
@@ -81,6 +140,10 @@ public class CommandlineJava implements Cloneable {
         return vmCommand.createArgument();
     }
 
+    public void addSysproperty(Environment.Variable sysp) {
+        sysProperties.addVariable(sysp);
+    }
+
     public void setVm(String vm) {
         vmCommand.setExecutable(vm);
     }
@@ -109,7 +172,8 @@ public class CommandlineJava implements Cloneable {
     }
 
     public String[] getCommandline() {
-        int size = vmCommand.size() + javaCommand.size();
+        int size = 
+            vmCommand.size() + javaCommand.size() + sysProperties.size();
         if (classpath != null && classpath.size() > 0) {
             size += 2;
         }
@@ -117,13 +181,19 @@ public class CommandlineJava implements Cloneable {
         String[] result = new String[size];
         System.arraycopy(vmCommand.getCommandline(), 0, 
                          result, 0, vmCommand.size());
+
+        int pos = vmCommand.size();
+        if (sysProperties.size() > 0) {
+            System.arraycopy(sysProperties.getVariables(), 0,
+                             result, pos, sysProperties.size());
+            pos += sysProperties.size();
+        }
         if (classpath != null && classpath.size() > 0) {
-            result[vmCommand.size()] = "-classpath";
-            result[vmCommand.size()+1] = classpath.toString();
+            result[pos++] = "-classpath";
+            result[pos++] = classpath.toString();
         }
         System.arraycopy(javaCommand.getCommandline(), 0, 
-                         result, result.length-javaCommand.size(), 
-                         javaCommand.size());
+                         result, pos, javaCommand.size());
         return result;
     }
 
@@ -152,10 +222,23 @@ public class CommandlineJava implements Cloneable {
         return classpath;
     }
 
+    public void setSystemProperties() throws BuildException {
+        sysProperties.setSystem();
+    }
+
+    public void restoreSystemProperties() throws BuildException {
+        sysProperties.restoreSystem();
+    }
+
+    public SysProperties getSystemProperties() {
+        return sysProperties;
+    }
+
     public Object clone() {
         CommandlineJava c = new CommandlineJava();
         c.vmCommand = (Commandline) vmCommand.clone();
         c.javaCommand = (Commandline) javaCommand.clone();
+        c.sysProperties = (SysProperties) sysProperties.clone();
         c.classpath = (Path) classpath.clone();
         c.vmVersion = vmVersion;
         return c;
@@ -167,4 +250,5 @@ public class CommandlineJava implements Cloneable {
     public void clearJavaArgs() {
         javaCommand.clearArgs();
     }
+
 }
diff --git a/src/main/org/apache/tools/ant/types/Environment.java b/src/main/org/apache/tools/ant/types/Environment.java
index 9cbdd692c..1aa0c0157 100644
--- a/src/main/org/apache/tools/ant/types/Environment.java
+++ b/src/main/org/apache/tools/ant/types/Environment.java
@@ -64,7 +64,7 @@ import java.util.Vector;
  */
 public class Environment {
 
-    private Vector variables;
+    protected Vector variables;
 
     public static class Variable {
         private String key, value;
@@ -81,6 +81,14 @@ public class Environment {
             this.value = value;
         }
         
+        public String getKey() {
+            return this.key;
+        }
+        
+        public String getValue() {
+            return this.value;
+        }
+        
         public void setPath(Path path) {
             this.value = path.toString();
         }