bodewig 00/07/31 05:09:33
Modified: . WHATSNEW build.xml
src/main/org/apache/tools/ant/taskdefs ExecuteJava.java
Java.java
src/main/org/apache/tools/ant/taskdefs/optional/ejb
DDCreator.java EjbJar.java Ejbc.java WLRun.java
WLStop.java
src/main/org/apache/tools/ant/types CommandlineJava.java
Log:
Java rewritten so that it no longer extends Exec.
Revision Changes Path
1.6 +4 -2 jakarta-ant/WHATSNEW
Index: WHATSNEW
===================================================================
RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- WHATSNEW 2000/07/28 11:49:25 1.5
+++ WHATSNEW 2000/07/31 12:09:25 1.6
@@ -10,11 +10,13 @@
* Path and EnumeratedAttribute have been moved from
org.apache.tools.ant to org.apache.tools.ant.types.
+* the class attribute of <java> has been removed.
+
Other changes:
--------------
-* New tasks: sql, junit, javacc, execon. All pending documentation,
-most of them pending review.
+* New tasks: sql, junit, mparse, execon. All except sql pending
+documentation, most of them pending review.
* <java> uses ClassLoader of its own in no-fork mode if a classpath is
specified.
1.51 +1 -1 jakarta-ant/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-ant/build.xml,v
retrieving revision 1.50
retrieving revision 1.51
diff -u -r1.50 -r1.51
--- build.xml 2000/07/31 00:27:38 1.50
+++ build.xml 2000/07/31 12:09:25 1.51
@@ -295,8 +295,8 @@
<java fork="yes"
classname="junit.textui.TestRunner"
- args="org.apache.tools.ant.AllJUnitTests"
taskname="junit">
+ <arg value="org.apache.tools.ant.AllJUnitTests" />
<classpath>
<pathelement location="${lib.dir}/${name}.jar" />
<pathelement location="${build.tests}" />
1.3 +18 -5
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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ExecuteJava.java 2000/07/24 15:52:57 1.2
+++ ExecuteJava.java 2000/07/31 12:09:28 1.3
@@ -55,9 +55,11 @@
package org.apache.tools.ant.taskdefs;
+import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.types.Path;
+import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Commandline;
+import org.apache.tools.ant.types.Path;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -69,17 +71,28 @@
public class ExecuteJava {
private Commandline javaCommand = null;
+ private Path classpath = null;
public void setJavaCommand(Commandline javaCommand) {
this.javaCommand = javaCommand;
}
+
+ public void setClasspath(Path p) {
+ classpath = p;
+ }
- public void execute() throws BuildException{
+ public void execute(Project project) throws BuildException{
final String classname = javaCommand.getExecutable();
final Object[] argument = { javaCommand.getArguments() };
- final Class[] param = { argument[0].getClass() };
try {
- final Class target = Class.forName(classname);
+ final Class[] param = { Class.forName("[Ljava.lang.String;") };
+ Class target = null;
+ if (classpath == null) {
+ target = Class.forName(classname);
+ } else {
+ AntClassLoader loader = new AntClassLoader(project,
classpath);
+ target = loader.forceLoadClass(classname);
+ }
final Method main = target.getMethod("main", param);
main.invoke(null, argument);
} catch (NullPointerException e) {
@@ -89,7 +102,7 @@
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (!(t instanceof SecurityException)) {
- throw new BuildException(t.toString());
+ throw new BuildException(t);
}
// else ignore because the security exception is thrown
// if the invoked application tried to call System.exit()
1.13 +94 -104
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.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- Java.java 2000/07/24 15:52:57 1.12
+++ Java.java 2000/07/31 12:09:28 1.13
@@ -57,8 +57,13 @@
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 java.io.File;
+import java.io.IOException;
import java.lang.reflect.*;
import java.util.*;
@@ -68,28 +73,35 @@
*
* @author Stefano Mazzocchi <a href="mailto:[EMAIL PROTECTED]">[EMAIL
PROTECTED]</a>
*/
-public class Java extends Exec {
+public class Java extends Task {
- private String classname = null;
- private String args = null;
- private String jvmargs = null;
- private Path classpath = null;
+ private CommandlineJava cmdl = new CommandlineJava();
private boolean fork = false;
+ private File dir = null;
+ private boolean failOnError = false;
/**
* Do the execution.
*/
public void execute() throws BuildException {
- executeJava();
+ int err = -1;
+ if ((err = executeJava()) != 0) {
+ if (failOnError) {
+ throw new BuildException("Java returned: "+err, location);
+ } else {
+ log("Java Result: " + err, Project.MSG_ERR);
+ }
+ }
}
/**
* Do the execution and return a return code.
*
- * @return the return code from the execute java cklass if it was
executed in
+ * @return the return code from the execute java class if it was
executed in
* a separate VM (fork = "yes").
*/
public int executeJava() throws BuildException {
+ String classname = cmdl.getClassname();
log("Calling " + classname, Project.MSG_VERBOSE);
if (classname == null) {
@@ -97,32 +109,16 @@
}
if (fork) {
- StringBuffer b = new StringBuffer();
- b.append("java ");
- if (classpath != null) {
- b.append("-classpath ");
- b.append(classpath.toString());
- b.append(" ");
- }
- if (jvmargs != null) {
- b.append(jvmargs);
- b.append(" ");
+ return run(cmdl.getCommandline());
+ } else {
+ if (cmdl.getVmCommand().size() > 1) {
+ log("JVM args ignored when same JVM is used.",
Project.MSG_WARN);
}
- b.append(classname);
- if (args != null) {
- b.append(" ");
- b.append(args);
+ if (dir != null) {
+ log("Working directory ignored when same JVM is used.",
Project.MSG_WARN);
}
- return run(b.toString());
- } else {
- Vector argList = tokenize(args);
- if (jvmargs != null) {
- log("JVM args and classpath ignored when same JVM is used.",
Project.MSG_VERBOSE);
- }
-
- log("Java args: " + argList.toString(), Project.MSG_VERBOSE);
- run(classname, argList);
+ run(cmdl);
return 0;
}
}
@@ -131,123 +127,117 @@
* Set the classpath to be used for this compilation.
*/
public void setClasspath(Path s) {
- if (this.classpath == null) {
- this.classpath = s;
- } else {
- this.classpath.append(s);
- }
+ createClasspath().append(s);
}
/**
* Creates a nested classpath element
*/
public Path createClasspath() {
- if (classpath == null) {
- classpath = new Path(project);
- }
- return classpath;
+ return cmdl.createClasspath(project);
}
/**
- * Set the source file (deprecated).
+ * Set the class name.
*/
- public void setClass(String s) {
- log("The class attribute is deprecated. " +
- "Please use the classname attribute.",
- Project.MSG_WARN);
- this.classname = s;
+ public void setClassname(String s) {
+ cmdl.setClassname(s);
}
/**
- * Set the source file.
+ * Set the command line arguments for the class.
*/
- public void setClassname(String s) {
- this.classname = s;
+ public void setArgs(String s) {
+ cmdl.createArgument().setLine(s);
}
/**
- * Set the destination file.
+ * Creates a nested arg element.
*/
- public void setArgs(String s) {
- this.args = s;
+ public Commandline.Argument createArg() {
+ return cmdl.createArgument();
}
/**
* Set the forking flag.
*/
- public void setFork(String s) {
- this.fork = Project.toBoolean(s);
+ public void setFork(boolean s) {
+ this.fork = s;
}
/**
- * Set the jvm arguments.
+ * Set the command line arguments for the JVM.
*/
public void setJvmargs(String s) {
- this.jvmargs = s;
+ cmdl.createVmArgument().setLine(s);
}
/**
+ * Creates a nested jvmarg element.
+ */
+ public Commandline.Argument createJvmarg() {
+ return cmdl.createVmArgument();
+ }
+
+ /**
+ * Throw a BuildException if process returns non 0.
+ */
+ public void setFailonerror(boolean fail) {
+ failOnError = fail;
+ }
+
+ /**
+ * The working directory of the process
+ */
+ public void setDir(File d) {
+ this.dir = d;
+ }
+
+ /**
* Executes the given classname with the given arguments as it
* was a command line application.
*/
- protected void run(String classname, Vector args) throws BuildException {
- try {
- Class c = null;
- if (classpath == null) {
- c = Class.forName(classname);
- }
- else {
- AntClassLoader loader = new AntClassLoader(project,
classpath);
- c = loader.forceLoadClass(classname);
- }
-
- Class[] param = { Class.forName("[Ljava.lang.String;") };
- Method main = c.getMethod("main", param);
- Object[] a = { array(args) };
- main.invoke(null, a);
- } catch (NullPointerException e) {
- throw new BuildException("Could not find main() method in " +
classname);
- } catch (ClassNotFoundException e) {
- throw new BuildException("Could not find " + classname + ". Make
sure you have it in your classpath");
- } catch (InvocationTargetException e) {
- Throwable t = e.getTargetException();
- if (!(t instanceof SecurityException)) {
- throw new BuildException(t.toString());
- }
- // else ignore because the security exception is thrown
- // if the invoked application tried to call System.exit()
- } catch (Exception e) {
- throw new BuildException(e.toString());
- }
+ private void run(CommandlineJava command) throws BuildException {
+ ExecuteJava exe = new ExecuteJava();
+ exe.setJavaCommand(command.getJavaCommand());
+ exe.setClasspath(command.getClasspath());
+ exe.execute(project);
}
/**
- * Transforms an argument string into a vector of strings.
+ * Executes the given classname with the given arguments in a separate
VM.
*/
- protected Vector tokenize(String args) {
- Vector v = new Vector();
- if (args == null) return v;
+ private int run(String[] command) throws BuildException {
+ Execute exe = new Execute(new LogStreamHandler(this,
Project.MSG_INFO,
+ Project.MSG_WARN),
+ null);
+ exe.setAntRun(project);
- StringTokenizer t = new StringTokenizer(args, " ");
-
- while (t.hasMoreTokens()) {
- v.addElement(t.nextToken());
- }
+ if (dir == null) dir = project.getBaseDir();
+ exe.setWorkingDirectory(dir);
- return v;
+ exe.setCommandline(command);
+ try {
+ return exe.execute();
+ } catch (IOException e) {
+ throw new BuildException(e, location);
+ }
}
-
+
/**
- * Transforms a vector of strings into an array.
+ * Executes the given classname with the given arguments as it
+ * was a command line application.
*/
- protected String[] array(Vector v) {
- String[] s = new String[v.size()];
- Enumeration e = v.elements();
-
- for (int i = 0; e.hasMoreElements(); i++) {
- s[i] = (String) e.nextElement();
+ protected void run(String classname, Vector args) throws BuildException {
+ CommandlineJava cmdj = new CommandlineJava();
+ cmdj.setClassname(classname);
+ for (int i=0; i<args.size(); i++) {
+ cmdj.createArgument().setValue((String) args.elementAt(i));
}
-
- return s;
+ if (cmdl.getClasspath() != null) {
+ cmdj.createClasspath(project).append(cmdl.getClasspath());
+ }
+ run(cmdj);
}
+
}
1.5 +1 -1
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/DDCreator.java
Index: DDCreator.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/DDCreator.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- DDCreator.java 2000/07/24 15:53:00 1.4
+++ DDCreator.java 2000/07/31 12:09:29 1.5
@@ -126,7 +126,7 @@
String systemClassPath = System.getProperty("java.class.path");
String execClassPath = project.translatePath(systemClassPath + ":" +
classpath);
Java ddCreatorTask = (Java)project.createTask("java");
- ddCreatorTask.setFork("yes");
+ ddCreatorTask.setFork(true);
ddCreatorTask.setClassname("org.apache.tools.ant.taskdefs.optional.ejb.DDCreatorHelper");
ddCreatorTask.setArgs(args);
ddCreatorTask.setClasspath(new Path(project, execClassPath));
1.3 +1 -1
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/EjbJar.java
Index: EjbJar.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/EjbJar.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- EjbJar.java 2000/07/26 12:17:30 1.2
+++ EjbJar.java 2000/07/31 12:09:29 1.3
@@ -448,7 +448,7 @@
javaTask = (Java) this.project.createTask("java");
javaTask.setClassname("weblogic.ejbc");
javaTask.setArgs(args);
- javaTask.setFork("false");
+ javaTask.setFork(false);
this.log("Calling weblogic.ejbc for " + sourceJar.toString(),
Project.MSG_INFO);
1.5 +1 -1
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/Ejbc.java
Index: Ejbc.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/Ejbc.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Ejbc.java 2000/07/24 15:53:01 1.4
+++ Ejbc.java 2000/07/31 12:09:30 1.5
@@ -137,7 +137,7 @@
String[] files = ds.getIncludedFiles();
Java helperTask = (Java)project.createTask("java");
- helperTask.setFork("yes");
+ helperTask.setFork(true);
helperTask.setClassname("org.apache.tools.ant.taskdefs.optional.ejb.EjbcHelper");
String args = "";
args += " " + descriptorDirectory;
1.4 +1 -1
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WLRun.java
Index: WLRun.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WLRun.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- WLRun.java 2000/07/24 15:53:01 1.3
+++ WLRun.java 2000/07/31 12:09:30 1.4
@@ -146,7 +146,7 @@
String execClassPath = project.translatePath(classpath);
Java weblogicServer = (Java)project.createTask("java");
- weblogicServer.setFork("yes");
+ weblogicServer.setFork(true);
weblogicServer.setClassname("weblogic.Server");
String jvmArgs = "";
if (weblogicClasspath != null) {
1.4 +1 -1
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WLStop.java
Index: WLStop.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WLStop.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- WLStop.java 2000/07/24 15:53:01 1.3
+++ WLStop.java 2000/07/31 12:09:30 1.4
@@ -114,7 +114,7 @@
String execClassPath = project.translatePath(classpath);
Java weblogicAdmin = (Java)project.createTask("java");
- weblogicAdmin.setFork("yes");
+ weblogicAdmin.setFork(true);
weblogicAdmin.setClassname("weblogic.Admin");
String args = serverURL + " SHUTDOWN " + username + " " + password +
" " + delay;
1.3 +16 -0
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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- CommandlineJava.java 2000/07/24 15:53:06 1.2
+++ CommandlineJava.java 2000/07/31 12:09:32 1.3
@@ -93,6 +93,10 @@
javaCommand.setExecutable(classname);
}
+ public String getClassname() {
+ return javaCommand.getExecutable();
+ }
+
public Path createClasspath(Project p) {
if (classpath == null) {
classpath = new Path(p);
@@ -134,5 +138,17 @@
size += 2;
}
return size;
+ }
+
+ public Commandline getJavaCommand() {
+ return javaCommand;
+ }
+
+ public Commandline getVmCommand() {
+ return vmCommand;
+ }
+
+ public Path getClasspath() {
+ return classpath;
}
}