1 20 22 package org.netbeans.modules.j2ee.sun.ide.dm; 23 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyChangeSupport ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.net.Authenticator ; 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import java.util.Locale ; 35 import java.net.InetAddress ; 36 import java.rmi.RemoteException ; 37 import java.rmi.ServerException ; 38 import java.lang.reflect.Method ; 39 import java.util.Map ; 40 import java.util.ResourceBundle ; 41 import java.util.concurrent.atomic.AtomicBoolean ; 42 import javax.enterprise.deploy.model.DeployableObject ; 43 import javax.enterprise.deploy.shared.ActionType ; 44 import javax.enterprise.deploy.shared.CommandType ; 45 import javax.enterprise.deploy.shared.StateType ; 46 import javax.enterprise.deploy.spi.Target ; 47 import javax.enterprise.deploy.spi.TargetModuleID ; 48 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 49 import javax.enterprise.deploy.spi.status.ClientConfiguration ; 50 import javax.enterprise.deploy.spi.status.ProgressObject ; 51 import javax.enterprise.deploy.spi.status.ProgressListener ; 52 import javax.enterprise.deploy.spi.status.ProgressEvent ; 53 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 54 import javax.enterprise.deploy.shared.DConfigBeanVersionType ; 55 import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException ; 56 import javax.enterprise.deploy.spi.DeploymentManager ; 57 import javax.enterprise.deploy.spi.DeploymentConfiguration ; 58 import javax.enterprise.deploy.shared.ModuleType ; 59 import javax.enterprise.deploy.spi.exceptions.TargetException ; 60 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException ; 61 import javax.enterprise.deploy.spi.factories.DeploymentFactory ; 62 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 63 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 64 import org.netbeans.modules.j2ee.sun.api.SunURIManager; 65 import org.netbeans.modules.j2ee.sun.appsrvapi.PortDetector; 66 import org.netbeans.modules.j2ee.sun.ide.editors.AdminAuthenticator; 67 import org.netbeans.modules.j2ee.sun.ide.j2ee.DeploymentManagerProperties; 68 import org.netbeans.modules.j2ee.sun.ide.j2ee.ProgressEventSupport; 69 import org.netbeans.modules.j2ee.sun.ide.j2ee.runtime.actions.ViewLogAction; 70 71 import org.netbeans.modules.j2ee.sun.share.configbean.SunONEDeploymentConfiguration; 72 73 import org.netbeans.modules.j2ee.sun.ide.j2ee.mbmapping.JvmOptions; 74 import org.netbeans.modules.j2ee.sun.ide.j2ee.mbmapping.ServerInfo; 75 76 import java.util.jar.JarOutputStream ; 77 import java.io.FileOutputStream ; 78 import java.util.HashMap ; 79 import org.netbeans.modules.j2ee.sun.share.plan.Util; 80 81 82 83 import org.netbeans.modules.j2ee.sun.ide.j2ee.Utils; 84 85 import org.netbeans.modules.j2ee.sun.api.ServerInterface; 86 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface; 87 import org.netbeans.modules.j2ee.sun.api.ResourceConfiguratorInterface; 88 import java.util.Properties ; 89 import org.openide.DialogDescriptor; 90 import org.openide.DialogDisplayer; 91 92 import org.openide.ErrorManager; 93 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager; 94 import org.netbeans.modules.j2ee.sun.ide.j2ee.DomainEditor; 95 import org.openide.filesystems.FileLock; 96 import org.openide.filesystems.FileObject; 97 import org.openide.filesystems.FileUtil; 98 import org.openide.util.NbBundle; 99 import org.openide.util.RequestProcessor; 100 104 public class SunDeploymentManager implements Constants, DeploymentManager , SunDeploymentManagerInterface { 105 106 private PropertyChangeSupport propertySupport; 107 private DeploymentManager innerDM; 108 private DeploymentFactory df; 109 private String host, userName, password; 110 private String uri; 111 private boolean isConnected; 112 114 private static Map passwordForURI = Collections.synchronizedMap((Map )new HashMap (2,1)); 115 116 117 118 int adminPortNumber; 119 120 String nonAdminPortNumber = null; 122 private static String SECURESTRINGDETECTION =":https"; 123 127 private transient boolean secureStatusHasBeenChecked = false; 128 private boolean runningState=false; 129 private boolean secure =false; 130 private boolean goodUserNamePassword =false; 131 private boolean maybeRunningButWrongUserName =false; 132 private long timeStampCheckingRunning =0; 133 private File platformRoot =null; 134 private String domainDir = null; 135 136 140 private boolean isLocal = false; 141 143 static final ResourceBundle bundle = ResourceBundle.getBundle("org.netbeans.modules.j2ee.sun.ide.dm.Bundle"); 145 151 152 153 public SunDeploymentManager( DeploymentFactory df, String uri,String userName, String password, File platformRootDir) throws DeploymentManagerCreationException { 154 propertySupport = new PropertyChangeSupport (this); 155 this.df= df; 156 this.uri =uri; 157 this.platformRoot = platformRootDir; 158 secure = uri.endsWith(SECURESTRINGDETECTION); 159 String uriNonSecure =uri; 160 if (secure){ 161 uriNonSecure = uri.substring(0, uri.length()- SECURESTRINGDETECTION.length()); 162 } 163 host = getHostFromURI(uriNonSecure); 164 adminPortNumber = getPortFromURI(uriNonSecure); 165 try { 166 InstanceProperties props = SunURIManager.getInstanceProperties(platformRoot, host, adminPortNumber); 167 168 if (userName == null && props != null) { 169 this.userName = props.getProperty(InstanceProperties.USERNAME_ATTR); 170 } else { 171 this.userName = userName; 172 } 173 if (password == null && props != null) { 174 this.password = props.getProperty(InstanceProperties.PASSWORD_ATTR); 175 } else { 176 this.password = password; 177 } 178 if (!"".equals(this.password)){ 179 passwordForURI.put(uri+platformRoot,this.password); 180 } else { 181 this.password = (String ) passwordForURI.get(uri+platformRoot); 182 if (this.password==null) { 183 this.password=""; 184 } 185 186 } 187 188 189 isConnected = this.userName != null; 190 } catch (IllegalStateException ise) { 191 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ise); 194 } 195 resetInnerDeploymentManager(); 196 calculateIsLocal(); 197 } 198 199 200 201 204 205 206 207 private void resetInnerDeploymentManager() throws DeploymentManagerCreationException { 208 try { 209 if (isConnected){ 210 if (df!=null) { 211 innerDM = df.getDeploymentManager(uri,userName,password); 212 } 213 } else{ 214 if (df!=null) { 215 innerDM = df.getDisconnectedDeploymentManager(uri); 216 } 217 } 218 } catch (NoClassDefFoundError ee) { 222 throw ee; 223 } catch (Exception e) { 224 e.printStackTrace(); 225 throw new DeploymentManagerCreationException ("DeploymentManagerCreationException" + e.getMessage()); 226 } 227 if (secure) { 228 try{ 230 Class [] argClass = new Class [1]; 231 argClass[0] = DeploymentManager .class; 232 Object [] argObject = new Object [1]; 233 argObject[0] = innerDM; 234 235 ClassLoader loader = getExtendedClassLoader(); 236 if(loader != null){ 237 Class cc = loader.loadClass("org.netbeans.modules.j2ee.sun.bridge.AppServerBridge"); 238 Method setServerConnectionEnvironment = cc.getMethod("setServerConnectionEnvironment", argClass); setServerConnectionEnvironment.invoke(null, argObject); 240 } 241 }catch(Exception ex){ 242 ex.printStackTrace(); 244 } 245 } 246 247 } 248 public String getUserName() { 249 InstanceProperties props = SunURIManager.getInstanceProperties(platformRoot, host, adminPortNumber); 250 if (null != props) { 251 this.userName = props.getProperty(InstanceProperties.USERNAME_ATTR); 252 } 253 return userName; 254 } 255 256 public String getPassword() { 257 InstanceProperties props = SunURIManager.getInstanceProperties(platformRoot, host, adminPortNumber); 258 if (null != props) { 259 this.password = props.getProperty(InstanceProperties.PASSWORD_ATTR); 260 } 261 if (password.equals("")){ password = (String ) passwordForURI.get(uri+platformRoot); 263 if (this.password==null) { 264 this.password=""; 265 } 266 267 } 268 return password; 269 } 270 271 public String getHost() { 272 return host; 273 } 274 275 public int getPort() { 276 return adminPortNumber; 277 } 278 282 public String getNonAdminPortNumber() { 283 return nonAdminPortNumber; 284 } 285 286 290 public boolean isRestartNeeded(){ 291 if (secureStatusHasBeenChecked==false){ 292 return false; 293 } 294 295 if (goodUserNamePassword==false){ 296 return false; 297 } 298 if (isRunning()==false){ 299 return false; 300 } 301 boolean retVal; 302 try{ 303 ServerInfo si = new ServerInfo(getMBeanServerConnection()); 304 retVal = si.isRestartRequired(); 305 } catch (java.rmi.RemoteException e){ 306 retVal = false; } 308 return retVal; 309 } 310 311 312 public void fixJVMDebugOptions() throws java.rmi.RemoteException { 313 JvmOptions jvmInfo = new JvmOptions(getMBeanServerConnection()); 314 jvmInfo.getAddressValue(); 316 if (jvmInfo.isWindows()){ 317 if (jvmInfo.isSharedMemory()==false){ jvmInfo.setDefaultTransportForDebug(getHost()+getPort()); 319 } 320 } 321 } 322 String lastAddress = null; 323 public String getDebugAddressValue() throws java.rmi.RemoteException { 324 String retVal = lastAddress; 325 JvmOptions jvmInfo = null; 326 try { 327 jvmInfo = new JvmOptions(getMBeanServerConnection()); 328 retVal = jvmInfo.getAddressValue(); 329 lastAddress = retVal; 330 } catch (java.rmi.RemoteException re) { 331 if (null == lastAddress){ 332 throw re; 333 } 334 335 } 336 return retVal; 337 } 338 339 boolean lastIsSharedMem = false; 340 public boolean isDebugSharedMemory() throws java.rmi.RemoteException { 341 boolean retVal = lastIsSharedMem; 342 JvmOptions jvmInfo = null; 343 try { 344 jvmInfo = new JvmOptions(getMBeanServerConnection()); 345 retVal = jvmInfo.isSharedMemory(); 346 lastIsSharedMem = retVal; 347 } catch (java.rmi.RemoteException re) { 348 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 349 re); 350 } catch (Exception e) { 351 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,e); 352 } 353 return retVal; 354 355 356 } 357 public DeploymentConfiguration createConfiguration(DeployableObject dObj) 369 throws javax.enterprise.deploy.spi.exceptions.InvalidModuleException { 370 return new SunONEDeploymentConfiguration(dObj); 371 } 373 374 File getInternalPlanFile(InputStream plan) throws IllegalStateException { 375 JarOutputStream jar = null; 376 378 try { 379 File tmpFile = File.createTempFile("dplan","tmp"); 380 jar = new JarOutputStream (new FileOutputStream (tmpFile)); 381 Util.convert(plan,jar); 382 jar.close(); 383 jar = null; 384 tmpFile.deleteOnExit(); 385 return tmpFile; 386 } catch (java.io.IOException ioe) { 387 IllegalStateException ise = 388 new IllegalStateException ("file handling issues"); 389 ise.initCause(ioe); 390 throw ise; 391 } finally { 392 if (jar != null){ 393 try { 394 jar.close(); 395 } catch (Throwable t) { 396 jsr88Logger.severe("bad one"); 397 } 398 } 399 } 400 } 401 402 403 421 422 public ProgressObject distribute(Target [] target, 423 InputStream archive, InputStream plan) throws IllegalStateException { 424 InputStream innerPlan = null; 425 ThrowExceptionIfSuspended(); 427 ViewLogAction.viewLog(this); 429 430 ClassLoader origClassLoader=Thread.currentThread().getContextClassLoader(); 431 try { 432 final File f = getInternalPlanFile(plan); 436 innerPlan = new FileInputStream (f); 437 ProgressObject retVal; 441 Thread.currentThread().setContextClassLoader(ServerLocationManager.getServerOnlyClassLoader(getPlatformRoot())); 445 retVal = innerDM.distribute(target, archive, innerPlan); 446 retVal.addProgressListener(new FileDeleter(f)); 447 return retVal; 449 450 451 } catch (IllegalStateException ise) { 452 throw ise; 453 } catch (java.io.IOException ioe) { 454 IllegalStateException ise = 455 new IllegalStateException ("file handling issues"); 456 ise.initCause(ioe); 457 throw ise; 458 } finally { 459 Thread.currentThread().setContextClassLoader(origClassLoader); 460 if (null != innerPlan){ 461 try { 462 innerPlan.close(); 463 } catch (Throwable t) { 464 jsr88Logger.severe("bad two"); 465 } 466 } 467 } 468 } 469 470 public ProgressObject distribute(Target [] target, ModuleType moduleType, InputStream inputStream, InputStream inputStream0) throws IllegalStateException { 471 return distribute(target, inputStream, inputStream0); 472 } 473 474 public ProgressObject distribute(Target [] target, File archive, File plan) 475 throws IllegalStateException { 476 ThrowExceptionIfSuspended(); 477 478 File [] resourceDirs = Utils.getResourceDirs(archive); 479 if(resourceDirs != null){ 480 Utils.registerResources(resourceDirs, (ServerInterface)getManagement()); 481 } 482 483 ClassLoader origClassLoader=Thread.currentThread().getContextClassLoader(); 484 try { 485 Thread.currentThread().setContextClassLoader(ServerLocationManager.getServerOnlyClassLoader(getPlatformRoot())); 486 ViewLogAction.viewLog(this); 488 return innerDM.distribute(target, archive, null); 492 } catch ( Exception fnfe) { 495 IllegalStateException ise = 496 new IllegalStateException (); 497 ise.initCause(fnfe); 498 throw ise; 499 } finally{ 500 Thread.currentThread().setContextClassLoader(origClassLoader); 501 } 502 503 } 504 505 public TargetModuleID [] getAvailableModules(ModuleType modType, Target [] target) 506 throws TargetException , IllegalStateException { 507 508 try { 509 ThrowExceptionIfSuspended(); 510 } catch (RuntimeException re) { 511 return new TargetModuleID [0]; 512 } 513 ClassLoader origClassLoader=Thread.currentThread().getContextClassLoader(); 514 Thread.currentThread().setContextClassLoader(ServerLocationManager.getServerOnlyClassLoader(getPlatformRoot())); 515 516 try { 517 try { 518 grabInnerDM(false); 519 TargetModuleID [] tm = innerDM.getAvailableModules(modType, target); 520 528 return tm; 529 } finally { 530 releaseInnerDM(); 531 } 532 } finally{ 533 Thread.currentThread().setContextClassLoader(origClassLoader); 534 } 535 } 536 537 public Locale getCurrentLocale() { ThrowExceptionIfSuspended(); 540 return innerDM.getCurrentLocale(); 541 } 542 543 public DConfigBeanVersionType getDConfigBeanVersion() { ThrowExceptionIfSuspended(); 546 return innerDM.getDConfigBeanVersion(); 547 } 548 549 public Locale getDefaultLocale() { 550 ThrowExceptionIfSuspended(); 552 return innerDM.getDefaultLocale(); 553 } 554 555 public TargetModuleID [] getNonRunningModules(ModuleType mType, Target [] target) 556 throws TargetException , IllegalStateException { 557 ThrowExceptionIfSuspended(); 559 return innerDM.getNonRunningModules(mType, target); 560 } 561 562 public TargetModuleID [] getRunningModules(ModuleType mType, Target [] target) 563 throws TargetException , IllegalStateException { 564 ClassLoader origClassLoader=Thread.currentThread().getContextClassLoader(); 565 Thread.currentThread().setContextClassLoader(ServerLocationManager.getServerOnlyClassLoader(getPlatformRoot())); 566 try{ 567 try { 568 grabInnerDM(false); 569 TargetModuleID [] ttt= innerDM.getRunningModules(mType, target); 570 return ttt; 571 } finally { 572 releaseInnerDM(); 573 } 574 } finally{ 575 Thread.currentThread().setContextClassLoader(origClassLoader); 576 } 577 578 } 579 580 public Locale [] getSupportedLocales() { 581 ThrowExceptionIfSuspended(); 583 return innerDM.getSupportedLocales(); 584 } 585 586 public Target [] getTargets() throws IllegalStateException { 587 try { 588 ThrowExceptionIfSuspended(); 589 } catch (Exception ex) { 590 return new Target [0]; 591 } 592 Target [] retVal = null; 593 if (secureStatusHasBeenChecked==false){ retVal = null; 595 } else { 596 if (null == innerDM) { 600 retVal = new Target [1]; 601 retVal[0] = new FakeTarget(); 602 } else { 604 try { 605 grabInnerDM(false); 606 if (isLocal()){ 610 try{ 611 Object configDir = getManagement().invoke(new javax.management.ObjectName ("com.sun.appserv:type=domain,category=config"),"getConfigDir", null, null); 612 if (configDir==null){ 613 mmm=null; 614 return null; 615 } 616 String dir = configDir.toString(); 617 File domainLocationAsReturnedByTheServer = new File (dir).getParentFile(); 618 619 String l1 = domainLocationAsReturnedByTheServer.getCanonicalPath(); 620 if (null == domainDir) { 621 DeploymentManagerProperties dmProps = new DeploymentManagerProperties(this); 622 domainDir = dmProps.getLocation(); 623 String domainName = dmProps.getDomainName(); 624 domainDir = new File (domainDir).getCanonicalPath(); 625 domainDir += File.separator + 626 domainName; 627 } 628 629 if (l1.equals(domainDir)==false){ 631 return null; 632 } 633 } catch (java.rmi.RemoteException ee) { 634 return null; } catch (Throwable ee) { 636 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 637 ee); 638 return null; } 640 } 641 ClassLoader origClassLoader=Thread.currentThread().getContextClassLoader(); 642 try { 643 Thread.currentThread().setContextClassLoader(ServerLocationManager.getServerOnlyClassLoader(getPlatformRoot())); 644 retVal = innerDM.getTargets(); 645 } catch (IllegalStateException ise) { 646 retVal = new Target [0]; 647 } finally{ 649 Thread.currentThread().setContextClassLoader(origClassLoader); 650 651 } 652 } finally { 653 releaseInnerDM(); 654 } 655 } 656 } 657 658 return retVal; 659 } 660 661 public boolean isDConfigBeanVersionSupported(DConfigBeanVersionType vers) { 662 ThrowExceptionIfSuspended(); 664 return innerDM.isDConfigBeanVersionSupported(vers); 665 } 666 667 public boolean isLocaleSupported(Locale locale) { 668 ThrowExceptionIfSuspended(); 670 return innerDM.isLocaleSupported(locale); 671 } 672 673 public boolean isRedeploySupported() { 674 ThrowExceptionIfSuspended(); 676 return innerDM.isRedeploySupported(); 677 } 678 679 public ProgressObject redeploy(TargetModuleID [] targetModuleID, 680 InputStream archive, InputStream plan) 681 throws UnsupportedOperationException , IllegalStateException { 682 ThrowExceptionIfSuspended(); 684 685 InputStream innerPlan = null; 686 ViewLogAction.viewLog(this); 688 689 ClassLoader origClassLoader=Thread.currentThread().getContextClassLoader(); 690 try { 691 Thread.currentThread().setContextClassLoader(ServerLocationManager.getServerOnlyClassLoader(getPlatformRoot())); 692 grabInnerDM(false); 695 ProgressObject retVal = innerDM.redeploy(targetModuleID, archive, innerPlan); 696 if (null != retVal) { 697 retVal.addProgressListener(new ReleaseInnerDMPL(Thread.currentThread())); 698 } 699 return retVal; 700 } catch (IllegalStateException ise) { 701 releaseInnerDM(); 702 throw ise; 703 } catch (Exception ioe) { 704 IllegalStateException ise = 705 new IllegalStateException ("file handling issues"); 706 ise.initCause(ioe); 707 releaseInnerDM(); 708 throw ise; 709 } finally { 710 Thread.currentThread().setContextClassLoader(origClassLoader); 711 if (null != innerPlan){ 712 try { 713 innerPlan.close(); 714 } catch (Throwable t) { 715 t.printStackTrace(); 716 jsr88Logger.severe("bad two"); 717 } 718 } 719 } 720 } 721 722 public ProgressObject redeploy(TargetModuleID [] targetModuleID, 723 File archive, File plan) 724 throws UnsupportedOperationException , IllegalStateException { 725 ThrowExceptionIfSuspended(); 726 727 File [] resourceDirs = Utils.getResourceDirs(archive); 728 if(resourceDirs != null){ 729 Utils.registerResources(resourceDirs, (ServerInterface)getManagement()); 730 } 731 732 ViewLogAction.viewLog(this); 733 734 ClassLoader origClassLoader=Thread.currentThread().getContextClassLoader(); 735 Thread.currentThread().setContextClassLoader(ServerLocationManager.getServerOnlyClassLoader(getPlatformRoot())); 736 try{ 737 grabInnerDM(false); 738 ProgressObject retVal = innerDM.redeploy(targetModuleID, archive, null); 739 if (null != retVal) { 740 retVal.addProgressListener(new ReleaseInnerDMPL(Thread.currentThread())); 741 } 742 return retVal; 743 } catch (IllegalStateException ise) { 744 releaseInnerDM(); 745 throw ise; 746 } finally{ 747 Thread.currentThread().setContextClassLoader(origClassLoader); 748 } 749 } 750 751 public void release() { 752 759 760 } 761 762 public void setDConfigBeanVersion(DConfigBeanVersionType versionType) 763 throws DConfigBeanVersionUnsupportedException { 764 ThrowExceptionIfSuspended(); 766 innerDM.setDConfigBeanVersion(versionType); 767 } 768 769 public void setLocale(Locale locale) 770 throws UnsupportedOperationException { 771 ThrowExceptionIfSuspended(); 773 innerDM.setLocale(locale); 774 } 775 776 public ProgressObject start(TargetModuleID [] targetModuleID) 777 throws IllegalStateException { 778 ThrowExceptionIfSuspended(); 779 ClassLoader origClassLoader=Thread.currentThread().getContextClassLoader(); 780 Thread.currentThread().setContextClassLoader(ServerLocationManager.getServerOnlyClassLoader(getPlatformRoot())); 781 try{ 782 TargetModuleID [] weeded = weedOutAppClientTMID(targetModuleID); 785 ProgressObject retVal = null; 786 if (weeded.length < 1) { 787 retVal = new ShortCircuitProgressObject(CommandType.START, 788 NbBundle.getMessage(SunDeploymentManager.class,"MESS_STARTED"), 789 StateType.COMPLETED,targetModuleID); 790 } else { 791 retVal = innerDM.start(weeded); 792 } 793 return retVal; 794 } finally{ 795 Thread.currentThread().setContextClassLoader(origClassLoader); 796 } 797 } 798 799 public ProgressObject stop(TargetModuleID [] targetModuleID) 800 throws IllegalStateException { 801 ThrowExceptionIfSuspended(); 802 ClassLoader origClassLoader=Thread.currentThread().getContextClassLoader(); 803 Thread.currentThread().setContextClassLoader(ServerLocationManager.getServerOnlyClassLoader(getPlatformRoot())); 804 try{ 805 TargetModuleID [] weeded = weedOutAppClientTMID(targetModuleID); 808 ProgressObject retVal = null; 809 if (weeded.length < 1) { 810 retVal = new ShortCircuitProgressObject(CommandType.STOP, 811 NbBundle.getMessage(SunDeploymentManager.class,"MESS_STOPPED"), 812 StateType.COMPLETED,targetModuleID); 813 } else { 814 retVal = innerDM.stop(weeded); 815 } 816 return retVal; 817 } finally{ 818 Thread.currentThread().setContextClassLoader(origClassLoader); 819 } 820 } 821 822 private TargetModuleID [] weedOutAppClientTMID(TargetModuleID [] tmids) { 823 ArrayList <TargetModuleID > retList = new ArrayList <TargetModuleID >(); 824 try{ 825 Class [] argClass = new Class [1]; 826 argClass[0] = TargetModuleID .class; 827 Object [] argObject = new Object [1]; 828 829 ClassLoader loader = getExtendedClassLoader(); 830 if(loader != null){ 831 Class cc = loader.loadClass("org.netbeans.modules.j2ee.sun.bridge.AppServerBridge"); 832 Method isCar = cc.getMethod("isCar", argClass); for (TargetModuleID tmid : tmids) { 834 argObject[0] = tmid; 835 boolean doNotAddToRetList = ((Boolean ) isCar.invoke(null,argObject)).booleanValue(); 836 if (!doNotAddToRetList) { 837 retList.add(tmid); 838 } 839 } 840 } 841 }catch(Exception ex){ 842 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 844 } 845 return retList.toArray(new TargetModuleID [retList.size()]); 846 } 847 848 public ProgressObject undeploy(TargetModuleID [] targetModuleID) 849 throws IllegalStateException { 850 ThrowExceptionIfSuspended(); 852 ClassLoader origClassLoader=Thread.currentThread().getContextClassLoader(); 853 Thread.currentThread().setContextClassLoader(ServerLocationManager.getServerOnlyClassLoader(getPlatformRoot())); 854 855 ProgressObject retVal = null; 856 try{ 857 grabInnerDM(false); 858 retVal = innerDM.undeploy(targetModuleID); 859 if (null != retVal) { 860 retVal.addProgressListener(new ReleaseInnerDMPL(Thread.currentThread())); 861 } 862 return retVal; 863 } 864 865 finally{ 866 if (null == retVal) { 867 releaseInnerDM(); 868 } 869 Thread.currentThread().setContextClassLoader(origClassLoader); 870 871 } 872 } 873 874 875 private void calculateIsLocal(){ 876 boolean isset = false; 877 InstanceProperties ip = SunURIManager.getInstanceProperties(getPlatformRoot(), 878 getHost(), getPort()); 879 if (ip!=null){ Object domainDir = ip.getProperty("LOCATION"); 881 if ("".equals(domainDir)) { 882 isLocal = false; 883 isset = true; 884 } 885 } 886 if (!isset) { 887 if (getHost().equals("localhost")) { 888 isLocal = true; 889 } else { 890 try { 891 new Thread () { 892 public void run() { 893 try { 894 String ia = InetAddress.getByName(getHost()).getHostAddress(); 895 if(ia.equals("127.0.0.1")){ isLocal = true; 897 } else { 898 String localCanonName = InetAddress.getLocalHost().getCanonicalHostName(); 899 String currentCanonName = InetAddress.getByName(getHost()).getCanonicalHostName(); 900 901 isLocal = (localCanonName.equals(currentCanonName) ) ? true : false; 902 } 903 } catch (Exception e) { 904 jsr88Logger.severe(e.getMessage()); 906 } 907 } 908 }.start(); 909 910 } catch (Throwable t) { 911 return; 913 } 914 } 915 } 916 } 917 918 920 public boolean isLocal() { 921 922 return isLocal; 924 } 925 926 928 929 934 public synchronized boolean isRunning() { 935 return isRunning(false); 936 937 } 938 943 public boolean isRunning(boolean forced) { 944 945 boolean retVal = false; 946 947 if (isSuspended()) { 948 retVal = true; 949 } else { 950 951 959 long current=System.currentTimeMillis(); 960 if (forced==false && current-timeStampCheckingRunning<4000){ 962 retVal = runningState; 965 966 } else { 967 boolean newrunningState = false; 968 timeStampCheckingRunning = current; 969 970 try { 971 if (secureStatusHasBeenChecked&& (maybeRunningButWrongUserName==true)){ 972 testCredentials() ; } 974 ThrowExceptionIfSuspended(); 975 976 Target [] t= getTargets(); 977 if (t != null) { 978 if (t.length==0) 979 newrunningState = false; 980 else 981 982 newrunningState = true; 983 } 984 985 } catch (Throwable e) { 987 newrunningState =false; 988 } 990 runningState = newrunningState; 991 if ((runningState)&&(nonAdminPortNumber == null)){ 992 try{ 993 nonAdminPortNumber = (String )getManagement().getAttribute(new javax.management.ObjectName ("com.sun.appserv:type=http-listener,id=http-listener-1,config=server-config,category=config") ,"port"); 995 Runnable t = new Runnable () { 998 public void run() { 999 try { 1000 storeAntDeploymentProperties(getAntDeploymentPropertiesFile(),true); 1001 } catch (IOException ioe) { 1002 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ioe); 1004 } 1005 1006 } 1007 1008 }; 1009 RequestProcessor.getDefault().post(t); 1010 } catch (Throwable ee) { 1011 ee.printStackTrace(); 1012 } 1013 } 1014 timeStampCheckingRunning = System.currentTimeMillis(); 1015 1017 retVal = runningState; 1018 } 1019 } 1020 return retVal; 1021 } 1022 1023 1024 1025 1026 1027 public void ThrowExceptionIfSuspended(){ 1028 1029 1031 1032 if (secureStatusHasBeenChecked == false) { 1033 mmm=null; 1035 try{ 1036 secure=PortDetector.isSecurePort(getHost(),getPort()); 1037 1038 if (secure==true){ 1039 if (!uri.endsWith(SECURESTRINGDETECTION)){ 1040 uri=uri+SECURESTRINGDETECTION; } 1042 resetInnerDeploymentManager(); 1043 1045 } 1046 secureStatusHasBeenChecked = true; 1049 testCredentials(); 1051 1052 1053 } catch(Exception e){ 1054 secureStatusHasBeenChecked = false; 1059 } 1062 } 1063 if (isSuspended()){ 1064 1065 throw new RuntimeException (bundle.getString("MSG_ServerInDebug")) ; 1067 } 1068 if (secureStatusHasBeenChecked&& (goodUserNamePassword==false)){ 1069 throw new RuntimeException (bundle.getString("MSG_WRONG_UserPassword")) ; 1070 } 1071 1072 } 1073 1074 1075 1079 public boolean isSuspended() { 1080 return org.netbeans.modules.j2ee.sun.ide.j2ee.StartSunServer.isSuspended(this); 1081 } 1082 1083 1084 1085 1086 1087 private ServerInterface mmm=null; 1088 1089 public ServerInterface getManagement() { 1090 if(mmm==null){ 1091 1092 try{ 1093 Class [] argClass = new Class [1]; 1094 argClass[0] = javax.enterprise.deploy.spi.DeploymentManager .class; 1095 Object [] argObject = new Object [1]; 1096 argObject[0] = this; 1097 1098 ClassLoader loader = getExtendedClassLoader(); 1099 if(loader != null){ 1100 Class cc = loader.loadClass("org.netbeans.modules.j2ee.sun.share.management.ServerMEJB"); 1101 mmm = (ServerInterface)cc.newInstance(); 1102 mmm.setDeploymentManager(this); 1103 } 1104 }catch(Exception ex){ 1105 mmm = null; 1106 ex.printStackTrace(); 1108 } 1109 1110 1111 } 1112 return mmm; 1113 } 1114 1115 1116 public void testCredentials() { 1117 1118 Authenticator.setDefault(new AdminAuthenticator(this)); 1119 1120 goodUserNamePassword =false; 1121 maybeRunningButWrongUserName=false; 1122 try { 1123 Class [] argClass = new Class [1]; 1124 argClass[0] = javax.enterprise.deploy.spi.DeploymentManager .class; 1125 Object [] argObject = new Object [1]; 1126 argObject[0] = this; 1127 1128 ClassLoader loader = getExtendedClassLoader(); 1129 if(loader != null){ 1130 Class cc = loader.loadClass("org.netbeans.modules.j2ee.sun.share.management.ServerMEJB"); 1131 ServerInterface si; 1132 1133 si = (ServerInterface) cc.newInstance(); 1134 si.setDeploymentManager(this); 1135 si.checkCredentials(); 1136 goodUserNamePassword =true; 1137 } 1138 1139 1140 } catch (ClassNotFoundException ex) { 1141 goodUserNamePassword =false; 1142 } catch (InstantiationException ex) { 1143 goodUserNamePassword =false; 1144 } catch (IllegalAccessException ex) { 1145 goodUserNamePassword =false; 1146 } catch (IOException e){ 1147 if(!e.getMessage().contains("500")){ maybeRunningButWrongUserName =true ; 1149 } 1150 1151 String serverTitle = getInstanceDisplayName(); 1152 DialogDescriptor desc = new DialogDescriptor( 1153 NbBundle.getMessage(SunDeploymentManager.class, 1154 "ERR_AUTH_DIALOG_MSG", new Object [] { ((serverTitle != null) ? serverTitle : NbBundle.getMessage(SunDeploymentManager.class, "WORD_SERVER")), e.getLocalizedMessage() } ), 1157 NbBundle.getMessage(SunDeploymentManager.class,"ERR_AUTH_DIALOG_TITLE")); desc.setModal(false); 1159 desc.setMessageType(DialogDescriptor.ERROR_MESSAGE); 1160 desc.setOptions(new Object [] { DialogDescriptor.OK_OPTION }); 1161 desc.setOptionsAlign(DialogDescriptor.BOTTOM_ALIGN); 1162 DialogDisplayer.getDefault().notify(desc); 1163 } 1164 1165 } 1166 1167 private String getInstanceDisplayName() { 1168 InstanceProperties ip = SunURIManager.getInstanceProperties(getPlatformRoot(), getHost(), getPort()); 1169 return (ip != null) ? ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR) : null; 1170 } 1171 1172 private ResourceConfiguratorInterface resourceConfigurator = null; 1173 1174 public ResourceConfiguratorInterface getResourceConfigurator() { 1175 if(resourceConfigurator == null){ 1176 org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceConfigurator r = new org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceConfigurator(); 1177 r.setDeploymentManager(this); 1178 resourceConfigurator = r; 1179 } 1199 return resourceConfigurator; 1200 } 1201 1202 private javax.management.MBeanServerConnection getMBeanServerConnection() throws RemoteException , ServerException { 1203 ServerInterface serverMgmt = getManagement(); 1204 return (javax.management.MBeanServerConnection )serverMgmt.getMBeanServerConnection(); 1205 } 1206 1207 1208 public boolean isMaybeRunningButWrongUserName() { 1209 return maybeRunningButWrongUserName; 1210 } 1211 1212 public boolean isSecure() { 1213 return secure; 1214 } 1215 1216 1217 1218 1219 1220 static String getHostFromURI(String uri) { 1221 String retVal = null; 1222 try { 1223 int len1 = uri.lastIndexOf(':'); 1224 String partial = uri.substring(0,len1); 1226 int len2 = partial.lastIndexOf(':'); 1227 1228 retVal = uri.substring(len2+1,len1); 1229 } catch (Throwable t) { 1230 jsr88Logger.warning("getHostFromURI:: invalid uri: "+ uri); 1231 } 1232 return retVal; 1233 } 1234 1235 static int getPortFromURI(String uri) { 1236 int retVal = -1; 1237 int len1 = uri.lastIndexOf(':'); 1238 if (-1 != len1){ 1239 try { 1241 retVal = Integer.parseInt(uri.substring(len1+1)); 1242 } catch (NumberFormatException nfe) { 1243 jsr88Logger.warning(nfe.getMessage()); 1244 } 1245 } 1246 return retVal; 1247 } 1248 1249 1250 1251 public void refreshDeploymentManager(){ 1252 try{ 1253 resetInnerDeploymentManager(); 1254 }catch(Exception ex) { 1255 return; } 1257 } 1258 1259 1260 public File getPlatformRoot() { 1261 return platformRoot; 1262 } 1263 1264 private ClassLoader getExtendedClassLoader(){ 1265 1266 return ServerLocationManager.getNetBeansAndServerClassLoader(getPlatformRoot()); 1267 } 1268 1269 public void setUserName(String name) { 1270 mmm = null; 1271 secureStatusHasBeenChecked=false; 1272 String oldValue = userName; 1273 userName = name; 1274 propertySupport.firePropertyChange("name", oldValue, userName); 1275 } 1276 1277 public void setPassword(String pw) { 1278 mmm= null; 1279 secureStatusHasBeenChecked=false; 1280 String oldValue = password; 1281 password = pw; 1282 passwordForURI.put(uri+platformRoot,password); 1283 1284 refreshDeploymentManager(); 1285 1286 propertySupport.firePropertyChange("password", oldValue, password); 1287 1288 1289 } 1290 1291 public File getAntDeploymentPropertiesFile() { 1292 return new File (System.getProperty("netbeans.user"), getInstanceID() + ".properties"); } 1294 1295 public void storeAntDeploymentProperties(File file, boolean create) throws IOException { 1296 if (!create && !file.exists()) { 1297 return; 1298 } 1299 Properties antProps = new Properties(); 1300 antProps.setProperty("sjsas.root", getPlatformRoot().getAbsolutePath()); antProps.setProperty("sjsas.url", getWebUrl()); antProps.setProperty("sjsas.username", getUserName()); antProps.setProperty("sjsas.password", getPassword()); antProps.setProperty("sjsas.host",getHost()); 1305 antProps.setProperty("sjsas.port",getPort()+""); 1306 boolean ret = file.createNewFile(); 1307 FileObject fo = FileUtil.toFileObject(file); 1308 FileLock lock = null; 1309 try { 1310 lock = fo.lock(); 1311 OutputStream os = fo.getOutputStream(lock); 1312 try { 1313 antProps.store(os,""); 1314 } finally { 1315 if (null != os) { 1316 os.close(); 1317 } 1318 } 1319 } finally { 1320 if (null != lock) { 1321 lock.releaseLock(); 1322 } 1323 } 1324 } 1325 1326 private static String PROP_INSTANCE_ID = "PROP_INSTANCE_ID"; 1327 1328 String getInstanceID() { 1330 InstanceProperties ip = SunURIManager.getInstanceProperties(platformRoot,host,adminPortNumber); 1331 String name = null; 1332 if (null != ip) { 1333 name = ip.getProperty(PROP_INSTANCE_ID); 1334 } 1335 if (name == null) { 1336 boolean isGF = ServerLocationManager.isGlassFish(platformRoot); 1337 String prefix = isGF ? "glassfish" : "sjsas8"; String [] instanceURLs = Deployment.getDefault().getInstancesOfServer("J2EE"); int len = 0; 1340 if (null != instanceURLs) { 1341 len = instanceURLs.length; 1342 } 1343 for (int i = 0; name == null; i++) { 1344 if (i == 0) { 1345 name = prefix; 1346 } else { 1347 name = prefix + "_" + i; } 1349 1350 for (int j = 0; j < len; j++) { String url = instanceURLs[j]; 1352 String uri = null; 1353 if (null != ip) { 1354 uri = ip.getProperty(InstanceProperties.URL_ATTR); 1355 } 1356 if (!url.equals(uri)) { 1357 InstanceProperties iip = InstanceProperties.getInstanceProperties(url); 1358 if (iip != null) { 1359 String anotherName = iip.getProperty(PROP_INSTANCE_ID); 1360 if (name.equals(anotherName)) { 1361 name = null; 1362 break; 1363 } 1364 } 1365 } 1366 } 1367 } 1368 if (null != ip) { 1369 ip.setProperty(PROP_INSTANCE_ID, name); 1370 } 1371 } 1372 return name; 1373 } 1374 1375 private String getWebUrl() { 1376 return "http://"+host+":"+nonAdminPortNumber; } 1378 1379 public HashMap getSunDatasourcesFromXml(){ 1380 DomainEditor dEditor = new DomainEditor(this); 1381 return dEditor.getSunDatasourcesFromXml(); 1382 } 1383 1384 public HashMap getConnPoolsFromXml(){ 1385 DomainEditor dEditor = new DomainEditor(this); 1386 return dEditor.getConnPoolsFromXml(); 1387 } 1388 1389 public void addPropertyChangeListener(PropertyChangeListener listener) { 1390 propertySupport.addPropertyChangeListener(listener); 1391 } 1392 1393 public void removePropertyChangeListener(PropertyChangeListener listener) { 1394 propertySupport.removePropertyChangeListener(listener); 1395 } 1396 1397 class FakeTarget implements Target { 1400 1401 public String getDescription() { 1402 return "fakeTargetDescr"; } 1404 public String getName() { 1405 return "fakeTargetName"; } 1407 1408 } 1409 1410 private AtomicBoolean locked = new AtomicBoolean (false); 1411 1412 public boolean grabInnerDM(boolean returnInsteadOfWait) { 1413 while (true) { 1414 if (locked.compareAndSet(false,true)) { 1415 break; 1417 } else { 1418 try { 1419 if (returnInsteadOfWait) { 1420 return false; 1421 } 1422 synchronized (locked) { locked.wait(500); 1425 } 1426 } catch (InterruptedException ie) { 1427 } 1429 } 1430 } 1431 return true; 1432 } 1433 1434 public void releaseInnerDM() { 1435 locked.set(false); 1436 synchronized (locked) { locked.notifyAll(); 1439 } 1440 } 1441 1442 private class ReleaseInnerDMPL implements ProgressListener { 1443 Thread locker; 1444 ReleaseInnerDMPL(Thread locker) { 1445 this.locker = locker; 1446 } 1447 1448 public void handleProgressEvent(ProgressEvent progressEvent) { 1449 DeploymentStatus dms = progressEvent.getDeploymentStatus(); 1450 if (!dms.isRunning()) { 1451 locked.set(false); 1452 synchronized (locked) { locked.notifyAll(); 1455 } 1456 } 1457 } 1458 } 1459 1460 1463 class FileDeleter implements ProgressListener { 1465 File f; 1466 public FileDeleter(File f) { 1467 this.f = f; 1468 } 1469 1470 public void handleProgressEvent(ProgressEvent pe) { 1471 DeploymentStatus ds = pe.getDeploymentStatus(); 1472 if (ds.isCompleted() || ds.isFailed()) { 1473 f.delete(); 1475 } 1476 } 1477 } 1478 1480 1487 static class ShortCircuitProgressObject implements ProgressObject { 1489 1490 private CommandType ct; 1491 private String message; 1492 private StateType st; 1493 private TargetModuleID [] tmids; 1494 1495 ProgressEventSupport pes = new ProgressEventSupport(this); 1496 1497 1504 ShortCircuitProgressObject(CommandType ct, String message, StateType st, TargetModuleID [] tmids) { 1505 this.ct = ct; 1506 this.message = message; 1507 this.st = st; 1508 this.tmids = tmids; 1509 } 1510 1511 1515 public DeploymentStatus getDeploymentStatus() { 1516 return new DeploymentStatus () { 1517 public ActionType getAction() { 1518 return ActionType.EXECUTE; 1519 } 1520 public CommandType getCommand() { 1521 return ct; 1522 } 1523 public String getMessage() { 1524 return message; 1525 } 1526 public StateType getState() { 1527 return st; 1528 } 1529 public boolean isCompleted() { 1530 return st.equals(StateType.COMPLETED); 1531 } 1532 public boolean isFailed() { 1533 return st.equals(StateType.FAILED); 1534 } 1535 public boolean isRunning() { 1536 return st.equals(StateType.RUNNING); 1537 } 1538 }; 1539 } 1540 1541 1545 public TargetModuleID [] getResultTargetModuleIDs() { 1546 return tmids; 1547 } 1548 1549 1554 public ClientConfiguration getClientConfiguration(TargetModuleID targetModuleID) { 1555 return null; 1556 } 1557 1558 1562 public boolean isCancelSupported() { 1563 return false; 1564 } 1565 1566 1570 public void cancel() throws OperationUnsupportedException { 1571 } 1572 1573 1577 public boolean isStopSupported() { 1578 return false; 1579 } 1580 1581 1585 public void stop() throws OperationUnsupportedException { 1586 } 1587 1588 1592 public void addProgressListener(ProgressListener progressListener) { 1593 pes.addProgressListener(progressListener); 1594 } 1595 1596 1600 public void removeProgressListener(ProgressListener progressListener) { 1601 pes.removeProgressListener(progressListener); 1602 } 1603 } 1604} 1606 | Popular Tags |