1 19 package org.netbeans.modules.j2ee.websphere6.optional; 20 21 import java.io.*; 22 import java.net.*; 23 import java.util.*; 24 25 import javax.enterprise.deploy.shared.*; 26 import javax.enterprise.deploy.spi.*; 27 import javax.enterprise.deploy.spi.exceptions.*; 28 import javax.enterprise.deploy.spi.status.*; 29 30 import org.openide.*; 31 import org.openide.util.*; 32 import org.netbeans.modules.j2ee.deployment.plugins.api.*; 33 34 import org.netbeans.modules.j2ee.websphere6.*; 35 import org.netbeans.modules.j2ee.websphere6.ui.nodes.actions.ShowServerLogAction; 36 import org.netbeans.modules.j2ee.websphere6.util.*; 37 38 45 public class WSStartServer extends StartServer { 46 47 50 private WSDeploymentManager dm; 51 52 56 private int state; 57 58 61 private static Map isDebugModeMap = Collections.synchronizedMap((Map)new HashMap(2,1)); 62 63 68 public WSStartServer(DeploymentManager dm) { 69 this.dm = (WSDeploymentManager) dm; 71 72 this.state = isRunning() ? STATE_STARTED : STATE_STOPPED; 74 } 75 76 public boolean supportsStartDebugging(Target target) { 78 return true; 79 } 80 81 91 public ProgressObject startDebugging(Target target) { 92 if (WSDebug.isEnabled()) WSDebug.notify("starting server in debug mode"); 95 WSServerProgress serverProgress = new WSServerProgress(this); 97 98 serverProgress.notifyStart(StateType.RUNNING, ""); 101 if (state == STATE_STOPPING) { 103 for (int i = 0; i < WSStopRunnable.TIMEOUT; i += WSStopRunnable.DELAY) { 105 if (state == STATE_STOPPING) { 106 try { 107 Thread.sleep(WSStopRunnable.DELAY); 108 } catch (InterruptedException e) { 109 } 111 } 112 113 if (state == STATE_STOPPED) { 114 break; 115 } 116 } 117 } 118 119 if (state == STATE_STARTING || state == STATE_STARTED) { 121 serverProgress.notifyStart(StateType.COMPLETED, ""); 124 return serverProgress; 125 } 126 127 RequestProcessor.getDefault().post(new WSStartDebugRunnable( 129 serverProgress), 0, Thread.NORM_PRIORITY); 130 131 isDebugModeMap.put(dm.getHost() + dm.getPort() + dm.getDomainRoot(), new Object ()); 134 135 state = STATE_STARTING; 137 138 return serverProgress; 140 } 141 142 143 144 153 public boolean isDebuggable(Target target) { 154 155 if(!isDebugModeMap.containsKey(dm.getHost() + dm.getPort() + dm.getDomainRoot())){ 156 return false; 157 } 158 159 if(!isRunning()){ 160 isDebugModeMap.remove(dm.getHost() + dm.getPort() + dm.getDomainRoot()); 161 return false; 162 } 163 return true; 164 } 165 166 167 168 173 public boolean isAlsoTargetServer(Target target) { 174 return true; 175 } 176 177 186 public ServerDebugInfo getDebugInfo(Target target) { 187 return new ServerDebugInfo(dm.getHost(), new Integer ( 188 dm.getInstanceProperties().getProperty( 189 WSDeploymentFactory.DEBUGGER_PORT_ATTR)).intValue()); 190 } 191 192 197 public boolean supportsStartDeploymentManager() { 198 if (dm.getInstanceProperties().getProperty( 200 WSDeploymentFactory.IS_LOCAL_ATTR).equals("true")) { return true; 202 } else { 203 return false; 204 } 205 } 206 207 212 public ProgressObject stopDeploymentManager() { 213 if (WSDebug.isEnabled()) WSDebug.notify("stopping server"); 216 WSServerProgress serverProgress = new WSServerProgress(this); 218 219 serverProgress.notifyStop(StateType.RUNNING, ""); 222 if (state == STATE_STARTING) { 224 for (int i = 0; i < WSStartRunnable.TIMEOUT; i += WSStartRunnable.DELAY) { 226 if (state == STATE_STARTING) { 227 try { 228 Thread.sleep(WSStartRunnable.DELAY); 229 } catch (InterruptedException e) { 230 } 232 } 233 234 if (state == STATE_STARTED) { 235 break; 236 } 237 } 238 } 239 240 if (state == STATE_STOPPING || state == STATE_STOPPED) { 242 serverProgress.notifyStop(StateType.COMPLETED, ""); 245 return serverProgress; 246 } 247 248 RequestProcessor.getDefault().post(new WSStopRunnable(serverProgress), 250 0, Thread.NORM_PRIORITY); 251 252 isDebugModeMap.remove(dm.getHost() + dm.getPort() + dm.getDomainRoot()); 255 256 state = STATE_STOPPING; 258 259 return serverProgress; 261 } 262 263 268 public ProgressObject startDeploymentManager() { 269 if (WSDebug.isEnabled()) WSDebug.notify("starting server"); 272 WSServerProgress serverProgress = new WSServerProgress(this); 274 275 serverProgress.notifyStart(StateType.RUNNING, ""); 278 if (state == STATE_STOPPING) { 280 for (int i = 0; i < WSStopRunnable.TIMEOUT; i += WSStopRunnable.DELAY) { 282 if (state == STATE_STOPPING) { 283 try { 284 Thread.sleep(WSStopRunnable.DELAY); 285 } catch (InterruptedException e) { 286 } 288 } 289 290 if (state == STATE_STOPPED) { 291 break; 292 } 293 } 294 } 295 296 if (state == STATE_STARTING || state == STATE_STARTED) { 298 serverProgress.notifyStart(StateType.COMPLETED, ""); 301 return serverProgress; 302 } 303 304 RequestProcessor.getDefault().post(new WSStartRunnable(serverProgress), 306 0, Thread.NORM_PRIORITY); 307 308 isDebugModeMap.remove(dm.getHost() + dm.getPort() + dm.getDomainRoot()); 312 313 state = STATE_STARTING; 315 316 return serverProgress; 318 } 319 320 326 public boolean needsStartForTargetList() { 327 return true; 328 } 329 330 336 public boolean needsStartForConfigure() { 337 return false; } 339 340 346 public boolean needsStartForAdminConfig() { 347 return true; 348 } 349 350 356 public boolean isRunning() { 357 try { 359 new Socket(dm.getHost(), new Integer (dm.getPort()).intValue()); 360 361 return true; 363 } catch (UnknownHostException e) { 364 ErrorManager.getDefault().notify(ErrorManager.ERROR, e); 365 } catch (IOException e) { 366 } 369 370 return false; 372 } 373 374 383 private static String [] properties2StringArray(Properties properties) { 384 List list = new ArrayList(); 386 387 Enumeration keys = properties.keys(); 389 390 while (keys.hasMoreElements()) { 392 String key = (String ) keys.nextElement(); 394 395 list.add(key + "=" + properties.getProperty(key)); } 398 399 return (String []) list.toArray(new String [list.size()]); 401 } 402 403 409 private void setDebugMode(boolean debugEnabled, int debuggerPort) { 410 File configXmlFile = new File(dm.getInstanceProperties().getProperty( 412 WSDeploymentFactory.CONFIG_XML_PATH)); 413 414 String contents = WSUtil.readFile(configXmlFile); 416 417 if (debugEnabled) { 419 contents = contents.replaceAll("debugMode=\"false\"", "debugMode=\"true\""); 420 contents = contents.replaceFirst("suspend=n,address=[0-9]+\" genericJvmArguments", "suspend=n,address=" + debuggerPort + "\" genericJvmArguments"); 421 if (WSDebug.isEnabled()) { 422 WSDebug.notify("setting the address string to: " + "suspend=n,address=" + debuggerPort); 423 } 424 } else { 425 contents = contents.replaceAll("debugMode=\"true\"", "debugMode=\"false\""); 426 } 427 428 WSUtil.writeFile(configXmlFile, contents); 430 } 431 432 439 private class WSStartRunnable implements Runnable { 440 441 444 private String domainHome; 445 446 450 private String serverName; 451 452 456 private WSServerProgress serverProgress; 457 458 464 public WSStartRunnable(WSServerProgress serverProgress) { 465 this.serverProgress = serverProgress; 467 468 domainHome = dm.getInstanceProperties().getProperty( 471 WSDeploymentFactory.DOMAIN_ROOT_ATTR); 472 serverName = dm.getInstanceProperties().getProperty( 473 WSDeploymentFactory.SERVER_NAME_ATTR); 474 } 475 476 479 public void run() { 480 try { 481 long start = System.currentTimeMillis(); 484 485 setDebugMode(false, 0); 487 488 Process serverProcess = Runtime.getRuntime(). 490 exec(new String []{domainHome + "/bin/" + (Utilities.isWindows() ? STARTUP_BAT : STARTUP_SH), 492 serverName}); 493 494 new WSTailer(serverProcess.getInputStream(), NbBundle. 497 getMessage(WSStartServer.class, 498 "TXT_ioWindowTitle",dm.getServerTitleMessage())).start(); 500 502 new WSTailer(new File(dm.getLogFilePath()), 503 NbBundle.getMessage(ShowServerLogAction.class, 504 "LBL_LogWindowTitle", dm.getServerTitleMessage())).start(); 505 506 while (System.currentTimeMillis() - start < TIMEOUT) { 509 if (!isRunning()) { 512 serverProgress.notifyStart(StateType.RUNNING, 513 ""); } else { 515 serverProgress.notifyStart(StateType.COMPLETED, 516 ""); 518 state = STATE_STARTED; 520 521 return; 522 } 523 524 try { 527 Thread.sleep(DELAY); 528 } catch (InterruptedException e) {} 529 } 530 531 serverProgress.notifyStart(StateType.FAILED, ""); serverProcess.destroy(); 535 536 state = STATE_STOPPED; 538 } catch (IOException e) { 539 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 540 } 541 } 542 543 547 private static final int TIMEOUT = 120000; 548 549 552 private static final int DELAY = 5000; 553 554 557 private static final String STARTUP_SH = "startServer.sh"; 559 562 private static final String STARTUP_BAT = "startServer.bat"; } 564 565 572 private class WSStartDebugRunnable implements Runnable { 573 574 577 private String domainHome; 578 579 583 private String serverName; 584 585 590 private String debuggerPort; 591 592 596 private WSServerProgress serverProgress; 597 598 604 public WSStartDebugRunnable(WSServerProgress serverProgress) { 605 this.serverProgress = serverProgress; 607 608 domainHome = dm.getInstanceProperties().getProperty( 611 WSDeploymentFactory.DOMAIN_ROOT_ATTR); 612 debuggerPort = dm.getInstanceProperties().getProperty( 613 WSDeploymentFactory.DEBUGGER_PORT_ATTR); 614 serverName = dm.getInstanceProperties().getProperty( 615 WSDeploymentFactory.SERVER_NAME_ATTR); 616 } 617 618 621 public void run() { 622 try { 623 long start = System.currentTimeMillis(); 626 627 setDebugMode(true, new Integer (debuggerPort).intValue()); 629 630 Process serverProcess = Runtime.getRuntime(). 632 exec(new String []{domainHome + "/bin/" + (Utilities.isWindows() ? STARTUP_BAT : STARTUP_SH), 634 serverName}); 635 636 639 new WSTailer(serverProcess.getInputStream(), 640 NbBundle.getMessage(WSStartServer.class, 641 "TXT_ioWindowTitle",dm.getServerTitleMessage())).start(); 643 new WSTailer(new File(dm.getLogFilePath()), 645 NbBundle.getMessage(ShowServerLogAction.class, 646 "LBL_LogWindowTitle", dm.getServerTitleMessage())). 647 start(); 648 649 while (System.currentTimeMillis() - start < TIMEOUT) { 652 if (!isRunning()) { 655 serverProgress.notifyStart(StateType.RUNNING, 656 ""); } else { 658 serverProgress.notifyStart(StateType.COMPLETED, 659 ""); 661 state = STATE_STARTED; 663 664 return; 665 } 666 667 try { 670 Thread.sleep(DELAY); 671 } catch (InterruptedException e) {} 672 } 673 674 serverProgress.notifyStart(StateType.FAILED, ""); serverProcess.destroy(); 678 679 state = STATE_STOPPED; 681 } catch (IOException e) { 682 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 683 } 684 } 685 686 690 private static final int TIMEOUT = 120000; 691 692 695 private static final int DELAY = 5000; 696 697 700 private static final String STARTUP_SH = "startServer.sh"; 702 705 private static final String STARTUP_BAT = "startServer.bat"; } 707 708 715 private class WSStopRunnable implements Runnable { 716 717 720 private String domainHome; 721 722 726 private String serverName; 727 728 732 private WSServerProgress serverProgress; 733 734 740 public WSStopRunnable(WSServerProgress serverProgress) { 741 this.serverProgress = serverProgress; 743 744 domainHome = dm.getInstanceProperties().getProperty( 747 WSDeploymentFactory.DOMAIN_ROOT_ATTR); 748 serverName = dm.getInstanceProperties().getProperty( 749 WSDeploymentFactory.SERVER_NAME_ATTR); 750 } 751 752 755 public void run() { 756 try { 757 long start = System.currentTimeMillis(); 760 761 Process serverProcess = Runtime.getRuntime().exec( 763 new String []{domainHome + "/bin/" + (Utilities.isWindows() ? SHUTDOWN_BAT : SHUTDOWN_SH), 765 serverName}); 766 767 770 new WSTailer(serverProcess.getInputStream(), 771 NbBundle.getMessage(WSStartServer.class, 772 "TXT_ioWindowTitle", dm.getServerTitleMessage())).start(); 774 new WSTailer(new File(dm.getLogFilePath()), 776 NbBundle.getMessage(ShowServerLogAction.class, 777 "LBL_LogWindowTitle", dm.getServerTitleMessage())).start(); 778 779 while (System.currentTimeMillis() - start < TIMEOUT) { 782 if (isRunning()) { 783 serverProgress.notifyStop(StateType.RUNNING, ""); } else { 785 serverProgress.notifyStop(StateType.COMPLETED, ""); 787 state = STATE_STOPPED; 789 790 return; 791 } 792 793 try { 796 Thread.sleep(DELAY); 797 } catch (InterruptedException e) {} 798 } 799 800 serverProgress.notifyStop(StateType.FAILED, ""); serverProcess.destroy(); 804 805 state = STATE_STARTED; 807 } catch (IOException e) { 808 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 809 } 810 } 811 812 816 private static final int TIMEOUT = 120000; 817 818 821 private static final int DELAY = 5000; 822 823 826 private static final String SHUTDOWN_SH = "stopServer.sh"; 828 831 private static final String SHUTDOWN_BAT = "stopServer.bat"; } 833 834 840 private static class WSServerProgress implements ProgressObject { 841 842 845 private Vector listeners = new Vector(); 846 847 850 private DeploymentStatus deploymentStatus; 851 852 855 private Object source; 856 857 864 public WSServerProgress(Object source) { 865 this.source = source; 866 } 867 868 874 public void notifyStart(StateType state, String message) { 875 notify(new WSDeploymentStatus(ActionType.EXECUTE, CommandType.START, state, message)); 878 } 879 880 886 public void notifyStop(StateType state, String message) { 887 notify(new WSDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, state, message)); 890 } 891 892 898 public void notify(DeploymentStatus deploymentStatus) { 899 ProgressEvent evt = new ProgressEvent(source, null, deploymentStatus); 902 903 this.deploymentStatus = deploymentStatus; 905 906 java.util.Vector targets = null; 909 synchronized (this) { 910 if (listeners != null) { 911 targets = (java.util.Vector ) listeners.clone(); 912 } 913 } 914 915 if (targets != null) { 917 for (int i = 0; i < targets.size(); i++) { 918 ProgressListener target = (ProgressListener) targets.elementAt(i); 919 target.handleProgressEvent(evt); 920 } 921 } 922 } 923 924 931 public ClientConfiguration getClientConfiguration(TargetModuleID targetModuleID) { 932 return null; 933 } 934 935 940 public void removeProgressListener(ProgressListener progressListener) { 941 listeners.remove(progressListener); 942 } 943 944 949 public void addProgressListener(ProgressListener progressListener) { 950 listeners.add(progressListener); 951 } 952 953 958 public DeploymentStatus getDeploymentStatus() { 959 return deploymentStatus; 960 } 961 962 966 public TargetModuleID[] getResultTargetModuleIDs() { 967 return new TargetModuleID[]{}; 968 } 969 970 974 public boolean isStopSupported() { 975 return false; 976 } 977 978 982 public void stop() throws OperationUnsupportedException { 983 throw new OperationUnsupportedException(""); } 985 986 990 public boolean isCancelSupported() { 991 return false; 992 } 993 994 998 public void cancel() throws OperationUnsupportedException { 999 throw new OperationUnsupportedException(""); } 1001 } 1002 1003 1007 private static class WSDeploymentStatus implements DeploymentStatus { 1008 1011 private ActionType action; 1012 1013 1016 private CommandType command; 1017 1018 1021 private StateType state; 1022 1023 1026 private String message; 1027 1028 1036 public WSDeploymentStatus(ActionType action, CommandType command, StateType state, String message) { 1037 this.action = action; 1039 this.command = command; 1040 this.state = state; 1041 this.message = message; 1042 } 1043 1044 1049 public ActionType getAction() { 1050 return action; 1051 } 1052 1053 1058 public CommandType getCommand() { 1059 return command; 1060 } 1061 1062 1067 public String getMessage() { 1068 return message; 1069 } 1070 1071 1076 public StateType getState() { 1077 return state; 1078 } 1079 1080 1086 public boolean isCompleted() { 1087 return StateType.COMPLETED.equals(state); 1088 } 1089 1090 1095 public boolean isFailed() { 1096 return StateType.FAILED.equals(state); 1097 } 1098 1099 1104 public boolean isRunning() { 1105 return StateType.RUNNING.equals(state); 1106 } 1107 }; 1108 1109 private static final int STATE_STOPPED = 0; 1115 private static final int STATE_STARTING = 1; 1116 private static final int STATE_STARTED = 2; 1117 private static final int STATE_STOPPING = 3; 1118} | Popular Tags |