Browse Source

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 <JFernandez@viquity.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268080 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
d8bff4d35a
5 changed files with 132 additions and 11 deletions
  1. +8
    -0
      docs/index.html
  2. +16
    -1
      src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java
  3. +10
    -4
      src/main/org/apache/tools/ant/taskdefs/Java.java
  4. +89
    -5
      src/main/org/apache/tools/ant/types/CommandlineJava.java
  5. +9
    -1
      src/main/org/apache/tools/ant/types/Environment.java

+ 8
- 0
docs/index.html View File

@@ -2487,6 +2487,13 @@ the one that is currently running Ant.</p>
<p>Use nested <code>&lt;arg&gt;</code> and <code>&lt;jvmarg&gt;</code> <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 elements to specify arguments for the or the forked VM. See <a
href="index.html#arg">Command line arguments</a>.</p> 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> <h4>classpath</h4>
<p><code>Java</code>'s <em>classpath</em> attribute is a <a <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 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> &lt;java classname=&quot;test.Main&quot; /&gt;</pre> <pre> &lt;java classname=&quot;test.Main&quot; /&gt;</pre>
<pre> &lt;java classname=&quot;test.Main&quot; <pre> &lt;java classname=&quot;test.Main&quot;
fork=&quot;yes&quot; &gt; fork=&quot;yes&quot; &gt;
&lt;sysproperty key=&quot;DEBUG&quot; value=&quot;true&quot; /&gt;
&lt;arg value=&quot;-h&quot; /&gt; &lt;arg value=&quot;-h&quot; /&gt;
&lt;jvmarg value=&quot;-Xrunhprof:cpu=samples,file=log.txt,depth=3&quot; /&gt; &lt;jvmarg value=&quot;-Xrunhprof:cpu=samples,file=log.txt,depth=3&quot; /&gt;
&lt;/java&gt; &lt;/java&gt;


+ 16
- 1
src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java View File

@@ -59,6 +59,7 @@ import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Commandline; 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.Path;


