1 19 20 package org.netbeans.modules.tomcat5.ide; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.util.Collections ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 import javax.enterprise.deploy.shared.ActionType ; 34 import javax.enterprise.deploy.shared.CommandType ; 35 import javax.enterprise.deploy.shared.StateType ; 36 import javax.enterprise.deploy.spi.DeploymentManager ; 37 import javax.enterprise.deploy.spi.Target ; 38 import javax.enterprise.deploy.spi.TargetModuleID ; 39 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 40 import javax.enterprise.deploy.spi.status.ClientConfiguration ; 41 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 42 import javax.enterprise.deploy.spi.status.ProgressListener ; 43 import javax.enterprise.deploy.spi.status.ProgressObject ; 44 import org.netbeans.api.java.platform.JavaPlatform; 45 import org.netbeans.modules.j2ee.deployment.plugins.api.StartServer; 46 import org.netbeans.modules.tomcat5.progress.ProgressEventSupport; 47 import org.netbeans.modules.tomcat5.progress.Status; 48 import org.netbeans.modules.tomcat5.util.LogManager; 49 import org.netbeans.modules.tomcat5.util.Utils; 50 import org.openide.ErrorManager; 51 import org.openide.execution.NbProcessDescriptor; 52 import org.openide.util.NbBundle; 53 import org.openide.util.RequestProcessor; 54 import org.netbeans.modules.j2ee.deployment.plugins.api.ServerDebugInfo; 55 import org.netbeans.modules.j2ee.deployment.profiler.api.ProfilerServerSettings; 56 import org.netbeans.modules.j2ee.deployment.profiler.api.ProfilerSupport; 57 import org.netbeans.modules.tomcat5.TomcatFactory; 58 import org.netbeans.modules.tomcat5.TomcatManager; 59 import org.netbeans.modules.tomcat5.TomcatManager.TomcatVersion; 60 import org.netbeans.modules.tomcat5.util.EditableProperties; 61 import org.netbeans.modules.tomcat5.util.TomcatProperties; 62 import org.openide.filesystems.FileObject; 63 import org.openide.filesystems.FileUtil; 64 import org.openide.util.Utilities; 65 import org.xml.sax.SAXException ; 66 67 71 public final class StartTomcat extends StartServer implements ProgressObject { 72 73 public static final String CATALINA_BAT = "catalina.bat"; public static final String CATALINA_SH = "catalina.sh"; public static final String CATALINA_50_BAT = "catalina.50.bat"; public static final String CATALINA_50_SH = "catalina.50.sh"; 78 public static final String SETCLASSPATH_BAT = "setclasspath.bat"; public static final String SETCLASSPATH_SH = "setclasspath.sh"; 81 public static final String TAG_CATALINA_HOME = "catalina_home"; public static final String TAG_CATALINA_BASE = "catalina_base"; 84 public static final String TAG_JPDA = "jpda"; public static final String TAG_JPDA_STARTUP = "jpda_startup"; 87 88 public static final String TAG_EXEC_CMD = "catalina"; public static final String TAG_EXEC_STARTUP = "exec_startup"; public static final String TAG_EXEC_SHUTDOWN = "exec_shutdown"; public static final String TAG_SECURITY_OPT = "security_option"; public static final String TAG_FORCE_OPT = "force_option"; 94 95 public static final String TAG_DEBUG_CMD = "catalina"; 97 98 private static final int MODE_RUN = 0; 99 100 private static final int MODE_DEBUG = 1; 101 102 private static final int MODE_PROFILE = 2; 103 104 105 private static final long TIMEOUT_DELAY = 180000; 106 107 private TomcatManager tm; 108 109 private ProgressEventSupport pes; 110 private int currentServerPort; 112 private static Map isDebugModeUri = Collections.synchronizedMap((Map )new HashMap (2,1)); 113 114 public StartTomcat (DeploymentManager manager) { 115 assert manager instanceof TomcatManager : 116 "Illegal DeploymentManager instance: " + manager.getClass().getName(); tm = (TomcatManager)manager; 118 tm.setStartTomcat (this); 119 pes = new ProgressEventSupport (this); 120 currentServerPort = tm.getServerPort(); 121 } 122 123 public boolean supportsStartDeploymentManager () { 124 return true; 125 } 126 127 public boolean supportsStartProfiling(Target target) { 128 return true; 129 } 130 131 133 public ProgressObject startDeploymentManager () { 134 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 135 TomcatFactory.getEM ().log ("StartTomcat.startDeploymentManager called on "+tm); } 137 pes.fireHandleProgressEvent (null, new Status (ActionType.EXECUTE, CommandType.START, "", StateType.RUNNING)); 138 RequestProcessor.getDefault().post( 139 new StartRunnable(MODE_RUN, CommandType.START, null), 140 0, 141 Thread.NORM_PRIORITY); 142 isDebugModeUri.remove(tm.getUri()); 143 return this; 144 } 145 146 151 public boolean isAlsoTargetServer(Target target) { return true; } 152 153 156 public boolean needsStartForConfigure() { return false; } 157 158 162 public boolean needsStartForTargetList() { return false; } 163 164 167 public boolean needsStartForAdminConfig() { return false; } 168 169 172 public boolean isRunning() { 173 return tm.isRunning (true); 174 } 175 176 179 public boolean isDebuggable(Target target) { 180 if (!isDebugModeUri.containsKey(tm.getUri())) { 181 return false; 182 } 183 if (!isRunning()) { 184 isDebugModeUri.remove(tm.getUri()); 185 return false; 186 } 187 return true; 188 } 189 190 196 public ProgressObject stopDeploymentManager() { 197 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 198 TomcatFactory.getEM ().log ("StartTomcat.stopDeploymentManager called on "+tm); } 200 pes.fireHandleProgressEvent (null, new Status (ActionType.EXECUTE, CommandType.STOP, "", StateType.RUNNING)); 201 RequestProcessor.getDefault().post( 202 new StartRunnable(MODE_RUN, CommandType.STOP, null), 203 0, 204 Thread.NORM_PRIORITY); 205 isDebugModeUri.remove(tm.getUri()); 206 return this; 207 } 208 209 216 public ProgressObject startDebugging(Target target) { 217 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 218 TomcatFactory.getEM ().log ("StartTomcat.startDebugging called on "+tm); } 220 pes.fireHandleProgressEvent (null, new Status (ActionType.EXECUTE, CommandType.START, "", StateType.RUNNING)); 221 RequestProcessor.getDefault().post( 222 new StartRunnable(MODE_DEBUG, CommandType.START, null), 223 0, 224 Thread.NORM_PRIORITY); 225 return this; 226 } 227 228 public ProgressObject startProfiling(Target target, ProfilerServerSettings settings) { 229 if (TomcatFactory.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 230 TomcatFactory.getEM().log("StartTomcat.startProfiling called on " + tm); } 232 pes.fireHandleProgressEvent(null, new Status ( 233 ActionType.EXECUTE, 234 CommandType.START, 235 "", StateType.RUNNING)); 237 RequestProcessor.getDefault().post( 238 new StartRunnable(MODE_PROFILE, CommandType.START, settings), 239 0, 240 Thread.NORM_PRIORITY); 241 return this; 242 } 243 244 public ServerDebugInfo getDebugInfo(Target target) { 245 ServerDebugInfo sdi; 246 TomcatProperties tp = tm.getTomcatProperties(); 247 if (tp.getDebugType().toLowerCase().indexOf("socket") != -1) { sdi = new ServerDebugInfo("localhost", tp.getDebugPort()); } else { 250 sdi = new ServerDebugInfo("localhost", tp.getSharedMem()); } 252 return sdi; 253 } 254 255 private class StartRunnable implements Runnable { 256 257 private int mode; 258 private CommandType command = CommandType.START; 259 private ProfilerServerSettings profilerSettings; 260 261 public StartRunnable(int mode, CommandType command, ProfilerServerSettings profilerSettings) { 262 this.mode = mode; 263 this.command = command; 264 this.profilerSettings = profilerSettings; 265 } 266 267 public synchronized void run () { 268 TomcatProperties tp = tm.getTomcatProperties(); 270 File homeDir = tp.getCatalinaHome(); 271 if (homeDir == null || !homeDir.exists()) { 272 fireCmdExecProgressEvent( 273 command == CommandType.START ? "MSG_NoHomeDirStart" : "MSG_NoHomeDirStop", 274 StateType.FAILED); 275 return; 276 } 277 File baseDir = tp.getCatalinaBase(); 278 if (baseDir == null) { 279 baseDir = homeDir; 280 } else { 281 if (baseDir != null) { 282 String [] files = baseDir.list(); 283 if (files == null || files.length == 0) { 284 baseDir = tm.createBaseDir(baseDir, homeDir); 285 } 286 } 287 if (baseDir == null) { 288 fireCmdExecProgressEvent( 289 command == CommandType.START ? "MSG_NoBaseDirStart" : "MSG_NoBaseDirStop", 290 StateType.FAILED); 291 return; 292 } 293 } 294 295 File startupScript = getStartupScript(); 297 if (!startupScript.exists()) { 298 final String MSG = NbBundle.getMessage( 299 StartTomcat.class, 300 command == CommandType.START ? "MSG_StartFailedNoStartScript" : "MSG_StopFailedNoStartScript", 301 startupScript.getAbsolutePath()); 302 pes.fireHandleProgressEvent( 303 null, 304 new Status (ActionType.EXECUTE, command, MSG, StateType.FAILED)); 305 return; 306 } 307 308 if (command == CommandType.START) { 310 try { 311 MonitorSupport.synchronizeMonitorWithFlag(tm, true, !tm.isBundledTomcat()); 312 } catch (IOException e) { 313 if (MonitorSupport.getMonitorFlag(tm)) { 314 MonitorSupport.setMonitorFlag(tm, false); 316 fireCmdExecProgressEvent(tm.isTomcat60() ? "MSG_enableMonitorSupportErr60" : "MSG_enableMonitorSupportErr", StateType.FAILED); 317 } else { 318 fireCmdExecProgressEvent("MSG_disableMonitorSupportErr", StateType.FAILED); 320 } 321 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 322 return; 323 } catch (SAXException e) { 324 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 326 } 327 try { 328 DebugSupport.allowDebugging(tm); 329 } 330 catch (IOException e) { 331 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 333 } 334 catch (SAXException e) { 335 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 337 } 338 } 339 340 currentServerPort = tm.getServerPort(); int shutdownPort = tm.getShutdownPort(); 342 343 if (command == CommandType.START) { 344 if (!Utils.isPortFree(currentServerPort)) { 346 fireCmdExecProgressEvent("MSG_StartFailedServerPortInUse", String.valueOf(currentServerPort), StateType.FAILED); 347 return; 348 } 349 if (!Utils.isPortFree(shutdownPort)) { 350 fireCmdExecProgressEvent("MSG_StartFailedShutdownPortInUse", String.valueOf(shutdownPort), StateType.FAILED); 351 return; 352 } 353 } 354 355 String javaOpts = tp.getJavaOpts(); 357 if (tp.getProxyEnabled()) { 360 StringBuilder sb = new StringBuilder (javaOpts); 361 final String [] PROXY_PROPS = { 362 "http.proxyHost", "http.proxyPort", "http.nonProxyHosts", "https.proxyHost", "https.proxyPort", }; 368 boolean isWindows = Utilities.isWindows(); 369 for (String prop : PROXY_PROPS) { 370 if (javaOpts.indexOf(prop) == -1) { 371 String value = System.getProperty(prop); 372 if (value != null) { 373 if (isWindows && "http.nonProxyHosts".equals(prop)) { value = "\"" + value + "\""; } 377 sb.append(" -D").append(prop).append("=").append(value); } 379 } 380 } 381 javaOpts = sb.toString(); 382 } 383 384 JavaPlatform platform = mode == MODE_PROFILE ? profilerSettings.getJavaPlatform() 385 : getJavaPlatform(); 386 String jdkVersion = platform.getSpecification().getVersion().toString(); 387 388 if (tm.isBundledTomcat()) { 389 patchCatalinaProperties(tp.getCatalinaDir(), "1.4".equals(jdkVersion)); } 393 394 if ((mode == MODE_DEBUG) && (command == CommandType.START)) { 395 396 NbProcessDescriptor pd = null; 397 if (tp.getSecManager()) { 398 pd = defaultDebugStartDesc (TAG_DEBUG_CMD, TAG_JPDA_STARTUP, TAG_SECURITY_OPT); 399 } else { 400 pd = defaultDebugStartDesc (TAG_DEBUG_CMD, TAG_JPDA_STARTUP); 401 } 402 try { 403 fireCmdExecProgressEvent("MSG_startProcess", StateType.RUNNING); 404 Process p = null; 405 406 String address; 407 String transport; 408 if (tp.getDebugType().toLowerCase().indexOf("socket") != -1) { transport = "dt_socket"; address = Integer.toString(tp.getDebugPort()); 411 } else { 412 transport = "dt_shmem"; address = tp.getSharedMem(); 414 } 415 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 416 TomcatFactory.getEM ().log ("transport: " + transport); TomcatFactory.getEM ().log ("address: " + address); } 419 p = pd.exec ( 420 new TomcatFormat(startupScript, homeDir), 421 new String [] { 422 "JAVA_HOME=" + getJavaHome(platform), "JRE_HOME=", "JAVA_OPTS=" + javaOpts, "JPDA_TRANSPORT=" + transport, "JPDA_ADDRESS=" + address, "CATALINA_HOME=" + homeDir.getAbsolutePath(), "CATALINA_BASE=" + baseDir.getAbsolutePath(), "NB_TOMCAT_JDK=" + jdkVersion }, 433 true, 434 new File (homeDir, "bin") ); 436 tm.setTomcatProcess(p); 437 openLogs(); 438 } catch (java.io.IOException ioe) { 439 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 440 TomcatFactory.getEM ().notify (ErrorManager.INFORMATIONAL, ioe); 441 } 442 fireCmdExecProgressEvent(command == CommandType.START ? "MSG_StartFailedIOE" : "MSG_StopFailedIOE", 443 startupScript.getAbsolutePath(), StateType.FAILED); 444 return; 445 } 446 } else if ((mode == MODE_PROFILE) && (command == CommandType.START)) { 447 NbProcessDescriptor pd = null; 448 if (tp.getSecManager()) { 449 pd = defaultExecDesc(TAG_EXEC_CMD, TAG_EXEC_STARTUP, TAG_SECURITY_OPT); 450 } else { 451 pd = defaultExecDesc(TAG_EXEC_CMD, TAG_EXEC_STARTUP); 452 } 453 try { 454 fireCmdExecProgressEvent("MSG_StartingInProfileMode", StateType.RUNNING); 455 Process p = null; 456 457 String [] profJvmArgs = profilerSettings.getJvmArgs(); 458 StringBuffer catalinaOpts = new StringBuffer (); 460 for (int i = 0; i < profJvmArgs.length; i++) { 461 catalinaOpts.append(profJvmArgs[i]).append(" "); } 463 String [] defaultEnv = new String [] { 464 "JAVA_HOME=" + getJavaHome(platform), "JRE_HOME=", "JAVA_OPTS=" + javaOpts, "CATALINA_OPTS=" + catalinaOpts.toString(), "CATALINA_HOME=" + homeDir.getAbsolutePath(), "CATALINA_BASE=" + baseDir.getAbsolutePath(), "NB_TOMCAT_JDK=" + jdkVersion }; 474 String [] profEnv = profilerSettings.getEnv(); 475 String [] envp = new String [defaultEnv.length + profEnv.length]; 477 System.arraycopy(profEnv, 0, envp, 0, profEnv.length); 478 System.arraycopy(defaultEnv, 0, envp, profEnv.length, defaultEnv.length); 479 p = pd.exec( 480 new TomcatFormat(startupScript, homeDir), 481 envp, 482 true, 483 new File (homeDir, "bin") ); 485 tm.setTomcatProcess(p); 486 openLogs(); 487 } catch (java.io.IOException ioe) { 488 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 489 TomcatFactory.getEM ().notify (ErrorManager.INFORMATIONAL, ioe); 490 } 491 fireCmdExecProgressEvent(command == CommandType.START ? "MSG_StartFailedIOE" : "MSG_StopFailedIOE", 492 startupScript.getAbsolutePath(), StateType.FAILED); 493 return; 494 } 495 } else { 496 NbProcessDescriptor pd = null; 497 if (command == CommandType.START) { 498 if (tp.getSecManager()) { 499 pd = defaultExecDesc(TAG_EXEC_CMD, TAG_EXEC_STARTUP, TAG_SECURITY_OPT); 500 } else { 501 pd = defaultExecDesc(TAG_EXEC_CMD, TAG_EXEC_STARTUP); 502 } 503 } else { 504 if (tp.getForceStop() && Utilities.isUnix()) { 505 pd = defaultExecDesc(TAG_EXEC_CMD, TAG_EXEC_SHUTDOWN, TAG_FORCE_OPT); 506 } else { 507 pd = defaultExecDesc(TAG_EXEC_CMD, TAG_EXEC_SHUTDOWN); 508 } 509 } 510 try { 511 fireCmdExecProgressEvent(command == CommandType.START ? "MSG_startProcess" : "MSG_stopProcess", 512 StateType.RUNNING); 513 Process p = pd.exec ( 514 new TomcatFormat (startupScript, homeDir), 515 new String [] { 516 "JAVA_HOME=" + getJavaHome(platform), "JRE_HOME=", "JAVA_OPTS=" + javaOpts, "CATALINA_HOME=" + homeDir.getAbsolutePath(), "CATALINA_BASE=" + baseDir.getAbsolutePath(), "NB_TOMCAT_JDK=" + jdkVersion }, 525 true, 526 new File (homeDir, "bin") 527 ); 528 if (command == CommandType.START) { 529 tm.setTomcatProcess(p); 530 openLogs(); 531 } else { 532 RequestProcessor.getDefault().post(new StreamConsumer(p.getInputStream()), 0, Thread.MIN_PRIORITY); 534 RequestProcessor.getDefault().post(new StreamConsumer(p.getErrorStream()), 0, Thread.MIN_PRIORITY); 535 } 536 } catch (java.io.IOException ioe) { 537 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 538 TomcatFactory.getEM ().notify (ErrorManager.INFORMATIONAL, ioe); } 540 fireCmdExecProgressEvent(command == CommandType.START ? "MSG_StartFailedIOE" : "MSG_StopFailedIOE", 541 startupScript.getAbsolutePath(), StateType.FAILED); 542 return; 543 } 544 } 545 fireCmdExecProgressEvent("MSG_waiting", StateType.RUNNING); 546 if (hasCommandSucceeded()) { 547 if (command == CommandType.START && mode == MODE_DEBUG) { 548 isDebugModeUri.put(tm.getUri(), new Object ()); 549 } 550 fireCmdExecProgressEvent(command == CommandType.START ? "MSG_Started" : "MSG_Stopped", 551 StateType.COMPLETED); 552 } else { 553 fireCmdExecProgressEvent(command == CommandType.START ? "MSG_StartFailed" : "MSG_StopFailed", 554 StateType.FAILED); 555 } 556 } 557 558 559 private void openLogs() { 560 LogManager logManager = tm.logManager(); 561 if (logManager.hasJuliLog()) { 562 logManager.openJuliLog(); 563 } 564 logManager.closeServerLog(); 565 logManager.openServerLog(); 566 } 567 568 575 private void fireCmdExecProgressEvent(String resName, StateType stateType) { 576 String msg = NbBundle.getMessage(StartTomcat.class, resName); 577 pes.fireHandleProgressEvent( 578 null, 579 new Status (ActionType.EXECUTE, command, msg, stateType)); 580 } 581 582 590 private void fireCmdExecProgressEvent(String resName, Object arg1, StateType stateType) { 591 String msg = NbBundle.getMessage(StartTomcat.class, resName, arg1); 592 pes.fireHandleProgressEvent( 593 null, 594 new Status (ActionType.EXECUTE, command, msg, stateType)); 595 } 596 597 604 private boolean hasCommandSucceeded() { 605 long timeout = System.currentTimeMillis() + TIMEOUT_DELAY; 606 while (true) { 607 boolean isRunning = isRunning(); 608 if (command == CommandType.START) { 609 if (isRunning) { 610 return true; 611 } 612 if (isStopped()) { 613 return false; 615 } 616 if (mode == MODE_PROFILE) { 617 int state = ProfilerSupport.getState(); 618 if (state == ProfilerSupport.STATE_BLOCKING || 619 state == ProfilerSupport.STATE_RUNNING || 620 state == ProfilerSupport.STATE_PROFILING) { 621 return true; 622 } else if (state == ProfilerSupport.STATE_INACTIVE) { 623 return false; 624 } 625 } 626 } 627 if (command == CommandType.STOP) { 628 if (isStopped()) { 629 try { 632 Thread.sleep(2000); 633 } catch(InterruptedException ie) {} 634 return true; 635 } 636 } 637 if (System.currentTimeMillis() > timeout) { 639 return false; 640 } 641 try { 642 Thread.sleep(1000); } catch(InterruptedException ie) {} 644 } 645 } 646 } 647 648 651 private boolean isStopped() { 652 Process proc = tm.getTomcatProcess(); 653 if (proc != null) { 654 try { 655 proc.exitValue(); 656 return true; 658 } catch (IllegalThreadStateException e) { 659 return false; 661 } 662 } else { 663 int timeout = tm.getTomcatProperties().getRunningCheckTimeout(); 664 return !Utils.pingTomcat(tm.getServerPort(), timeout); 665 } 666 } 667 668 671 public ProgressObject startServer (Target target) { 672 return null; 673 } 674 675 public boolean supportsStartDebugging(Target target) { 676 return true; 677 } 678 679 public ClientConfiguration getClientConfiguration (TargetModuleID targetModuleID) { 680 return null; 681 } 682 683 public DeploymentStatus getDeploymentStatus () { 684 return pes.getDeploymentStatus (); 685 } 686 687 public TargetModuleID [] getResultTargetModuleIDs () { 688 return new TargetModuleID [] {}; 689 } 690 691 public boolean isCancelSupported () { 692 return false; 693 } 694 695 public void cancel () 696 throws OperationUnsupportedException { 697 throw new OperationUnsupportedException (""); 698 } 699 700 public boolean isStopSupported () { 701 return false; 702 } 703 704 public void stop () 705 throws OperationUnsupportedException { 706 throw new OperationUnsupportedException (""); 707 } 708 709 public void addProgressListener (ProgressListener pl) { 710 pes.addProgressListener (pl); 711 } 712 713 public void removeProgressListener (ProgressListener pl) { 714 pes.removeProgressListener (pl); 715 } 716 717 718 public String toString () { 719 return "StartTomcat [" + tm + "]"; } 721 722 public int getCurrentServerPort() { 723 return currentServerPort; 724 } 725 726 728 private static NbProcessDescriptor defaultExecDesc(String command, String argCommand, String option) { 729 return new NbProcessDescriptor ( 730 "{" + command + "}", "{" + argCommand + "}" + " {" + option + "}", NbBundle.getMessage (StartTomcat.class, "MSG_TomcatExecutionCommand") 733 ); 734 } 735 736 private static NbProcessDescriptor defaultExecDesc(String command, String argCommand) { 737 return new NbProcessDescriptor ( 738 "{" + command + "}", "{" + argCommand + "}", NbBundle.getMessage (StartTomcat.class, "MSG_TomcatExecutionCommand") 741 ); 742 } 743 744 private static NbProcessDescriptor defaultDebugStartDesc(String command, String jpdaCommand, String option) { 745 return new NbProcessDescriptor ( 746 "{" + command + "}", "{" + TAG_JPDA + "}" + " {" + jpdaCommand + "}" + " {" + option + "}", NbBundle.getMessage (StartTomcat.class, "MSG_TomcatExecutionCommand") 749 ); 750 } 751 752 private static NbProcessDescriptor defaultDebugStartDesc(String command, String jpdaCommand) { 753 return new NbProcessDescriptor ( 754 "{" + command + "}", "{" + TAG_JPDA + "}" + " {" + jpdaCommand + "}", NbBundle.getMessage (StartTomcat.class, "MSG_TomcatExecutionCommand") 757 ); 758 } 759 760 private String getJavaHome(JavaPlatform platform) { 761 FileObject fo = (FileObject)platform.getInstallFolders().iterator().next(); 762 return FileUtil.toFile(fo).getAbsolutePath(); 763 } 764 765 766 private File getStartupScript() { 767 TomcatProperties tp = tm.getTomcatProperties(); 768 if (tp.getCustomScript()) { 769 return new File (tp.getScriptPath()); 770 } 771 if (tm.getTomcatVersion() == TomcatVersion.TOMCAT_50 773 && "1.5".equals(getJavaPlatform().getSpecification().getVersion().toString())) { String startupScript = Utilities.isWindows() ? CATALINA_50_BAT : CATALINA_50_SH; 775 File scriptFile = new File (tp.getCatalinaHome(), "/bin/" + startupScript); if (scriptFile.exists()) { 777 return scriptFile; 778 } 779 } 780 String startupScript = Utilities.isWindows() ? CATALINA_BAT : CATALINA_SH; 781 return new File (tp.getCatalinaHome(), "/bin/" + startupScript); } 783 784 private JavaPlatform getJavaPlatform() { 785 return tm.getTomcatProperties().getJavaPlatform(); 786 } 787 788 790 private void patchCatalinaProperties(File catalinaBase, final boolean endorsedEnabled) { 791 File catalinaProp = new File (catalinaBase, "conf/catalina.properties"); if (!catalinaProp.exists()) { 793 return; } 795 EditableProperties props = new EditableProperties(); 796 try { 797 InputStream is = new BufferedInputStream (new FileInputStream (catalinaProp)); 798 try { 799 props.load(is); 800 String COMMON_LOADER = "common.loader"; String commonLoader = props.getProperty(COMMON_LOADER); 802 if (commonLoader != null) { 803 String COMMON_ENDORSED = "${catalina.home}/common/endorsed/*.jar"; int idx = commonLoader.indexOf(COMMON_ENDORSED); 805 if (endorsedEnabled) { 806 if (idx == -1) { String COMMON_LIB = "${catalina.home}/" + tm.libFolder() + "/*.jar"; int commonLibIdx = commonLoader.indexOf(COMMON_LIB); 809 StringBuffer sb = new StringBuffer (commonLibIdx == -1 810 ? commonLoader 811 : commonLoader.substring(0, commonLibIdx)); 812 if (commonLibIdx != -1) { 813 sb.append(COMMON_ENDORSED).append(',').append(commonLoader.substring(commonLibIdx)); 814 } else { 815 if (commonLoader.trim().length() != 0) { 816 sb.append(','); 817 } 818 sb.append(COMMON_ENDORSED); 819 } 820 props.setProperty(COMMON_LOADER, sb.toString()); 821 } else { 822 return; 823 } 824 } else { 825 if (idx != -1) { String strBefore = commonLoader.substring(0, idx); 827 int commaIdx = strBefore.lastIndexOf(','); 828 StringBuffer sb = new StringBuffer (commonLoader.substring(0, commaIdx == -1 ? idx : commaIdx)); 829 String strAfter = commonLoader.substring(idx + COMMON_ENDORSED.length()); 830 if (commaIdx == -1) { 831 int trailingCommaIdx = strAfter.indexOf(','); 833 if (trailingCommaIdx != -1) { 834 strAfter = strAfter.substring(trailingCommaIdx + 1); 835 } 836 } 837 sb.append(strAfter); 838 props.setProperty(COMMON_LOADER, sb.toString()); 839 } else { 840 return; 841 } 842 } 843 } 844 } finally { 845 is.close(); 846 } 847 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (catalinaProp)); 849 try { 850 props.store(out); 851 } finally { 852 out.close(); 853 } 854 } catch (FileNotFoundException fnfe) { 855 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fnfe); 856 } catch (IOException ioe) { 857 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 858 } 859 } 860 861 863 private static class StreamConsumer implements Runnable { 864 865 private BufferedInputStream in; 866 867 public StreamConsumer(InputStream is) { 868 in = new BufferedInputStream (is); 869 } 870 871 public void run() { 872 try { 873 byte buffer[] = new byte[1024]; 874 while (true) { 875 int n = in.read(buffer); 876 if (n < 0) { 877 break; 878 } 879 if (TomcatFactory.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 880 TomcatFactory.getEM().log(ErrorManager.INFORMATIONAL, new String (buffer, 0, n)); 881 } 882 } 883 } catch (IOException ioe) { 884 if (TomcatFactory.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 885 TomcatFactory.getEM().notify(ErrorManager.INFORMATIONAL, ioe); 886 } 887 } finally { 888 try { in.close(); } catch (IOException ioe) {}; 889 } 890 } 891 }; 892 893 896 private static class TomcatFormat extends org.openide.util.MapFormat { 897 898 private static final long serialVersionUID = 992972967554321415L; 899 900 public TomcatFormat(File startupScript, File homeDir) { 901 super(new java.util.HashMap ()); 902 java.util.Map map = getMap (); 903 String scriptPath = startupScript.getAbsolutePath(); 904 map.put(TAG_EXEC_CMD, scriptPath); 905 map.put(TAG_EXEC_STARTUP, "run"); map.put(TAG_EXEC_SHUTDOWN, "stop"); map.put(TAG_DEBUG_CMD, scriptPath); 908 map.put(TAG_JPDA, "jpda"); map.put(TAG_JPDA_STARTUP, "run"); map.put(TAG_SECURITY_OPT, "-security"); map.put(TAG_FORCE_OPT, "-force"); map.put(TAG_CATALINA_HOME, homeDir.getAbsolutePath()); 913 } 914 } 915 } 916 | Popular Tags |