KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > j2ee > deployment > EARConfigBuilder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.j2ee.deployment;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URI JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedHashMap JavaDoc;
32 import java.util.LinkedHashSet JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.jar.JarFile JavaDoc;
38 import java.util.zip.ZipEntry JavaDoc;
39
40 import javax.xml.namespace.QName JavaDoc;
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 /**
97  * @version $Rev: 476127 $ $Date: 2006-11-17 07:56:28 -0500 (Fri, 17 Nov 2006) $
98  */

99 public class EARConfigBuilder implements ConfigurationBuilder, CorbaGBeanNameSource {
100
101     private static final Log log = LogFactory.getLog(EARConfigBuilder.class);
102     private static final String JavaDoc LINE_SEP = System.getProperty("line.separator");
103
104     private final static QName JavaDoc APPLICATION_QNAME = GerApplicationDocument.type.getDocumentElementName();
105
106     private final ConfigurationManager configurationManager;
107     private final Collection JavaDoc 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 JavaDoc repositories,
133             Collection JavaDoc ejbConfigBuilder,
134             Collection JavaDoc webConfigBuilder,
135             Collection JavaDoc connectorConfigBuilder,
136             Collection JavaDoc resourceReferenceBuilder,
137             Collection JavaDoc appClientConfigBuilder,
138             Collection JavaDoc securityBuilders,
139             Collection JavaDoc 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 JavaDoc 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 JavaDoc repositories,
203             SingleElementCollection ejbConfigBuilder,
204             SingleElementCollection webConfigBuilder,
205             SingleElementCollection connectorConfigBuilder,
206             SingleElementCollection resourceReferenceBuilder,
207             SingleElementCollection appClientConfigBuilder,
208             Collection JavaDoc securityBuilders,
209             Collection JavaDoc 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 JavaDoc getDeploymentPlan(File JavaDoc planFile, JarFile JavaDoc 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         //Only "synthetic" ears with only external modules can have no jar file.
266
if (jarFile == null) {
267             return null;
268         }
269
270         // get the modules either the application plan or for a stand alone module from the specific deployer
271
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 JavaDoc(Collections.singleton(module)),
294                 Collections.EMPTY_SET,
295                 null);
296     }
297
298     private ApplicationInfo getEarPlan(File JavaDoc planFile, JarFile JavaDoc earFile, ModuleIDBuilder idBuilder) throws DeploymentException {
299         String JavaDoc specDD;
300         ApplicationType application = null;
301         if (earFile != null) {
302             try {
303                 URL JavaDoc applicationXmlUrl = DeploymentUtil.createJarURL(earFile, "META-INF/application.xml");
304                 specDD = DeploymentUtil.readAll(applicationXmlUrl);
305             } catch (Exception JavaDoc e) {
306                 //no application.xml, not for us
307
return null;
308             }
309             //we found something called application.xml in the right place, if we can't parse it it's an error
310
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             // load the geronimo-application.xml from either the supplied plan or from the earFile
321
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 JavaDoc 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 JavaDoc e) {
335                 //TODO isn't this an error?
336
}
337
338             // if we got one extract the validate it otherwise create a default one
339
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 JavaDoc(earFile.getName()).getName(), "ear");
349         // Make this EAR's settings the default for child modules
350
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         // get the modules either the application plan or for a stand alone module from the specific deployer
357
// todo change module so you can extract the real module path back out.. then we can eliminate
358
// the moduleLocations and have addModules return the modules
359
Set JavaDoc moduleLocations = new HashSet JavaDoc();
360         LinkedHashSet JavaDoc modules = new LinkedHashSet JavaDoc();
361         try {
362             addModules(earFile, application, gerApplication, moduleLocations, modules, environment, earName, idBuilder);
363         } catch (Throwable JavaDoc e) {
364             // close all the modules
365
for (Iterator JavaDoc 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 JavaDoc) {
373                 throw (RuntimeException JavaDoc) e;
374             } else if (e instanceof Error JavaDoc) {
375                 throw (Error JavaDoc) 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 JavaDoc module) {
392         // construct the empty geronimo-application.xml
393
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         // set the configId
400
String JavaDoc id = application.getId();
401         if (id == null) {
402             File JavaDoc fileName = new File JavaDoc(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 JavaDoc schemaLocationURL = "http://java.sun.com/xml/ns/j2ee/application_1_4.xsd";
426         String JavaDoc 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 JavaDoc plan, JarFile JavaDoc module, ModuleIDBuilder idBuilder) throws IOException JavaDoc, DeploymentException {
447         ApplicationInfo applicationInfo = (ApplicationInfo) plan;
448         Artifact test = applicationInfo.getEnvironment().getConfigId();
449         if(!test.isResolved()) {
450             throw new IllegalStateException JavaDoc("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 JavaDoc plan, JarFile JavaDoc earFile, Collection JavaDoc configurationStores, ArtifactResolver artifactResolver, ConfigurationStore targetConfigurationStore) throws IOException JavaDoc, 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 JavaDoc 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             // Create the output ear context
475
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             // Copy over all files that are _NOT_ modules (e.g. META-INF and APP-INF files)
492
Set JavaDoc moduleLocations = applicationInfo.getModuleLocations();
493             if (ConfigurationModuleType.EAR == applicationType && earFile != null) {
494                 for (Enumeration JavaDoc e = earFile.entries(); e.hasMoreElements();) {
495                     ZipEntry JavaDoc entry = (ZipEntry JavaDoc) e.nextElement();
496                     String JavaDoc entryName = entry.getName();
497                     boolean addEntry = true;
498                     for (Iterator JavaDoc iter = moduleLocations.iterator(); iter.hasNext();) {
499                         String JavaDoc location = (String JavaDoc) 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             // each module installs it's files into the output context.. this is different for each module type
514
LinkedHashSet JavaDoc modules = applicationInfo.getModules();
515             for (Iterator JavaDoc 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             // give each module a chance to populate the earContext now that a classloader is available
523
ClassLoader JavaDoc cl = earContext.getClassLoader();
524             for (Iterator JavaDoc iterator = modules.iterator(); iterator.hasNext();) {
525                 Module module = (Module) iterator.next();
526                 getBuilder(module).initContext(earContext, module, cl);
527             }
528
529             // add gbeans declared in the geronimo-application.xml
530
if (geronimoApplication != null) {
531                 securityBuilders.build(geronimoApplication, earContext, earContext);
532                 serviceBuilders.build(geronimoApplication, earContext, earContext);
533             }
534
535             // Create the J2EEApplication managed object
536
if (ConfigurationModuleType.EAR == applicationType) {
537                 GBeanData gbeanData = new GBeanData(earContext.getModuleName(), J2EEApplicationImpl.GBEAN_INFO);
538                 try {
539                     String JavaDoc originalSpecDD = applicationInfo.getOriginalSpecDD();
540                     if (originalSpecDD == null) {
541                         originalSpecDD = "Synthetic EAR";
542                     }
543                     gbeanData.setAttribute("deploymentDescriptor", originalSpecDD);
544                 } catch (Exception JavaDoc 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 JavaDoc thisApp = Collections.singletonMap(NameFactory.J2EE_APPLICATION, earContext.getModuleName().getNameProperty(NameFactory.J2EE_NAME));
550                 LinkedHashSet JavaDoc resourcePatterns = new LinkedHashSet JavaDoc();
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             // each module can now add it's GBeans
570
for (Iterator JavaDoc iterator = modules.iterator(); iterator.hasNext();) {
571                 Module module = (Module) iterator.next();
572                 getBuilder(module).addGBeans(earContext, module, cl, repositories);
573             }
574
575             // it's the caller's responsibility to close the context...
576
return earContext;
577         } catch (GBeanAlreadyExistsException e) {
578             cleanupContext(earContext, configurationDir);
579             throw new DeploymentException(e);
580         } catch (IOException JavaDoc e) {
581             cleanupContext(earContext, configurationDir);
582             throw e;
583         } catch (DeploymentException e) {
584             cleanupContext(earContext, configurationDir);
585             throw e;
586         } catch(RuntimeException JavaDoc e) {
587             cleanupContext(earContext, configurationDir);
588             throw e;
589         } catch(Error JavaDoc e) {
590             cleanupContext(earContext, configurationDir);
591             throw e;
592         } finally {
593             Set JavaDoc modules = applicationInfo.getModules();
594             for (Iterator JavaDoc 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 JavaDoc configurationDir) {
602         List JavaDoc configurations = new ArrayList JavaDoc();
603         if (earContext != null) {
604             configurations.addAll(earContext.getAdditionalDeployment());
605             try {
606                 earContext.close();
607             } catch (IOException JavaDoc ioe) {
608                 // ignore any cleanup problems
609
} catch (DeploymentException de) {
610                 // ignore any cleanup problems
611
}
612         }
613         // configurationDir is created before we create an EARContext
614
if (configurationDir != null) {
615             cleanupConfigurationDir(configurationDir);
616         }
617         // cleanup any other configurations generated (e.g. AppClient config dirs)
618
for (Iterator JavaDoc iterator = configurations.iterator(); iterator.hasNext();) {
619             ConfigurationData configurationData = (ConfigurationData) iterator.next();
620             cleanupConfigurationDir(configurationData.getConfigurationDir());
621         }
622     }
623
624     private boolean cleanupConfigurationDir(File JavaDoc configurationDir)
625     {
626         LinkedList JavaDoc cannotBeDeletedList = new LinkedList JavaDoc();
627
628         if (!DeploymentUtil.recursiveDelete(configurationDir,cannotBeDeletedList)) {
629             // Output a message to help user track down file problem
630
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 JavaDoc filter(Map JavaDoc original, String JavaDoc key, String JavaDoc value) {
641         LinkedHashMap JavaDoc filter = new LinkedHashMap JavaDoc(original);
642         filter.put(key, value);
643         return filter;
644     }
645
646     private void addModules(JarFile JavaDoc earFile, ApplicationType application, GerApplicationType gerApplication, Set JavaDoc moduleLocations, LinkedHashSet JavaDoc modules, Environment environment, AbstractName earName, ModuleIDBuilder idBuilder) throws DeploymentException {
647         Map JavaDoc altVendorDDs = new HashMap JavaDoc();
648         try {
649             if (earFile != null) {
650                 ModuleType[] moduleTypes = application.getModuleArray();
651                 //paths is used to check that all modules in the geronimo plan are in the application.xml.
652
Set JavaDoc paths = new HashSet JavaDoc();
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                 // build map from module path to alt vendor dd
667
GerModuleType gerModuleTypes[] = gerApplication.getModuleArray();
668                 for (int i = 0; i < gerModuleTypes.length; i++) {
669                     GerModuleType gerModule = gerModuleTypes[i];
670                     String JavaDoc 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                         // the the url of the alt dd
686
try {
687                             altVendorDDs.put(path, DeploymentUtil.toTempFile(earFile, gerModule.getAltDd().getStringValue()));
688                         } catch (IOException JavaDoc e) {
689                             throw new DeploymentException("Invalid alt vendor dd url: " + gerModule.getAltDd().getStringValue(), e);
690                         }
691                     } else {
692                         //dd is included explicitly
693
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                 // get a set containing all of the files in the ear that are actually modules
702
for (int i = 0; i < moduleTypes.length; i++) {
703                     ModuleType moduleXml = moduleTypes[i];
704
705                     String JavaDoc modulePath;
706                     ModuleBuilder builder;
707
708                     Object JavaDoc moduleContextInfo = null;
709                     String JavaDoc 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 JavaDoc altSpecDD = null;
746                     if (moduleXml.isSetAltDd()) {
747                         try {
748                             altSpecDD = DeploymentUtil.createJarURL(earFile, moduleXml.getAltDd().getStringValue());
749                         } catch (MalformedURLException JavaDoc 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 JavaDoc 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             //deploy the extension modules
779
GerExtModuleType gerExtModuleTypes[] = gerApplication.getExtModuleArray();
780             for (int i = 0; i < gerExtModuleTypes.length; i++) {
781                 GerExtModuleType gerExtModule = gerExtModuleTypes[i];
782                 String JavaDoc moduleName;
783                 ModuleBuilder builder;
784                 Object JavaDoc moduleContextInfo = null;
785                 String JavaDoc 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                     //ext modules must use vendor plan to set context-root
802
// moduleContextInfo = gerExtModule.getWeb().getContextRoot().getStringValue().trim();
803
} 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                 //dd is included explicitly
821
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 JavaDoc vendorDD = anys[0];
826
827                 JarFile JavaDoc moduleFile;
828                 if (gerExtModule.isSetInternalPath()) {
829                     String JavaDoc modulePath = gerExtModule.getInternalPath().trim();
830                     moduleLocations.add(modulePath);
831                     try {
832                         moduleFile = new NestedJarFile(earFile, modulePath);
833                     } catch (IOException JavaDoc e) {
834                         throw new DeploymentException("Invalid moduleFile: " + modulePath, e);
835                     }
836                 } else {
837                     String JavaDoc path = gerExtModule.getExternalPath().trim();
838                     Artifact artifact = Artifact.create(path);
839                     File JavaDoc location = null;
840                     for (Iterator JavaDoc 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 JavaDoc(location);
852                     } catch (IOException JavaDoc e) {
853                         throw new DeploymentException("Could not access contents of " + moduleTypeName, e);
854                     }
855
856                 }
857
858
859                 URL JavaDoc altSpecDD = null;
860                 //todo implement an alt-spec-dd element.
861
// if (moduleXml.isSetAltDd()) {
862
// try {
863
// altSpecDD = DeploymentUtil.createJarURL(earFile, moduleXml.getAltDd().getStringValue());
864
// } catch (MalformedURLException e) {
865
// throw new DeploymentException("Invalid alt spec dd url: " + moduleXml.getAltDd().getStringValue(), e);
866
// }
867
// }
868

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             // delete all the temp files created for alt vendor dds
889
for (Iterator JavaDoc iterator = altVendorDDs.values().iterator(); iterator.hasNext();) {
890                 Object JavaDoc altVendorDD = iterator.next();
891                 if (altVendorDD instanceof File JavaDoc) {
892                     ((File JavaDoc) 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 JavaDoc("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 JavaDoc[]{
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