set system properties even in non-fork mode. Submitted by: Jose Alberto Fernandez <JFernandez@viquity.com> git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268080 13f79535-47bb-0310-9956-ffa450edef68master
@@ -2487,6 +2487,13 @@ the one that is currently running Ant.</p> | |||
<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 @@ href="#path">PATH like structure</a> and can also be set via a nested | |||
<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> | |||
@@ -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(); | |||
} | |||
} | |||
} | |||
} | |||
} |
@@ -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); | |||
} | |||
@@ -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(); | |||
} | |||
} |
@@ -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(); | |||
} | |||