bodewig 00/10/12 08:59:18
Modified: docs index.html
src/main/org/apache/tools/ant/taskdefs ExecuteJava.java
Java.java
src/main/org/apache/tools/ant/types CommandlineJava.java
Environment.java
Log:
Added a nested sysproperty element to <java>. This allows the user to
set system properties even in non-fork mode.
Submitted by: Jose Alberto Fernandez <[EMAIL PROTECTED]>
Revision Changes Path
1.129 +8 -0 jakarta-ant/docs/index.html
Index: index.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/index.html,v
retrieving revision 1.128
retrieving revision 1.129
diff -u -r1.128 -r1.129
--- index.html 2000/10/12 15:00:22 1.128
+++ index.html 2000/10/12 15:59:14 1.129
@@ -2487,6 +2487,13 @@
<p>Use nested <code><arg></code> and <code><jvmarg></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><sysproperty></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
@@ -2505,6 +2512,7 @@
<pre> <java classname="test.Main" /></pre>
<pre> <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>
1.4 +16 -1
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java
Index: ExecuteJava.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ExecuteJava.java 2000/07/31 12:09:28 1.3
+++ ExecuteJava.java 2000/10/12 15:59:16 1.4
@@ -59,6 +59,7 @@
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 @@
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 @@
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 @@
}
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 @@
// if the invoked application tried to call System.exit()
} catch (Exception e) {
throw new BuildException(e);
+ } finally {
+ if (sysProperties != null) {
+ sysProperties.restoreSystem();
+ }
}
- }
+ }
}
1.22 +10 -4
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Java.java
Index: Java.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Java.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- Java.java 2000/09/20 15:07:53 1.21
+++ Java.java 2000/10/12 15:59:16 1.22
@@ -58,10 +58,7 @@
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;
@@ -204,6 +201,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,6 +240,8 @@
ExecuteJava exe = new ExecuteJava();
exe.setJavaCommand(command.getJavaCommand());
exe.setClasspath(command.getClasspath());
+ exe.setSystemProperties(command.getSystemProperties());
+
exe.execute(project);
}
1.6 +89 -5
jakarta-ant/src/main/org/apache/tools/ant/types/CommandlineJava.java
Index: CommandlineJava.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/types/CommandlineJava.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- CommandlineJava.java 2000/09/12 12:26:42 1.5
+++ CommandlineJava.java 2000/10/12 15:59:18 1.6
@@ -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,67 @@
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");
setVmversion(org.apache.tools.ant.Project.getJavaVersion());
@@ -81,6 +140,10 @@
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 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 @@
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 @@
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 void clearJavaArgs() {
javaCommand.clearArgs();
}
+
}
1.4 +9 -1
jakarta-ant/src/main/org/apache/tools/ant/types/Environment.java
Index: Environment.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/types/Environment.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Environment.java 2000/08/03 11:25:14 1.3
+++ Environment.java 2000/10/12 15:59:18 1.4
@@ -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) {