import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@@ -72,6 +73,7 @@ public class ExecuteJava {


private Commandline javaCommand = null; private Commandline javaCommand = null;
private Path classpath = null; private Path classpath = null;
private CommandlineJava.SysProperties sysProperties = null;


public void setJavaCommand(Commandline javaCommand) { public void setJavaCommand(Commandline javaCommand) {
this.javaCommand = javaCommand; this.javaCommand = javaCommand;
@@ -81,10 +83,18 @@ public class ExecuteJava {
classpath = p; classpath = p;
} }


public void setSystemProperties(CommandlineJava.SysProperties s) {
sysProperties = s;
}

public void execute(Project project) throws BuildException{ public void execute(Project project) throws BuildException{
final String classname = javaCommand.getExecutable(); final String classname = javaCommand.getExecutable();
final Object[] argument = { javaCommand.getArguments() }; final Object[] argument = { javaCommand.getArguments() };
try { try {
if (sysProperties != null) {
sysProperties.restoreSystem();
}

final Class[] param = { Class.forName("[Ljava.lang.String;") }; final Class[] param = { Class.forName("[Ljava.lang.String;") };
Class target = null; Class target = null;
if (classpath == null) { if (classpath == null) {
@@ -95,6 +105,7 @@ public class ExecuteJava {
} }
final Method main = target.getMethod("main", param); final Method main = target.getMethod("main", param);
main.invoke(null, argument); main.invoke(null, argument);

} catch (NullPointerException e) { } catch (NullPointerException e) {
throw new BuildException("Could not find main() method in " + classname); throw new BuildException("Could not find main() method in " + classname);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@@ -108,6 +119,10 @@ public class ExecuteJava {
// if the invoked application tried to call System.exit() // if the invoked application tried to call System.exit()
} catch (Exception e) { } catch (Exception e) {
throw new BuildException(e); throw new BuildException(e);
} finally {
if (sysProperties != null) {
sysProperties.restoreSystem();
}
} }
}
}
} }

+ 10
- 4
src/main/org/apache/tools/ant/taskdefs/Java.java View File

@@ -58,10 +58,7 @@ import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task; 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.File;
import java.io.IOException; import java.io.IOException;
@@ -203,6 +200,13 @@ public class Java extends Task {
cmdl.setVm(s); 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. * Throw a BuildException if process returns non 0.
*/ */
@@ -236,6 +240,8 @@ public class Java extends Task {
ExecuteJava exe = new ExecuteJava(); ExecuteJava exe = new ExecuteJava();
exe.setJavaCommand(command.getJavaCommand()); exe.setJavaCommand(command.getJavaCommand());
exe.setClasspath(command.getClasspath()); exe.setClasspath(command.getClasspath());
exe.setSystemProperties(command.getSystemProperties());
exe.execute(project); exe.execute(project);
} }




+ 89
- 5
src/main/org/apache/tools/ant/types/CommandlineJava.java View File

@@ -54,7 +54,9 @@


package org.apache.tools.ant.types; package org.apache.tools.ant.types;


import java.util.*;
import org.apache.tools.ant.Project; 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 vmCommand = new Commandline();
private Commandline javaCommand = new Commandline(); private Commandline javaCommand = new Commandline();
private SysProperties sysProperties = new SysProperties();
private Path classpath = null; private Path classpath = null;
private String vmVersion; 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() { public CommandlineJava() {
setVm("java"); setVm("java");
@@ -81,6 +140,10 @@ public class CommandlineJava implements Cloneable {
return vmCommand.createArgument(); return vmCommand.createArgument();
} }


public void addSysproperty(Environment.Variable sysp) {
sysProperties.addVariable(sysp);
}

public void setVm(String vm) { public void setVm(String vm) {
vmCommand.setExecutable(vm); vmCommand.setExecutable(vm);
} }
@@ -109,7 +172,8 @@ public class CommandlineJava implements Cloneable {
} }


public String[] getCommandline() { public String[] getCommandline() {
int size = vmCommand.size() + javaCommand.size();
int size =
vmCommand.size() + javaCommand.size() + sysProperties.size();
if (classpath != null && classpath.size() > 0) { if (classpath != null && classpath.size() > 0) {
size += 2; size += 2;
} }
@@ -117,13 +181,19 @@ public class CommandlineJava implements Cloneable {
String[] result = new String[size]; String[] result = new String[size];
System.arraycopy(vmCommand.getCommandline(), 0, System.arraycopy(vmCommand.getCommandline(), 0,
result, 0, vmCommand.size()); 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) { 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, System.arraycopy(javaCommand.getCommandline(), 0,
result, result.length-javaCommand.size(),
javaCommand.size());
result, pos, javaCommand.size());
return result; return result;
} }


@@ -152,10 +222,23 @@ public class CommandlineJava implements Cloneable {
return classpath; return classpath;
} }


public void setSystemProperties() throws BuildException {
sysProperties.setSystem();
}

public void restoreSystemProperties() throws BuildException {
sysProperties.restoreSystem();
}

public SysProperties getSystemProperties() {
return sysProperties;
}

public Object clone() { public Object clone() {
CommandlineJava c = new CommandlineJava(); CommandlineJava c = new CommandlineJava();
c.vmCommand = (Commandline) vmCommand.clone(); c.vmCommand = (Commandline) vmCommand.clone();
c.javaCommand = (Commandline) javaCommand.clone(); c.javaCommand = (Commandline) javaCommand.clone();
c.sysProperties = (SysProperties) sysProperties.clone();
c.classpath = (Path) classpath.clone(); c.classpath = (Path) classpath.clone();
c.vmVersion = vmVersion; c.vmVersion = vmVersion;
return c; return c;
@@ -167,4 +250,5 @@ public class CommandlineJava implements Cloneable {
public void clearJavaArgs() { public void clearJavaArgs() {
javaCommand.clearArgs(); javaCommand.clearArgs();
} }

} }

+ 9
- 1
src/main/org/apache/tools/ant/types/Environment.java View File

@@ -64,7 +64,7 @@ import java.util.Vector;
*/ */
public class Environment { public class Environment {


private Vector variables;
protected Vector variables;


public static class Variable { public static class Variable {
private String key, value; private String key, value;
@@ -81,6 +81,14 @@ public class Environment {
this.value = value; this.value = value;
} }
public String getKey() {
return this.key;
}
public String getValue() {
return this.value;
}
public void setPath(Path path) { public void setPath(Path path) {
this.value = path.toString(); this.value = path.toString();
} }


Loading…
Cancel
Save