1 19 package org.netbeans.modules.j2ee.weblogic9.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.weblogic9.*; 35 import org.netbeans.modules.j2ee.weblogic9.util.*; 36 37 44 public class WLStartServer extends StartServer { 45 46 private static final int SERVER_CHECK_TIMEOUT = 2000; 47 48 51 private WLDeploymentManager dm; 52 53 56 private Process serverProcess; 57 58 63 public WLStartServer(DeploymentManager dm) { 64 this.dm = (WLDeploymentManager) dm; 66 } 67 68 78 public ProgressObject startDebugging(Target target) { 79 if (WLDebug.isEnabled()) WLDebug.notify(getClass(), "starting server in " + "debug mode"); 83 WLServerProgress serverProgress = new WLServerProgress(this); 85 86 String serverName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 88 serverProgress.notifyStart(StateType.RUNNING, NbBundle.getMessage(WLStartServer.class, "MSG_START_SERVER_IN_PROGRESS", serverName)); 90 RequestProcessor.getDefault().post(new WLStartDebugRunnable( 92 serverProgress), 0, Thread.NORM_PRIORITY); 93 94 isDebuggable = true; 96 97 return serverProgress; 99 } 100 101 104 private boolean isDebuggable; 105 106 115 public boolean isDebuggable(Target target) { 116 return isDebuggable; 117 } 118 119 124 public boolean isAlsoTargetServer(Target target) { 125 return true; 126 } 127 128 137 public ServerDebugInfo getDebugInfo(Target target) { 138 return new ServerDebugInfo(dm.getHost(), new Integer ( 139 dm.getInstanceProperties().getProperty( 140 WLPluginProperties.DEBUGGER_PORT_ATTR)).intValue()); 141 } 142 143 148 public boolean supportsStartDeploymentManager() { 149 if (dm.getInstanceProperties().getProperty( 151 WLPluginProperties.IS_LOCAL_ATTR).equals("true")) { return true; 153 } else { 154 return false; 155 } 156 } 157 158 163 public ProgressObject stopDeploymentManager() { 164 if (WLDebug.isEnabled()) WLDebug.notify(getClass(), "stopping server!"); 167 WLServerProgress serverProgress = new WLServerProgress(this); 169 170 String serverName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 172 serverProgress.notifyStart(StateType.RUNNING, NbBundle.getMessage(WLStartServer.class, "MSG_STOP_SERVER_IN_PROGRESS", serverName)); 174 RequestProcessor.getDefault().post(new WLStopRunnable(serverProgress), 0, Thread.NORM_PRIORITY); 176 177 isDebuggable = false; 179 180 return serverProgress; 182 } 183 184 189 public ProgressObject startDeploymentManager() { 190 if (WLDebug.isEnabled()) WLDebug.notify(getClass(), "starting server!"); 193 WLServerProgress serverProgress = new WLServerProgress(this); 195 196 String serverName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 198 serverProgress.notifyStart(StateType.RUNNING, NbBundle.getMessage(WLStartServer.class, "MSG_START_SERVER_IN_PROGRESS", serverName)); 200 RequestProcessor.getDefault().post(new WLStartRunnable( 202 serverProgress), 0, Thread.NORM_PRIORITY); 203 204 isDebuggable = false; 207 208 return serverProgress; 210 } 211 212 218 public boolean needsStartForTargetList() { 219 return true; 220 } 221 222 228 public boolean needsStartForConfigure() { 229 return false; 230 } 231 232 238 public boolean needsStartForAdminConfig() { 239 return true; 240 } 241 242 248 public boolean isRunning() { 249 return isRunning(true); 250 } 251 252 258 public boolean isRunning(boolean checkResponse) { 259 Process proc = dm.getServerProcess(); 260 if (proc != null) { 261 try { 262 proc.exitValue(); 263 return false; 265 } catch (IllegalThreadStateException e) { 266 if (!checkResponse) { 268 return true; 269 } 270 } 271 } 272 if (checkResponse) { 273 String host = dm.getHost(); 274 int port = new Integer (dm.getPort()).intValue(); 275 return ping(host, port, SERVER_CHECK_TIMEOUT); } else { 277 return false; } 279 } 280 281 285 private boolean isStopped() { 286 Process proc = dm.getServerProcess(); 287 if (proc != null) { 288 try { 289 proc.exitValue(); 290 return true; 292 } catch (IllegalThreadStateException e) { 293 return false; 295 } 296 } else { 297 String host = dm.getHost(); 298 int port = new Integer (dm.getPort()).intValue(); 299 return !ping(host, port, SERVER_CHECK_TIMEOUT); } 301 } 302 303 304 public static boolean ping(String host, int port, int timeout) { 305 Socket socket = new Socket(); 307 try { 308 try { 309 socket.connect(new InetSocketAddress(host, port), timeout); socket.setSoTimeout(timeout); 311 PrintWriter out = new PrintWriter(socket.getOutputStream(), true); 312 try { 313 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 314 try { 315 out.println("GET /console/login/LoginForm.jsp HTTP/1.1\nHost:\n"); 318 return "HTTP/1.1 200 OK".equals(in.readLine()); } finally { 321 in.close(); 322 } 323 } finally { 324 out.close(); 325 } 326 } finally { 327 socket.close(); 328 } 329 } catch (IOException ioe) { 330 return false; 331 } 332 } 333 334 343 private static String [] properties2StringArray(Properties properties) { 344 List list = new ArrayList(); 346 347 Enumeration keys = properties.keys(); 349 350 while (keys.hasMoreElements()) { 352 String key = (String ) keys.nextElement(); 354 355 list.add(key + "=" + properties.getProperty(key)); 357 } 358 359 return (String []) list.toArray(new String [list.size()]); 361 } 362 363 public boolean supportsStartDebugging(Target target) { 364 return supportsStartDeploymentManager(); 366 } 367 368 375 private class WLStartRunnable implements Runnable { 376 377 380 private String domainHome; 381 382 386 private WLServerProgress serverProgress; 387 388 394 public WLStartRunnable(WLServerProgress serverProgress) { 395 this.serverProgress = serverProgress; 397 398 domainHome = dm.getInstanceProperties().getProperty( 401 WLPluginProperties.DOMAIN_ROOT_ATTR); 402 } 403 404 407 public void run() { 408 try { 409 long start = System.currentTimeMillis(); 412 413 serverProcess = Runtime.getRuntime().exec( 415 domainHome + "/" + (Utilities.isWindows() ? STARTUP_BAT : STARTUP_SH)); 417 418 dm.setServerProcess(serverProcess); 419 420 new WLTailer(serverProcess.getInputStream(), dm.getURI()); 423 424 String serverName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 425 426 while (System.currentTimeMillis() - start < TIMEOUT) { 429 if (isRunning()) { 431 serverProgress.notifyStart(StateType.COMPLETED, NbBundle.getMessage(WLStartServer.class, "MSG_SERVER_STARTED", serverName)); return; 433 } 434 435 try { 438 Thread.sleep(DELAY); 439 } catch (InterruptedException e) {} 440 } 441 442 serverProgress.notifyStart(StateType.FAILED, NbBundle.getMessage(WLStartServer.class, "MSG_StartServerTimeout")); 445 } catch (IOException e) { 446 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 447 } 448 } 449 450 454 private static final int TIMEOUT = 900000; 455 456 459 private static final int DELAY = 5000; 460 461 464 private static final String STARTUP_SH = "startWebLogic.sh"; 466 469 private static final String STARTUP_BAT = "startWebLogic.cmd"; } 471 472 479 private class WLStartDebugRunnable implements Runnable { 480 481 484 private String domainHome; 485 486 491 private String debuggerPort; 492 493 497 private WLServerProgress serverProgress; 498 499 505 public WLStartDebugRunnable(WLServerProgress serverProgress) { 506 this.serverProgress = serverProgress; 508 509 domainHome = dm.getInstanceProperties().getProperty( 512 WLPluginProperties.DOMAIN_ROOT_ATTR); 513 debuggerPort = dm.getInstanceProperties().getProperty( 514 WLPluginProperties.DEBUGGER_PORT_ATTR); 515 } 516 517 520 public void run() { 521 try { 522 long start = System.currentTimeMillis(); 525 526 527 File cwd = new File (domainHome); 529 assert cwd.isDirectory() : "Working directory for weblogic does not exist:" + domainHome; org.openide.execution.NbProcessDescriptor pd = new org.openide.execution.NbProcessDescriptor(domainHome + "/" + (Utilities.isWindows() ? STARTUP_BAT : STARTUP_SH), ""); 531 String envp[]; 532 envp = new String [] {"JAVA_OPTIONS=-Xdebug -Xnoagent -Djava.compiler=none -Xrunjdwp:server=y,suspend=n,transport=dt_socket,address=" + debuggerPort}; 534 serverProcess = pd.exec(null, envp, true, cwd); 535 536 dm.setServerProcess(serverProcess); 537 538 new WLTailer(serverProcess.getInputStream(), dm.getURI()); 541 542 String serverName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 543 544 while (System.currentTimeMillis() - start < TIMEOUT) { 547 if (isRunning()) { 549 serverProgress.notifyStart(StateType.COMPLETED, NbBundle.getMessage(WLStartServer.class, "MSG_SERVER_STARTED", serverName)); return; 551 } 552 553 try { 556 Thread.sleep(DELAY); 557 } catch (InterruptedException e) {} 558 } 559 560 serverProgress.notifyStart(StateType.FAILED, NbBundle.getMessage(WLStartServer.class, "MSG_StartServerTimeout")); 563 } catch (IOException e) { 564 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 565 } 566 } 567 568 572 private static final int TIMEOUT = 900000; 573 574 577 private static final int DELAY = 5000; 578 579 582 private static final String STARTUP_SH = "startWebLogic.sh"; 584 587 private static final String STARTUP_BAT = "startWebLogic.cmd"; } 589 590 597 private class WLStopRunnable implements Runnable { 598 599 602 private String domainHome; 603 604 608 private WLServerProgress serverProgress; 609 610 616 public WLStopRunnable(WLServerProgress serverProgress) { 617 this.serverProgress = serverProgress; 619 620 domainHome = dm.getInstanceProperties().getProperty( 623 WLPluginProperties.DOMAIN_ROOT_ATTR); 624 } 625 626 629 public void run() { 630 try { 631 long start = System.currentTimeMillis(); 634 635 Process serverProcess = Runtime.getRuntime().exec( 637 domainHome + "/bin/" + (Utilities.isWindows() ? SHUTDOWN_BAT : SHUTDOWN_SH)); 639 640 new WLTailer(serverProcess.getInputStream(), dm.getURI()); 643 644 String serverName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR); 645 646 while (System.currentTimeMillis() - start < TIMEOUT) { 647 if (!isStopped()) { 648 serverProgress.notifyStop(StateType.RUNNING, NbBundle.getMessage(WLStartServer.class, "MSG_STOP_SERVER_IN_PROGRESS", serverName)); } else { 650 try { 651 serverProcess.waitFor(); 652 } catch (InterruptedException ex) { 653 } 654 long pbLagTime = (System.currentTimeMillis() - start) / 4; 655 try { 656 Thread.sleep(pbLagTime); 657 } catch (InterruptedException e) {} 658 serverProgress.notifyStop(StateType.COMPLETED, NbBundle.getMessage(WLStartServer.class, "MSG_SERVER_STOPPED", serverName)); return; 660 } 661 662 try { 664 Thread.sleep(DELAY); 665 } catch (InterruptedException e) {} 666 } 667 668 serverProgress.notifyStop(StateType.FAILED, NbBundle.getMessage(WLStartServer.class, "MSG_StopServerTimeout")); 671 serverProcess.destroy(); 672 } catch (IOException e) { 673 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 674 } 675 } 676 677 681 private static final int TIMEOUT = 120000; 682 683 686 private static final int DELAY = 5000; 687 688 691 private static final String SHUTDOWN_SH = "stopWebLogic.sh"; 693 696 private static final String SHUTDOWN_BAT = "stopWebLogic.cmd"; } 698 699 705 private static class WLServerProgress implements ProgressObject { 706 707 710 private Vector listeners = new Vector(); 711 712 715 private DeploymentStatus deploymentStatus; 716 717 720 private Object source; 721 722 729 public WLServerProgress(Object source) { 730 this.source = source; 731 } 732 733 739 public void notifyStart(StateType state, String message) { 740 notify(new WLDeploymentStatus(ActionType.EXECUTE, CommandType.START, state, message)); 743 } 744 745 751 public void notifyStop(StateType state, String message) { 752 notify(new WLDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, state, message)); 755 } 756 757 763 public void notify(DeploymentStatus deploymentStatus) { 764 ProgressEvent evt = new ProgressEvent(source, null, deploymentStatus); 767 768 this.deploymentStatus = deploymentStatus; 770 771 772 java.util.Vector targets = null; 775 synchronized (this) { 776 if (listeners != null) { 777 targets = (java.util.Vector ) listeners.clone(); 778 } 779 } 780 781 782 if (targets != null) { 784 for (int i = 0; i < targets.size(); i++) { 785 ProgressListener target = (ProgressListener) targets.elementAt(i); 786 target.handleProgressEvent(evt); 787 } 788 } 789 } 790 791 798 public ClientConfiguration getClientConfiguration(TargetModuleID targetModuleID) { 799 return null; 800 } 801 802 807 public void removeProgressListener(ProgressListener progressListener) { 808 listeners.remove(progressListener); 809 } 810 811 816 public void addProgressListener(ProgressListener progressListener) { 817 listeners.add(progressListener); 818 } 819 820 825 public DeploymentStatus getDeploymentStatus() { 826 return deploymentStatus; 827 } 828 829 833 public TargetModuleID[] getResultTargetModuleIDs() { 834 return new TargetModuleID[]{}; 835 } 836 837 841 public boolean isStopSupported() { 842 return false; 843 } 844 845 849 public void stop() throws OperationUnsupportedException { 850 throw new OperationUnsupportedException(""); } 852 853 857 public boolean isCancelSupported() { 858 return false; 859 } 860 861 865 public void cancel() throws OperationUnsupportedException { 866 throw new OperationUnsupportedException(""); } 868 } 869 870 874 private static class WLDeploymentStatus implements DeploymentStatus { 875 878 private ActionType action; 879 880 883 private CommandType command; 884 885 888 private StateType state; 889 890 893 private String message; 894 895 903 public WLDeploymentStatus(ActionType action, CommandType command, StateType state, String message) { 904 this.action = action; 906 this.command = command; 907 this.state = state; 908 this.message = message; 909 } 910 911 916 public ActionType getAction() { 917 return action; 918 } 919 920 925 public CommandType getCommand() { 926 return command; 927 } 928 929 934 public String getMessage() { 935 return message; 936 } 937 938 943 public StateType getState() { 944 return state; 945 } 946 947 953 public boolean isCompleted() { 954 return StateType.COMPLETED.equals(state); 955 } 956 957 962 public boolean isFailed() { 963 return StateType.FAILED.equals(state); 964 } 965 966 971 public boolean isRunning() { 972 return StateType.RUNNING.equals(state); 973 } 974 }; 975 976 } | Popular Tags |