1 12 package org.eclipse.ant.core; 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.lang.reflect.Method ; 16 import java.net.URL ; 17 import java.util.ArrayList ; 18 import java.util.Arrays ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.StringTokenizer ; 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 56 public class AntRunner implements IApplication { 57 58 private static boolean buildRunning= false; 59 protected String buildFileLocation = IAntCoreConstants.DEFAULT_BUILD_FILENAME; 60 protected List buildListeners; 61 protected String [] targets; 62 protected Map userProperties; 63 protected int messageOutputLevel = 2; protected String buildLoggerClassName; 65 protected String inputHandlerClassName; 66 protected String [] arguments; 67 protected String [] propertyFiles; 68 protected URL [] customClasspath; 69 protected String antHome; 70 private IProgressMonitor progressMonitor = null; 71 72 77 public void setBuildFileLocation(String buildFileLocation) { 78 if (buildFileLocation == null) { 79 this.buildFileLocation = IAntCoreConstants.DEFAULT_BUILD_FILENAME; 80 } else { 81 this.buildFileLocation = buildFileLocation; 82 } 83 } 84 85 99 public void setMessageOutputLevel(int level) { 100 messageOutputLevel = level; 101 } 102 103 109 public void setArguments(String arguments) { 110 this.arguments = getArray(arguments); 111 } 112 113 116 private String [] getArray(String args) { 117 StringBuffer sb = new StringBuffer (); 118 boolean waitingForQuote = false; 119 ArrayList result = new ArrayList (); 120 for (StringTokenizer tokens = new StringTokenizer (args, ", \"", true); tokens.hasMoreTokens();) { String token = tokens.nextToken(); 122 if (waitingForQuote) { 123 if (token.equals("\"")) { result.add(sb.toString()); 125 sb.setLength(0); 126 waitingForQuote = false; 127 } else { 128 sb.append(token); 129 } 130 } else { 131 if (token.equals("\"")) { if (result.size() > 0) { 134 int index = result.size() - 1; 135 String last = (String ) 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(" "))) result.add(token); 145 } 146 } 147 } 148 return (String []) result.toArray(new String [result.size()]); 149 } 150 151 157 public void setArguments(String [] arguments) { 158 this.arguments = arguments; 159 } 160 161 166 public void setExecutionTargets(String [] executionTargets) { 167 this.targets = executionTargets; 168 } 169 170 181 public void addBuildListener(String className) { 182 if (className == null) { 183 return; 184 } 185 if (buildListeners == null) { 186 buildListeners = new ArrayList (5); 187 } 188 buildListeners.add(className); 189 } 190 191 203 public void addBuildLogger(String className) { 204 buildLoggerClassName = className; 205 } 206 207 212 public void addUserProperties(Map properties) { 213 if (userProperties == null) { 214 userProperties= new HashMap (properties); 215 } else { 216 userProperties.putAll(properties); 217 } 218 } 219 220 229 public synchronized TargetInfo[] getAvailableTargets() throws CoreException { 230 Class classInternalAntRunner= null; 231 Object runner= null; 232 ClassLoader originalClassLoader= Thread.currentThread().getContextClassLoader(); 233 try { 234 classInternalAntRunner = getInternalAntRunner(); 235 runner = classInternalAntRunner.newInstance(); 236 237 basicConfigure(classInternalAntRunner, runner); 238 239 Method getTargets = classInternalAntRunner.getMethod("getTargets", null); Object results = getTargets.invoke(runner, null); 242 Method getDefault= classInternalAntRunner.getMethod("getDefaultTarget", null); String defaultName= (String )getDefault.invoke(runner, null); 245 List infos = (List ) results; 247 248 ProjectInfo project= new ProjectInfo((String )infos.remove(0), (String )infos.remove(0)); 249 int i= 0; 250 Iterator iter= infos.iterator(); 251 TargetInfo[] targetInfo= new TargetInfo[infos.size()]; 252 List info; 253 while (iter.hasNext()) { 254 info= (List )iter.next(); 255 targetInfo[i++] = new TargetInfo(project, (String )info.get(0), (String )info.get(1), (String [])info.get(2), info.get(0).equals(defaultName)); 256 } 257 return targetInfo; 258 } catch (NoClassDefFoundError e) { 259 problemLoadingClass(e); 260 return new TargetInfo[0]; 262 } catch (ClassNotFoundException e) { 263 problemLoadingClass(e); 264 return new TargetInfo[0]; 266 } catch (InvocationTargetException e) { 267 handleInvocationTargetException(runner, classInternalAntRunner, e); 268 return new TargetInfo[0]; 270 } catch (Exception e) { 271 String 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 classInternalAntRunner, Object runner) throws NoSuchMethodException , IllegalAccessException , InvocationTargetException { 279 Method setBuildFileLocation = classInternalAntRunner.getMethod("setBuildFileLocation", new Class [] { String .class }); setBuildFileLocation.invoke(runner, new Object [] { buildFileLocation }); 281 282 if (antHome != null) { 283 Method setAntHome = classInternalAntRunner.getMethod("setAntHome", new Class [] { String .class }); setAntHome.invoke(runner, new Object [] { antHome }); 285 } 286 287 setProperties(runner, classInternalAntRunner); 288 289 if (arguments != null && arguments.length > 0) { 290 Method setArguments = classInternalAntRunner.getMethod("setArguments", new Class [] { String [].class }); setArguments.invoke(runner, new Object [] { arguments }); 292 } 293 } 294 295 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 []{buildFileLocation}), null); 315 throw new CoreException(status); 316 } 317 buildRunning= true; 318 Object runner= null; 319 Class classInternalAntRunner= null; 320 ClassLoader originalClassLoader= Thread.currentThread().getContextClassLoader(); 321 try { 322 classInternalAntRunner = getInternalAntRunner(); 323 runner = classInternalAntRunner.newInstance(); 324 Method setBuildFileLocation = classInternalAntRunner.getMethod("setBuildFileLocation", new Class [] { String .class }); setBuildFileLocation.invoke(runner, new Object [] { buildFileLocation }); 327 328 if (customClasspath != null) { 330 Method setCustomClasspath = classInternalAntRunner.getMethod("setCustomClasspath", new Class [] { URL [].class }); setCustomClasspath.invoke(runner, new Object [] { customClasspath }); 332 } 333 334 if (buildListeners != null) { 336 Method addBuildListeners = classInternalAntRunner.getMethod("addBuildListeners", new Class [] { List .class }); addBuildListeners.invoke(runner, new Object [] { buildListeners }); 338 } 339 340 if (buildLoggerClassName == null) { 341 buildLoggerClassName= ""; } 344 Method addBuildLogger = classInternalAntRunner.getMethod("addBuildLogger", new Class [] { String .class }); addBuildLogger.invoke(runner, new Object [] { buildLoggerClassName }); 347 348 if (inputHandlerClassName != null) { 349 Method setInputHandler = classInternalAntRunner.getMethod("setInputHandler", new Class [] { String .class }); setInputHandler.invoke(runner, new Object [] { inputHandlerClassName }); 352 } 353 354 basicConfigure(classInternalAntRunner, runner); 355 356 if (monitor != null) { 358 progressMonitor = monitor; 359 Method setProgressMonitor = classInternalAntRunner.getMethod("setProgressMonitor", new Class [] { IProgressMonitor.class }); setProgressMonitor.invoke(runner, new Object [] { monitor }); 361 } 362 363 if (messageOutputLevel != 2) { Method setMessageOutputLevel = classInternalAntRunner.getMethod("setMessageOutputLevel", new Class [] { int.class }); setMessageOutputLevel.invoke(runner, new Object [] { new Integer (messageOutputLevel)}); 367 } 368 369 if (targets != null) { 371 Method setExecutionTargets = classInternalAntRunner.getMethod("setExecutionTargets", new Class [] { String [].class }); setExecutionTargets.invoke(runner, new Object [] { targets }); 373 } 374 375 Method run = classInternalAntRunner.getMethod("run", null); run.invoke(runner, null); 378 } catch (NoClassDefFoundError e) { 379 problemLoadingClass(e); 380 } catch (ClassNotFoundException e) { 381 problemLoadingClass(e); 382 } catch (InvocationTargetException e) { 383 handleInvocationTargetException(runner, classInternalAntRunner, e); 384 } catch (Exception e) { 385 String 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 getInternalAntRunner() throws ClassNotFoundException { 395 ClassLoader loader = getClassLoader(); 396 Thread.currentThread().setContextClassLoader(loader); 397 return loader.loadClass("org.eclipse.ant.internal.core.ant.InternalAntRunner"); } 399 400 private void setProperties(Object runner, Class classInternalAntRunner) 401 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException { 402 if (userProperties != null) { 404 Method addUserProperties = classInternalAntRunner.getMethod("addUserProperties", new Class [] { Map .class }); addUserProperties.invoke(runner, new Object [] { userProperties }); 406 } 407 408 if (propertyFiles != null) { 410 Method addPropertyFiles = classInternalAntRunner.getMethod("addPropertyFiles", new Class [] { String [].class }); addPropertyFiles.invoke(runner, new Object [] { propertyFiles }); 412 } 413 } 414 415 422 protected void handleInvocationTargetException(Object runner, Class classInternalAntRunner, InvocationTargetException e) throws CoreException { 423 Throwable realException = e.getTargetException(); 424 if (realException instanceof OperationCanceledException) { 425 return; 426 } 427 String message= null; 428 if (runner != null) { 429 try { 430 Method getBuildErrorMessage = classInternalAntRunner.getMethod("getBuildExceptionErrorMessage", new Class [] { Throwable .class }); message= (String )getBuildErrorMessage.invoke(runner, new Object [] { realException }); 432 } catch (Exception ex) { 433 } 435 } 436 if (message == null && ((realException instanceof NoClassDefFoundError ) || (realException instanceof ClassNotFoundException ))) { 438 problemLoadingClass(realException); 439 return; 440 } 441 boolean internalError= false; 442 if (message == null) { 443 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 e) throws CoreException { 455 String missingClassName= e.getMessage(); 456 String 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 []{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 473 public void run() throws CoreException { 474 run(null); 475 } 476 477 489 public Object run(Object argArray) throws Exception { 490 ClassLoader originalClassLoader= Thread.currentThread().getContextClassLoader(); 491 try { 492 AntCorePlugin.getPlugin().setRunningHeadless(true); 494 495 if (Platform.inDebugMode()) { 501 String [] args = (String []) argArray; 502 String [] newArgs = new String [args.length + 1]; 503 System.arraycopy(argArray, 0, newArgs, 0, args.length); 504 newArgs[args.length] = "-debug"; argArray = newArgs; 506 } 507 ClassLoader loader = getClassLoader(); 508 Thread.currentThread().setContextClassLoader(loader); 509 Class classInternalAntRunner = loader.loadClass("org.eclipse.ant.internal.core.ant.InternalAntRunner"); Object runner = classInternalAntRunner.newInstance(); 511 Method run = classInternalAntRunner.getMethod("run", new Class [] { Object .class }); run.invoke(runner, new Object [] { argArray }); 513 } finally { 514 Thread.currentThread().setContextClassLoader(originalClassLoader); 515 } 516 517 return EXIT_OK; 518 } 519 520 private ClassLoader getClassLoader() { 521 if (customClasspath == null) { 522 return AntCorePlugin.getPlugin().getNewClassLoader(); 523 } 524 AntCorePreferences preferences = AntCorePlugin.getPlugin().getPreferences(); 525 List fullClasspath= new ArrayList (); 526 fullClasspath.addAll(Arrays.asList(customClasspath)); 527 fullClasspath.addAll(Arrays.asList(preferences.getExtraClasspathURLs())); 528 return new AntClassLoader((URL [])fullClasspath.toArray(new URL [fullClasspath.size()]), preferences.getPluginClassLoaders()); 529 } 530 531 543 public void setInputHandler(String className) { 544 inputHandlerClassName= className; 545 } 546 547 552 public void setPropertyFiles(String [] propertyFiles) { 553 this.propertyFiles= propertyFiles; 554 } 555 556 560 public void setCustomClasspath(URL [] customClasspath) { 561 this.customClasspath = customClasspath; 562 } 563 564 569 public void setAntHome(String antHome) { 570 this.antHome= antHome; 571 } 572 580 public static boolean isBuildRunning() { 581 return buildRunning; 582 } 583 584 597 public Object start(IApplicationContext context) throws Exception { 598 Map contextArguments = context.getArguments(); 599 return run(contextArguments.get(IApplicationContext.APPLICATION_ARGS)); 600 } 601 602 605 public void stop() { 606 if (progressMonitor != null) { 607 progressMonitor.setCanceled(true); 608 } 609 } 610 } 611 | Popular Tags |