Hi, here is a patch that allows defining system properties in Java Tasks.
This works for internally executed java tasks as well as when using forked VMs.
 
The new nested element is: <sysproperty> and has the same attributes as <env>
on the <exec> task.
 
The patch was constructed with "cvs diff -u"
 
Let me know if there is any problem with it.
 
Jose Alberto
 

 

Attachment: Jose Alberto Fernandez.vcf
Description: Binary data

Index: docs/index.html
===================================================================
RCS file: /home/cvspublic/jakarta-ant/docs/index.html,v
retrieving revision 1.120
diff -u -r1.120 index.html
--- docs/index.html     2000/09/29 15:40:29     1.120
+++ docs/index.html     2000/10/02 20:19:03
@@ -2407,6 +2407,13 @@
 <p>Use nested <code>&lt;arg&gt;</code> and <code>&lt;jvmarg&gt;</code>
 elements to specify arguments for the or the forked VM. See <a
 href="index.html#arg">Command line arguments</a>.</p>
+<h4>sysproperty</h4>
+<p>Use nested <code>&lt;sysproperty&gt;</code>
+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 <a href="index.html#env">environment
+variables</a>.</p>
 <h4>classpath</h4>
 <p><code>Java</code>'s <em>classpath</em> attribute is a <a
 href="#path">PATH like structure</a> and can also be set via a nested
@@ -2425,6 +2432,7 @@
 <pre>  &lt;java classname=&quot;test.Main&quot; /&gt;</pre>
 <pre>  &lt;java classname=&quot;test.Main&quot;
         fork=&quot;yes&quot; &gt;
+    &lt;sysproperty key=&quot;DEBUG&quot; value=&quot;true&quot; /&gt; 
     &lt;arg value=&quot;-h&quot; /&gt; 
     &lt;jvmarg value=&quot;-Xrunhprof:cpu=samples,file=log.txt,depth=3&quot; 
/&gt; 
   &lt;/java&gt;
Index: src/main/org/apache/tools/ant/taskdefs/Java.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Java.java,v
retrieving revision 1.21
diff -u -r1.21 Java.java
--- src/main/org/apache/tools/ant/taskdefs/Java.java    2000/09/20 15:07:53     
1.21
+++ src/main/org/apache/tools/ant/taskdefs/Java.java    2000/10/02 20:19:29
@@ -62,6 +62,7 @@
 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.Environment;
 
 import java.io.File;
 import java.io.IOException;
@@ -204,6 +205,13 @@
     }
         
     /**
+     * Add a nested sysproperty element.
+     */
+    public void addSysproperty(Environment.Variable sysp) {
+        cmdl.addSysproperty(sysp);
+    }
+
+    /**
      * Throw a BuildException if process returns non 0.
      */
     public void setFailonerror(boolean fail) {
@@ -236,7 +244,14 @@
         ExecuteJava exe = new ExecuteJava();
         exe.setJavaCommand(command.getJavaCommand());
         exe.setClasspath(command.getClasspath());
-        exe.execute(project);
+
+        command.setSystemProperties();
+        try {
+          exe.execute(project);
+        }
+        finally {
+          command.restoreSystemProperties();
+        }
     }
 
     /**
Index: src/main/org/apache/tools/ant/types/CommandlineJava.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/types/CommandlineJava.java,v
retrieving revision 1.5
diff -u -r1.5 CommandlineJava.java
--- src/main/org/apache/tools/ant/types/CommandlineJava.java    2000/09/12 
12:26:42     1.5
+++ src/main/org/apache/tools/ant/types/CommandlineJava.java    2000/10/02 
20:19:49
@@ -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,10 +66,70 @@
 
     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.setProperty(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");
         setVmversion(org.apache.tools.ant.Project.getJavaVersion());
@@ -81,6 +143,10 @@
         return vmCommand.createArgument();
     }
 
+    public void addSysproperty(Environment.Variable sysp) {
+        sysProperties.addVariable(sysp);
+    }
+
     public void setVm(String vm) {
         vmCommand.setExecutable(vm);
     }
@@ -109,7 +175,8 @@
     }
 
     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 +184,19 @@
         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 +225,19 @@
         return classpath;
     }
 
+    public void setSystemProperties() throws BuildException {
+        sysProperties.setSystem();
+    }
+
+    public void restoreSystemProperties() throws BuildException {
+        sysProperties.restoreSystem();
+    }
+
     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 +249,5 @@
     public void clearJavaArgs() {
         javaCommand.clearArgs();
     }
+
 }
Index: src/main/org/apache/tools/ant/types/Environment.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/types/Environment.java,v
retrieving revision 1.3
diff -u -r1.3 Environment.java
--- src/main/org/apache/tools/ant/types/Environment.java        2000/08/03 
11:25:14     1.3
+++ src/main/org/apache/tools/ant/types/Environment.java        2000/10/02 
20:19:49
@@ -64,7 +64,7 @@
  */
 public class Environment {
 
-    private Vector variables;
+    protected Vector variables;
 
     public static class Variable {
         private String key, value;
@@ -79,6 +79,14 @@
         
         public void setValue(String value) {
             this.value = value;
+        }
+        
+        public String getKey() {
+            return this.key;
+        }
+        
+        public String getValue() {
+            return this.value;
         }
         
         public void setPath(Path path) {

Reply via email to