KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > core > AntRunner


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Michael Giroux (michael.giroux@objectweb.org) - bug 149739
11  *******************************************************************************/

12 package org.eclipse.ant.core;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import org.eclipse.ant.internal.core.AntClassLoader;
26 import org.eclipse.ant.internal.core.IAntCoreConstants;
27 import org.eclipse.ant.internal.core.InternalCoreAntMessages;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.OperationCanceledException;
32 import org.eclipse.core.runtime.Platform;
33 import org.eclipse.core.runtime.Status;
34 import org.eclipse.equinox.app.IApplication;
35 import org.eclipse.equinox.app.IApplicationContext;
36 import org.eclipse.osgi.util.NLS;
37
38 /**
39  * Entry point for running Ant builds inside Eclipse (within the same JRE).
40  * Clients may instantiate this class; it is not intended to be subclassed.
41  * <p/>
42  * <div class="TableSubHeadingColor">
43  * <b>Usage note:</b><br/>
44  * Clients may use the <code>addBuildListener</code>,
45  * <code>addBuildLogger</code> and <code>setInputHandler</code>
46  * methods to configure classes that will be invoked during the
47  * build. When using these methods, it is necessary to package
48  * the classes in a jar that is not on the client plugin's classpath.
49  * The jar must be added to the Ant classpath. One way to add
50  * the jar to the Ant classpath is to use the
51  * <code>org.eclipse.ant.core.extraClasspathEntries</code> extension.
52  * <p>Refer to the "Platform Ant Support" chapter of the Programmer's Guide
53  * section in the Platform Plug-in Developer Guide for complete details.</p>
54  * </div>
55  */

