1 17 package org.apache.geronimo.j2ee.deployment; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.net.URI ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.LinkedHashMap ; 32 import java.util.LinkedHashSet ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 import java.util.Map ; 36 import java.util.Set ; 37 import java.util.jar.JarFile ; 38 import java.util.zip.ZipEntry ; 39 40 import javax.xml.namespace.QName ; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 import org.apache.geronimo.common.DeploymentException; 45 import org.apache.geronimo.deployment.ConfigurationBuilder; 46 import org.apache.geronimo.deployment.DeploymentContext; 47 import org.apache.geronimo.deployment.ModuleIDBuilder; 48 import org.apache.geronimo.deployment.NamespaceDrivenBuilder; 49 import org.apache.geronimo.deployment.NamespaceDrivenBuilderCollection; 50 import org.apache.geronimo.deployment.service.EnvironmentBuilder; 51 import org.apache.geronimo.deployment.service.GBeanBuilder; 52 import org.apache.geronimo.deployment.util.DeploymentUtil; 53 import org.apache.geronimo.deployment.util.NestedJarFile; 54 import org.apache.geronimo.deployment.xbeans.ArtifactType; 55 import org.apache.geronimo.deployment.xbeans.EnvironmentType; 56 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil; 57 import org.apache.geronimo.gbean.AbstractName; 58 import org.apache.geronimo.gbean.AbstractNameQuery; 59 import org.apache.geronimo.gbean.GBeanData; 60 import org.apache.geronimo.gbean.GBeanInfo; 61 import org.apache.geronimo.gbean.GBeanInfoBuilder; 62 import org.apache.geronimo.gbean.ReferencePatterns; 63 import org.apache.geronimo.gbean.SingleElementCollection; 64 import org.apache.geronimo.j2ee.ApplicationInfo; 65 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 66 import org.apache.geronimo.j2ee.management.impl.J2EEApplicationImpl; 67 import org.apache.geronimo.kernel.GBeanAlreadyExistsException; 68 import org.apache.geronimo.kernel.Kernel; 69 import org.apache.geronimo.kernel.Naming; 70 import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException; 71 import org.apache.geronimo.kernel.config.ConfigurationData; 72 import org.apache.geronimo.kernel.config.ConfigurationManager; 73 import org.apache.geronimo.kernel.config.ConfigurationModuleType; 74 import org.apache.geronimo.kernel.config.ConfigurationStore; 75 import org.apache.geronimo.kernel.config.ConfigurationUtil; 76 import org.apache.geronimo.kernel.config.SimpleConfigurationManager; 77 import org.apache.geronimo.kernel.repository.Artifact; 78 import org.apache.geronimo.kernel.repository.ArtifactResolver; 79 import org.apache.geronimo.kernel.repository.Environment; 80 import org.apache.geronimo.kernel.repository.Repository; 81 import org.apache.geronimo.management.J2EEResource; 82 import org.apache.geronimo.management.J2EEServer; 83 import org.apache.geronimo.schema.SchemaConversionUtils; 84 import org.apache.geronimo.xbeans.geronimo.j2ee.GerApplicationDocument; 85 import org.apache.geronimo.xbeans.geronimo.j2ee.GerApplicationType; 86 import org.apache.geronimo.xbeans.geronimo.j2ee.GerExtModuleType; 87 import org.apache.geronimo.xbeans.geronimo.j2ee.GerModuleType; 88 import org.apache.geronimo.xbeans.geronimo.j2ee.GerSecurityDocument; 89 import org.apache.geronimo.xbeans.j2ee.ApplicationType; 90 import org.apache.geronimo.xbeans.j2ee.ModuleType; 91 import org.apache.geronimo.xbeans.j2ee.ApplicationDocument; 92 import org.apache.xmlbeans.XmlException; 93 import org.apache.xmlbeans.XmlObject; 94 import org.apache.xmlbeans.XmlCursor; 95 96 99 public class EARConfigBuilder implements ConfigurationBuilder, CorbaGBeanNameSource { 100 101 private static final Log log = LogFactory.getLog(EARConfigBuilder.class); 102 private static final String LINE_SEP = System.getProperty("line.separator"); 103 104 private final static QName APPLICATION_QNAME = GerApplicationDocument.type.getDocumentElementName(); 105 106 private final ConfigurationManager configurationManager; 107 private final Collection repositories; 108 private final SingleElementCollection ejbConfigBuilder; 109 private final SingleElementCollection webConfigBuilder; 110 private final SingleElementCollection connectorConfigBuilder; 111 private final SingleElementCollection appClientConfigBuilder; 112 private final SingleElementCollection resourceReferenceBuilder; 113 private final NamespaceDrivenBuilderCollection securityBuilders; 114 private final NamespaceDrivenBuilderCollection serviceBuilders; 115 116 private final Environment defaultEnvironment; 117 private final AbstractNameQuery serverName; 118 private final AbstractNameQuery transactionManagerObjectName; 119 private final AbstractNameQuery connectionTrackerObjectName; 120 private final AbstractNameQuery transactionalTimerObjectName; 121 private final AbstractNameQuery nonTransactionalTimerObjectName; 122 private final AbstractNameQuery corbaGBeanObjectName; 123 private final Naming naming; 124 125 public EARConfigBuilder(Environment defaultEnvironment, 126 AbstractNameQuery transactionManagerAbstractName, 127 AbstractNameQuery connectionTrackerAbstractName, 128 AbstractNameQuery transactionalTimerAbstractName, 129 AbstractNameQuery nonTransactionalTimerAbstractName, 130 AbstractNameQuery corbaGBeanAbstractName, 131 AbstractNameQuery serverName, 132 Collection repositories, 133 Collection ejbConfigBuilder, 134 Collection webConfigBuilder, 135 Collection connectorConfigBuilder, 136 Collection resourceReferenceBuilder, 137 Collection appClientConfigBuilder, 138 Collection securityBuilders, 139 Collection serviceBuilders, 140 Kernel kernel) { 141 this(defaultEnvironment, 142 transactionManagerAbstractName, 143 connectionTrackerAbstractName, 144 transactionalTimerAbstractName, 145 nonTransactionalTimerAbstractName, 146 corbaGBeanAbstractName, 147 serverName, 148 ConfigurationUtil.getConfigurationManager(kernel), 149 repositories, 150 new SingleElementCollection(ejbConfigBuilder), 151 new SingleElementCollection(webConfigBuilder), 152 new SingleElementCollection(connectorConfigBuilder), 153 new SingleElementCollection(resourceReferenceBuilder), 154 new SingleElementCollection(appClientConfigBuilder), 155 securityBuilders, 156 serviceBuilders, 157 kernel.getNaming()); 158 } 159 public EARConfigBuilder(Environment defaultEnvironment, 160 AbstractNameQuery transactionManagerAbstractName, 161 AbstractNameQuery connectionTrackerAbstractName, 162 AbstractNameQuery transactionalTimerAbstractName, 163 AbstractNameQuery nonTransactionalTimerAbstractName, 164 AbstractNameQuery corbaGBeanAbstractName, 165 AbstractNameQuery serverName, 166 Collection repositories, 167 ModuleBuilder ejbConfigBuilder, 168 ModuleBuilder webConfigBuilder, 169 ModuleBuilder connectorConfigBuilder, 170 ActivationSpecInfoLocator activationSpecInfoLocator, 171 ModuleBuilder appClientConfigBuilder, 172 NamespaceDrivenBuilder securityBuilder, 173 NamespaceDrivenBuilder serviceBuilder, 174 Naming naming) { 175 this(defaultEnvironment, 176 transactionManagerAbstractName, 177 connectionTrackerAbstractName, 178 transactionalTimerAbstractName, 179 nonTransactionalTimerAbstractName, 180 corbaGBeanAbstractName, 181 serverName, 182 null, 183 repositories, 184 new SingleElementCollection(ejbConfigBuilder), 185 new SingleElementCollection(webConfigBuilder), 186 new SingleElementCollection(connectorConfigBuilder), 187 new SingleElementCollection(activationSpecInfoLocator), 188 new SingleElementCollection(appClientConfigBuilder), 189 securityBuilder == null? Collections.EMPTY_SET: Collections.singleton(securityBuilder), 190 serviceBuilder == null? Collections.EMPTY_SET: Collections.singleton(serviceBuilder), 191 naming); 192 } 193 194 private EARConfigBuilder(Environment defaultEnvironment, 195 AbstractNameQuery transactionManagerAbstractName, 196 AbstractNameQuery connectionTrackerAbstractName, 197 AbstractNameQuery transactionalTimerAbstractName, 198 AbstractNameQuery nonTransactionalTimerAbstractName, 199 AbstractNameQuery corbaGBeanAbstractName, 200 AbstractNameQuery serverName, 201 ConfigurationManager configurationManager, 202 Collection repositories, 203 SingleElementCollection ejbConfigBuilder, 204 SingleElementCollection webConfigBuilder, 205 SingleElementCollection connectorConfigBuilder, 206 SingleElementCollection resourceReferenceBuilder, 207 SingleElementCollection appClientConfigBuilder, 208 Collection securityBuilders, 209 Collection serviceBuilders, 210 Naming naming) { 211 this.configurationManager = configurationManager; 212 this.repositories = repositories; 213 this.defaultEnvironment = defaultEnvironment; 214 215 this.ejbConfigBuilder = ejbConfigBuilder; 216 this.resourceReferenceBuilder = resourceReferenceBuilder; 217 this.webConfigBuilder = webConfigBuilder; 218 this.connectorConfigBuilder = connectorConfigBuilder; 219 this.appClientConfigBuilder = appClientConfigBuilder; 220 this.securityBuilders = new NamespaceDrivenBuilderCollection(securityBuilders, GerSecurityDocument.type.getDocumentElementName()); 221 this.serviceBuilders = new NamespaceDrivenBuilderCollection(serviceBuilders, GBeanBuilder.SERVICE_QNAME); 222 223 this.transactionManagerObjectName = transactionManagerAbstractName; 224 this.connectionTrackerObjectName = connectionTrackerAbstractName; 225 this.transactionalTimerObjectName = transactionalTimerAbstractName; 226 this.nonTransactionalTimerObjectName = nonTransactionalTimerAbstractName; 227 this.corbaGBeanObjectName = corbaGBeanAbstractName; 228 this.serverName = serverName; 229 this.naming = naming; 230 } 231 232 233 public AbstractNameQuery getCorbaGBeanName() { 234 return corbaGBeanObjectName; 235 } 236 237 private ModuleBuilder getEjbConfigBuilder() { 238 return (ModuleBuilder) ejbConfigBuilder.getElement(); 239 } 240 241 private ModuleBuilder getWebConfigBuilder() { 242 return (ModuleBuilder) webConfigBuilder.getElement(); 243 } 244 245 private ModuleBuilder getConnectorConfigBuilder() { 246 return (ModuleBuilder) connectorConfigBuilder.getElement(); 247 } 248 249 private ModuleBuilder getAppClientConfigBuilder() { 250 return (ModuleBuilder) appClientConfigBuilder.getElement(); 251 } 252 253 private ActivationSpecInfoLocator getResourceReferenceBuilder() { 254 return (ActivationSpecInfoLocator) resourceReferenceBuilder.getElement(); 255 } 256 257 public Object getDeploymentPlan(File planFile, JarFile jarFile, ModuleIDBuilder idBuilder) throws DeploymentException { 258 if (planFile == null && jarFile == null) { 259 return null; 260 } 261 ApplicationInfo plan = getEarPlan(planFile, jarFile, idBuilder); 262 if (plan != null) { 263 return plan; 264 } 265 if (jarFile == null) { 267 return null; 268 } 269 270 Module module = null; 272 if (getWebConfigBuilder() != null) { 273 module = getWebConfigBuilder().createModule(planFile, jarFile, naming, idBuilder); 274 } 275 if (module == null && getEjbConfigBuilder() != null) { 276 module = getEjbConfigBuilder().createModule(planFile, jarFile, naming, idBuilder); 277 } 278 if (module == null && getConnectorConfigBuilder() != null) { 279 module = getConnectorConfigBuilder().createModule(planFile, jarFile, naming, idBuilder); 280 } 281 if (module == null && getAppClientConfigBuilder() != null) { 282 module = getAppClientConfigBuilder().createModule(planFile, jarFile, naming, idBuilder); 283 } 284 if (module == null) { 285 return null; 286 } 287 288 return new ApplicationInfo(module.getType(), 289 module.getEnvironment(), 290 module.getModuleName(), 291 null, 292 null, 293 new LinkedHashSet (Collections.singleton(module)), 294 Collections.EMPTY_SET, 295 null); 296 } 297 298 private ApplicationInfo getEarPlan(File planFile, JarFile earFile, ModuleIDBuilder idBuilder) throws DeploymentException { 299 String specDD; 300 ApplicationType application = null; 301 if (earFile != null) { 302 try { 303 URL applicationXmlUrl = DeploymentUtil.createJarURL(earFile, "META-INF/application.xml"); 304 specDD = DeploymentUtil.readAll(applicationXmlUrl); 305 } catch (Exception e) { 306 return null; 308 } 309 try { 311 XmlObject xmlObject = XmlBeansUtil.parse(specDD); 312 application = convertToApplicationSchema(xmlObject).getApplication(); 313 } catch (XmlException e) { 314 throw new DeploymentException("Could not parse application.xml", e); 315 } 316 } 317 318 GerApplicationType gerApplication = null; 319 try { 320 XmlObject rawPlan; 322 try { 323 if (planFile != null) { 324 rawPlan = XmlBeansUtil.parse(planFile.toURL(), getClass().getClassLoader()); 325 gerApplication = (GerApplicationType) SchemaConversionUtils.fixGeronimoSchema(rawPlan, APPLICATION_QNAME, GerApplicationType.type); 326 if (gerApplication == null) { 327 return null; 328 } 329 } else { 330 URL path = DeploymentUtil.createJarURL(earFile, "META-INF/geronimo-application.xml"); 331 rawPlan = XmlBeansUtil.parse(path, getClass().getClassLoader()); 332 gerApplication = (GerApplicationType) SchemaConversionUtils.fixGeronimoSchema(rawPlan, APPLICATION_QNAME, GerApplicationType.type); 333 } 334 } catch (IOException e) { 335 } 337 338 if (gerApplication == null) { 340 gerApplication = createDefaultPlan(application, earFile); 341 } 342 } catch (XmlException e) { 343 throw new DeploymentException(e); 344 } 345 346 EnvironmentType environmentType = gerApplication.getEnvironment(); 347 Environment environment = EnvironmentBuilder.buildEnvironment(environmentType, defaultEnvironment); 348 idBuilder.resolve(environment, earFile == null ? planFile.getName() : new File (earFile.getName()).getName(), "ear"); 349 idBuilder.setDefaultGroup(environment.getConfigId().getGroupId()); 351 idBuilder.setDefaultVersion(environment.getConfigId().getVersion()); 352 353 Artifact artifact = environment.getConfigId(); 354 AbstractName earName = naming.createRootName(artifact, artifact.toString(), NameFactory.J2EE_APPLICATION); 355 356 Set moduleLocations = new HashSet (); 360 LinkedHashSet modules = new LinkedHashSet (); 361 try { 362 addModules(earFile, application, gerApplication, moduleLocations, modules, environment, earName, idBuilder); 363 } catch (Throwable e) { 364 for (Iterator iterator = modules.iterator(); iterator.hasNext();) { 366 Module module = (Module) iterator.next(); 367 module.close(); 368 } 369 370 if (e instanceof DeploymentException) { 371 throw (DeploymentException) e; 372 } else if (e instanceof RuntimeException ) { 373 throw (RuntimeException ) e; 374 } else if (e instanceof Error ) { 375 throw (Error ) e; 376 } 377 throw new DeploymentException(e); 378 } 379 380 return new ApplicationInfo(ConfigurationModuleType.EAR, 381 environment, 382 earName, 383 application, 384 gerApplication, 385 modules, 386 moduleLocations, 387 application == null ? null : application.toString()); 388 } 389 390 391 private GerApplicationType createDefaultPlan(ApplicationType application, JarFile module) { 392 GerApplicationType gerApplication = GerApplicationType.Factory.newInstance(); 394 EnvironmentType environmentType = gerApplication.addNewEnvironment(); 395 ArtifactType artifactType = environmentType.addNewModuleId(); 396 397 artifactType.setGroupId(Artifact.DEFAULT_GROUP_ID); 398 399 String id = application.getId(); 401 if (id == null) { 402 File fileName = new File (module.getName()); 403 id = fileName.getName(); 404 if (id.endsWith(".ear")) { 405 id = id.substring(0, id.length() - 4); 406 } 407 if (id.endsWith("/")) { 408 id = id.substring(0, id.length() - 1); 409 } 410 } 411 412 artifactType.setArtifactId(id); 413 artifactType.setVersion("" + System.currentTimeMillis()); 414 artifactType.setType("car"); 415 return gerApplication; 416 } 417 418 static ApplicationDocument convertToApplicationSchema(XmlObject xmlObject) throws XmlException { 419 if (ApplicationDocument.type.equals(xmlObject.schemaType())) { 420 XmlBeansUtil.validateDD(xmlObject); 421 return (ApplicationDocument) xmlObject; 422 } 423 XmlCursor cursor = xmlObject.newCursor(); 424 XmlCursor moveable = xmlObject.newCursor(); 425 String schemaLocationURL = "http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"; 426 String version = "1.4"; 427 try { 428 SchemaConversionUtils.convertToSchema(cursor, SchemaConversionUtils.J2EE_NAMESPACE, schemaLocationURL, version); 429 cursor.toStartDoc(); 430 cursor.toChild(SchemaConversionUtils.J2EE_NAMESPACE, "application"); 431 cursor.toFirstChild(); 432 SchemaConversionUtils.convertToDescriptionGroup(SchemaConversionUtils.J2EE_NAMESPACE, cursor, moveable); 433 } finally { 434 cursor.dispose(); 435 moveable.dispose(); 436 } 437 XmlObject result = xmlObject.changeType(ApplicationDocument.type); 438 if (result != null) { 439 XmlBeansUtil.validateDD(result); 440 return (ApplicationDocument) result; 441 } 442 XmlBeansUtil.validateDD(xmlObject); 443 return (ApplicationDocument) xmlObject; 444 } 445 446 public Artifact getConfigurationID(Object plan, JarFile module, ModuleIDBuilder idBuilder) throws IOException , DeploymentException { 447 ApplicationInfo applicationInfo = (ApplicationInfo) plan; 448 Artifact test = applicationInfo.getEnvironment().getConfigId(); 449 if(!test.isResolved()) { 450 throw new IllegalStateException ("Module ID should be fully resolved by now (not "+test+")"); 451 } 452 return test; 453 } 454 455 public DeploymentContext buildConfiguration(boolean inPlaceDeployment, Artifact configId, Object plan, JarFile earFile, Collection configurationStores, ArtifactResolver artifactResolver, ConfigurationStore targetConfigurationStore) throws IOException , DeploymentException { 456 assert plan != null; 457 ApplicationInfo applicationInfo = (ApplicationInfo) plan; 458 459 EARContext earContext = null; 460 ConfigurationModuleType applicationType = applicationInfo.getType(); 461 applicationInfo.getEnvironment().setConfigId(configId); 462 File configurationDir; 463 try { 464 configurationDir = targetConfigurationStore.createNewConfigurationDir(configId); 465 } catch (ConfigurationAlreadyExistsException e) { 466 throw new DeploymentException(e); 467 } 468 469 ConfigurationManager configurationManager = this.configurationManager; 470 if (configurationManager == null) { 471 configurationManager = new SimpleConfigurationManager(configurationStores, artifactResolver, repositories); 472 } 473 try { 474 earContext = new EARContext(configurationDir, 476 inPlaceDeployment ? DeploymentUtil.toFile(earFile) : null, 477 applicationInfo.getEnvironment(), 478 applicationType, 479 naming, 480 configurationManager, 481 repositories, 482 serverName, 483 applicationInfo.getBaseName(), 484 transactionManagerObjectName, 485 connectionTrackerObjectName, 486 transactionalTimerObjectName, 487 nonTransactionalTimerObjectName, 488 corbaGBeanObjectName 489 ); 490 491 Set moduleLocations = applicationInfo.getModuleLocations(); 493 if (ConfigurationModuleType.EAR == applicationType && earFile != null) { 494 for (Enumeration e = earFile.entries(); e.hasMoreElements();) { 495 ZipEntry entry = (ZipEntry ) e.nextElement(); 496 String entryName = entry.getName(); 497 boolean addEntry = true; 498 for (Iterator iter = moduleLocations.iterator(); iter.hasNext();) { 499 String location = (String ) iter.next(); 500 if (entryName.startsWith(location)) { 501 addEntry = false; 502 break; 503 } 504 } 505 if (addEntry) { 506 earContext.addFile(URI.create(entry.getName()), earFile, entry); 507 } 508 } 509 } 510 511 GerApplicationType geronimoApplication = (GerApplicationType) applicationInfo.getVendorDD(); 512 513 LinkedHashSet modules = applicationInfo.getModules(); 515 for (Iterator iterator = modules.iterator(); iterator.hasNext();) { 516 Module module = (Module) iterator.next(); 517 getBuilder(module).installModule(earFile, earContext, module, configurationStores, targetConfigurationStore, repositories); 518 } 519 520 earContext.flush(); 521 522 ClassLoader cl = earContext.getClassLoader(); 524 for (Iterator iterator = modules.iterator(); iterator.hasNext();) { 525 Module module = (Module) iterator.next(); 526 getBuilder(module).initContext(earContext, module, cl); 527 } 528 529 if (geronimoApplication != null) { 531 securityBuilders.build(geronimoApplication, earContext, earContext); 532 serviceBuilders.build(geronimoApplication, earContext, earContext); 533 } 534 535 if (ConfigurationModuleType.EAR == applicationType) { 537 GBeanData gbeanData = new GBeanData(earContext.getModuleName(), J2EEApplicationImpl.GBEAN_INFO); 538 try { 539 String originalSpecDD = applicationInfo.getOriginalSpecDD(); 540 if (originalSpecDD == null) { 541 originalSpecDD = "Synthetic EAR"; 542 } 543 gbeanData.setAttribute("deploymentDescriptor", originalSpecDD); 544 } catch (Exception e) { 545 throw new DeploymentException("Error initializing J2EEApplication managed object"); 546 } 547 gbeanData.setReferencePatterns("Server", new ReferencePatterns(new AbstractNameQuery(J2EEServer.class.getName()))); 548 549 Map thisApp = Collections.singletonMap(NameFactory.J2EE_APPLICATION, earContext.getModuleName().getNameProperty(NameFactory.J2EE_NAME)); 550 LinkedHashSet resourcePatterns = new LinkedHashSet (); 551 resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JAVA_MAIL_RESOURCE), J2EEResource.class.getName())); 552 resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JCA_CONNECTION_FACTORY), J2EEResource.class.getName())); 553 resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JDBC_RESOURCE), J2EEResource.class.getName())); 554 resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JDBC_DRIVER), J2EEResource.class.getName())); 555 resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JMS_RESOURCE), J2EEResource.class.getName())); 556 resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JNDI_RESOURCE), J2EEResource.class.getName())); 557 resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JTA_RESOURCE), J2EEResource.class.getName())); 558 resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.RMI_IIOP_RESOURCE), J2EEResource.class.getName())); 559 resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.URL_RESOURCE), J2EEResource.class.getName())); 560 gbeanData.setReferencePatterns("Resources", resourcePatterns); 561 562 gbeanData.setReferencePatterns("AppClientModules", new ReferencePatterns(new AbstractNameQuery(null, thisApp, org.apache.geronimo.management.AppClientModule.class.getName()))); 563 gbeanData.setReferencePatterns("EJBModules", new ReferencePatterns(new AbstractNameQuery(null, thisApp, org.apache.geronimo.management.EJBModule.class.getName()))); 564 gbeanData.setReferencePatterns("ResourceAdapterModules", new ReferencePatterns(new AbstractNameQuery(null, thisApp, org.apache.geronimo.management.geronimo.ResourceAdapterModule.class.getName()))); 565 gbeanData.setReferencePatterns("WebModules", new ReferencePatterns(new AbstractNameQuery(null, thisApp, org.apache.geronimo.management.geronimo.WebModule.class.getName()))); 566 earContext.addGBean(gbeanData); 567 } 568 569 for (Iterator iterator = modules.iterator(); iterator.hasNext();) { 571 Module module = (Module) iterator.next(); 572 getBuilder(module).addGBeans(earContext, module, cl, repositories); 573 } 574 575 return earContext; 577 } catch (GBeanAlreadyExistsException e) { 578 cleanupContext(earContext, configurationDir); 579 throw new DeploymentException(e); 580 } catch (IOException e) { 581 cleanupContext(earContext, configurationDir); 582 throw e; 583 } catch (DeploymentException e) { 584 cleanupContext(earContext, configurationDir); 585 throw e; 586 } catch(RuntimeException e) { 587 cleanupContext(earContext, configurationDir); 588 throw e; 589 } catch(Error e) { 590 cleanupContext(earContext, configurationDir); 591 throw e; 592 } finally { 593 Set modules = applicationInfo.getModules(); 594 for (Iterator iterator = modules.iterator(); iterator.hasNext();) { 595 Module module = (Module) iterator.next(); 596 module.close(); 597 } 598 } 599 } 600 601 private void cleanupContext(EARContext earContext, File configurationDir) { 602 List configurations = new ArrayList (); 603 if (earContext != null) { 604 configurations.addAll(earContext.getAdditionalDeployment()); 605 try { 606 earContext.close(); 607 } catch (IOException ioe) { 608 } catch (DeploymentException de) { 610 } 612 } 613 if (configurationDir != null) { 615 cleanupConfigurationDir(configurationDir); 616 } 617 for (Iterator iterator = configurations.iterator(); iterator.hasNext();) { 619 ConfigurationData configurationData = (ConfigurationData) iterator.next(); 620 cleanupConfigurationDir(configurationData.getConfigurationDir()); 621 } 622 } 623 624 private boolean cleanupConfigurationDir(File configurationDir) 625 { 626 LinkedList cannotBeDeletedList = new LinkedList (); 627 628 if (!DeploymentUtil.recursiveDelete(configurationDir,cannotBeDeletedList)) { 629 log.warn("Unable to delete " + cannotBeDeletedList.size() + 631 " files while recursively deleting directory " 632 + configurationDir + LINE_SEP + 633 "The first file that could not be deleted was:" + LINE_SEP + " "+ 634 ( !cannotBeDeletedList.isEmpty() ? cannotBeDeletedList.getFirst() : "") ); 635 return false; 636 } 637 return true; 638 } 639 640 private static Map filter(Map original, String key, String value) { 641 LinkedHashMap filter = new LinkedHashMap (original); 642 filter.put(key, value); 643 return filter; 644 } 645 646 private void addModules(JarFile earFile, ApplicationType application, GerApplicationType gerApplication, Set moduleLocations, LinkedHashSet modules, Environment environment, AbstractName earName, ModuleIDBuilder idBuilder) throws DeploymentException { 647 Map altVendorDDs = new HashMap (); 648 try { 649 if (earFile != null) { 650 ModuleType[] moduleTypes = application.getModuleArray(); 651 Set paths = new HashSet (); 653 for (int i = 0; i < moduleTypes.length; i++) { 654 ModuleType type = moduleTypes[i]; 655 if (type.isSetEjb()) { 656 paths.add(type.getEjb().getStringValue()); 657 } else if (type.isSetWeb()) { 658 paths.add(type.getWeb().getWebUri().getStringValue()); 659 } else if (type.isSetConnector()) { 660 paths.add(type.getConnector().getStringValue()); 661 } else if (type.isSetJava()) { 662 paths.add(type.getJava().getStringValue()); 663 } 664 } 665 666 GerModuleType gerModuleTypes[] = gerApplication.getModuleArray(); 668 for (int i = 0; i < gerModuleTypes.length; i++) { 669 GerModuleType gerModule = gerModuleTypes[i]; 670 String path = null; 671 if (gerModule.isSetEjb()) { 672 path = gerModule.getEjb().getStringValue(); 673 } else if (gerModule.isSetWeb()) { 674 path = gerModule.getWeb().getStringValue(); 675 } else if (gerModule.isSetConnector()) { 676 path = gerModule.getConnector().getStringValue(); 677 } else if (gerModule.isSetJava()) { 678 path = gerModule.getJava().getStringValue(); 679 } 680 if (!paths.contains(path)) { 681 throw new DeploymentException("Geronimo deployment plan refers to module '" + path + "' but that was not defined in the META-INF/application.xml"); 682 } 683 684 if (gerModule.isSetAltDd()) { 685 try { 687 altVendorDDs.put(path, DeploymentUtil.toTempFile(earFile, gerModule.getAltDd().getStringValue())); 688 } catch (IOException e) { 689 throw new DeploymentException("Invalid alt vendor dd url: " + gerModule.getAltDd().getStringValue(), e); 690 } 691 } else { 692 XmlObject[] anys = gerModule.selectChildren(GerModuleType.type.qnameSetForWildcardElements()); 694 if (anys.length != 1) { 695 throw new DeploymentException("Unexpected count of xs:any elements in embedded vendor plan " + anys.length + " qnameset: " + GerModuleType.type.qnameSetForWildcardElements()); 696 } 697 altVendorDDs.put(path, anys[0]); 698 } 699 } 700 701 for (int i = 0; i < moduleTypes.length; i++) { 703 ModuleType moduleXml = moduleTypes[i]; 704 705 String modulePath; 706 ModuleBuilder builder; 707 708 Object moduleContextInfo = null; 709 String moduleTypeName; 710 if (moduleXml.isSetEjb()) { 711 modulePath = moduleXml.getEjb().getStringValue(); 712 builder = getEjbConfigBuilder(); 713 if (builder == null) { 714 throw new DeploymentException("Cannot deploy ejb application; No ejb deployer defined: " + modulePath); 715 } 716 moduleTypeName = "an EJB"; 717 } else if (moduleXml.isSetWeb()) { 718 modulePath = moduleXml.getWeb().getWebUri().getStringValue(); 719 if (getWebConfigBuilder() == null) { 720 throw new DeploymentException("Cannot deploy web application; No war deployer defined: " + modulePath); 721 } 722 builder = getWebConfigBuilder(); 723 moduleTypeName = "a war"; 724 moduleContextInfo = moduleXml.getWeb().getContextRoot().getStringValue().trim(); 725 } else if (moduleXml.isSetConnector()) { 726 modulePath = moduleXml.getConnector().getStringValue(); 727 if (getConnectorConfigBuilder() == null) { 728 throw new DeploymentException("Cannot deploy resource adapter; No rar deployer defined: " + modulePath); 729 } 730 builder = getConnectorConfigBuilder(); 731 moduleTypeName = "a connector"; 732 } else if (moduleXml.isSetJava()) { 733 modulePath = moduleXml.getJava().getStringValue(); 734 if (getAppClientConfigBuilder() == null) { 735 throw new DeploymentException("Cannot deploy app client; No app client deployer defined: " + modulePath); 736 } 737 builder = getAppClientConfigBuilder(); 738 moduleTypeName = "an application client"; 739 } else { 740 throw new DeploymentException("Could not find a module builder for module: " + moduleXml); 741 } 742 743 moduleLocations.add(modulePath); 744 745 URL altSpecDD = null; 746 if (moduleXml.isSetAltDd()) { 747 try { 748 altSpecDD = DeploymentUtil.createJarURL(earFile, moduleXml.getAltDd().getStringValue()); 749 } catch (MalformedURLException e) { 750 throw new DeploymentException("Invalid alt sped dd url: " + moduleXml.getAltDd().getStringValue(), e); 751 } 752 } 753 754 NestedJarFile moduleFile; 755 try { 756 moduleFile = new NestedJarFile(earFile, modulePath); 757 } catch (IOException e) { 758 throw new DeploymentException("Invalid moduleFile: " + modulePath, e); 759 } 760 761 Module module = builder.createModule(altVendorDDs.get(modulePath), 762 moduleFile, 763 modulePath, 764 altSpecDD, 765 environment, 766 moduleContextInfo, 767 earName, 768 naming, idBuilder); 769 770 if (module == null) { 771 throw new DeploymentException("Module was not " + moduleTypeName + ": " + modulePath); 772 } 773 774 modules.add(module); 775 } 776 } 777 778 GerExtModuleType gerExtModuleTypes[] = gerApplication.getExtModuleArray(); 780 for (int i = 0; i < gerExtModuleTypes.length; i++) { 781 GerExtModuleType gerExtModule = gerExtModuleTypes[i]; 782 String moduleName; 783 ModuleBuilder builder; 784 Object moduleContextInfo = null; 785 String moduleTypeName; 786 787 if (gerExtModule.isSetEjb()) { 788 moduleName = gerExtModule.getEjb().getStringValue(); 789 builder = getEjbConfigBuilder(); 790 if (builder == null) { 791 throw new DeploymentException("Cannot deploy ejb application; No ejb deployer defined: " + moduleName); 792 } 793 moduleTypeName = "an EJB"; 794 } else if (gerExtModule.isSetWeb()) { 795 moduleName = gerExtModule.getWeb().getStringValue(); 796 if (getWebConfigBuilder() == null) { 797 throw new DeploymentException("Cannot deploy web application; No war deployer defined: " + moduleName); 798 } 799 builder = getWebConfigBuilder(); 800 moduleTypeName = "a war"; 801 } else if (gerExtModule.isSetConnector()) { 804 moduleName = gerExtModule.getConnector().getStringValue(); 805 if (getConnectorConfigBuilder() == null) { 806 throw new DeploymentException("Cannot deploy resource adapter; No rar deployer defined: " + moduleName); 807 } 808 builder = getConnectorConfigBuilder(); 809 moduleTypeName = "a connector"; 810 } else if (gerExtModule.isSetJava()) { 811 moduleName = gerExtModule.getJava().getStringValue(); 812 if (getAppClientConfigBuilder() == null) { 813 throw new DeploymentException("Cannot deploy app client; No app client deployer defined: " + moduleName); 814 } 815 builder = getAppClientConfigBuilder(); 816 moduleTypeName = "an application client"; 817 } else { 818 throw new DeploymentException("Could not find a module builder for module: " + gerExtModule); 819 } 820 XmlObject[] anys = gerExtModule.selectChildren(GerExtModuleType.type.qnameSetForWildcardElements()); 822 if (anys.length != 1) { 823 throw new DeploymentException("Unexpected count of xs:any elements in embedded vendor plan " + anys.length + " qnameset: " + GerExtModuleType.type.qnameSetForWildcardElements()); 824 } 825 Object vendorDD = anys[0]; 826 827 JarFile moduleFile; 828 if (gerExtModule.isSetInternalPath()) { 829 String modulePath = gerExtModule.getInternalPath().trim(); 830 moduleLocations.add(modulePath); 831 try { 832 moduleFile = new NestedJarFile(earFile, modulePath); 833 } catch (IOException e) { 834 throw new DeploymentException("Invalid moduleFile: " + modulePath, e); 835 } 836 } else { 837 String path = gerExtModule.getExternalPath().trim(); 838 Artifact artifact = Artifact.create(path); 839 File location = null; 840 for (Iterator iterator = repositories.iterator(); iterator.hasNext();) { 841 Repository repository = (Repository) iterator.next(); 842 if (repository.contains(artifact)) { 843 location = repository.getLocation(artifact); 844 break; 845 } 846 } 847 if (location == null) { 848 throw new DeploymentException(moduleTypeName + " is missing in repositories: " + path); 849 } 850 try { 851 moduleFile = new JarFile (location); 852 } catch (IOException e) { 853 throw new DeploymentException("Could not access contents of " + moduleTypeName, e); 854 } 855 856 } 857 858 859 URL altSpecDD = null; 860 869 870 Module module = builder.createModule(vendorDD, 871 moduleFile, 872 moduleName, 873 altSpecDD, 874 environment, 875 moduleContextInfo, 876 earName, 877 naming, idBuilder); 878 879 if (module == null) { 880 throw new DeploymentException("Module was not " + moduleTypeName + ": " + moduleName); 881 } 882 883 modules.add(module); 884 } 885 886 887 } finally { 888 for (Iterator iterator = altVendorDDs.values().iterator(); iterator.hasNext();) { 890 Object altVendorDD = iterator.next(); 891 if (altVendorDD instanceof File ) { 892 ((File ) altVendorDD).delete(); 893 } 894 } 895 } 896 } 897 898 private ModuleBuilder getBuilder(Module module) throws DeploymentException { 899 if (module instanceof EJBModule) { 900 if (getEjbConfigBuilder() == null) { 901 throw new DeploymentException("Cannot deploy ejb application; No ejb deployer defined: " + module.getModuleURI()); 902 } 903 return getEjbConfigBuilder(); 904 } else if (module instanceof WebModule) { 905 if (getWebConfigBuilder() == null) { 906 throw new DeploymentException("Cannot deploy web application; No war deployer defined: " + module.getModuleURI()); 907 } 908 return getWebConfigBuilder(); 909 } else if (module instanceof ConnectorModule) { 910 if (getConnectorConfigBuilder() == null) { 911 throw new DeploymentException("Cannot deploy resource adapter; No rar deployer defined: " + module.getModuleURI()); 912 } 913 return getConnectorConfigBuilder(); 914 } else if (module instanceof AppClientModule) { 915 if (getAppClientConfigBuilder() == null) { 916 throw new DeploymentException("Cannot deploy app client; No app client deployer defined: " + module.getModuleURI()); 917 } 918 return getAppClientConfigBuilder(); 919 } 920 throw new IllegalArgumentException ("Unknown module type: " + module.getClass().getName()); 921 } 922 923 public static final GBeanInfo GBEAN_INFO; 924 925 static { 926 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(EARConfigBuilder.class, NameFactory.CONFIG_BUILDER); 927 infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true); 928 infoBuilder.addAttribute("transactionManagerAbstractName", AbstractNameQuery.class, true); 929 infoBuilder.addAttribute("connectionTrackerAbstractName", AbstractNameQuery.class, true); 930 infoBuilder.addAttribute("transactionalTimerAbstractName", AbstractNameQuery.class, true); 931 infoBuilder.addAttribute("nonTransactionalTimerAbstractName", AbstractNameQuery.class, true); 932 infoBuilder.addAttribute("corbaGBeanAbstractName", AbstractNameQuery.class, true); 933 infoBuilder.addAttribute("serverName", AbstractNameQuery.class, true); 934 935 infoBuilder.addReference("Repositories", Repository.class, "Repository"); 936 infoBuilder.addReference("EJBConfigBuilder", ModuleBuilder.class, NameFactory.MODULE_BUILDER); 937 infoBuilder.addReference("WebConfigBuilder", ModuleBuilder.class, NameFactory.MODULE_BUILDER); 938 infoBuilder.addReference("ConnectorConfigBuilder", ModuleBuilder.class, NameFactory.MODULE_BUILDER); 939 infoBuilder.addReference("ActivationSpecInfoLocator", ActivationSpecInfoLocator.class, NameFactory.MODULE_BUILDER); 940 infoBuilder.addReference("AppClientConfigBuilder", ModuleBuilder.class, NameFactory.MODULE_BUILDER); 941 infoBuilder.addReference("SecurityBuilders", NamespaceDrivenBuilder.class, NameFactory.MODULE_BUILDER); 942 infoBuilder.addReference("ServiceBuilders", NamespaceDrivenBuilder.class, NameFactory.MODULE_BUILDER); 943 944 infoBuilder.addAttribute("kernel", Kernel.class, false); 945 946 infoBuilder.setConstructor(new String []{ 947 "defaultEnvironment", 948 "transactionManagerAbstractName", 949 "connectionTrackerAbstractName", 950 "transactionalTimerAbstractName", 951 "nonTransactionalTimerAbstractName", 952 "corbaGBeanAbstractName", 953 "serverName", 954 "Repositories", 955 "EJBConfigBuilder", 956 "WebConfigBuilder", 957 "ConnectorConfigBuilder", 958 "ActivationSpecInfoLocator", 959 "AppClientConfigBuilder", 960 "SecurityBuilders", 961 "ServiceBuilders", 962 "kernel" 963 }); 964 965 GBEAN_INFO = infoBuilder.getBeanInfo(); 966 } 967 968 public static GBeanInfo getGBeanInfo() { 969 return GBEAN_INFO; 970 } 971 972 } 973 | Popular Tags |