1 16 17 package org.apache.commons.launcher; 18 19 import java.io.File ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.StringTokenizer ; 28 import org.apache.commons.launcher.types.ArgumentSet; 29 import org.apache.commons.launcher.types.ConditionalArgument; 30 import org.apache.commons.launcher.types.ConditionalVariable; 31 import org.apache.commons.launcher.types.JVMArgumentSet; 32 import org.apache.commons.launcher.types.SysPropertySet; 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Task; 35 import org.apache.tools.ant.types.Path; 36 import org.apache.tools.ant.types.Reference; 37 38 62 public class LaunchTask extends Task { 63 64 66 69 public final static String ARG_PROP_NAME = "launch.arg."; 70 71 74 public final static String TASK_NAME = "launch"; 75 76 79 private static ArrayList childProcesses = new ArrayList (); 80 81 83 86 private boolean appendOutput = false; 87 88 91 private Process childProc = null; 92 93 96 private Path classpath = null; 97 98 101 private boolean debug = false; 102 103 106 private boolean displayMinimizedWindow = false; 107 108 111 private boolean disposeMinimizedWindow = true; 112 113 116 private boolean failOnError = false; 117 118 121 private LaunchFilter filter = null; 122 123 126 private String filterClassName = null; 127 128 131 private Path filterClasspath = null; 132 133 136 private String mainClassName = null; 137 138 141 private File minimizedWindowIcon = null; 142 143 146 private String minimizedWindowTitle = null; 147 148 151 private File outputFile = null; 152 153 156 private boolean print = false; 157 158 161 private boolean redirect = false; 162 163 166 private boolean requireTools = false; 167 168 171 private ArgumentSet taskArgumentSet = new ArgumentSet(); 172 173 176 private JVMArgumentSet taskJVMArgumentSet = new JVMArgumentSet(); 177 178 181 private SysPropertySet taskSysPropertySet = new SysPropertySet(); 182 183 186 private boolean useArgs = true; 187 188 191 private boolean useSystemIn = true; 192 193 196 private boolean waitForChild = true; 197 198 200 205 public static Process [] getChildProcesses() { 206 207 return (Process [])childProcesses.toArray(new Process [childProcesses.size()]); 208 209 } 210 211 213 221 public void addArg(ConditionalArgument arg) { 222 223 taskArgumentSet.addArg(arg); 224 225 } 226 227 232 public void addArgset(ArgumentSet set) { 233 234 taskArgumentSet.addArgset(set); 235 236 } 237 238 246 public void addJvmarg(ConditionalArgument jvmArg) { 247 248 taskJVMArgumentSet.addJvmarg(jvmArg); 249 250 } 251 252 257 public void addJvmargset(JVMArgumentSet set) { 258 259 taskJVMArgumentSet.addJvmargset(set); 260 261 } 262 263 271 public void addSysproperty(ConditionalVariable var) { 272 273 taskSysPropertySet.addSysproperty(var); 274 275 } 276 277 282 public void addSyspropertyset(SysPropertySet set) { 283 284 taskSysPropertySet.addSyspropertyset(set); 285 286 } 287 288 293 public Path createClasspath() { 294 295 if (classpath == null) 296 classpath = new Path(project); 297 return classpath; 298 299 } 300 301 307 public Path createFilterclasspath() { 308 309 if (filterClasspath == null) 310 filterClasspath = new Path(project); 311 return filterClasspath; 312 313 } 314 315 322 public void execute() throws BuildException { 323 324 try { 325 326 if (!Launcher.isStarted()) 329 throw new BuildException(Launcher.getLocalizedString("no.run.standalone", this.getClass().getName())); 330 331 if (Launcher.isStopped()) 333 throw new BuildException(); 334 335 if (mainClassName == null) 336 throw new BuildException(Launcher.getLocalizedString("classname.null", this.getClass().getName())); 337 338 ArrayList taskJVMArgs = taskJVMArgumentSet.getList(); 340 ArrayList jvmArgs = new ArrayList (taskJVMArgs.size()); 341 for (int i = 0; i < taskJVMArgs.size(); i++) { 342 ConditionalArgument value = (ConditionalArgument)taskJVMArgs.get(i); 343 if (testIfCondition(value.getIf()) && testUnlessCondition(value.getUnless())) { 345 String [] list = value.getParts(); 346 for (int j = 0; j < list.length; j++) 347 jvmArgs.add(list[j]); 348 } 349 } 350 351 ArrayList taskSysProps = taskSysPropertySet.getList(); 354 HashMap sysProps = new HashMap (taskSysProps.size()); 355 for (int i = 0; i < taskSysProps.size(); i++) { 356 ConditionalVariable variable = (ConditionalVariable)taskSysProps.get(i); 357 if (testIfCondition(variable.getIf()) && testUnlessCondition(variable.getUnless())) 359 sysProps.put(variable.getKey(), variable.getValue()); 360 } 361 362 ArrayList taskArgs = taskArgumentSet.getList(); 364 ArrayList appArgs = new ArrayList (taskArgs.size()); 365 for (int i = 0; i < taskArgs.size(); i++) { 366 ConditionalArgument value = (ConditionalArgument)taskArgs.get(i); 367 if (testIfCondition(value.getIf()) && testUnlessCondition(value.getUnless())) { 369 String [] list = value.getParts(); 370 for (int j = 0; j < list.length; j++) 371 appArgs.add(list[j]); 372 } 373 } 374 375 if (useArgs) { 377 int currentArg = 0; 378 String arg = null; 379 while ((arg = project.getUserProperty(LaunchTask.ARG_PROP_NAME + Integer.toString(currentArg++))) != null) 380 appArgs.add(arg); 381 } 382 383 String filteredClasspath = null; 386 if (classpath != null) 387 filteredClasspath = classpath.toString(); 388 String filteredMainClassName = mainClassName; 389 boolean filteredRedirect = redirect; 390 File filteredOutputFile = outputFile; 391 boolean filteredAppendOutput = appendOutput; 392 boolean filteredDebug = debug; 393 boolean filteredDisplayMinimizedWindow = displayMinimizedWindow; 394 boolean filteredDisposeMinimizedWindow = disposeMinimizedWindow; 395 boolean filteredFailOnError = failOnError; 396 String filteredMinimizedWindowTitle = minimizedWindowTitle; 397 File filteredMinimizedWindowIcon = minimizedWindowIcon; 398 boolean filteredPrint = print; 399 boolean filteredRequireTools = requireTools; 400 boolean filteredUseSystemIn = useSystemIn; 401 boolean filteredWaitForChild = waitForChild; 402 403 if (filterClassName != null) { 407 if (filter == null) { 408 try { 409 ClassLoader loader = this.getClass().getClassLoader(); 410 if (filterClasspath != null) { 411 String [] fileList = filterClasspath.list(); 413 URL [] urls = new URL [fileList.length]; 414 for (int i = 0; i < fileList.length; i++) 415 urls[i] = new File (fileList[i]).toURL(); 416 loader = new URLClassLoader (urls, loader); 417 } 418 Class filterClass = loader.loadClass(filterClassName); 419 filter = (LaunchFilter)filterClass.newInstance(); 420 LaunchCommand command = new LaunchCommand(); 422 command.setJvmargs(jvmArgs); 423 command.setSysproperties(sysProps); 424 command.setArgs(appArgs); 425 command.setClasspath(filteredClasspath); 426 command.setClassname(filteredMainClassName); 427 command.setRedirectoutput(filteredRedirect); 428 command.setOutput(filteredOutputFile); 429 command.setAppendoutput(filteredAppendOutput); 430 command.setDebug(filteredDebug); 431 command.setDisplayminimizedwindow(filteredDisplayMinimizedWindow); 432 command.setDisposeminimizedwindow(filteredDisposeMinimizedWindow); 433 command.setFailonerror(filteredFailOnError); 434 command.setMinimizedwindowtitle(filteredMinimizedWindowTitle); 435 command.setMinimizedwindowicon(filteredMinimizedWindowIcon); 436 command.setPrint(filteredPrint); 437 command.setRequiretools(filteredRequireTools); 438 command.setUsesystemin(filteredUseSystemIn); 439 command.setWaitforchild(filteredWaitForChild); 440 filter.filter(command); 441 jvmArgs = command.getJvmargs(); 442 sysProps = command.getSysproperties(); 443 appArgs = command.getArgs(); 444 filteredClasspath = command.getClasspath(); 445 filteredMainClassName = command.getClassname(); 446 filteredRedirect = command.getRedirectoutput(); 447 filteredOutputFile = command.getOutput(); 448 filteredAppendOutput = command.getAppendoutput(); 449 filteredDebug = command.getDebug(); 450 filteredDisplayMinimizedWindow = command.getDisplayminimizedwindow(); 451 filteredDisposeMinimizedWindow = command.getDisposeminimizedwindow(); 452 filteredFailOnError = command.getFailonerror(); 453 filteredMinimizedWindowTitle = command.getMinimizedwindowtitle(); 454 filteredMinimizedWindowIcon = command.getMinimizedwindowicon(); 455 filteredPrint = command.getPrint(); 456 filteredRequireTools = command.getRequiretools(); 457 filteredUseSystemIn = command.getUsesystemin(); 458 filteredWaitForChild = command.getWaitforchild(); 459 if (filteredMainClassName == null) 461 throw new BuildException(Launcher.getLocalizedString("classname.null", this.getClass().getName())); 462 if (jvmArgs == null) 463 jvmArgs = new ArrayList (); 464 if (sysProps == null) 465 sysProps = new HashMap (); 466 if (appArgs == null) 467 appArgs = new ArrayList (); 468 } catch (BuildException be) { 469 throw new BuildException(filterClassName + " " + Launcher.getLocalizedString("filter.exception", this.getClass().getName()), be); 470 } catch (ClassCastException cce) { 471 throw new BuildException(filterClassName + " " + Launcher.getLocalizedString("filter.not.filter", this.getClass().getName())); 472 } catch (Exception e) { 473 throw new BuildException(e); 474 } 475 } 476 } 477 478 if (filteredDebug) { 480 filteredWaitForChild = true; 481 filteredUseSystemIn = true; 482 } 483 484 StringBuffer fullClasspath = new StringBuffer (Launcher.getBootstrapFile().getPath()); 486 if (filteredRequireTools) { 487 fullClasspath.append(File.pathSeparator); 488 fullClasspath.append(Launcher.getToolsClasspath()); 489 } 490 if (filteredClasspath != null) { 491 fullClasspath.append(File.pathSeparator); 492 fullClasspath.append(filteredClasspath); 493 } 494 495 sysProps.remove(ChildMain.WAIT_FOR_CHILD_PROP_NAME); 497 if (filteredWaitForChild) 498 sysProps.put(ChildMain.WAIT_FOR_CHILD_PROP_NAME, ""); 499 500 sysProps.remove(ChildMain.DISPLAY_MINIMIZED_WINDOW_PROP_NAME); 502 sysProps.remove(ChildMain.MINIMIZED_WINDOW_TITLE_PROP_NAME); 503 sysProps.remove(ChildMain.MINIMIZED_WINDOW_ICON_PROP_NAME); 504 sysProps.remove(ChildMain.DISPOSE_MINIMIZED_WINDOW_PROP_NAME); 505 if (!filteredWaitForChild && filteredDisplayMinimizedWindow) { 506 sysProps.put(ChildMain.DISPLAY_MINIMIZED_WINDOW_PROP_NAME, ""); 507 if (filteredMinimizedWindowTitle != null) 508 sysProps.put(ChildMain.MINIMIZED_WINDOW_TITLE_PROP_NAME, filteredMinimizedWindowTitle); 509 else 510 sysProps.put(ChildMain.MINIMIZED_WINDOW_TITLE_PROP_NAME, getOwningTarget().getName()); 511 if (filteredMinimizedWindowIcon != null) 512 sysProps.put(ChildMain.MINIMIZED_WINDOW_ICON_PROP_NAME, filteredMinimizedWindowIcon.getCanonicalPath()); 513 if (filteredDisposeMinimizedWindow) 515 sysProps.put(ChildMain.DISPOSE_MINIMIZED_WINDOW_PROP_NAME, ""); 516 } 517 518 sysProps.remove(ChildMain.OUTPUT_FILE_PROP_NAME); 520 if (!filteredWaitForChild && filteredRedirect) { 521 if (filteredOutputFile != null) { 522 String outputFilePath = filteredOutputFile.getCanonicalPath(); 523 try { 525 File parentFile = new File (filteredOutputFile.getParent()); 526 if ( !parentFile.exists() ) { 528 parentFile.mkdirs(); 530 } 533 filteredOutputFile.createNewFile(); 534 } catch (IOException ioe) { 535 throw new BuildException(outputFilePath + " " + Launcher.getLocalizedString("output.file.not.creatable", this.getClass().getName())); 536 } 537 if (!filteredOutputFile.canWrite()) 538 throw new BuildException(outputFilePath + " " + Launcher.getLocalizedString("output.file.not.writable", this.getClass().getName())); 539 sysProps.put(ChildMain.OUTPUT_FILE_PROP_NAME, outputFilePath); 540 if (filteredAppendOutput) 541 sysProps.put(ChildMain.APPEND_OUTPUT_PROP_NAME, ""); 542 Launcher.getLog().println(Launcher.getLocalizedString("redirect.notice", this.getClass().getName()) + " " + outputFilePath); 543 } else { 544 throw new BuildException(Launcher.getLocalizedString("output.file.null", this.getClass().getName())); 545 } 546 } 547 548 File heartbeatFile = null; 553 FileOutputStream heartbeatOutputStream = null; 554 if (filteredWaitForChild) { 555 File tmpDir = null; 556 String tmpDirName = (String )sysProps.get("java.io.tmpdir"); 557 if (tmpDirName != null) 558 tmpDir = new File (tmpDirName); 559 heartbeatFile = File.createTempFile(ChildMain.HEARTBEAT_FILE_PROP_NAME + ".", "", tmpDir); 560 heartbeatOutputStream = new FileOutputStream (heartbeatFile); 563 sysProps.put(ChildMain.HEARTBEAT_FILE_PROP_NAME, heartbeatFile.getCanonicalPath()); 564 } 565 566 String [] cmd = new String [5 + jvmArgs.size() + sysProps.size() + appArgs.size()]; 568 int nextCmdArg = 0; 569 if (filteredDebug) 570 cmd[nextCmdArg++] = Launcher.getJDBCommand(); 571 else 572 cmd[nextCmdArg++] = Launcher.getJavaCommand(); 573 for (int i = 0; i < jvmArgs.size(); i++) 575 cmd[nextCmdArg++] = (String )jvmArgs.get(i); 576 Iterator sysPropsKeys = sysProps.keySet().iterator(); 578 while (sysPropsKeys.hasNext()) { 579 String key = (String )sysPropsKeys.next(); 580 if (key == null) 581 continue; 582 String value = (String )sysProps.get(key); 583 if (value == null) 584 value = ""; 585 cmd[nextCmdArg++] = "-D" + key + "=" + value; 586 } 587 cmd[nextCmdArg++] = "-classpath"; 591 cmd[nextCmdArg++] = fullClasspath.toString(); 592 int mainClassArg = nextCmdArg; 594 cmd[nextCmdArg++] = ChildMain.class.getName(); 595 cmd[nextCmdArg++] = filteredMainClassName; 596 for (int i = 0; i < appArgs.size(); i++) 598 { 599 cmd[nextCmdArg++] = (String )appArgs.get(i); 600 } 601 if (filteredPrint) { 603 String osname = System.getProperty("os.name").toLowerCase(); 605 StringBuffer buf = new StringBuffer (cmd.length * 100); 606 String quote = null; 607 String replaceQuote = null; 608 if (osname.indexOf("windows") >= 0) { 609 quote = "\""; 611 replaceQuote = quote + quote + quote; 612 } else { 613 quote = "'"; 615 replaceQuote = quote + "\\" + quote + quote; 616 } 617 for (int i = 0; i < cmd.length; i++) { 618 if (i == mainClassArg) 621 continue; 622 if (i > 0) 623 buf.append(" "); 624 buf.append(quote); 625 StringTokenizer tokenizer = new StringTokenizer (cmd[i], quote, true); 626 while (tokenizer.hasMoreTokens()) { 627 String token = tokenizer.nextToken(); 628 if (quote.equals(token)) 629 buf.append(replaceQuote); 630 else 631 buf.append(token); 632 } 633 buf.append(quote); 634 } 635 System.err.println(Launcher.getLocalizedString("executing.child.command", this.getClass().getName()) + ":"); 637 System.err.println(buf.toString()); 638 } 639 640 if (Launcher.isStopped()) 642 throw new BuildException(); 643 Process proc = null; 644 synchronized (LaunchTask.childProcesses) { 645 proc = Runtime.getRuntime().exec(cmd); 646 if (filteredWaitForChild) { 648 childProc = proc; 649 LaunchTask.childProcesses.add(proc); 650 } 651 } 652 if (filteredWaitForChild) { 653 StreamConnector stdout = 654 new StreamConnector(proc.getInputStream(), System.out); 655 StreamConnector stderr = 656 new StreamConnector(proc.getErrorStream(), System.err); 657 stdout.start(); 658 stderr.start(); 659 if (filteredUseSystemIn) { 660 StreamConnector stdin = 661 new StreamConnector(System.in, proc.getOutputStream()); 662 stdin.start(); 663 } 664 proc.waitFor(); 665 stdout.join(); 667 stderr.join(); 668 if (heartbeatOutputStream != null) 669 heartbeatOutputStream.close(); 670 if (heartbeatFile != null) 671 heartbeatFile.delete(); 672 int exitValue = proc.exitValue(); 673 if (filteredFailOnError && exitValue != 0) 674 throw new BuildException(Launcher.getLocalizedString("child.failed", this.getClass().getName()) + " " + exitValue); 675 } 676 if (Launcher.isStopped()) 679 throw new BuildException(); 680 681 } catch (BuildException be) { 682 throw be; 683 } catch (Exception e) { 684 if (Launcher.isStopped()) 685 throw new BuildException(Launcher.getLocalizedString("launch.task.stopped", this.getClass().getName())); 686 else 687 throw new BuildException(e); 688 } 689 690 } 691 692 700 public void setUseargs(boolean useArgs) { 701 702 this.useArgs = useArgs; 703 704 } 705 706 715 public void setUsesystemin(boolean useSystemIn) { 716 717 this.useSystemIn = useSystemIn; 718 719 } 720 721 731 public void setWaitforchild(boolean waitForChild) { 732 733 this.waitForChild = waitForChild; 734 735 } 736 737 742 public void setClassname(String mainClassName) { 743 744 this.mainClassName = mainClassName; 745 746 } 747 748 753 public void setClasspath(Path classpath) { 754 755 createClasspath().append(classpath); 756 757 } 758 759 764 public void setClasspathref(Reference ref) { 765 766 createClasspath().setRefid(ref); 767 768 } 769 770 776 public void setDebug(boolean debug) { 777 778 this.debug = debug; 779 780 } 781 782 793 public void setDisplayminimizedwindow(boolean displayMinimizedWindow) { 794 795 this.displayMinimizedWindow = displayMinimizedWindow; 796 797 } 798 799 815 public void setDisposeminimizedwindow(boolean disposeMinimizedWindow) { 816 817 this.disposeMinimizedWindow = disposeMinimizedWindow; 818 819 } 820 821 827 public void setFailonerror(boolean failOnError) { 828 829 this.failOnError = failOnError; 830 831 } 832 838 public void setFilterclassname(String filterClassName) { 839 840 this.filterClassName = filterClassName; 841 842 } 843 844 849 public void setFilterclasspath(Path filterClasspath) { 850 851 createFilterclasspath().append(filterClasspath); 852 853 } 854 855 863 public void setMinimizedwindowtitle(String minimizedWindowTitle) { 864 865 this.minimizedWindowTitle = minimizedWindowTitle; 866 867 } 868 869 877 public void setMinimizedwindowicon(File minimizedWindowIcon) { 878 879 this.minimizedWindowIcon = minimizedWindowIcon; 880 881 } 882 883 890 public void setOutput(File outputFile) { 891 892 this.outputFile = outputFile; 893 894 } 895 896 902 public void setPrint(boolean print) { 903 904 this.print = print; 905 906 } 907 908 916 public void setAppendoutput(boolean appendOutput) { 917 918 this.appendOutput = appendOutput; 919 920 } 921 922 930 public void setRedirectoutput(boolean redirect) { 931 932 this.redirect = redirect; 933 934 } 935 936 946 public void setRequiretools(boolean requireTools) { 947 948 this.requireTools = requireTools; 949 950 } 951 952 960 private boolean testIfCondition(String ifCondition) { 961 962 if (ifCondition == null || "".equals(ifCondition)) 963 return true; 964 return project.getProperty(ifCondition) != null; 965 966 } 967 968 976 private boolean testUnlessCondition(String unlessCondition) { 977 978 if (unlessCondition == null || "".equals(unlessCondition)) 979 return true; 980 return project.getProperty(unlessCondition) == null; 981 982 } 983 984 } 985 | Popular Tags |