56 public class AntRunner implements IApplication {
57
58     private static boolean buildRunning= false;
59     protected String JavaDoc buildFileLocation = IAntCoreConstants.DEFAULT_BUILD_FILENAME;
60     protected List JavaDoc buildListeners;
61     protected String JavaDoc[] targets;
62     protected Map JavaDoc userProperties;
63     protected int messageOutputLevel = 2; // Project.MSG_INFO
64
protected String JavaDoc buildLoggerClassName;
65     protected String JavaDoc inputHandlerClassName;
66     protected String JavaDoc[] arguments;
67     protected String JavaDoc[] propertyFiles;
68     protected URL JavaDoc[] customClasspath;
69     protected String JavaDoc antHome;
70     private IProgressMonitor progressMonitor = null;
71
72     /**
73      * Sets the build file location on the file system.
74      *
75      * @param buildFileLocation the file system location of the build file
76      */

77     public void setBuildFileLocation(String JavaDoc buildFileLocation) {
78         if (buildFileLocation == null) {
79             this.buildFileLocation = IAntCoreConstants.DEFAULT_BUILD_FILENAME;
80         } else {
81             this.buildFileLocation = buildFileLocation;
82         }
83     }
84
85     /**
86      * Set the message output level.
87      * <p>
88      * Valid values are:
89      * <ul>
90      * <li><code>org.apache.tools.ant.Project.ERR</code>,
91      * <li><code>org.apache.tools.ant.Project.WARN</code>,
92      * <li><code>org.apache.tools.ant.Project.INFO</code>,
93      * <li><code>org.apache.tools.ant.Project.VERBOSE</code> or
94      * <li><code>org.apache.tools.ant.Project.DEBUG</code>
95      * </ul>
96      *
97      * @param level the message output level
98      */

99     public void setMessageOutputLevel(int level) {
100         messageOutputLevel = level;
101     }
102
103     /**
104      * Sets the arguments to be passed to the build (e.g. -Dos=win32 -Dws=win32
105      * - verbose).
106      *
107      * @param arguments the arguments to be passed to the build
108      */

109     public void setArguments(String JavaDoc arguments) {
110         this.arguments = getArray(arguments);
111     }
112
113     /*
114      * Helper method to ensure an array is converted into an ArrayList.
115      */

116     private String JavaDoc[] getArray(String JavaDoc args) {
117         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
118         boolean waitingForQuote = false;
119         ArrayList JavaDoc result = new ArrayList JavaDoc();
120         for (StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(args, ", \"", true); tokens.hasMoreTokens();) { //$NON-NLS-1$
121
String JavaDoc token = tokens.nextToken();
122             if (waitingForQuote) {
123                 if (token.equals("\"")) { //$NON-NLS-1$
124
result.add(sb.toString());
125                     sb.setLength(0);
126                     waitingForQuote = false;
127                 } else {
128                     sb.append(token);
129                 }
130             } else {
131                 if (token.equals("\"")) { //$NON-NLS-1$
132
// test if we have something like -Dproperty="value"
133
if (result.size() > 0) {
134                         int index = result.size() - 1;
135                         String JavaDoc last = (String JavaDoc) result.get(index);
136                         if (last.charAt(last.length() - 1) == '=') {
137                             result.remove(index);
138                             sb.append(last);
139                         }
140                     }
141                     waitingForQuote = true;
142                 } else {
143                     if (!(token.equals(",") || token.equals(" "))) //$NON-NLS-1$ //$NON-NLS-2$
144
result.add(token);
145                 }
146             }
147         }
148         return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
149     }
150
151     /**
152      * Sets the arguments to be passed to the build (e.g. -Dos=win32 -Dws=win32 -verbose).
153      *
154      * @param arguments the arguments to be passed to the build
155      * @since 2.1
156      */

157     public void setArguments(String JavaDoc[] arguments) {
158         this.arguments = arguments;
159     }
160
161     /**
162      * Sets the targets and execution order.
163      *
164      * @param executionTargets which targets should be run and in which order
165      */

166     public void setExecutionTargets(String JavaDoc[] executionTargets) {
167         this.targets = executionTargets;
168     }
169
170     /**
171      * Adds a build listener. The parameter <code>className</code>
172      * is the class name of an <code>org.apache.tools.ant.BuildListener</code>
173      * implementation. The class will be instantiated at runtime and the
174      * listener will be called on build events
175      * (<code>org.apache.tools.ant.BuildEvent</code>).
176      *
177      * <p>Refer to {@link AntRunner Usage Note} for implementation details.
178      *
179      * @param className a build listener class name
180      */

181     public void addBuildListener(String JavaDoc className) {
182         if (className == null) {
183             return;
184         }
185         if (buildListeners == null) {
186             buildListeners = new ArrayList JavaDoc(5);
187         }
188         buildListeners.add(className);
189     }
190
191     /**
192      * Sets the build logger. The parameter <code>className</code>
193      * is the class name of an <code>org.apache.tools.ant.BuildLogger</code>
194      * implementation. The class will be instantiated at runtime and the
195      * logger will be called on build events
196      * (<code>org.apache.tools.ant.BuildEvent</code>).
197      * Only one build logger is permitted for any build.
198      *
199      * <p>Refer to {@link AntRunner Usage Note} for implementation details.
200      *
201      * @param className a build logger class name
202      */

203     public void addBuildLogger(String JavaDoc className) {
204         buildLoggerClassName = className;
205     }
206
207     /**
208      * Adds user-defined properties. Keys and values must be String objects.
209      *
210      * @param properties a Map of user-defined properties
211      */

212     public void addUserProperties(Map JavaDoc properties) {
213         if (userProperties == null) {
214             userProperties= new HashMap JavaDoc(properties);
215         } else {
216             userProperties.putAll(properties);
217         }
218     }
219
220     /**
221      * Returns the buildfile target information.
222      *
223      * @return an array containing the target information
224      *
225      * @see TargetInfo
226      * @since 2.1
227      * @throws CoreException Thrown if problem is encountered determining the targets
228      */

229     public synchronized TargetInfo[] getAvailableTargets() throws CoreException {
230         Class JavaDoc classInternalAntRunner= null;
231         Object JavaDoc runner= null;
232         ClassLoader JavaDoc originalClassLoader= Thread.currentThread().getContextClassLoader();
233         try {
234             classInternalAntRunner = getInternalAntRunner();
235             runner = classInternalAntRunner.newInstance();
236             
237             basicConfigure(classInternalAntRunner, runner);
238                     
239             // get the info for each targets
240
Method JavaDoc getTargets = classInternalAntRunner.getMethod("getTargets", null); //$NON-NLS-1$
241
Object JavaDoc results = getTargets.invoke(runner, null);
242             // get the default target
243
Method JavaDoc getDefault= classInternalAntRunner.getMethod("getDefaultTarget", null); //$NON-NLS-1$
244
String JavaDoc defaultName= (String JavaDoc)getDefault.invoke(runner, null);
245             // collect the info into target objects
246
List JavaDoc infos = (List JavaDoc) results;
247             
248             ProjectInfo project= new ProjectInfo((String JavaDoc)infos.remove(0), (String JavaDoc)infos.remove(0));
249             int i= 0;
250             Iterator JavaDoc iter= infos.iterator();
251             TargetInfo[] targetInfo= new TargetInfo[infos.size()];
252             List JavaDoc info;
253             while (iter.hasNext()) {
254                 info= (List JavaDoc)iter.next();
255                 targetInfo[i++] = new TargetInfo(project, (String JavaDoc)info.get(0), (String JavaDoc)info.get(1), (String JavaDoc[])info.get(2), info.get(0).equals(defaultName));
256             }
257             return targetInfo;
258         } catch (NoClassDefFoundError JavaDoc e) {
259             problemLoadingClass(e);
260             //not possible to reach this line
261
return new TargetInfo[0];
262         } catch (ClassNotFoundException JavaDoc e) {
263             problemLoadingClass(e);
264             //not possible to reach this line
265
return new TargetInfo[0];
266         } catch (InvocationTargetException JavaDoc e) {
267             handleInvocationTargetException(runner, classInternalAntRunner, e);
268             //not possible to reach this line
269
return new TargetInfo[0];
270         } catch (Exception JavaDoc e) {
271             String JavaDoc message = (e.getMessage() == null) ? InternalCoreAntMessages.AntRunner_Build_Failed__3 : e.getMessage();
272             throw new CoreException(new Status(IStatus.ERROR, AntCorePlugin.PI_ANTCORE, AntCorePlugin.ERROR_RUNNING_BUILD, message, e));
273         } finally {
274             Thread.currentThread().setContextClassLoader(originalClassLoader);
275         }
276     }
277
278     private void basicConfigure(Class JavaDoc classInternalAntRunner, Object JavaDoc runner) throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
279         Method JavaDoc setBuildFileLocation = classInternalAntRunner.getMethod("setBuildFileLocation", new Class JavaDoc[] { String JavaDoc.class }); //$NON-NLS-1$
280
setBuildFileLocation.invoke(runner, new Object JavaDoc[] { buildFileLocation });
281         
282         if (antHome != null) {
283             Method JavaDoc setAntHome = classInternalAntRunner.getMethod("setAntHome", new Class JavaDoc[] { String JavaDoc.class }); //$NON-NLS-1$
284
setAntHome.invoke(runner, new Object JavaDoc[] { antHome });
285         }
286         
287         setProperties(runner, classInternalAntRunner);
288         
289         if (arguments != null && arguments.length > 0) {
290             Method JavaDoc setArguments = classInternalAntRunner.getMethod("setArguments", new Class JavaDoc[] { String JavaDoc[].class }); //$NON-NLS-1$
291
setArguments.invoke(runner, new Object JavaDoc[] { arguments });
292         }
293     }
294
295     /**
296      * Runs the build file. If a progress monitor is specified it will be
297      * available during the script execution as a reference in the Ant Project
298      * (<code>org.apache.tools.ant.Project.getReferences()</code>). A long-
299      * running task could, for example, get the monitor during its execution and
300      * check for cancellation. The key value to retrieve the progress monitor
301      * instance is <code>AntCorePlugin.ECLIPSE_PROGRESS_MONITOR</code>.
302      *
303      * Only one build can occur at any given time.
304      *
305      * Sets the current threads context class loader to the AntClassLoader
306      * for the duration of the build.
307      *
308      * @param monitor a progress monitor, or <code>null</code> if progress
309      * reporting and cancellation are not desired
310      * @throws CoreException Thrown if a build is already occurring or if an exception occurs during the build
311      */

