1 20 22 23 package org.netbeans.modules.j2ee.sun.ide.j2ee; 24 25 26 import java.io.ByteArrayOutputStream ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.net.ServerSocket ; 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.util.Collections ; 35 import javax.enterprise.deploy.spi.status.ProgressEvent ; 36 import org.netbeans.modules.derby.spi.support.DerbySupport; 37 import org.netbeans.modules.j2ee.deployment.profiler.api.ProfilerServerSettings; 38 import org.netbeans.modules.j2ee.deployment.profiler.api.ProfilerSupport; 39 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager; 40 41 import javax.enterprise.deploy.shared.ActionType ; 42 import javax.enterprise.deploy.shared.CommandType ; 43 import javax.enterprise.deploy.shared.StateType ; 44 import javax.enterprise.deploy.spi.Target ; 45 import javax.enterprise.deploy.spi.TargetModuleID ; 46 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 47 import javax.enterprise.deploy.spi.status.ClientConfiguration ; 48 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 49 import javax.enterprise.deploy.spi.status.ProgressListener ; 50 import javax.enterprise.deploy.spi.status.ProgressObject ; 51 import javax.enterprise.deploy.spi.DeploymentManager ; 52 import org.netbeans.api.debugger.DebuggerManager; 53 import org.netbeans.api.debugger.Session; 54 import org.netbeans.api.debugger.jpda.AttachingDICookie; 55 import org.netbeans.api.debugger.jpda.JPDADebugger; 56 import org.netbeans.modules.j2ee.sun.ide.dm.SunDeploymentManager; 57 import org.netbeans.modules.j2ee.sun.ide.j2ee.ui.MasterPasswordInputDialog; 58 import org.openide.DialogDisplayer; 59 import org.openide.ErrorManager; 60 import org.openide.NotifyDescriptor; 61 62 import org.openide.util.RequestProcessor; 63 import org.openide.util.NbBundle; 64 65 import org.netbeans.modules.j2ee.deployment.plugins.api.StartServer; 66 import org.netbeans.modules.j2ee.deployment.plugins.api.ServerDebugInfo; 67 68 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface; 69 import org.netbeans.modules.j2ee.sun.api.SunServerStateInterface; 70 71 import org.netbeans.modules.j2ee.sun.ide.j2ee.ui.Util; 72 import org.openide.windows.InputOutput; 73 74 78 public class StartSunServer extends StartServer implements ProgressObject , SunServerStateInterface, 79 Runnable { 80 private ProgressEventSupport pes; 81 private CommandType ct = null; 82 private int cmd = CMD_NONE; 83 private DeploymentManager dm; 84 private boolean debug = false; 85 private ServerDebugInfo debugInfo = null; 86 private boolean shouldStopDeploymentManagerSilently =false; 87 private static final int CMD_NONE = 0; 88 91 private static final int CMD_START = 1; 92 95 private static final int CMD_STOP = 2; 96 99 private static final int CMD_RESTART = 3; 100 101 102 private static final long TIMEOUT_DELAY = 300000; private static final long PROFILER_TIMEOUT_DELAY = 600000; private static Map debugInfoMap = Collections.synchronizedMap((Map )new HashMap (2,1)); 106 private String httpPort =null; 108 private int current_mode = MODE_RUN; 109 110 private static final int MODE_RUN = 0; 111 112 private static final int MODE_DEBUG = 1; 113 114 private static final int MODE_PROFILE = 2; 115 116 private String installRoot; 117 private final DeploymentManagerProperties dmProps; 118 private String domain; 119 private String domainDir; 120 121 private StartSunServer(DeploymentManager deploymentManager) { 122 this.dm = deploymentManager; 123 this.dmProps = new DeploymentManagerProperties(deploymentManager); 124 125 File irf = ((SunDeploymentManagerInterface)dm).getPlatformRoot(); 126 if (null != irf && irf.exists()) { 127 installRoot = irf.getAbsolutePath(); 128 } 129 pes = new ProgressEventSupport(this); 130 131 java.util.logging.Logger.getLogger("javax.enterprise.system.tools.admin.client").setLevel(java.util.logging.Level.OFF); 132 java.util.logging.Logger.getLogger("javax.enterprise.system.tools.avk.tools.verifier").setLevel(java.util.logging.Level.OFF); 133 java.util.logging.Logger.getLogger("javax.enterprise.system.tools.avk.appverification").setLevel(java.util.logging.Level.OFF); 134 java.util.logging.Logger.getLogger("javax.enterprise.system.tools.avk.appverification.tools").setLevel(java.util.logging.Level.OFF); 135 java.util.logging.Logger.getLogger("javax.enterprise.system.tools.avk.appverification.xml").setLevel(java.util.logging.Level.OFF); 136 137 } 138 139 static private Map <DeploymentManager , StartServer> dm2StartServer = new HashMap <DeploymentManager , StartServer>(); 140 141 static public StartServer get(DeploymentManager dm) { 142 StartServer retVal = null; 143 synchronized (dm2StartServer) { 144 retVal = dm2StartServer.get(dm); 145 if (null == retVal) { 146 retVal = new StartSunServer(dm); 147 dm2StartServer.put(dm,retVal); 148 } 149 } 150 return retVal; 151 } 152 153 public DeploymentManager getDeploymentManager() { 154 return dm; 155 } 156 157 public boolean supportsStartDeploymentManager() { 158 boolean ret = true; 159 File domainDirectory = null; 160 String domain = dmProps.getDomainName(); 161 String domainDir = dmProps.getLocation(); 162 if (null == domain || domain.trim().length() < 1 || 163 null == domainDir || domainDir.trim().length() < 1) { 164 ret = false; 166 } else { 167 domainDirectory = new File (domainDir,domain); 168 ret =((SunDeploymentManagerInterface)dm).isLocal(); 169 ret &= domainDirectory.canWrite(); 170 } 171 return ret; 172 } 173 174 public boolean supportsStartDebugging(Target target) { 175 return supportsStartDeploymentManager(); 176 } 177 189 public boolean supportsStartProfiling(Target target) { 190 return supportsStartDeploymentManager(); 191 } 192 193 public boolean isProfiling(Target target) { 194 return isRunning(); 195 } 196 public ProgressObject startProfiling(Target target, ProfilerServerSettings settings) { 197 198 pes.fireHandleProgressEvent(null, new Status( 199 ActionType.EXECUTE, 200 CommandType.START, 201 "", StateType.RUNNING)); 203 current_mode=MODE_PROFILE; 204 return startTarget(target, MODE_PROFILE, settings); } 206 207 208 216 public void stopDeploymentManagerSilently() { 217 shouldStopDeploymentManagerSilently =true; 218 stopDeploymentManager(); 219 } 221 222 226 public boolean canStopDeploymentManagerSilently() { 227 return true; 228 } 229 230 public ProgressObject startDeploymentManager() { 231 232 233 ct = CommandType.START; 234 pes.clearProgressListener(); 235 236 237 if (cmd == CMD_NONE) { 238 cmd = CMD_START; 239 } 240 addProgressListener(new ProgressListener () { 241 public void handleProgressEvent(ProgressEvent pe) { 242 if (pe.getDeploymentStatus().isCompleted()) { 243 getDebugInfo(); 244 } 245 } 246 }); 247 ConfigureProfiler.removeProfilerFromDomain(dm); 253 pes.fireHandleProgressEvent(null, new Status(ActionType.EXECUTE, 254 ct, "", 255 StateType.RUNNING)); 256 debug=false; 257 RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY); 258 260 261 return this; 262 } 263 264 public ProgressObject stopDeploymentManager() { 265 266 SunDeploymentManager sunDm = (SunDeploymentManager)this.dm; 267 ct = CommandType.STOP; 268 pes.clearProgressListener(); 269 cmd = CMD_STOP; 270 271 ConfigureProfiler.removeProfilerFromDomain(dm); 274 275 boolean running = false; 276 try { 289 Target [] targs = sunDm.getTargets(); 290 running = (targs == null) ? false : targs.length > 0; 291 } catch (IllegalStateException ise) { 292 running = false; 293 } 294 if(current_mode==MODE_PROFILE && !running && !portInUse()){ 295 current_mode =MODE_RUN; 296 pes.fireHandleProgressEvent(null, new Status(ActionType.EXECUTE, ct, 298 "", StateType.COMPLETED)); cmd = CMD_NONE; 300 } else { 302 if(current_mode==MODE_PROFILE) { 304 current_mode =MODE_RUN; 305 } 306 pes.fireHandleProgressEvent(null, new Status(ActionType.EXECUTE, 307 ct, "", StateType.RUNNING)); RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY); 309 } 310 return this; 311 } 312 313 316 317 public void viewLogFile(){ 318 getLogViewerWindow(); 319 } 320 321 325 private InputOutput getLogViewerWindow(){ 326 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)this.dm; 327 return org.netbeans.modules.j2ee.sun.ide.j2ee.runtime.actions.ViewLogAction.viewLog(sunDm,false,false); 328 } 329 330 public synchronized void run() { 331 int errorCode=-1; 332 333 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)dm; 334 335 if (null == domain) { 336 domain = dmProps.getDomainName(); 337 } 338 if (null == domainDir) { 339 domainDir = dmProps.getLocation(); 340 } 341 342 if (cmd == CMD_STOP || cmd == CMD_RESTART) { 343 344 try{ 345 if (debug==false){ 346 if (!ServerLocationManager.isGlassFish(sunDm.getPlatformRoot())){ 349 sunDm.fixJVMDebugOptions(); 350 } 351 } 352 } catch(Exception ex){ 354 Util.showInformation( ex.getLocalizedMessage()); 355 } 356 357 358 String asadminCmd = domainDir + File.separator + domain +File.separator + "bin" + File.separator + "stopserv"; 359 if (File.separator.equals("\\")) { 360 asadminCmd = asadminCmd + ".bat"; } 362 String arr[] = { asadminCmd, " "}; 363 364 errorCode = exec(arr, CMD_STOP, null); 365 if (errorCode != 0) { 366 pes.fireHandleProgressEvent(null,new Status(ActionType.EXECUTE, 367 ct, NbBundle.getMessage(StartSunServer.class, "LBL_ErrorStoppingServer"), StateType.FAILED)); 368 369 cmd = CMD_NONE; 370 pes.clearProgressListener(); 371 return; 373 }else { 374 debugInfoMap.remove(sunDm.getHost()+sunDm.getPort()); 375 LogViewerSupport.removeLogViewerSupport(dmProps.getUrl()); 376 } 377 } 378 379 380 381 if (cmd == CMD_START || cmd == CMD_RESTART) { 382 try{ 384 HttpMonitorSupport.synchronizeMonitorWithFlag((SunDeploymentManagerInterface) dm); 385 } catch (Exception eee){ 386 } 387 388 try{ 389 HttpProxyUpdater hpu = new HttpProxyUpdater(sunDm.getManagement(), false); 390 if(dmProps.isSyncHttpProxyOn()){ 391 hpu.addHttpProxySettings(); 392 } 393 }catch(Exception ex){ 394 } 395 396 String asadminCmd = installRoot + File.separator + "bin" + File.separator + "asadmin"; 399 if (File.separator.equals("\\")) { 400 asadminCmd = asadminCmd + ".bat"; } 402 String debugString = "false"; if (debug){ 404 debugString = "true"; } 406 String mpw = readMasterPasswordFile(); 407 if (mpw==null){ 408 pes.fireHandleProgressEvent(null,new Status(ActionType.EXECUTE, 409 ct, NbBundle.getMessage(StartSunServer.class, "LBL_ErrorStartingServer"), StateType.FAILED)); cmd = CMD_NONE; 411 pes.clearProgressListener(); 412 return; 414 } 415 File passWordFile = Utils.createTempPasswordFile(sunDm.getPassword(), mpw); if (passWordFile==null){ 417 pes.fireHandleProgressEvent(null,new Status(ActionType.EXECUTE, 418 ct, NbBundle.getMessage(StartSunServer.class, "LBL_ErrorStartingServer"), StateType.FAILED)); cmd = CMD_NONE; 420 pes.clearProgressListener(); 421 return; } 423 String arrd[] = { asadminCmd, "start-domain", "--debug="+debugString , 424 "--user" , sunDm.getUserName(), "--passwordfile", passWordFile.getAbsolutePath() , "--domaindir", domainDir, domain }; 428 429 if (ServerLocationManager.isJavaDBPresent(sunDm.getPlatformRoot())){ 431 DerbySupport.ensureStarted(); 432 } 433 434 InputOutput io = getLogViewerWindow(); 435 errorCode = exec(arrd, CMD_START, io); 436 437 if (errorCode != 0) { 438 439 if (((SunDeploymentManager)sunDm).isMaybeRunningButWrongUserName()==false){ 440 441 pes.fireHandleProgressEvent(null,new Status(ActionType.EXECUTE, 442 ct, NbBundle.getMessage(StartSunServer.class, "LBL_ErrorStartingServer"), StateType.FAILED)); } else{ pes.fireHandleProgressEvent(null,new Status(ActionType.EXECUTE, 445 ct, NbBundle.getMessage(SunDeploymentManager.class,"ERR_AUTH_DIALOG_TITLE"), StateType.FAILED)); 447 } 448 cmd = CMD_NONE; 449 pes.clearProgressListener(); 450 return; } 452 } 453 454 455 456 if(current_mode==MODE_PROFILE){ 457 pes.fireHandleProgressEvent(null, new Status(ActionType.EXECUTE, ct, "", StateType.COMPLETED)); cmd = CMD_NONE; 459 pes.clearProgressListener(); 460 461 return; 462 } 463 464 boolean running = false; 465 try { 466 running = sunDm.isRunning(true); 467 } catch (RuntimeException re) { 468 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, re); 469 } 470 if (cmd != CMD_STOP && !running) { 471 viewLogFile(); 472 for (int l=0;l<5;l++){ 474 try { 475 Thread.sleep(3000); 476 if(((SunDeploymentManagerInterface)dm).isRunning(true)){ pes.fireHandleProgressEvent(null, new Status(ActionType.EXECUTE, ct, "", StateType.COMPLETED)); 478 cmd = CMD_NONE; 479 pes.clearProgressListener(); 480 return; 481 } 482 483 } catch (Exception e) { 484 } 485 } 486 pes.fireHandleProgressEvent(null,new Status(ActionType.EXECUTE, 488 ct, NbBundle.getMessage(StartSunServer.class, "LBL_ErrorStartingServer"), StateType.FAILED)); 489 cmd = CMD_NONE; 490 pes.clearProgressListener(); 491 return; } 493 494 495 pes.fireHandleProgressEvent(null, new Status(ActionType.EXECUTE, ct, "", StateType.COMPLETED)); 496 cmd = CMD_NONE; 497 pes.clearProgressListener(); 498 return; 499 } 500 501 502 510 private int exec(String [] arr, int type , InputOutput io) { 511 512 int exitValue = -1; 513 514 try { 515 Process process = Runtime.getRuntime().exec(arr); 516 517 523 ByteArrayOutputStream eos = new ByteArrayOutputStream (); 524 525 StreamFlusher sfErr=new StreamFlusher(process.getErrorStream(), eos); 527 sfErr.start(); 528 529 ByteArrayOutputStream oos = new ByteArrayOutputStream (); 532 533 StreamFlusher sfOut=new StreamFlusher(process.getInputStream(), oos); 535 sfOut.start(); 536 537 if (shouldStopDeploymentManagerSilently==true){ 538 540 shouldStopDeploymentManagerSilently =false; 541 return 0; 542 } 543 pes.fireHandleProgressEvent(null, new Status(ActionType.EXECUTE, ct, "" ,StateType.RUNNING)); 544 try { 545 if(current_mode==MODE_PROFILE){ 546 try { 550 Thread.sleep(3000); 551 552 } catch (Exception e) { 553 } 554 555 if (hasCommandSucceeded()){ 556 return 0; 557 } else { 558 if (null != io) 559 io.getOut().println(oos.toString()); 560 return -1; 561 } 562 } else { 563 exitValue = process.waitFor(); 567 568 if (exitValue == 0) { 569 return 0; 571 } else { 575 if (null != io) 576 io.getOut().println(oos.toString()); 577 } 578 } 579 } catch (InterruptedException ie) { 580 } 582 583 } catch (IOException e) { 584 } 585 586 return exitValue; 587 } 588 589 590 591 592 593 594 595 596 private static final String MASTER_PASSWORD_ALIAS="master-password"; private char[] getMasterPasswordPassword() { 598 return MASTER_PASSWORD_ALIAS.toCharArray(); 599 } 600 601 603 protected String readMasterPasswordFile() { 604 String mpw= "changeit"; 612 String domain = dmProps.getDomainName(); 613 String domainDir = dmProps.getLocation(); 614 final File pwdFile = new File (domainDir + File.separator + domain +File.separator+"master-password"); 615 if (pwdFile.exists()) { 616 try { 617 618 SunDeploymentManagerInterface sdm = (SunDeploymentManagerInterface)dm; 619 ClassLoader loader = ServerLocationManager.getNetBeansAndServerClassLoader(sdm.getPlatformRoot()); 620 Class pluginRootFactoryClass =loader.loadClass("com.sun.enterprise.security.store.PasswordAdapter"); java.lang.reflect.Constructor constructor =pluginRootFactoryClass.getConstructor(new Class [] {String .class, getMasterPasswordPassword().getClass()}); 622 Object PasswordAdapter =constructor.newInstance(new Object [] {pwdFile.getAbsolutePath(),getMasterPasswordPassword() }); 623 Class PasswordAdapterClazz = PasswordAdapter.getClass(); 624 java.lang.reflect.Method method =PasswordAdapterClazz.getMethod("getPasswordForAlias", new Class []{ MASTER_PASSWORD_ALIAS.getClass()}); mpw = (String )method.invoke(PasswordAdapter, new Object [] {MASTER_PASSWORD_ALIAS }); 626 627 628 return mpw; 629 } catch (Exception ex) { 630 return mpw; 632 } 633 } else { 634 MasterPasswordInputDialog d=new MasterPasswordInputDialog(); 635 if (DialogDisplayer.getDefault().notify(d) ==NotifyDescriptor.OK_OPTION){ 636 mpw = d.getInputText(); 637 try { 639 640 File pwdFile2 = new File (domainDir + File.separator + domain +File.separator+"config/domain-passwords"); 641 SunDeploymentManagerInterface sdm = (SunDeploymentManagerInterface)dm; 642 ClassLoader loader = ServerLocationManager.getNetBeansAndServerClassLoader(sdm.getPlatformRoot()); 643 Class pluginRootFactoryClass =loader.loadClass("com.sun.enterprise.security.store.PasswordAdapter"); java.lang.reflect.Constructor constructor =pluginRootFactoryClass.getConstructor(new Class [] {String .class, getMasterPasswordPassword().getClass()}); 645 constructor.newInstance(new Object [] {pwdFile2.getAbsolutePath(),mpw.toCharArray() }); 647 648 return mpw; 649 650 } catch (Exception ex) { 651 return null; 654 } 655 } else{ 656 return null; 657 658 } 659 } 660 } 661 662 663 664 669 public boolean isRunning() { 670 try { 671 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)dm; 672 boolean runningState =sunDm.isRunning(); 673 if ( (runningState)&&(httpPort==null)) { 674 675 httpPort = sunDm.getNonAdminPortNumber(); 676 if (httpPort!=null){ 677 DeploymentManagerProperties dmProps = new DeploymentManagerProperties(dm); 679 dmProps.setHttpPortNumber(httpPort); 680 } 681 } 682 return runningState; 683 } catch(RuntimeException ex) { 684 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 685 return false; 686 } 687 } 688 695 private boolean hasCommandSucceeded() { 696 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)this.dm; 697 long to =TIMEOUT_DELAY; 698 if(current_mode==MODE_PROFILE){ 699 to = PROFILER_TIMEOUT_DELAY; 700 } 701 702 long timeout = System.currentTimeMillis() + to; 703 try { 704 705 while (true) { 706 boolean isRunning = sunDm.isRunning(true); 707 if (ct == CommandType.START) { 708 if (isRunning) { 709 return true; 710 } 711 if (((SunDeploymentManager)sunDm).isMaybeRunningButWrongUserName()){ 712 return false; 713 } 714 if (current_mode == MODE_PROFILE) { 715 int state = ProfilerSupport.getState(); 716 if (state == ProfilerSupport.STATE_BLOCKING || 717 state == ProfilerSupport.STATE_RUNNING || 718 state == ProfilerSupport.STATE_PROFILING) { 719 720 return true; 721 } else if (state == ProfilerSupport.STATE_INACTIVE) { 722 return false; 724 } 725 } 726 } 727 if (ct == CommandType.STOP && !isRunning) { 728 return true; 729 } 730 731 if (System.currentTimeMillis() > timeout) { 733 return false; 734 } 735 try { 736 Thread.sleep(1000); } catch(InterruptedException ie) {} 738 } 739 } catch (RuntimeException e) { 740 return false; 742 } 743 } 744 745 752 public boolean needsRestart(Target target) { 753 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)this.dm; 754 return sunDm.isRestartNeeded(); 755 756 } 757 758 761 public boolean isDebuggable(Target target) { 762 try { 763 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)this.dm; 764 if (sunDm.isRunning()==false){ return false; 766 } 767 if ( sunDm.isLocal()) { 768 769 return (null!=debugInfoMap.get(sunDm.getHost()+sunDm.getPort())); }else { 771 debugInfoMap.put(sunDm.getHost()+sunDm.getPort(),getDebugInfo()); 772 return true; 773 } 774 } catch(RuntimeException ex) { 775 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 776 return false; 777 } 778 } 779 780 public boolean isDebugged() { 781 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)this.dm; 782 if (sunDm.isRunning()==false){ return false; 784 } 785 return (null!=debugInfoMap.get(sunDm.getHost()+sunDm.getPort())); } 787 791 792 public ProgressObject startTarget(Target Target, int mode, ProfilerServerSettings settings) { 793 this.debug = mode==MODE_DEBUG; 797 pes.clearProgressListener(); 798 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)this.dm; 799 if (debug==true){ 800 debugInfoMap.put(sunDm.getHost()+sunDm.getPort(),getDebugInfo()); 801 802 }else{ 803 debugInfoMap.remove(sunDm.getHost()+sunDm.getPort()); 804 805 } 806 807 if (settings!=null){ 808 ConfigureProfiler.instrumentProfilerInDOmain(dm , null,settings.getJvmArgs()) ; 809 } else{ 810 ConfigureProfiler.removeProfilerFromDomain(dm); 812 813 } 814 cmd = CMD_START; 815 816 if (isRunning()) { 817 cmd = CMD_RESTART; 818 } 819 820 821 ct = CommandType.START; 822 pes.clearProgressListener(); 823 addProgressListener(new ProgressListener () { 824 public void handleProgressEvent(ProgressEvent pe) { 825 if (pe.getDeploymentStatus().isCompleted()) { 826 getDebugInfo(); 827 } 828 } 829 }); 830 pes.fireHandleProgressEvent(null, new Status(ActionType.EXECUTE,ct, "",StateType.RUNNING)); 836 RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY); 837 return this; 839 } 840 841 private boolean portInUse() { 842 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)dm; 843 boolean retVal = true; 844 ServerSocket ss = null; 845 try { 846 ss = new ServerSocket (sunDm.getPort()); 847 retVal = false; 848 } catch (IOException ioe) { 849 } finally { 851 if (null != ss) { 852 try { 853 ss.close(); 854 } catch (IOException ioe) { 855 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 856 ioe); 857 } 858 } 859 } 860 return retVal; 861 } 862 863 public ProgressObject stopTarget(Target target) { 864 pes.clearProgressListener(); 866 boolean running = false; 867 try { 868 running = ((SunDeploymentManagerInterface)dm).isRunning(true); 869 } catch (RuntimeException re) { 870 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, re); 871 } 872 if (!running) { 873 pes.fireHandleProgressEvent(null, new Status(ActionType.EXECUTE, 874 CommandType.STOP, "", 875 StateType.COMPLETED)); 876 return this; 877 } 878 return stopDeploymentManager(); 879 } 880 881 885 public ServerDebugInfo getDebugInfo(Target target) { 886 return getDebugInfo(); 893 } 894 895 private ServerDebugInfo getDebugInfo() { 896 897 try{ 898 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)this.dm; 899 if (sunDm.isRunning()==false){ 900 return null; 901 } 902 if (isSuspended((SunDeploymentManagerInterface)this.dm)){ 903 return (ServerDebugInfo)debugInfoMap.get(sunDm.getHost()+sunDm.getPort()); 904 } 905 906 String addr= sunDm.getDebugAddressValue(); 907 908 if (sunDm.isDebugSharedMemory()){ debugInfo = new ServerDebugInfo(sunDm.getHost(), addr); 910 if (debug==true){ 912 debugInfoMap.put(sunDm.getHost()+sunDm.getPort(),debugInfo); 913 } 914 915 } else{ int port = Integer.parseInt(addr); 917 debugInfo = new ServerDebugInfo(sunDm.getHost(), port); 918 if (debug==true){ 920 debugInfoMap.put(sunDm.getHost()+sunDm.getPort(),debugInfo); 921 } 922 923 924 } 925 }catch(Exception ex){ 926 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 927 debugInfo =null; 928 } 929 return debugInfo; 930 } 931 932 933 934 935 939 public static boolean isSuspended(SunDeploymentManagerInterface sunDm) { 940 boolean retVal = false; 941 Session[] sessions = DebuggerManager.getDebuggerManager().getSessions(); 942 943 for (int i=0; ! retVal && i < sessions.length; i++) { 944 Session s = sessions[i]; 945 if (s != null) { 946 Object o = s.lookupFirst(null,AttachingDICookie.class); 947 if (o != null) { 948 Object d = s.lookupFirst(null,JPDADebugger.class); 949 if (d != null) { 950 JPDADebugger jpda = (JPDADebugger)d; 951 if (jpda.getState() == JPDADebugger.STATE_STOPPED) { AttachingDICookie attCookie = (AttachingDICookie)o; 953 String shmName = attCookie.getSharedMemoryName(); 954 if (shmName!=null) { 955 if (shmName.startsWith(sunDm.getHost())) { 956 retVal = true; 957 } 958 } else { int attachedPort = attCookie.getPortNumber(); 960 ServerDebugInfo dbi = (ServerDebugInfo)debugInfoMap.get(sunDm.getHost()+sunDm.getPort()); 961 if (null != dbi) { 962 if (sameMachine(attCookie.getHostName(), sunDm.getHost()) && 963 dbi.getPort() == attachedPort) { 964 retVal = true; 965 } 966 } 967 968 } 969 } 970 } 971 } 972 } 973 } 974 return retVal; 975 } 976 977 978 979 980 981 public DeploymentStatus getDeploymentStatus() { 982 return pes.getDeploymentStatus(); 983 } 984 985 public void addProgressListener(ProgressListener pl) { 986 pes.addProgressListener(pl); 987 } 988 989 public void removeProgressListener(ProgressListener pl) { 990 pes.removeProgressListener(pl); 991 } 992 993 994 public void cancel() throws OperationUnsupportedException { 995 throw new OperationUnsupportedException (""); 996 } 997 998 public ClientConfiguration getClientConfiguration(TargetModuleID id) { 999 return null; 1000 } 1001 1002 public TargetModuleID [] getResultTargetModuleIDs() { 1003 return null; 1004 } 1005 1006 public boolean isCancelSupported() { 1007 return false; 1008 } 1009 1010 public boolean isStopSupported() { 1011 return false; 1012 } 1013 1014 public void stop() throws OperationUnsupportedException { 1015 throw new OperationUnsupportedException (""); 1016 } 1017 1018 1019 1020 1021 public boolean isAlsoTargetServer(Target target) { 1022 1024 return true; 1025 } 1026 1027 public boolean needsStartForConfigure() { 1028 return false; 1029 } 1030 1031 public ProgressObject startDebugging(Target target) { 1032 current_mode = MODE_DEBUG; 1034 return startTarget(target, MODE_DEBUG, null); 1036 1037 } 1038 1039 public boolean needsStartForAdminConfig() { 1040 return true; 1041 } 1042 1043 public boolean needsStartForTargetList() { 1044 return true; 1045 } 1046 private static final String LOCALHOST="localhost"; private static final String LOCALADDRESS="127.0.0.1"; 1051 public static boolean sameMachine(String host1, String host2){ 1052 try { 1053 if (host1.equals(host2)){ 1054 return true; 1055 } 1056 if (host1.equals(LOCALHOST)){ 1057 if (host2.equals(LOCALADDRESS)){ 1058 return true; 1059 } 1060 String localCanonicalHostName = java.net.InetAddress.getLocalHost().getCanonicalHostName(); 1061 String h2 = java.net.InetAddress.getByName(host2).getCanonicalHostName(); 1062 if (localCanonicalHostName.equals(h2)){ 1063 return true; 1064 } 1065 } 1066 if (host1.equals(LOCALADDRESS)){ 1067 if (host2.equals(LOCALHOST)){ 1068 return true; 1069 } 1070 String localCanonicalHostName = java.net.InetAddress.getLocalHost().getCanonicalHostName(); 1071 String h2 = java.net.InetAddress.getByName(host2).getCanonicalHostName(); 1072 return true; 1073 } 1074 if (host2.equals(LOCALHOST)){ 1075 if (host1.equals(LOCALADDRESS)){ 1076 return true; 1077 } 1078 String localCanonicalHostName = java.net.InetAddress.getLocalHost().getCanonicalHostName(); 1079 String h1 = java.net.InetAddress.getByName(host1).getCanonicalHostName(); 1080 if (localCanonicalHostName.equals(h1)){ 1081 return true; 1082 } 1083 } 1084 if (host2.equals(LOCALADDRESS)){ 1085 if (host1.equals(LOCALHOST)){ 1086 return true; 1087 } 1088 String localCanonicalHostName = java.net.InetAddress.getLocalHost().getCanonicalHostName(); 1089 String h1 = java.net.InetAddress.getByName(host1).getCanonicalHostName(); 1090 if (localCanonicalHostName.equals(h1)){ 1091 return true; 1092 } 1093 } 1094 String h1 = java.net.InetAddress.getByName(host1).getCanonicalHostName(); 1095 String h2 = java.net.InetAddress.getByName(host2).getCanonicalHostName(); 1096 if (h1.equals(h2)){ 1097 return true; 1098 } 1099 } catch (java.net.UnknownHostException ex) { 1100 ex.printStackTrace(); 1101 } 1102 return false; 1103 } 1104 1105 1109 protected class StreamFlusher extends Thread { 1110 1111 private InputStream _input=null; 1112 private OutputStream _output=null; 1113 1114 public StreamFlusher(InputStream input, OutputStream output) { 1115 this._input=input; 1116 this._output=output; 1117 } 1118 1119 public void run() { 1120 1121 if (_input == null){ 1123 return; 1124 } 1125 1126 try { 1128 int byteCnt=0; 1129 byte[] buffer=new byte[4096]; 1130 while ((byteCnt=_input.read(buffer)) != -1) { 1131 if (_output != null && byteCnt > 0) { 1132 _output.write(buffer, 0, byteCnt); 1133 _output.flush(); 1134 } 1135 yield(); 1136 } 1137 } catch (IOException e) { 1138 } 1141 } 1142 } 1143} 1144 1145 | Popular Tags |