source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 9.0 KB
Line 
1/*
2 * Copyright 2000-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs;
19
20import java.io.File;
21import java.io.IOException;
22import java.io.PrintStream;
23import java.lang.reflect.InvocationTargetException;
24import java.lang.reflect.Method;
25import java.lang.reflect.Modifier;
26import org.apache.tools.ant.AntClassLoader;
27import org.apache.tools.ant.BuildException;
28import org.apache.tools.ant.Project;
29import org.apache.tools.ant.ProjectComponent;
30import org.apache.tools.ant.Task;
31import org.apache.tools.ant.taskdefs.condition.Os;
32import org.apache.tools.ant.types.Commandline;
33import org.apache.tools.ant.types.CommandlineJava;
34import org.apache.tools.ant.types.Path;
35import org.apache.tools.ant.types.Permissions;
36import org.apache.tools.ant.util.JavaEnvUtils;
37import org.apache.tools.ant.util.TimeoutObserver;
38import org.apache.tools.ant.util.Watchdog;
39
40/**
41 *
42 * @since Ant 1.2
43 */
44public class ExecuteJava implements Runnable, TimeoutObserver {
45
46 private Commandline javaCommand = null;
47 private Path classpath = null;
48 private CommandlineJava.SysProperties sysProperties = null;
49 private Permissions perm = null;
50 private Method main = null;
51 private Long timeout = null;
52 private Throwable caught = null;
53 private boolean timedOut = false;
54 private Thread thread = null;
55
56 public void setJavaCommand(Commandline javaCommand) {
57 this.javaCommand = javaCommand;
58 }
59
60 /**
61 * Set the classpath to be used when running the Java class
62 *
63 * @param p an Ant Path object containing the classpath.
64 */
65 public void setClasspath(Path p) {
66 classpath = p;
67 }
68
69 public void setSystemProperties(CommandlineJava.SysProperties s) {
70 sysProperties = s;
71 }
72
73 /**
74 * Permissions for the application run.
75 * @since Ant 1.6
76 * @param permissions
77 */
78 public void setPermissions(Permissions permissions) {
79 perm = permissions;
80 }
81
82 /**
83 * All output (System.out as well as System.err) will be written
84 * to this Stream.
85 *
86 * @deprecated manage output at the task level
87 */
88 public void setOutput(PrintStream out) {
89 }
90
91 /**
92 * @since Ant 1.5
93 */
94 public void setTimeout(Long timeout) {
95 this.timeout = timeout;
96 }
97
98 public void execute(Project project) throws BuildException {
99 final String classname = javaCommand.getExecutable();
100
101 AntClassLoader loader = null;
102 try {
103 if (sysProperties != null) {
104 sysProperties.setSystem();
105 }
106
107 final Class[] param = {Class.forName("[Ljava.lang.String;")};
108 Class target = null;
109 if (classpath == null) {
110 target = Class.forName(classname);
111 } else {
112 loader = project.createClassLoader(classpath);
113 loader.setParent(project.getCoreLoader());
114 loader.setParentFirst(false);
115 loader.addJavaLibraries();
116 loader.setIsolated(true);
117 loader.setThreadContextLoader();
118 loader.forceLoadClass(classname);
119 target = Class.forName(classname, true, loader);
120 }
121 main = target.getMethod("main", param);
122 if (main == null) {
123 throw new BuildException("Could not find main() method in "
124 + classname);
125 }
126
127 if ((main.getModifiers() & Modifier.STATIC) == 0) {
128 throw new BuildException("main() method in " + classname
129 + " is not declared static");
130 }
131
132
133 if (timeout == null) {
134 run();
135 } else {
136 thread = new Thread(this, "ExecuteJava");
137 Task currentThreadTask
138 = project.getThreadTask(Thread.currentThread());
139 project.registerThreadTask(thread, currentThreadTask);
140 // if we run into a timeout, the run-away thread shall not
141 // make the VM run forever - if no timeout occurs, Ant's
142 // main thread will still be there to let the new thread
143 // finish
144 thread.setDaemon(true);
145 Watchdog w = new Watchdog(timeout.longValue());
146 w.addTimeoutObserver(this);
147 synchronized (this) {
148 thread.start();
149 w.start();
150 try {
151 wait();
152 } catch (InterruptedException e) {
153 // ignore
154 }
155 if (timedOut) {
156 project.log("Timeout: sub-process interrupted",
157 Project.MSG_WARN);
158 } else {
159 thread = null;
160 w.stop();
161 }
162 }
163 }
164
165 if (caught != null) {
166 throw caught;
167 }
168
169 } catch (ClassNotFoundException e) {
170 throw new BuildException("Could not find " + classname + "."
171 + " Make sure you have it in your"
172 + " classpath");
173 } catch (BuildException e) {
174 throw e;
175 } catch (SecurityException e) {
176 throw e;
177 } catch (ThreadDeath e) {
178 throw e;
179 } catch (Throwable e) {
180 throw new BuildException(e);
181 } finally {
182 if (loader != null) {
183 loader.resetThreadContextLoader();
184 loader.cleanup();
185 loader = null;
186 }
187 if (sysProperties != null) {
188 sysProperties.restoreSystem();
189 }
190 }
191 }
192
193 /**
194 * @since Ant 1.5
195 */
196 public void run() {
197 final Object[] argument = {javaCommand.getArguments()};
198 try {
199 if (perm != null) {
200 perm.setSecurityManager();
201 }
202 main.invoke(null, argument);
203 } catch (InvocationTargetException e) {
204 Throwable t = e.getTargetException();
205 if (!(t instanceof InterruptedException)) {
206 caught = t;
207 } /* else { swallow, probably due to timeout } */
208 } catch (Throwable t) {
209 caught = t;
210 } finally {
211 if (perm != null) {
212 perm.restoreSecurityManager();
213 }
214 synchronized (this) {
215 notifyAll();
216 }
217 }
218 }
219
220 /**
221 * @since Ant 1.5
222 */
223 public synchronized void timeoutOccured(Watchdog w) {
224 if (thread != null) {
225 timedOut = true;
226 thread.interrupt();
227 }
228 notifyAll();
229 }
230
231 /**
232 * @since 1.19, Ant 1.5
233 */
234 public synchronized boolean killedProcess() {
235 return timedOut;
236 }
237
238 /**
239 * Runs the Java command in a separate VM, this does not give you
240 * the full flexibility of the Java task, but may be enough for
241 * simple needs.
242 *
243 * @since Ant 1.6.3
244 */
245 public int fork(ProjectComponent pc) throws BuildException {
246 CommandlineJava cmdl = new CommandlineJava();
247 cmdl.setClassname(javaCommand.getExecutable());
248 String[] args = javaCommand.getArguments();
249 for (int i = 0; i < args.length; i++) {
250 cmdl.createArgument().setValue(args[i]);
251 }
252 if (classpath != null) {
253 cmdl.createClasspath(pc.getProject()).append(classpath);
254 }
255 if (sysProperties != null) {
256 cmdl.addSysproperties(sysProperties);
257 }
258
259 Redirector redirector = new Redirector(pc);
260 Execute exe
261 = new Execute(redirector.createHandler(),
262 timeout == null
263 ? null
264 : new ExecuteWatchdog(timeout.longValue()));
265 exe.setAntRun(pc.getProject());
266 exe.setCommandline(cmdl.getCommandline());
267
268 try {
269 int rc = exe.execute();
270 redirector.complete();
271 timedOut = exe.killedProcess();
272 return rc;
273 } catch (IOException e) {
274 throw new BuildException(e);
275 }
276 }
277
278}
Note: See TracBrowser for help on using the repository browser.