312     public void run(IProgressMonitor monitor) throws CoreException {
313         if (buildRunning) {
314             IStatus status= new Status(IStatus.ERROR, AntCorePlugin.PI_ANTCORE, AntCorePlugin.ERROR_RUNNING_BUILD, NLS.bind(InternalCoreAntMessages.AntRunner_Already_in_progess, new String JavaDoc[]{buildFileLocation}), null);
315             throw new CoreException(status);
316         }
317         buildRunning= true;
318         Object JavaDoc runner= null;
319         Class JavaDoc classInternalAntRunner= null;
320         ClassLoader JavaDoc originalClassLoader= Thread.currentThread().getContextClassLoader();
321         try {
322             classInternalAntRunner = getInternalAntRunner();
323             runner = classInternalAntRunner.newInstance();
324             // set build file
325
Method JavaDoc setBuildFileLocation = classInternalAntRunner.getMethod("setBuildFileLocation", new Class JavaDoc[] { String JavaDoc.class }); //$NON-NLS-1$
326
setBuildFileLocation.invoke(runner, new Object JavaDoc[] { buildFileLocation });
327             
328             //set the custom classpath
329
if (customClasspath != null) {
330                 Method JavaDoc setCustomClasspath = classInternalAntRunner.getMethod("setCustomClasspath", new Class JavaDoc[] { URL JavaDoc[].class }); //$NON-NLS-1$
331
setCustomClasspath.invoke(runner, new Object JavaDoc[] { customClasspath });
332             }
333             
334             // add listeners
335
if (buildListeners != null) {
336                 Method JavaDoc addBuildListeners = classInternalAntRunner.getMethod("addBuildListeners", new Class JavaDoc[] { List JavaDoc.class }); //$NON-NLS-1$
337
addBuildListeners.invoke(runner, new Object JavaDoc[] { buildListeners });
338             }
339             
340             if (buildLoggerClassName == null) {
341                 //indicate that the default logger is not to be used
342
buildLoggerClassName= ""; //$NON-NLS-1$
343
}
344             // add build logger
345
Method JavaDoc addBuildLogger = classInternalAntRunner.getMethod("addBuildLogger", new Class JavaDoc[] { String JavaDoc.class }); //$NON-NLS-1$
346
addBuildLogger.invoke(runner, new Object JavaDoc[] { buildLoggerClassName });
347             
348             if (inputHandlerClassName != null) {
349                 // add the input handler
350
Method JavaDoc setInputHandler = classInternalAntRunner.getMethod("setInputHandler", new Class JavaDoc[] { String JavaDoc.class }); //$NON-NLS-1$
351
setInputHandler.invoke(runner, new Object JavaDoc[] { inputHandlerClassName });
352             }
353             
354             basicConfigure(classInternalAntRunner, runner);
355             
356             // add progress monitor
357
if (monitor != null) {
358                 progressMonitor = monitor;
359                 Method JavaDoc setProgressMonitor = classInternalAntRunner.getMethod("setProgressMonitor", new Class JavaDoc[] { IProgressMonitor.class }); //$NON-NLS-1$
360
setProgressMonitor.invoke(runner, new Object JavaDoc[] { monitor });
361             }
362             
363             // set message output level
364
if (messageOutputLevel != 2) { //changed from the default Project.MSG_INFO
365
Method JavaDoc setMessageOutputLevel = classInternalAntRunner.getMethod("setMessageOutputLevel", new Class JavaDoc[] { int.class }); //$NON-NLS-1$
366
setMessageOutputLevel.invoke(runner, new Object JavaDoc[] { new Integer JavaDoc(messageOutputLevel)});
367             }
368             
369             // set execution targets
370
if (targets != null) {
371                 Method JavaDoc setExecutionTargets = classInternalAntRunner.getMethod("setExecutionTargets", new Class JavaDoc[] { String JavaDoc[].class }); //$NON-NLS-1$
372
setExecutionTargets.invoke(runner, new Object JavaDoc[] { targets });
373             }
374
375             // run
376
Method JavaDoc run = classInternalAntRunner.getMethod("run", null); //$NON-NLS-1$
377
run.invoke(runner, null);
378         } catch (NoClassDefFoundError JavaDoc e) {
379             problemLoadingClass(e);
380         } catch (ClassNotFoundException JavaDoc e) {
381             problemLoadingClass(e);
382         } catch (InvocationTargetException JavaDoc e) {
383             handleInvocationTargetException(runner, classInternalAntRunner, e);
384         } catch (Exception JavaDoc e) {
385             String JavaDoc message = (e.getMessage() == null) ? InternalCoreAntMessages.AntRunner_Build_Failed__3 : e.getMessage();
386             IStatus status= new Status(IStatus.ERROR, AntCorePlugin.PI_ANTCORE, AntCorePlugin.ERROR_RUNNING_BUILD, message, e);
387             throw new CoreException(status);
388         } finally {
389             buildRunning= false;
390             Thread.currentThread().setContextClassLoader(originalClassLoader);
391         }
392     }
393
394     private Class JavaDoc getInternalAntRunner() throws ClassNotFoundException JavaDoc {
395         ClassLoader JavaDoc loader = getClassLoader();
396         Thread.currentThread().setContextClassLoader(loader);
397         return loader.loadClass("org.eclipse.ant.internal.core.ant.InternalAntRunner"); //$NON-NLS-1$
398
}
399
400     private void setProperties(Object JavaDoc runner, Class JavaDoc classInternalAntRunner)
401         throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
402         // add properties
403
if (userProperties != null) {
404             Method JavaDoc addUserProperties = classInternalAntRunner.getMethod("addUserProperties", new Class JavaDoc[] { Map JavaDoc.class }); //$NON-NLS-1$
405
addUserProperties.invoke(runner, new Object JavaDoc[] { userProperties });
406         }
407         
408         // add property files
409
if (propertyFiles != null) {
410             Method JavaDoc addPropertyFiles = classInternalAntRunner.getMethod("addPropertyFiles", new Class JavaDoc[] { String JavaDoc[].class }); //$NON-NLS-1$
411
addPropertyFiles.invoke(runner, new Object JavaDoc[] { propertyFiles });
412         }
413     }
414
415     /*
416      * Handles exceptions that are loaded by the Ant Class Loader by
417      * asking the Internal Ant Runner class for the correct error message.
418      *
419      * Handles OperationCanceledExceptions, nested NoClassDefFoundError and
420      * nested ClassNotFoundException
421      */

