1 23 24 package com.sun.enterprise.deployapi; 25 26 import com.sun.appserv.management.base.AMX; 27 import com.sun.appserv.management.base.XTypes; 28 import com.sun.appserv.management.client.AppserverConnectionSource; 29 import com.sun.appserv.management.client.ConnectionSource; 30 import com.sun.appserv.management.client.ProxyFactory; 31 import com.sun.appserv.management.client.TLSParams; 32 import com.sun.appserv.management.config.ClusterConfig; 33 import com.sun.appserv.management.config.DeployedItemRefConfig; 34 import com.sun.appserv.management.config.DomainConfig; 35 import com.sun.appserv.management.config.HTTPListenerConfig; 36 import com.sun.appserv.management.config.StandaloneServerConfig; 37 import com.sun.appserv.management.config.TemplateResolver; 38 import com.sun.appserv.management.config.WebModuleConfig; 39 import com.sun.appserv.management.DomainRoot; 40 import com.sun.appserv.management.helper.DeployedItemHelper; 41 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 42 import com.sun.enterprise.admin.util.HostAndPort; 43 import com.sun.enterprise.deployapi.config.SunDeploymentConfiguration; 44 import com.sun.enterprise.deployapi.ProgressObjectImpl; 45 import com.sun.enterprise.deployapi.ProgressObjectSink; 46 import com.sun.enterprise.deployment.client.DeploymentClientUtils; 47 import com.sun.enterprise.deployment.client.DeploymentFacility; 48 import com.sun.enterprise.deployment.client.DeploymentFacilityFactory; 49 import com.sun.enterprise.deployment.client.ServerConnectionEnvironment; 50 import com.sun.enterprise.deployment.client.ServerConnectionIdentifier; 51 import com.sun.enterprise.deployment.client.TargetType; 52 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 53 import com.sun.enterprise.deployment.deploy.shared.AbstractArchiveFactory; 54 import com.sun.enterprise.deployment.deploy.shared.Archive; 55 import com.sun.enterprise.deployment.deploy.shared.ArchiveFactory; 56 import com.sun.enterprise.deployment.deploy.shared.InputJarArchive; 57 import com.sun.enterprise.deployment.deploy.shared.JarArchiveFactory; 58 import com.sun.enterprise.deployment.deploy.shared.MemoryMappedArchive; 59 import com.sun.enterprise.deployment.deploy.shared.WritableArchive; 60 import com.sun.enterprise.deployment.deploy.spi.DeploymentManager; 61 import com.sun.enterprise.deployment.util.DeploymentProperties; 62 import com.sun.enterprise.util.LocalStringManagerImpl; 63 import com.sun.enterprise.util.Print; 64 65 import java.io.File ; 66 import java.io.InputStream ; 67 import java.io.IOException ; 68 69 import java.net.InetAddress ; 70 import java.net.MalformedURLException ; 71 import java.net.UnknownHostException ; 72 import java.net.URI ; 73 import java.net.URISyntaxException ; 74 import java.net.URL ; 75 76 import java.util.Collection ; 77 import java.util.HashMap ; 78 import java.util.Iterator ; 79 import java.util.List ; 80 import java.util.Locale ; 81 import java.util.Map ; 82 import java.util.Properties ; 83 import java.util.Set ; 84 import java.util.Vector ; 85 86 import javax.enterprise.deploy.model.DeployableObject ; 87 import javax.enterprise.deploy.shared.CommandType ; 88 import javax.enterprise.deploy.shared.DConfigBeanVersionType ; 89 import javax.enterprise.deploy.shared.ModuleType ; 90 import javax.enterprise.deploy.shared.StateType ; 91 import javax.enterprise.deploy.spi.DeploymentConfiguration ; 92 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 93 import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException ; 94 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException ; 95 import javax.enterprise.deploy.spi.exceptions.TargetException ; 96 import javax.enterprise.deploy.spi.status.ProgressObject ; 97 import javax.enterprise.deploy.spi.status.ProgressEvent ; 98 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 99 import javax.enterprise.deploy.spi.Target ; 100 import javax.enterprise.deploy.spi.TargetModuleID ; 101 102 import javax.management.AttributeNotFoundException ; 103 import javax.management.InstanceNotFoundException ; 104 import javax.management.MalformedObjectNameException ; 105 import javax.management.MBeanException ; 106 import javax.management.MBeanInfo ; 107 import javax.management.MBeanServerConnection ; 108 import javax.management.ObjectName ; 109 import javax.management.ReflectionException ; 110 111 import javax.net.ssl.X509TrustManager; 112 113 118 public class SunDeploymentManager implements DeploymentManager { 119 120 private SunTarget target=null; 122 123 private ConnectionSource dasConnection = null; 125 private DomainRoot rootProxy = null; 126 127 private DomainConfig domainConfigProxy = null; 129 private ProxyFactory proxyFactory = null; 130 131 private ServerConnectionIdentifier serverId = null; 133 134 135 private DeploymentFacility deploymentFacility = null; 136 private static final String applicationsMBeanName = 137 "com.sun.appserv:type=applications,category=config"; 139 private static LocalStringManagerImpl localStrings = 140 new LocalStringManagerImpl(SunDeploymentManager.class); 141 142 private static Locale defaultLocale = Locale.US; 143 private Locale currentLocale = defaultLocale; 144 private static Locale [] supportedLocales = { Locale.US }; 145 private String disconnectedMessage = localStrings.getLocalString( 146 "enterprise.deployapi.spi.disconnectedDM", "Illegal operation for a disconnected DeploymentManager"); 149 private static final String ENABLED_ATTRIBUTE_NAME = "Enabled"; 151 152 public SunDeploymentManager() { 153 } 154 155 156 public SunDeploymentManager(ServerConnectionIdentifier sci) { 157 this.target = new SunTarget(sci); 158 serverId = sci; 159 } 160 161 168 public void setServerConnectionEnvironment(ServerConnectionEnvironment env) { 169 serverId.setConnectionEnvironment(env); 170 } 171 172 182 public Target [] getTargets() throws IllegalStateException { 183 verifyConnected(); 184 Target [] result = null; 185 try { 186 DomainConfig domainCfg = getDomainConfigProxy(); 187 194 final Map standaloneServers = 195 domainCfg.getStandaloneServerConfigMap(); 196 final Map clusters = domainCfg.getClusterConfigMap(); 197 200 Vector targets = new Vector (); 201 202 for (Iterator it = standaloneServers.keySet().iterator(); it.hasNext(); ) { 203 targets.add(createSunTarget(target, (String )it.next(), 204 TargetType.STAND_ALONE_SERVER)); 205 } 206 for (Iterator it = clusters.keySet().iterator(); it.hasNext(); ) { 207 targets.add(createSunTarget(target, (String )it.next(), TargetType.CLUSTER)); 208 } 209 210 213 result = (Target []) targets.toArray(new SunTarget[0]); 214 } catch (Throwable e) { 215 IllegalStateException ex = new IllegalStateException (e.getMessage()); 216 ex.initCause(e); 217 throw ex; 218 } 219 return result; 220 } 221 222 230 private SunTarget createSunTarget(SunTarget target, String name, String type) throws IOException { 231 SunTarget aTarget = new SunTarget(target); 232 aTarget.setAppServerInstance(name); 233 aTarget.setConnectionSource(getDasConnection()); 234 aTarget.setTargetType(type); 235 return aTarget; 236 } 237 238 259 public TargetModuleID [] getRunningModules(ModuleType moduleType, 260 Target [] targetList) throws TargetException , IllegalStateException { 261 262 return getModules(moduleType, targetList, ENABLED_ATTRIBUTE_NAME, Boolean.TRUE); 263 } 264 265 286 public TargetModuleID [] getNonRunningModules(ModuleType moduleType, 287 Target [] targetList) throws TargetException , IllegalStateException { 288 return getModules(moduleType, targetList, ENABLED_ATTRIBUTE_NAME, Boolean.FALSE); 289 } 290 291 311 public TargetModuleID [] getAvailableModules(ModuleType moduleType, 312 Target [] targetList) throws TargetException , 313 IllegalStateException { 314 315 return getModules(moduleType, targetList, null, null); 316 } 317 318 324 private void verifyConnected() { 325 if(isDisconnected()) { 326 throw new IllegalStateException (disconnectedMessage); 327 } 328 } 329 330 334 private boolean isDisconnected(){ 335 return target == null; 336 } 337 338 339 350 private TargetModuleID [] getModules(ModuleType moduleType, Target [] targetList, 351 String attribute, Object status) throws TargetException , IllegalStateException { 352 353 verifyConnected(); 354 if (moduleType==null) { 355 return null; 356 } 357 358 try { 359 Vector resultingTMIDs = new Vector (); 360 for (int i = 0; i < targetList.length; i++) { 361 SunTarget aTarget = (SunTarget) targetList[i]; 362 363 366 String [] moduleNames = getModulesOnATarget(aTarget, moduleType, attribute, status); 367 368 371 addToTargetModuleIDs(moduleNames, moduleType, aTarget, resultingTMIDs); 372 } 373 374 377 TargetModuleID [] answer = (TargetModuleID []) resultingTMIDs.toArray(new SunTargetModuleID[0]); 378 return answer; 379 } catch(Exception e){ 380 TargetException tg = new TargetException (localStrings.getLocalString( 381 "enterprise.deployapi.spi.errorgetreqmods", 382 "Error getting required modules" 383 )); 384 tg.initCause(e); 385 throw tg; 386 } 387 } 388 389 392 private String [] getModulesOnATarget(SunTarget aTarget, ModuleType requiredType, String attribute, 393 Object status) throws IOException { 394 Set modules = null; 395 396 DeployedItemHelper helper = new DeployedItemHelper(getRootProxy()); 398 if( ! aTarget.getTargetType().equals(TargetType.CLUSTER)) { 399 modules = helper.queryStandaloneServerDeployedItemObjectNames(aTarget.getName()); 400 } else { 401 modules = helper.queryClusterDeployedItemObjectNames(aTarget.getName()); 402 } 403 404 if(attribute != null) { 406 modules = helper.filterByAttributeValue(modules, attribute, status); 407 } 408 409 Vector modulesMatchingType = new Vector (); 411 final String xtype = getXType(requiredType); 412 for(Iterator it = modules.iterator(); it.hasNext(); ) { 413 414 ObjectName objectName = (ObjectName ) it.next(); 415 String modId = objectName.getKeyProperty("name"); AMX proxy = (AMX)getDomainConfigProxy(). 417 getContainee(xtype, modId); 418 419 if (null != proxy) { 420 modulesMatchingType.add(modId); 421 } 422 } 423 String [] actualModules = null; 424 actualModules = (String []) modulesMatchingType.toArray(new String [0]); 425 return actualModules; 426 } 427 428 private String getXType(ModuleType moduleType) { 429 String result = DeploymentClientUtils.mapModuleTypeToXType(moduleType); 430 return result; 431 } 432 433 440 private void addToTargetModuleIDs(String [] moduleIDs, ModuleType type, SunTarget sunTarget, Collection resultingTMIDs) throws IOException { 441 442 for (int j = 0;j < moduleIDs.length;j++) { 443 444 HostAndPort webHost = getHostPort(moduleIDs[j], sunTarget); 446 447 450 SunTargetModuleID tmid = new SunTargetModuleID(moduleIDs[j], sunTarget); 451 tmid.setModuleType(type); 452 resultingTMIDs.add((TargetModuleID ) tmid); 453 454 458 try { 459 if (type.equals(ModuleType.EAR)) { 460 setJ2EEApplicationTargetModuleIDInfo(tmid, moduleIDs[j], webHost); 461 } else if (type.equals(ModuleType.WAR)) { 462 setWebApplicationTargetModuleIDInfo(tmid, moduleIDs[j], webHost); 463 } 464 } 465 catch(Exception exp){ 466 Print.dprintStackTrace(exp.getLocalizedMessage(), exp); 467 Print.dprint("***Exception occured while "+ "navigating or accessing admin proxies: Keep continuing\n"); } 470 } 471 } 472 473 480 private void setJ2EEApplicationTargetModuleIDInfo(SunTargetModuleID tmid, String moduleID, HostAndPort hostAndPort) throws AttributeNotFoundException , MalformedURLException , IOException { 481 482 487 addChildTargetModuleIDsToJ2EEUsingMBeans(tmid, moduleID, hostAndPort); 488 } 489 490 497 private void setWebApplicationTargetModuleIDInfo(SunTargetModuleID tmid, String moduleID, HostAndPort webHost) throws MalformedURLException , IOException { 498 499 SunTarget sunTarget = (SunTarget) tmid.getTarget(); 500 503 WebModuleConfig webProxy = (WebModuleConfig)getDomainConfigProxy(). 504 getContainee(XTypes.WEB_MODULE_CONFIG, moduleID); 505 506 String path = webProxy.getContextRoot(); 507 if (!path.startsWith("/")) { path = "/" + path; } 510 511 String host; 513 if(isPE()) { 514 host = tmid.getConnectionInfo().getHostName(); 515 } else { 516 host = webHost.getHost(); 517 } 518 URL webURL = new URL ("http", host, webHost.getPort(), path); tmid.setWebURL(webURL.toExternalForm()); 520 } 521 522 527 private HostAndPort getHostPort(String moduleID, SunTarget aTarget) throws IOException { 528 529 533 DeployedItemRefConfig cfg = null; 534 535 539 TemplateResolver resolver = null; 540 541 if( ! aTarget.getTargetType().equals(TargetType.CLUSTER)) { 542 StandaloneServerConfig svrProxy = 543 (StandaloneServerConfig)getDomainConfigProxy(). 544 getContainee(XTypes.STANDALONE_SERVER_CONFIG, 545 aTarget.getName()); 546 cfg = (DeployedItemRefConfig)svrProxy.getContainee( 547 XTypes.DEPLOYED_ITEM_REF_CONFIG, moduleID); 548 resolver = svrProxy; 549 } else { 550 ClusterConfig clProxy = (ClusterConfig)getDomainConfigProxy(). 551 getContainee(XTypes.CLUSTER_CONFIG, aTarget.getName()); 552 cfg = (DeployedItemRefConfig)clProxy.getContainee( 553 XTypes.DEPLOYED_ITEM_REF_CONFIG, moduleID); 554 557 resolver = null; 558 } 559 560 String vServers = cfg.getVirtualServers(); 562 563 if(vServers == null) { 565 return(getDefaultHostPort(resolver)); 567 } 568 569 String [] vsList = vServers.split(" ,"); if (vsList.length == 0) { 571 return getDefaultHostPort(resolver); 573 } 574 575 579 HostAndPort answer = null; 580 for (int i = 0; (i < vsList.length) && (answer == null); i++) { 581 585 answer = getVirtualServerHostAndPort(vsList[i], resolver); 586 } 587 if (answer == null) { 588 answer = getDefaultHostPort(resolver); 589 } 590 return answer; 591 } 592 593 599 private HostAndPort getVirtualServerHostAndPort(String vs, TemplateResolver resolver) throws IOException { 600 try { 601 return getVirtualServerHostAndPortUsingMBeans(vs); 602 } catch (Throwable thr) { 603 IOException ioe = new IOException (localStrings.getLocalString( 604 "enterprise.deployapi.spi.errretreivevirtualserverhostport", 605 "Error retrieving virtual server host and port" 606 )); 607 ioe.initCause(thr); 608 throw ioe; 609 } 610 } 611 612 617 private HostAndPort getVirtualServerHostAndPortUsingMBeans(String vs) throws IOException , MalformedObjectNameException , InstanceNotFoundException , MBeanException , ReflectionException { 618 HostAndPort result = null; 619 620 MBeanServerConnection mbsc = getMBeanServerConnection(); 621 622 Object [] params = new Object [] {vs, Boolean.FALSE}; 623 String [] signature = new String []{ "java.lang.String", "boolean"}; ObjectName applicationsMBean = new ObjectName (applicationsMBeanName); 625 result = (HostAndPort) mbsc.invoke(applicationsMBean, "getVirtualServerHostAndPort", params, signature); return result; 627 } 628 629 private HostAndPort getDefaultHostPort(TemplateResolver resolver) throws IOException { 630 try { 631 return getDefaultHostPortUsingMBeans(); 632 } catch (Throwable thr) { 633 IOException ioe = new IOException (localStrings.getLocalString( 634 "enterprise.deployapi.spi.errretrievedefaulthostport", 635 "Error retrieving default host and port" 636 )); 637 ioe.initCause(thr); 638 throw ioe; 639 } 640 } 641 642 645 private HostAndPort getDefaultHostPortUsingMBeans() throws IOException , MalformedObjectNameException , MBeanException , InstanceNotFoundException , ReflectionException { 646 MBeanServerConnection mbsc = getMBeanServerConnection(); 647 HostAndPort result = null; 648 649 Object [] params = new Object [] {Boolean.FALSE}; 650 String [] signature = new String []{ "boolean"}; 651 ObjectName applicationsMBean = new ObjectName (applicationsMBeanName); 652 result = (HostAndPort) mbsc.invoke(applicationsMBean, "getHostAndPort", params, signature); 653 return result; 654 } 655 656 private HostAndPort getHostPort(HTTPListenerConfig listenerProxy, TemplateResolver resolver) throws IOException { 657 String serverName = null; 658 int port = 0; 659 660 if(!listenerProxy.getEnabled()) { 662 return null; 663 } 664 665 if("__asadmin".equals(listenerProxy.getDefaultVirtualServer())) { 668 return null; 669 } 670 671 if(!listenerProxy.getSecurityEnabled()) { 672 serverName = listenerProxy.getServerName(); 673 if (serverName == null || serverName.trim().equals("")) { 674 serverName = getDefaultHostName(); 675 } 676 String portString = listenerProxy.getPort(); 677 port = new Integer (getPropertyValueFromTemplate(portString, resolver)).intValue(); 678 if(listenerProxy.getRedirectPort() != null) { 679 port = new Integer (getPropertyValueFromTemplate(listenerProxy.getRedirectPort(), resolver)).intValue(); 680 } 681 } 682 HostAndPort hostPort = new HostAndPort(serverName, port); 683 return hostPort; 684 } 685 686 690 private String getPropertyValueFromTemplate(String str, TemplateResolver resolver) throws IOException { 691 696 String result = resolver.resolveTemplateString(str); 697 return result; 698 } 699 700 private String getDefaultHostName() { 701 String defaultHostName = "localhost"; try { 703 InetAddress host = InetAddress.getLocalHost(); 704 defaultHostName = host.getCanonicalHostName(); 705 } catch(UnknownHostException uhe) { 706 } 707 return defaultHostName; 708 } 709 710 719 720 public DeploymentConfiguration createConfiguration(DeployableObject dObj) 721 throws InvalidModuleException 722 { 723 try { 724 SunDeploymentConfiguration deploymentConfiguration = new SunDeploymentConfiguration(dObj); 725 deploymentConfiguration.setDeploymentManager(this); 726 return deploymentConfiguration; 727 } catch(ConfigurationException e) { 728 InvalidModuleException ime = new InvalidModuleException (e.getMessage()); 729 ime.initCause(e); 730 throw ime; 731 } 732 } 733 734 735 753 754 public ProgressObject distribute(Target [] targetList, 755 File moduleArchive, File deploymentPlan) 756 throws IllegalStateException 757 { 758 return deploy(targetList, moduleArchive, deploymentPlan, null ); 759 } 760 761 780 public ProgressObject distribute(Target [] targetList, 781 InputStream moduleArchive, InputStream deploymentPlan) 782 throws IllegalStateException 783 { 784 return deploy(targetList, moduleArchive, deploymentPlan, null ); 785 } 786 787 807 808 public ProgressObject distribute(Target [] targetList, ModuleType type, 809 InputStream moduleArchive, InputStream deploymentPlan) 810 throws IllegalStateException 811 { 812 DeploymentProperties dProps = new DeploymentProperties(); 813 dProps.setType(type); 814 return deploy(targetList, moduleArchive, deploymentPlan, (Properties)dProps); 815 } 816 817 833 834 public ProgressObject start(TargetModuleID [] moduleIDList) 835 throws IllegalStateException 836 { 837 return executeCommandUsingFacility(CommandType.START, moduleIDList); 838 } 839 840 856 857 public ProgressObject stop(TargetModuleID [] moduleIDList) 858 throws IllegalStateException 859 { 860 return executeCommandUsingFacility(CommandType.STOP, moduleIDList); 861 } 862 863 864 881 882 public ProgressObject undeploy(TargetModuleID [] moduleIDList) 883 throws IllegalStateException 884 { 885 return executeCommandUsingFacility(CommandType.UNDEPLOY, moduleIDList); 886 } 887 888 889 898 public boolean isRedeploySupported() { 899 return true; 900 } 901 902 937 938 public ProgressObject redeploy(TargetModuleID [] moduleIDList, 939 File moduleArchive, File deploymentPlan) 940 throws UnsupportedOperationException , IllegalStateException 941 { 942 try { 943 947 TargetModuleIDCollection coll = new TargetModuleIDCollection(moduleIDList); 948 for (Iterator it = coll.iterator(); it.hasNext();) { 949 952 DeploymentFacilityModuleWork work = (DeploymentFacilityModuleWork) it.next(); 953 957 ProgressObject po = deploy(work.targets(), moduleArchive, deploymentPlan, getRedeployOptions(work.getModuleID())); 958 959 963 work.setProgressObject(po); 964 coll.getProgressObjectSink().sinkProgressObject(po); 965 } 966 return coll.getProgressObjectSink(); 967 } catch (Throwable e) { 968 return prepareErrorProgressObject(CommandType.REDEPLOY, e); 969 } 970 } 971 972 1008 1009 public ProgressObject redeploy(TargetModuleID [] moduleIDList, 1010 InputStream moduleArchive, InputStream deploymentPlan) 1011 throws UnsupportedOperationException , IllegalStateException 1012 { 1013 try { 1014 1018 TargetModuleIDCollection coll = new TargetModuleIDCollection(moduleIDList); 1019 for (Iterator it = coll.iterator(); it.hasNext();) { 1020 1023 DeploymentFacilityModuleWork work = (DeploymentFacilityModuleWork) it.next(); 1024 1028 DeploymentProperties dProps = getRedeployOptions(work.getModuleID()); 1029 dProps.setType(getModuleTypeFor(work.getModuleID())); 1030 ProgressObject po = deploy(work.targets(), moduleArchive, deploymentPlan, dProps); 1031 1032 1036 work.setProgressObject(po); 1037 coll.getProgressObjectSink().sinkProgressObject(po); 1038 } 1039 return coll.getProgressObjectSink(); 1040 } catch (Throwable e) { 1041 return prepareErrorProgressObject(CommandType.REDEPLOY, e); 1042 } 1043 } 1044 1045 1063 1064 public void release() { 1065 1068 if ( ! isDisconnected() ) { 1069 target.release(); 1070 target = null; 1071 } 1072 } 1073 1074 1080 public Locale getDefaultLocale() { 1081 return defaultLocale; 1082 } 1083 1084 1090 public Locale getCurrentLocale() { 1091 return currentLocale; 1092 } 1093 1094 1101 public void setLocale(Locale locale) throws UnsupportedOperationException { 1102 for (int i=0;i<supportedLocales.length;i++) { 1103 if (supportedLocales[i] == locale) { 1104 currentLocale = locale; 1105 return; 1106 } 1107 } 1108 throw new UnsupportedOperationException ( 1109 localStrings.getLocalString("enterprise.deployapi.spi.localnotsupported", "Locale {0} is not supported", new Object [] {locale})); } 1112 1113 1118 public Locale [] getSupportedLocales() { 1119 return supportedLocales; 1120 } 1121 1122 1128 public boolean isLocaleSupported(Locale locale) { 1129 Locale [] locales = getSupportedLocales(); 1130 for (int i=0;i<locales.length;i++) { 1131 if (locales[i].equals(locale)) 1132 return true; 1133 } 1134 return false; 1135 } 1136 1137 1146 public DConfigBeanVersionType getDConfigBeanVersion() { 1147 return DConfigBeanVersionType.V5; 1148 } 1149 1150 1159 public boolean isDConfigBeanVersionSupported(DConfigBeanVersionType version) { 1160 return version.getValue()==getDConfigBeanVersion().getValue(); 1161 } 1162 1163 1172 public void setDConfigBeanVersion(DConfigBeanVersionType version) throws 1173 DConfigBeanVersionUnsupportedException { 1174 1175 if (!isDConfigBeanVersionSupported(version)) { 1176 throw new DConfigBeanVersionUnsupportedException ( 1177 localStrings.getLocalString( 1178 "enterprise.deployapi.spi.dconfigbeanversionnotsupported", "DConfigBean version {0} is not supported", new Object [] {version.toString()})); 1181 } 1182 } 1183 1184 1189 private DeploymentProperties getRedeployOptions(String moduleID) { 1190 DeploymentProperties deplProps = new DeploymentProperties(); 1191 deplProps.setForce(true); 1192 deplProps.setName(moduleID); 1193 return deplProps; 1194 } 1195 1196 1199 private MBeanServerConnection getMBeanServerConnection() throws IllegalStateException { 1200 return getDasConnection().getExistingMBeanServerConnection(); 1201 } 1202 1203 1204 1215 private ProgressObject deploy(Target [] targetList, 1216 InputStream moduleStream, InputStream deploymentPlanStream, Properties presetOptions) 1217 throws IllegalStateException { 1218 1219 1224 MemoryMappedArchive moduleArchive = null; 1225 MemoryMappedArchive deploymentPlanArchive = null; 1226 1227 try { 1228 moduleArchive = new MemoryMappedArchive(moduleStream); 1229 if (deploymentPlanStream != null) { 1230 deploymentPlanArchive = new MemoryMappedArchive(deploymentPlanStream); 1231 } 1232 return deploy(targetList, moduleArchive, deploymentPlanArchive, presetOptions); 1233 } catch (Throwable e) { 1234 String msg = localStrings.getLocalString( 1235 "enterprise.deployapi.spi.errpreparearchstream", 1236 "Could not prepare archives for module and/or deployment plan input streams"); 1237 IllegalArgumentException ex = new IllegalArgumentException (msg); 1238 ex.initCause(e); 1239 return prepareErrorProgressObject(CommandType.DISTRIBUTE, ex); 1240 } 1241 } 1242 1243 1254 private ProgressObject deploy(Target [] targetList, 1255 File moduleArchive, File deploymentPlan, Properties presetOptions) 1256 throws IllegalStateException { 1257 1258 1262 AbstractArchive appArchive = null; 1263 AbstractArchive planArchive = null; 1264 ArchiveFactory archiveFactory = new ArchiveFactory(); 1265 1266 try { 1267 appArchive = archiveFactory.openArchive(toJarURI(moduleArchive)); 1268 1269 if(deploymentPlan != null && deploymentPlan.length() != 0) { 1270 planArchive = archiveFactory.openArchive(toJarURI(deploymentPlan)); 1271 if (planArchive == null) { 1272 throw new IllegalArgumentException (localStrings.getLocalString( 1273 "enterprise.deployapi.spi.noarchivisthandlesplan", 1274 "No archivist is able to handle the deployment plan {0}", 1275 new Object [] {deploymentPlan.getAbsolutePath()} 1276 )); 1277 } 1278 } 1279 1280 ProgressObject po = deploy(targetList, appArchive, planArchive, presetOptions); 1281 return po; 1282 } catch (Exception se) { 1283 String msg = localStrings.getLocalString( 1284 "enterprise.deployapi.spi.errpreparearchfile", 1285 "Could not prepare archives for module and/or deployment plan files"); 1286 IllegalArgumentException ex = new IllegalArgumentException (msg); 1287 ex.initCause(se); 1288 return prepareErrorProgressObject(CommandType.DISTRIBUTE, ex); 1289 } finally { 1290 closeArchives(CommandType.DISTRIBUTE, appArchive, moduleArchive.getAbsolutePath(), planArchive, (deploymentPlan != null) ? deploymentPlan.getAbsolutePath() : null); 1291 } 1292 } 1293 1294 1304 private ProgressObject deploy(Target [] targetList, 1305 AbstractArchive moduleArchive, AbstractArchive planArchive, Properties presetOptions) 1306 throws IllegalStateException { 1307 1308 verifyConnected(); 1309 1310 String moduleID = null; 1311 ProgressObject progressObj = null; 1312 1313 try { 1314 String UriPath = moduleArchive.getArchiveUri(); 1315 moduleID = computeModuleID(moduleArchive); 1316 Properties options = getProperties(UriPath, moduleID); 1317 1318 1322 if (presetOptions != null) { 1323 options.putAll(presetOptions); 1324 } 1325 progressObj = getDeploymentFacility().deploy(targetList, moduleArchive, planArchive, options); 1326 1327 } catch(Throwable e) { 1328 1331 progressObj = prepareErrorProgressObject(CommandType.DISTRIBUTE, e); 1332 } 1333 return progressObj; 1334 } 1335 1336 1346 private ProgressObject closeArchives(CommandType commandType, AbstractArchive moduleArchive, String moduleArchiveSpec, AbstractArchive planArchive, String planArchiveSpec) { 1347 ProgressObject errorPO = null; 1348 StringBuilder msg = new StringBuilder (); 1349 1350 IOException moduleIOE = closeArchive(moduleArchive); 1351 IOException planIOE = closeArchive(planArchive); 1352 1353 IOException excForProgressObject = null; 1354 String errorMsg = null; 1355 1359 if (moduleIOE != null) { 1360 excForProgressObject = moduleIOE; 1361 1365 if (planIOE != null) { 1366 errorMsg = localStrings.getLocalString( 1367 "enterprise.deployapi.spi.errclosearchs", 1368 "Could not close module archive {0} or deployment plan archive {1}", 1369 new Object [] {moduleArchiveSpec, planArchiveSpec} 1370 ); 1371 } else { 1372 1376 errorMsg = localStrings.getLocalString( 1377 1378 "enterprise.deployapi.spi.errclosemodulearch", 1379 "Could not close module archive {0}", 1380 new Object [] {moduleArchiveSpec} 1381 ); 1382 } 1383 } else if (planIOE != null) { 1384 1391 excForProgressObject = planIOE; 1392 errorMsg = localStrings.getLocalString( 1393 "enterprise.deployapi.spi.errcloseplanarch", 1394 "Could not close deployment plan archive {0}", 1395 new Object [] {planArchiveSpec} 1396 ); 1397 } 1398 1399 1403 if (errorMsg != null) { 1404 IOException ioe = new IOException (errorMsg); 1405 ioe.initCause(excForProgressObject); errorPO = prepareErrorProgressObject(commandType, ioe); 1407 } 1408 1409 return errorPO; 1410 } 1411 1412 1417 private IOException closeArchive(AbstractArchive archive) { 1418 IOException errorIOE = null; 1419 if (archive != null) { 1420 try { 1421 archive.close(); 1422 } catch (IOException ioe) { 1423 errorIOE = ioe; 1424 } 1425 } 1426 return errorIOE; 1427 } 1428 1429 1438 private String computeModuleID(AbstractArchive moduleArchive) throws Exception 1439 { 1440 1443 String moduleID = null; 1444 1445 String UriPath = moduleArchive.getArchiveUri(); 1446 if ((UriPath != null) && (UriPath.length() > 0)) { 1447 1450 moduleID = pathExcludingType(UriPath); 1451 1452 moduleID = moduleID.replace(' ','_'); 1454 1455 moduleID = moduleID.replace('\\', '_').replace('/', '_'); 1459 moduleID = moduleID.replace(':', '_').replace('*', '_'); 1460 moduleID = moduleID.replace('?', '_').replace('"', '_'); 1461 moduleID = moduleID.replace('<', '_').replace('>', '_'); 1462 moduleID = moduleID.replace('|', '_'); 1463 1464 moduleID = moduleID.replace(',', '_').replace('=', '_'); 1468 } 1469 1470 return moduleID; 1471 } 1472 1473 1486 private ProgressObject executeCommandUsingFacility( 1487 CommandType commandType, TargetModuleID [] targetModuleIDList) 1488 throws IllegalStateException { 1489 1490 verifyConnected(); 1491 try { 1492 DeploymentFacility df = getDeploymentFacility(); 1493 1494 1498 TargetModuleIDCollection coll = new TargetModuleIDCollection(targetModuleIDList); 1499 1500 1504 1505 for (Iterator it = coll.iterator(); it.hasNext();) { 1506 1510 DeploymentFacilityModuleWork work = (DeploymentFacilityModuleWork) it.next(); 1511 ProgressObject po = null; 1512 1513 if (commandType.equals(CommandType.START)) { 1514 po = df.enable(work.targets(), work.getModuleID()); 1515 1516 } else if (commandType.equals(CommandType.STOP)) { 1517 po = df.disable(work.targets(), work.getModuleID()); 1518 1519 } else if (commandType.equals(CommandType.UNDEPLOY)) { 1520 po = df.undeploy(work.targets(), work.getModuleID()); 1521 1522 } else { 1523 throw new IllegalArgumentException (localStrings.getLocalString( 1524 "enterprise.deployapi.spi.unexpcommand", 1525 "Received unexpected deployment facility command ${0}", 1526 new Object [] {commandType.toString()} 1527 )); 1528 } 1529 1530 1534 work.setProgressObject(po); 1535 coll.getProgressObjectSink().sinkProgressObject(po); 1536 } 1537 1538 1541 return coll.getProgressObjectSink(); 1542 1543 } catch (Throwable thr) { 1544 return prepareErrorProgressObject(commandType, thr); 1545 } 1546 } 1547 1548 1554 private ProgressObject prepareErrorProgressObject (CommandType commandType, Throwable thr) { 1555 DeploymentStatus ds = new DeploymentStatusImplWithError(CommandType.DISTRIBUTE, thr); 1556 SimpleProgressObjectImpl progressObj = new SimpleProgressObjectImpl(ds); 1557 ProgressEvent event = new ProgressEvent (progressObj, null , ds); 1558 progressObj.fireProgressEvent(event); 1559 return progressObj; 1560 } 1561 1562 protected Properties getProperties(String archiveName, String moduleID) { 1563 DeploymentProperties dProps = new DeploymentProperties(); 1564 dProps.setArchiveName(archiveName); 1565 dProps.setName(moduleID); 1566 dProps.setEnable(false); 1567 return (Properties)dProps; 1568 } 1569 1570 1575 private String pathExcludingType(String path) { 1576 1579 path = path.substring(path.lastIndexOf('/')+1); 1580 if (path.lastIndexOf('.')!=-1) { 1581 path = path.substring(0, path.lastIndexOf('.')); 1582 } 1583 return path; 1584 } 1585 1586 1593 private ConnectionSource getDasConnection() { 1594 if (dasConnection == null) { 1595 if (serverId == null) { 1596 throw new IllegalStateException (localStrings.getLocalString( 1597 "enterprise.deployapi.spi.targetorprinnotset", 1598 "Attempted to connect to DAS management interface but target and/or principal have not been set" 1599 )); 1600 } 1601 1602 TLSParams tlsParams = null; 1603 if (serverId.isSecure()) { 1604 X509TrustManager trustManager = 1605 (X509TrustManager)serverId.getConnectionEnvironment(). 1606 get(DefaultConfiguration.TRUST_MANAGER_PROPERTY_NAME); 1607 tlsParams = new TLSParams(trustManager, null); 1608 } 1609 1610 dasConnection = new AppserverConnectionSource( 1611 AppserverConnectionSource.PROTOCOL_HTTP, 1612 serverId.getHostName(), serverId.getHostPort(), 1613 serverId.getUserName(), serverId.getPassword(), 1614 tlsParams, null); 1615 } 1616 return dasConnection; 1617 } 1618 1619 1623 private DomainRoot getRootProxy() throws IOException { 1624 if (rootProxy == null) { 1625 rootProxy = getProxyFactory().createDomainRoot(); 1626 } 1627 rootProxy.waitAMXReady(); 1628 return rootProxy; 1629 } 1630 1631 1635 private DomainConfig getDomainConfigProxy() throws IOException { 1636 if (domainConfigProxy == null) { 1637 domainConfigProxy = getRootProxy().getDomainConfig(); 1638 } 1639 return domainConfigProxy; 1640 } 1641 1642 1646 private ProxyFactory getProxyFactory() { 1647 if (proxyFactory == null) { 1648 proxyFactory = ProxyFactory.getInstance(getDasConnection()); 1649 } 1650 return proxyFactory; 1651 } 1652 1653 1677 public ProgressObject distribute(Target [] targetList, 1678 Archive moduleArchive, 1679 Archive deploymentPlan, 1680 Object deploymentOptions) 1681 throws IllegalStateException { 1682 return null; 1683 } 1684 1685 1698 public WritableArchive getArchive(URI path, String name) 1699 throws IOException 1700 { 1701 1702 if (path==null) { 1703 File root = File.createTempFile(name,".jar"); path = root.toURI(); 1706 } 1707 ArchiveFactory factory = new ArchiveFactory(); 1708 boolean exists = false; 1709 if ((path.getScheme().equals("file")) || (path.getScheme().equals("jar"))) { 1712 File target = new File (path); 1713 exists = target.exists(); 1714 } else { 1715 return null; 1716 } 1717 if (exists) { 1718 return factory.openArchive(path); 1719 } else { 1720 return factory.createArchive(path); 1721 } 1722 } 1723 1724 1744 protected class TargetModuleIDCollection { 1745 1746 private HashMap moduleIDToInfoMap = new HashMap (); 1747 1748 1749 ProgressObjectSink progressObjectSink = null; 1750 1751 1757 public TargetModuleIDCollection(TargetModuleID [] targetModuleIDs) throws IllegalArgumentException { 1758 1759 for (int i = 0; i < targetModuleIDs.length; i++) { 1760 1763 Target candidateTarget = targetModuleIDs[i].getTarget(); 1764 if ( ! (candidateTarget instanceof SunTarget)) { 1765 throw new IllegalArgumentException ( 1766 localStrings.getLocalString("enterprise.deployapi.spi.notSunTarget", "Expected SunTarget instance but found instance of {0}", new Object [] {candidateTarget.getClass().getName() } )); } 1769 SunTarget candidateSunTarget = (SunTarget) candidateTarget; 1770 String moduleID = targetModuleIDs[i].getModuleID(); 1771 1772 1775 DeploymentFacilityModuleWork work = (DeploymentFacilityModuleWork) moduleIDToInfoMap.get(moduleID); 1776 if (work == null) { 1777 1780 work = new DeploymentFacilityModuleWork(moduleID); 1781 moduleIDToInfoMap.put(moduleID, work); 1782 } 1783 1787 work.addTarget(candidateTarget); 1788 } 1789 } 1790 1791 1798 public Iterator iterator() { 1799 return moduleIDToInfoMap.values().iterator(); 1800 } 1801 1802 1808 public int size() { 1809 return moduleIDToInfoMap.size(); 1810 } 1811 1812 1817 public ProgressObjectSink getProgressObjectSink() { 1818 if (progressObjectSink == null) { 1819 progressObjectSink = new ProgressObjectSink(); 1820 } 1821 return progressObjectSink; 1822 } 1823 } 1824 1825 1832 protected class DeploymentFacilityModuleWork { 1833 1834 1835 private String moduleID = null; 1836 1837 1838 private Collection targets = new Vector (); 1839 1840 1841 private ProgressObject progressObject = null; 1842 1843 1847 public DeploymentFacilityModuleWork(String moduleID) { 1848 this.moduleID = moduleID; 1849 } 1850 1851 1855 public void addTarget(Target target) { 1856 if ( ! (target instanceof SunTarget) ) { 1857 throw new IllegalArgumentException (localStrings.getLocalString( 1858 "enterprise.deployapi.spi.unexptargettyp", 1859 "Target must be of type SunTarget but encountered {0}", 1860 new Object [] {target.getClass().getName()} 1861 )); 1862 } 1863 targets.add(target); 1864 } 1865 1866 1871 public Target [] targets() { 1872 return (Target []) targets.toArray(new SunTarget[] {}); 1873 } 1874 1875 1880 public ProgressObject getProgressObject() { 1881 return this.progressObject; 1882 } 1883 1884 1890 public void setProgressObject (ProgressObject progressObject) { 1891 this.progressObject = progressObject; 1892 } 1893 1894 1898 public String getModuleID() { 1899 return this.moduleID; 1900 } 1901 } 1902 1903 1911 private URI toJarURI(File archiveFile) throws URISyntaxException { 1912 URI archiveFileURI = archiveFile.toURI(); 1913 URI answer = new URI ("jar", "" , archiveFileURI.getSchemeSpecificPart(), null, null); return answer; 1915 } 1916 1917 1922 private DeploymentFacility getDeploymentFacility() { 1923 if (this.deploymentFacility == null) { 1924 this.deploymentFacility = DeploymentFacilityFactory.getDeploymentFacility(); 1925 this.deploymentFacility.connect(serverId); 1926 } 1927 return this.deploymentFacility; 1928 } 1929 1930 1939 private void addChildTargetModuleIDsToJ2EEUsingMBeans(SunTargetModuleID sunTargetModuleID, String moduleID, HostAndPort hostAndPort) { 1940 SunTarget sunTarget = (SunTarget) sunTargetModuleID.getTarget(); 1941 try { 1942 MBeanServerConnection mbsc = getMBeanServerConnection(); 1943 1946 ObjectName query = new ObjectName ("com.sun.appserv:category=runtime,name=" + moduleID + ",J2EEServer=" + sunTarget.getAppServerInstance() + ",*"); 1947 Set mbeans = mbsc.queryNames(query, null); 1948 1949 1953 for (Iterator itr = mbeans.iterator();itr.hasNext();) { 1954 1955 ObjectName module = (ObjectName ) itr.next(); 1956 1957 1960 String subModules[] = (String []) mbsc.getAttribute(module, "modules"); 1961 for (int i = 0; i < subModules.length; i++) { 1962 Set subModuleObjectNames = mbsc.queryNames(new ObjectName (subModules[i]), null); 1963 if (subModuleObjectNames != null) { 1964 1967 for (Iterator subItr = subModuleObjectNames.iterator(); subItr.hasNext(); ) { 1968 ObjectName aSubModule = (ObjectName ) subItr.next(); 1969 SunTargetModuleID childTmid; 1970 String aSubModuleName = aSubModule.toString(); 1971 if (aSubModuleName.indexOf("WebModule")!=-1) { 1972 String id = (String ) mbsc.getAttribute(aSubModule, "iD"); 1973 String path = (String ) mbsc.getAttribute(aSubModule, "path"); 1974 childTmid = prepareWebChildTargetModuleID(id, sunTarget, hostAndPort, path); 1975 } else { 1976 String name = (String ) mbsc.getAttribute(aSubModule, "name"); 1977 ModuleType moduleType = deriveModuleTypeFromModuleName(aSubModuleName); 1978 childTmid = prepareNonWebChildTargetModuleID(moduleID, sunTarget, name, moduleType); 1979 } 1980 1981 sunTargetModuleID.addChildTargetModuleID(childTmid); 1982 childTmid.setParentTargetModuleID(sunTargetModuleID); 1983 } 1984 } 1985 } 1986 } 1987 } 1988 catch(Exception exp){ 1989 Print.dprintStackTrace(exp.getLocalizedMessage(), exp); 1990 Print.dprint("***Exception occured while "+ 1991 "accessing mbean details: Keep continuing\n"); 1992 } 1993 } 1994 1995 private SunTargetModuleID prepareWebChildTargetModuleID(String id, SunTarget sunTarget, HostAndPort hostAndPort, String path) throws MalformedURLException { 1996 id = id.replace(':','#'); 1997 SunTargetModuleID childTmid = new SunTargetModuleID(id, sunTarget); 1998 1999 String host; 2001 if(isPE()) { 2002 host = childTmid.getConnectionInfo().getHostName(); 2003 } else { 2004 host = hostAndPort.getHost(); 2005 } 2006 2007 URL webURL = new URL ("http", host, hostAndPort.getPort(), path); 2009 childTmid.setWebURL(webURL.toExternalForm()); 2010 childTmid.setModuleType(ModuleType.WAR); 2011 return childTmid; 2012 } 2013 2014 private SunTargetModuleID prepareNonWebChildTargetModuleID(String id, SunTarget sunTarget, String subModuleName, ModuleType moduleType) { 2015 String moduleID = id + "#" + subModuleName; 2016 SunTargetModuleID childTmid = new SunTargetModuleID(moduleID, sunTarget); 2017 childTmid.setModuleType(moduleType); 2018 return childTmid; 2019 } 2020 2021 private ModuleType deriveModuleTypeFromModuleName(String moduleName) { 2022 ModuleType moduleType = null; 2023 if (moduleName.indexOf("EJBModule")!=-1) { 2024 moduleType = ModuleType.EJB; 2025 } else if (moduleName.indexOf("AppClientModule")!=-1) { 2026 moduleType = ModuleType.CAR; 2027 } else { 2028 moduleType = ModuleType.RAR; 2029 } 2030 return moduleType; 2031 } 2032 2033 2037 public boolean isPE() { 2038 boolean result; 2039 try { 2040 result = applicationsConfigMBeanIsPE(); 2041 return result; 2042 } catch (Throwable thr) { 2043 throw new RuntimeException (localStrings.getLocalString( 2044 "enterprise.deployapi.spi.errcheckingtype", 2045 "Error checking type of DAS"), thr); } 2047 } 2048 2049 private boolean applicationsConfigMBeanIsPE() throws 2050 javax.management.MalformedObjectNameException , 2051 javax.management.InstanceNotFoundException , 2052 javax.management.IntrospectionException , 2053 javax.management.ReflectionException , 2054 IOException { 2055 boolean result; 2056 MBeanServerConnection mbsc = getMBeanServerConnection(); 2057 ObjectName applicationsMBean = new ObjectName (applicationsMBeanName); 2058 MBeanInfo info = mbsc.getMBeanInfo(applicationsMBean); 2059 String className = info.getClassName(); 2060 result = ! className.endsWith(EE_APPLICATIONS_CONFIG_MBEAN_SUFFIX); 2061 return result; 2062 } 2063 2064 private ModuleType getModuleTypeFor(String moduleID) throws Exception { 2065 MBeanServerConnection mbsc = getMBeanServerConnection(); 2066 ObjectName applicationsMBean = new ObjectName (applicationsMBeanName); 2067 String [] signature = new String [] {"java.lang.String"}; 2068 Object [] params = new Object [] {moduleID}; 2069 Integer result = (Integer ) 2070 mbsc.invoke(applicationsMBean, "getModuleType", params, signature); 2071 if (result == null) { 2072 String msg = localStrings.getLocalString( 2073 "enterprise.deployapi.spi.redeploy.modulenotfound", 2074 "Module " + moduleID + " not found"); 2075 throw new IllegalArgumentException (msg); 2076 } 2077 return ModuleType.getModuleType(result.intValue()); 2078 } 2079 2080 private static final String EE_APPLICATIONS_CONFIG_MBEAN_SUFFIX = ".EEApplicationsConfigMBean"; 2081} 2082 | Popular Tags |