422     protected void handleInvocationTargetException(Object JavaDoc runner, Class JavaDoc classInternalAntRunner, InvocationTargetException JavaDoc e) throws CoreException {
423         Throwable JavaDoc realException = e.getTargetException();
424         if (realException instanceof OperationCanceledException) {
425             return;
426         }
427         String JavaDoc message= null;
428         if (runner != null) {
429             try {
430                 Method JavaDoc getBuildErrorMessage = classInternalAntRunner.getMethod("getBuildExceptionErrorMessage", new Class JavaDoc[] { Throwable JavaDoc.class }); //$NON-NLS-1$
431
message= (String JavaDoc)getBuildErrorMessage.invoke(runner, new Object JavaDoc[] { realException });
432             } catch (Exception JavaDoc ex) {
433                 //do nothing as already in error state
434
}
435         }
436         // J9 throws NoClassDefFoundError nested in a InvocationTargetException
437
if (message == null && ((realException instanceof NoClassDefFoundError JavaDoc) || (realException instanceof ClassNotFoundException JavaDoc))) {
438             problemLoadingClass(realException);
439             return;
440         }
441         boolean internalError= false;
442         if (message == null) {
443             //error did not result from a BuildException
444
internalError= true;
445             message = (realException.getMessage() == null) ? InternalCoreAntMessages.AntRunner_Build_Failed__3 : realException.getMessage();
446         }
447         IStatus status= new Status(IStatus.ERROR, AntCorePlugin.PI_ANTCORE, AntCorePlugin.ERROR_RUNNING_BUILD, message, realException);
448         if (internalError) {
449             AntCorePlugin.getPlugin().getLog().log(status);
450         }
451         throw new CoreException(status);
452     }
453
454     protected void problemLoadingClass(Throwable JavaDoc e) throws CoreException {
455         String JavaDoc missingClassName= e.getMessage();
456         String JavaDoc message;
457         if (missingClassName != null) {
458             missingClassName= missingClassName.replace('/', '.');
459             message= InternalCoreAntMessages.AntRunner_Could_not_find_one_or_more_classes__Please_check_the_Ant_classpath__2;
460             message= NLS.bind(message, new String JavaDoc[]{missingClassName});
461         } else {
462             message= InternalCoreAntMessages.AntRunner_Could_not_find_one_or_more_classes__Please_check_the_Ant_classpath__1;
463         }
464         IStatus status= new Status(IStatus.ERROR, AntCorePlugin.PI_ANTCORE, AntCorePlugin.ERROR_RUNNING_BUILD, message, e);
465         AntCorePlugin.getPlugin().getLog().log(status);
466         throw new CoreException(status);
467     }
468
469     /**
470      * Runs the build file.
471      * @throws CoreException Thrown if a build is already occurring or if an exception occurs during the build
472      */

473     public void run() throws CoreException {
474         run(/*IProgressMonitor*/null);
475     }
476
477     /**
478      * Invokes the building of a project object and executes a build using either a given
479      * target or the default target. This method is called when running Eclipse headless
480      * and specifying <code>org.eclipse.ant.core.antRunner</code> as the application.
481      *
482      * Sets the current threads context class loader to the AntClassLoader
483      * for the duration of the build.
484      *
485      * @param argArray the command line arguments
486      * @exception Exception if a problem occurred during the buildfile execution
487      * @return an exit object (<code>EXIT_OK</code>) indicating normal termination if no exception occurs
488      */

489     public Object JavaDoc run(Object JavaDoc argArray) throws Exception JavaDoc {
490         ClassLoader JavaDoc originalClassLoader= Thread.currentThread().getContextClassLoader();
491         try {
492             //set the preferences for headless mode
493
AntCorePlugin.getPlugin().setRunningHeadless(true);
494             
495             // Add debug information if necessary - fix for bug 5672.
496
// Since the platform parses the -debug command line arg
497
// and removes it from the args passed to the applications,
498
// we have to check if Eclipse is in debug mode in order to
499
// forward the -debug argument to Ant.
500
if (Platform.inDebugMode()) {
501                 String JavaDoc[] args = (String JavaDoc[]) argArray;
502                 String JavaDoc[] newArgs = new String JavaDoc[args.length + 1];
503                 System.arraycopy(argArray, 0, newArgs, 0, args.length);
504                 newArgs[args.length] = "-debug"; //$NON-NLS-1$
505
argArray = newArgs;
506             }
507             ClassLoader JavaDoc loader = getClassLoader();
508             Thread.currentThread().setContextClassLoader(loader);
509             Class JavaDoc classInternalAntRunner = loader.loadClass("org.eclipse.ant.internal.core.ant.InternalAntRunner"); //$NON-NLS-1$
510
Object JavaDoc runner = classInternalAntRunner.newInstance();
511             Method JavaDoc run = classInternalAntRunner.getMethod("run", new Class JavaDoc[] { Object JavaDoc.class }); //$NON-NLS-1$
512
run.invoke(runner, new Object JavaDoc[] { argArray });
513         } finally {
514             Thread.currentThread().setContextClassLoader(originalClassLoader);
515         }
516
517         return EXIT_OK;
518     }
519     
520     private ClassLoader JavaDoc getClassLoader() {
521         if (customClasspath == null) {
522             return AntCorePlugin.getPlugin().getNewClassLoader();
523         }
524         AntCorePreferences preferences = AntCorePlugin.getPlugin().getPreferences();
525         List JavaDoc fullClasspath= new ArrayList JavaDoc();
526         fullClasspath.addAll(Arrays.asList(customClasspath));
527         fullClasspath.addAll(Arrays.asList(preferences.getExtraClasspathURLs()));
528         return new AntClassLoader((URL JavaDoc[])fullClasspath.toArray(new URL JavaDoc[fullClasspath.size()]), preferences.getPluginClassLoaders());
529     }
530     
531     /**
532      * Sets the input handler. The parameter <code>className</code>
533      * is the class name of an <code>org.apache.tools.ant.input.InputHandler</code>
534      * implementation. The class will be instantiated at runtime and the
535      * input handler will be used to respond to &lt;input&gt; requests
536      * Only one input handler is permitted for any build.
537      *
538      * <p>Refer to {@link AntRunner Usage Note} for implementation details.
539      *
540      * @param className an input handler class name
541      * @since 2.1
542      */

543     public void setInputHandler(String JavaDoc className) {
544         inputHandlerClassName= className;
545     }
546     
547     /**
548      * Sets the user specified property files.
549      * @param propertyFiles array of property file paths
550      * @since 2.1
551      */

552     public void setPropertyFiles(String JavaDoc[] propertyFiles) {
553         this.propertyFiles= propertyFiles;
554     }
555
556     /**
557      * Sets the custom classpath to use for this build
558      * @param customClasspath array of URLs that define the custom classpath
559      */

560     public void setCustomClasspath(URL JavaDoc[] customClasspath) {
561         this.customClasspath = customClasspath;
562     }
563     
564     /**
565      * Sets the Ant home to use for this build
566      * @param antHome String specifying the Ant home to use
567      * @since 2.1
568      */

569     public void setAntHome(String JavaDoc antHome) {
570         this.antHome= antHome;
571     }
572     /**
573      * Returns whether an Ant build is already in progress
574      *
575      * Only one Ant build can occur at any given time.
576      *
577      * @since 2.1
578      * @return boolean
579      */

580     public static boolean isBuildRunning() {
581         return buildRunning;
582     }
583
584     /**
585      * Invokes the building of a project object and executes a build using either a given
586      * target or the default target. This method is called when running Eclipse headless
587      * and specifying <code>org.eclipse.ant.core.antRunner</code> as the application.
588      *
589      * Sets the current threads context class loader to the <code>AntClassLoader</code>
590      * for the duration of the build.
591      *
592      * @param context the context used to start the application
593      * @exception Exception if a problem occurred during the buildfile execution
594      * @return an exit object (<code>EXIT_OK</code>) indicating normal termination if no exception occurs
595      * @see org.eclipse.equinox.app.IApplication#start(IApplicationContext)
596      */

597     public Object JavaDoc start(IApplicationContext context) throws Exception JavaDoc {
598         Map JavaDoc contextArguments = context.getArguments();
599         return run(contextArguments.get(IApplicationContext.APPLICATION_ARGS));
600     }
601
602     /*
603      * @see org.eclipse.equinox.app.IApplication#stop()
604      */

605     public void stop() {
606         if (progressMonitor != null) {
607             progressMonitor.setCanceled(true);
608         }
609     }
610 }
611
Popular Tags