001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.j2ee.deployment;
018    
019    import java.io.File;
020    import java.io.IOException;
021    import java.net.MalformedURLException;
022    import java.net.URI;
023    import java.net.URL;
024    import java.util.ArrayList;
025    import java.util.Collection;
026    import java.util.Collections;
027    import java.util.Enumeration;
028    import java.util.HashMap;
029    import java.util.HashSet;
030    import java.util.LinkedHashMap;
031    import java.util.LinkedHashSet;
032    import java.util.LinkedList;
033    import java.util.List;
034    import java.util.Map;
035    import java.util.Set;
036    import java.util.jar.JarEntry;
037    import java.util.jar.JarFile;
038    import java.util.jar.Manifest;
039    import java.util.jar.Attributes;
040    import java.util.zip.ZipEntry;
041    
042    import javax.xml.namespace.QName;
043    
044    import org.apache.commons.logging.Log;
045    import org.apache.commons.logging.LogFactory;
046    import org.apache.geronimo.common.DeploymentException;
047    import org.apache.geronimo.deployment.ConfigurationBuilder;
048    import org.apache.geronimo.deployment.DeploymentContext;
049    import org.apache.geronimo.deployment.ModuleIDBuilder;
050    import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
051    import org.apache.geronimo.deployment.NamespaceDrivenBuilderCollection;
052    import org.apache.geronimo.deployment.ClassPathList;
053    import org.apache.geronimo.deployment.ModuleList;
054    import org.apache.geronimo.deployment.service.EnvironmentBuilder;
055    import org.apache.geronimo.deployment.service.GBeanBuilder;
056    import org.apache.geronimo.deployment.util.DeploymentUtil;
057    import org.apache.geronimo.deployment.util.NestedJarFile;
058    import org.apache.geronimo.deployment.xbeans.ArtifactType;
059    import org.apache.geronimo.deployment.xbeans.EnvironmentType;
060    import org.apache.geronimo.deployment.xbeans.PatternType;
061    import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
062    import org.apache.geronimo.gbean.AbstractName;
063    import org.apache.geronimo.gbean.AbstractNameQuery;
064    import org.apache.geronimo.gbean.GBeanData;
065    import org.apache.geronimo.gbean.GBeanInfo;
066    import org.apache.geronimo.gbean.GBeanInfoBuilder;
067    import org.apache.geronimo.gbean.ReferencePatterns;
068    import org.apache.geronimo.gbean.SingleElementCollection;
069    import org.apache.geronimo.gbean.GBeanLifecycle;
070    import org.apache.geronimo.j2ee.ApplicationInfo;
071    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
072    import org.apache.geronimo.j2ee.management.impl.J2EEApplicationImpl;
073    import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
074    import org.apache.geronimo.kernel.Kernel;
075    import org.apache.geronimo.kernel.Naming;
076    import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException;
077    import org.apache.geronimo.kernel.config.ConfigurationData;
078    import org.apache.geronimo.kernel.config.ConfigurationManager;
079    import org.apache.geronimo.kernel.config.ConfigurationModuleType;
080    import org.apache.geronimo.kernel.config.ConfigurationStore;
081    import org.apache.geronimo.kernel.config.ConfigurationUtil;
082    import org.apache.geronimo.kernel.config.SimpleConfigurationManager;
083    import org.apache.geronimo.kernel.repository.Artifact;
084    import org.apache.geronimo.kernel.repository.ArtifactResolver;
085    import org.apache.geronimo.kernel.repository.Environment;
086    import org.apache.geronimo.kernel.repository.Repository;
087    import org.apache.geronimo.kernel.repository.MissingDependencyException;
088    import org.apache.geronimo.management.J2EEResource;
089    import org.apache.geronimo.management.J2EEServer;
090    import org.apache.geronimo.schema.SchemaConversionUtils;
091    import org.apache.geronimo.xbeans.geronimo.j2ee.GerApplicationDocument;
092    import org.apache.geronimo.xbeans.geronimo.j2ee.GerApplicationType;
093    import org.apache.geronimo.xbeans.geronimo.j2ee.GerExtModuleType;
094    import org.apache.geronimo.xbeans.geronimo.j2ee.GerModuleType;
095    import org.apache.geronimo.xbeans.geronimo.j2ee.GerSecurityDocument;
096    import org.apache.geronimo.xbeans.javaee.ApplicationDocument;
097    import org.apache.geronimo.xbeans.javaee.ApplicationType;
098    import org.apache.geronimo.xbeans.javaee.ModuleType;
099    import org.apache.xmlbeans.XmlCursor;
100    import org.apache.xmlbeans.XmlException;
101    import org.apache.xmlbeans.XmlObject;
102    
103    /**
104     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
105     */
106    public class EARConfigBuilder implements ConfigurationBuilder, CorbaGBeanNameSource, GBeanLifecycle {
107    
108        private static final Log log = LogFactory.getLog(EARConfigBuilder.class);
109        private static final String LINE_SEP = System.getProperty("line.separator");
110    
111        private final static QName APPLICATION_QNAME = GerApplicationDocument.type.getDocumentElementName();
112        private static final Map<String, String> NAMESPACE_UPDATES = new HashMap<String, String>();
113        static {
114            NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application", "http://geronimo.apache.org/xml/ns/j2ee/application-2.0");
115            NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application-1.1", "http://geronimo.apache.org/xml/ns/j2ee/application-2.0");
116            NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application-1.2", "http://geronimo.apache.org/xml/ns/j2ee/application-2.0");
117        }
118    
119        private final ConfigurationManager configurationManager;
120        private final Collection<? extends Repository> repositories;
121        private final SingleElementCollection ejbConfigBuilder;
122        private final SingleElementCollection webConfigBuilder;
123        private final SingleElementCollection connectorConfigBuilder;
124        private final SingleElementCollection appClientConfigBuilder;
125        private final SingleElementCollection resourceReferenceBuilder;
126        private final NamespaceDrivenBuilderCollection securityBuilders;
127        private final NamespaceDrivenBuilderCollection serviceBuilders;
128        private final Collection<ModuleBuilderExtension> persistenceUnitBuilders;
129    
130        private final Environment defaultEnvironment;
131        private final AbstractNameQuery serverName;
132        private final AbstractNameQuery transactionManagerObjectName;
133        private final AbstractNameQuery connectionTrackerObjectName;
134        private final AbstractNameQuery transactionalTimerObjectName;
135        private final AbstractNameQuery nonTransactionalTimerObjectName;
136        private final AbstractNameQuery corbaGBeanObjectName;
137        private final Naming naming;
138        private final Collection<? extends ArtifactResolver> artifactResolvers;
139    
140        public EARConfigBuilder(Environment defaultEnvironment,
141                AbstractNameQuery transactionManagerAbstractName,
142                AbstractNameQuery connectionTrackerAbstractName,
143                AbstractNameQuery transactionalTimerAbstractName,
144                AbstractNameQuery nonTransactionalTimerAbstractName,
145                AbstractNameQuery corbaGBeanAbstractName,
146                AbstractNameQuery serverName,
147                Collection<? extends Repository> repositories,
148                Collection ejbConfigBuilder,
149                Collection webConfigBuilder,
150                Collection connectorConfigBuilder,
151                Collection resourceReferenceBuilder,
152                Collection appClientConfigBuilder,
153                Collection securityBuilders,
154                Collection serviceBuilders,
155                Collection<ModuleBuilderExtension> persistenceUnitBuilders,
156                Collection<? extends ArtifactResolver> artifactResolvers,
157                Kernel kernel) {
158            this(defaultEnvironment,
159                    transactionManagerAbstractName,
160                    connectionTrackerAbstractName,
161                    transactionalTimerAbstractName,
162                    nonTransactionalTimerAbstractName,
163                    corbaGBeanAbstractName,
164                    serverName,
165                    ConfigurationUtil.getConfigurationManager(kernel),
166                    repositories,
167                    new SingleElementCollection(ejbConfigBuilder),
168                    new SingleElementCollection(webConfigBuilder),
169                    new SingleElementCollection(connectorConfigBuilder),
170                    new SingleElementCollection(resourceReferenceBuilder),
171                    new SingleElementCollection(appClientConfigBuilder),
172                    securityBuilders,
173                    serviceBuilders,
174                    persistenceUnitBuilders,
175                    kernel.getNaming(), artifactResolvers);
176        }
177    
178        public EARConfigBuilder(Environment defaultEnvironment,
179                AbstractNameQuery transactionManagerAbstractName,
180                AbstractNameQuery connectionTrackerAbstractName,
181                AbstractNameQuery transactionalTimerAbstractName,
182                AbstractNameQuery nonTransactionalTimerAbstractName,
183                AbstractNameQuery corbaGBeanAbstractName,
184                AbstractNameQuery serverName,
185                Collection<? extends Repository> repositories,
186                ModuleBuilder ejbConfigBuilder,
187                ModuleBuilder webConfigBuilder,
188                ModuleBuilder connectorConfigBuilder,
189                ActivationSpecInfoLocator activationSpecInfoLocator,
190                ModuleBuilder appClientConfigBuilder,
191                NamespaceDrivenBuilder securityBuilder,
192                NamespaceDrivenBuilder serviceBuilder,
193                ModuleBuilderExtension persistenceUnitBuilder,
194                Naming naming,
195                Collection<? extends ArtifactResolver> artifactResolvers) {
196            this(defaultEnvironment,
197                    transactionManagerAbstractName,
198                    connectionTrackerAbstractName,
199                    transactionalTimerAbstractName,
200                    nonTransactionalTimerAbstractName,
201                    corbaGBeanAbstractName,
202                    serverName,
203                    null,
204                    repositories,
205                    new SingleElementCollection(ejbConfigBuilder),
206                    new SingleElementCollection(webConfigBuilder),
207                    new SingleElementCollection(connectorConfigBuilder),
208                    new SingleElementCollection(activationSpecInfoLocator),
209                    new SingleElementCollection(appClientConfigBuilder),
210                    securityBuilder == null ? Collections.EMPTY_SET : Collections.singleton(securityBuilder),
211                    serviceBuilder == null ? Collections.EMPTY_SET : Collections.singleton(serviceBuilder),
212                    persistenceUnitBuilder == null ? Collections.EMPTY_SET : Collections.singleton(persistenceUnitBuilder),
213                    naming,
214                    artifactResolvers);
215        }
216    
217        private EARConfigBuilder(Environment defaultEnvironment,
218                 AbstractNameQuery transactionManagerAbstractName,
219                 AbstractNameQuery connectionTrackerAbstractName,
220                 AbstractNameQuery transactionalTimerAbstractName,
221                 AbstractNameQuery nonTransactionalTimerAbstractName,
222                 AbstractNameQuery corbaGBeanAbstractName,
223                 AbstractNameQuery serverName,
224                 ConfigurationManager configurationManager,
225                 Collection<? extends Repository> repositories,
226                 SingleElementCollection ejbConfigBuilder,
227                 SingleElementCollection webConfigBuilder,
228                 SingleElementCollection connectorConfigBuilder,
229                 SingleElementCollection resourceReferenceBuilder,
230                 SingleElementCollection appClientConfigBuilder,
231                 Collection securityBuilders,
232                 Collection serviceBuilders,
233                 Collection<ModuleBuilderExtension> persistenceUnitBuilders,
234                 Naming naming,
235                 Collection<? extends ArtifactResolver> artifactResolvers) {
236            this.configurationManager = configurationManager;
237            this.repositories = repositories;
238            this.defaultEnvironment = defaultEnvironment;
239    
240            this.ejbConfigBuilder = ejbConfigBuilder;
241            this.resourceReferenceBuilder = resourceReferenceBuilder;
242            this.webConfigBuilder = webConfigBuilder;
243            this.connectorConfigBuilder = connectorConfigBuilder;
244            this.appClientConfigBuilder = appClientConfigBuilder;
245            this.securityBuilders = new NamespaceDrivenBuilderCollection(securityBuilders, GerSecurityDocument.type.getDocumentElementName());
246            this.serviceBuilders = new NamespaceDrivenBuilderCollection(serviceBuilders, GBeanBuilder.SERVICE_QNAME);
247            this.persistenceUnitBuilders = persistenceUnitBuilders;
248            
249            this.transactionManagerObjectName = transactionManagerAbstractName;
250            this.connectionTrackerObjectName = connectionTrackerAbstractName;
251            this.transactionalTimerObjectName = transactionalTimerAbstractName;
252            this.nonTransactionalTimerObjectName = nonTransactionalTimerAbstractName;
253            this.corbaGBeanObjectName = corbaGBeanAbstractName;
254            this.serverName = serverName;
255            this.naming = naming;
256            this.artifactResolvers = artifactResolvers;
257        }
258    
259        public void doStart() throws Exception {
260            XmlBeansUtil.registerNamespaceUpdates(NAMESPACE_UPDATES);
261        }
262    
263        public void doStop() {
264            XmlBeansUtil.unregisterNamespaceUpdates(NAMESPACE_UPDATES);
265        }
266    
267        public void doFail() {
268            doStop();
269        }
270    
271        public AbstractNameQuery getCorbaGBeanName() {
272            return corbaGBeanObjectName;
273        }
274    
275        private ModuleBuilder getEjbConfigBuilder() {
276            return (ModuleBuilder) ejbConfigBuilder.getElement();
277        }
278    
279        private ModuleBuilder getWebConfigBuilder() {
280            return (ModuleBuilder) webConfigBuilder.getElement();
281        }
282    
283        private ModuleBuilder getConnectorConfigBuilder() {
284            return (ModuleBuilder) connectorConfigBuilder.getElement();
285        }
286    
287        private ModuleBuilder getAppClientConfigBuilder() {
288            return (ModuleBuilder) appClientConfigBuilder.getElement();
289        }
290    
291        public Object getDeploymentPlan(File planFile, JarFile jarFile, ModuleIDBuilder idBuilder) throws DeploymentException {
292            if (planFile == null && jarFile == null) {
293                return null;
294            }
295            ApplicationInfo plan = getEarPlan(planFile, jarFile, idBuilder);
296            if (plan != null) {
297                return plan;
298            }
299            //Only "synthetic" ears with only external modules can have no jar file.
300            if (jarFile == null) {
301                return null;
302            }
303    
304            // get the modules either the application plan or for a stand alone module from the specific deployer
305            Module module = null;
306            if (getWebConfigBuilder() != null) {
307                module = getWebConfigBuilder().createModule(planFile, jarFile, naming, idBuilder);
308            }
309            if (module == null && getEjbConfigBuilder() != null) {
310                module = getEjbConfigBuilder().createModule(planFile, jarFile, naming, idBuilder);
311            }
312            if (module == null && getConnectorConfigBuilder() != null) {
313                module = getConnectorConfigBuilder().createModule(planFile, jarFile, naming, idBuilder);
314            }
315            if (module == null && getAppClientConfigBuilder() != null) {
316                module = getAppClientConfigBuilder().createModule(planFile, jarFile, naming, idBuilder);
317            }
318            if (module == null) {
319                return null;
320            }
321    
322            if (module instanceof ApplicationInfo) {
323                return module;
324            }
325            
326            return new ApplicationInfo(module.getType(),
327                    module.getEnvironment(),
328                    module.getModuleName(),
329                    jarFile,
330                    null,
331                    null,
332                    new LinkedHashSet<Module>(Collections.singleton(module)),
333                    new ModuleList(),
334                    null);
335        }
336    
337        private ApplicationInfo getEarPlan(File planFile, JarFile earFile, ModuleIDBuilder idBuilder) throws DeploymentException {
338            String specDD;
339            ApplicationType application = null;
340            if (earFile != null) {
341                try {
342                    URL applicationXmlUrl = DeploymentUtil.createJarURL(earFile, "META-INF/application.xml");
343                    specDD = DeploymentUtil.readAll(applicationXmlUrl);
344                    //we found something called application.xml in the right place, if we can't parse it it's an error
345                    XmlObject xmlObject = XmlBeansUtil.parse(specDD);
346                    application = convertToApplicationSchema(xmlObject).getApplication();
347                } catch (XmlException e) {
348                    throw new DeploymentException("Could not parse application.xml", e);
349                } catch (Exception e) {
350                    //ee5 spec allows optional application.xml, continue with application == null
351                    if (!earFile.getName().endsWith(".ear")) {
352                        return null;
353                    }
354                    //TODO return application.xml that we can make metadata complete?
355                }
356            }
357    
358            GerApplicationType gerApplication = null;
359            try {
360                // load the geronimo-application.xml from either the supplied plan or from the earFile
361                XmlObject rawPlan;
362                try {
363                    if (planFile != null) {
364                        rawPlan = XmlBeansUtil.parse(planFile.toURL(), getClass().getClassLoader());
365                        gerApplication = (GerApplicationType) SchemaConversionUtils.fixGeronimoSchema(rawPlan, APPLICATION_QNAME, GerApplicationType.type);
366                        if (gerApplication == null) {
367                            return null;
368                        }
369                    } else {
370                        URL path = DeploymentUtil.createJarURL(earFile, "META-INF/geronimo-application.xml");
371                        rawPlan = XmlBeansUtil.parse(path, getClass().getClassLoader());
372                        gerApplication = (GerApplicationType) SchemaConversionUtils.fixGeronimoSchema(rawPlan, APPLICATION_QNAME, GerApplicationType.type);
373                    }
374                } catch (IOException e) {
375                    //TODO isn't this an error?
376                }
377    
378                // if we got one extract the validate it otherwise create a default one
379                if (gerApplication == null) {
380                    gerApplication = createDefaultPlan(application, earFile);
381                }
382            } catch (XmlException e) {
383                throw new DeploymentException(e);
384            }
385    
386            EnvironmentType environmentType = gerApplication.getEnvironment();
387            Environment environment = EnvironmentBuilder.buildEnvironment(environmentType, defaultEnvironment);
388            idBuilder.resolve(environment, earFile == null ? planFile.getName() : new File(earFile.getName()).getName(), "ear");
389            // Make this EAR's settings the default for child modules
390            idBuilder.setDefaultGroup(environment.getConfigId().getGroupId());
391            idBuilder.setDefaultVersion(environment.getConfigId().getVersion());
392    
393            Artifact artifact = environment.getConfigId();
394            AbstractName earName = naming.createRootName(artifact, artifact.toString(), NameFactory.J2EE_APPLICATION);
395    
396            // get the modules either the application plan or for a stand alone module from the specific deployer
397            // todo change module so you can extract the real module path back out.. then we can eliminate
398            // the moduleLocations and have addModules return the modules
399            ModuleList moduleLocations = new ModuleList();
400            LinkedHashSet<Module> modules = new LinkedHashSet<Module>();
401            try {
402                addModules(earFile, application, gerApplication, moduleLocations, modules, environment, earName, idBuilder);
403                if (application == null && modules.isEmpty()) {
404                    //if no application.xml and no modules detected, return null for stand-alone module processing
405                    return null;
406                }
407            } catch (Throwable e) {
408                // close all the modules
409                for (Module module : modules) {
410                    module.close();
411                }
412    
413                if (e instanceof DeploymentException) {
414                    throw (DeploymentException) e;
415                } else if (e instanceof RuntimeException) {
416                    throw (RuntimeException) e;
417                } else if (e instanceof Error) {
418                    throw (Error) e;
419                }
420                throw new DeploymentException(e);
421            }
422    
423            return new ApplicationInfo(ConfigurationModuleType.EAR,
424                    environment,
425                    earName,
426                    earFile,
427                    application,
428                    gerApplication,
429                    modules,
430                    moduleLocations,
431                    application == null ? null : application.toString());
432        }
433    
434    
435        private GerApplicationType createDefaultPlan(ApplicationType application, JarFile module) {
436            // construct the empty geronimo-application.xml
437            GerApplicationType gerApplication = GerApplicationType.Factory.newInstance();
438            EnvironmentType environmentType = gerApplication.addNewEnvironment();
439            ArtifactType artifactType = environmentType.addNewModuleId();
440    
441            artifactType.setGroupId(Artifact.DEFAULT_GROUP_ID);
442    
443            // set the configId
444            String id = application != null ? application.getId() : null;
445            if (id == null) {
446                File fileName = new File(module.getName());
447                id = fileName.getName();
448                if (id.endsWith(".ear")) {
449                    id = id.substring(0, id.length() - 4);
450                }
451                if (id.endsWith("/")) {
452                    id = id.substring(0, id.length() - 1);
453                }
454            }
455    
456            artifactType.setArtifactId(id);
457            artifactType.setVersion("" + System.currentTimeMillis());
458            artifactType.setType("car");
459            return gerApplication;
460        }
461    
462        static ApplicationDocument convertToApplicationSchema(XmlObject xmlObject) throws XmlException {
463            if (ApplicationDocument.type.equals(xmlObject.schemaType())) {
464                XmlBeansUtil.validateDD(xmlObject);
465                return (ApplicationDocument) xmlObject;
466            }
467            XmlCursor cursor = xmlObject.newCursor();
468            XmlCursor moveable = xmlObject.newCursor();
469            String schemaLocationURL = "http://java.sun.com/xml/ns/javaee/application_5.xsd";
470            String version = "5";
471            try {
472                cursor.toStartDoc();
473                cursor.toFirstChild();
474                if ("http://java.sun.com/xml/ns/j2ee".equals(cursor.getName().getNamespaceURI())) {
475                    SchemaConversionUtils.convertSchemaVersion(cursor, SchemaConversionUtils.JAVAEE_NAMESPACE, schemaLocationURL, version);
476                    XmlObject result = xmlObject.changeType(ApplicationDocument.type);
477                    XmlBeansUtil.validateDD(result);
478                    return (ApplicationDocument) result;
479                }
480    
481                // otherwise assume DTD
482                SchemaConversionUtils.convertToSchema(cursor, SchemaConversionUtils.JAVAEE_NAMESPACE, schemaLocationURL, version);
483                cursor.toStartDoc();
484                cursor.toChild(SchemaConversionUtils.JAVAEE_NAMESPACE, "application");
485                cursor.toFirstChild();
486                SchemaConversionUtils.convertToDescriptionGroup(SchemaConversionUtils.JAVAEE_NAMESPACE, cursor, moveable);
487            } finally {
488                cursor.dispose();
489                moveable.dispose();
490            }
491            XmlObject result = xmlObject.changeType(ApplicationDocument.type);
492            if (result != null) {
493                XmlBeansUtil.validateDD(result);
494                return (ApplicationDocument) result;
495            }
496            XmlBeansUtil.validateDD(xmlObject);
497            return (ApplicationDocument) xmlObject;
498        }
499    
500        public Artifact getConfigurationID(Object plan, JarFile module, ModuleIDBuilder idBuilder) throws IOException, DeploymentException {
501            ApplicationInfo applicationInfo = (ApplicationInfo) plan;
502            Artifact test = applicationInfo.getEnvironment().getConfigId();
503            if (!test.isResolved()) {
504                throw new IllegalStateException("Module ID should be fully resolved by now (not " + test + ")");
505            }
506            return test;
507        }
508    
509        public DeploymentContext buildConfiguration(boolean inPlaceDeployment, Artifact configId, Object plan, JarFile earFile, Collection configurationStores, ArtifactResolver artifactResolver, ConfigurationStore targetConfigurationStore) throws IOException, DeploymentException {
510            assert plan != null;
511            ApplicationInfo applicationInfo = (ApplicationInfo) plan;
512    
513            EARContext earContext = null;
514            ConfigurationModuleType applicationType = applicationInfo.getType();
515            applicationInfo.getEnvironment().setConfigId(configId);
516            File configurationDir = null;
517            try {
518                try {
519                    configurationDir = targetConfigurationStore.createNewConfigurationDir(configId);
520                } catch (ConfigurationAlreadyExistsException e) {
521                    throw new DeploymentException(e);
522                }
523    
524                ConfigurationManager configurationManager = this.configurationManager;
525                if (configurationManager == null) {
526                    configurationManager = new SimpleConfigurationManager(configurationStores, artifactResolver, repositories);
527                }
528    
529                // Create the output ear context
530                earContext = new EARContext(configurationDir,
531                        inPlaceDeployment ? DeploymentUtil.toFile(earFile) : null,
532                        applicationInfo.getEnvironment(),
533                        applicationType,
534                        naming,
535                        configurationManager,
536                        repositories,
537                        serverName,
538                        applicationInfo.getModuleName(),
539                        transactionManagerObjectName,
540                        connectionTrackerObjectName,
541                        transactionalTimerObjectName,
542                        nonTransactionalTimerObjectName,
543                        corbaGBeanObjectName
544                );
545                applicationInfo.setEarContext(earContext);
546                applicationInfo.setRootEarContext(earContext);
547                earContext.getGeneralData().put(ModuleList.class, applicationInfo.getModuleLocations());
548    
549                // Copy over all files that are _NOT_ modules (e.g. META-INF and APP-INF files)
550                Set moduleLocations = applicationInfo.getModuleLocations();
551                ClassPathList libClasspath = new ClassPathList();
552                if (ConfigurationModuleType.EAR == applicationType && earFile != null) {
553                    //get the value of the library-directory element in spec DD
554                    ApplicationType specDD = (ApplicationType) applicationInfo.getSpecDD();
555                    String libDir = getLibraryDirectory(specDD);
556                    for (Enumeration<JarEntry> e = earFile.entries(); e.hasMoreElements();) {
557                        ZipEntry entry = e.nextElement();
558                        String entryName = entry.getName();
559                        boolean addEntry = true;
560                        for (Object moduleLocation : moduleLocations) {
561                            String location = (String) moduleLocation;
562                            if (entryName.startsWith(location)) {
563                                addEntry = false;
564                                break;
565                            }
566                        }
567                        if (libDir != null && entry.getName().startsWith(libDir) && entry.getName().endsWith(".jar")) {
568                            NestedJarFile library = new NestedJarFile(earFile, entry.getName());
569                            earContext.addIncludeAsPackedJar(URI.create(entry.getName()), library);
570                            libClasspath.add(entry.getName());
571                        } else if (addEntry) {
572                            earContext.addFile(URI.create(entry.getName()), earFile, entry);
573                        }
574                    }
575                    if (!libClasspath.isEmpty()) {
576                        earContext.getGeneralData().put(ClassPathList.class, libClasspath);
577                    }
578                }
579    
580                GerApplicationType geronimoApplication = (GerApplicationType) applicationInfo.getVendorDD();
581    
582                // each module installs it's files into the output context.. this is different for each module type
583                LinkedHashSet modules = applicationInfo.getModules();
584                for (Object module2 : modules) {
585                    Module module = (Module) module2;
586                    getBuilder(module).installModule(earFile, earContext, module, configurationStores, targetConfigurationStore, repositories);
587                }
588    
589                earContext.flush();
590    
591                // give each module a chance to populate the earContext now that a classloader is available
592                ClassLoader cl = earContext.getClassLoader();
593                for (Object module3 : modules) {
594                    Module module = (Module) module3;
595                    getBuilder(module).initContext(earContext, module, cl);
596                }
597    
598                // add gbeans declared in the geronimo-application.xml
599                if (geronimoApplication != null) {
600                    securityBuilders.build(geronimoApplication, earContext, earContext);
601                    serviceBuilders.build(geronimoApplication, earContext, earContext);
602                }
603                
604                if (ConfigurationModuleType.EAR == applicationType) {
605                    // process persistence unit in EAR library directory
606                    earContext.getGeneralData().put(ClassPathList.class, libClasspath);
607                    for (ModuleBuilderExtension mbe: persistenceUnitBuilders) {
608                        mbe.initContext(earContext, applicationInfo, earContext.getClassLoader());
609                    }
610                    
611                    // Create the J2EEApplication managed object
612                    GBeanData gbeanData = new GBeanData(earContext.getModuleName(), J2EEApplicationImpl.GBEAN_INFO);
613                    try {
614                        String originalSpecDD = applicationInfo.getOriginalSpecDD();
615                        if (originalSpecDD == null) {
616                            originalSpecDD = "Synthetic EAR";
617                        }
618                        gbeanData.setAttribute("deploymentDescriptor", originalSpecDD);
619                    } catch (Exception e) {
620                        throw new DeploymentException("Error initializing J2EEApplication managed object", e);
621                    }
622                    gbeanData.setReferencePatterns("Server", new ReferencePatterns(new AbstractNameQuery(J2EEServer.class.getName())));
623    
624                    Map<String, String> thisApp = Collections.singletonMap(NameFactory.J2EE_APPLICATION, earContext.getModuleName().getNameProperty(NameFactory.J2EE_NAME));
625                    LinkedHashSet<AbstractNameQuery> resourcePatterns = new LinkedHashSet<AbstractNameQuery>();
626                    resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JAVA_MAIL_RESOURCE), J2EEResource.class.getName()));
627                    resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JCA_CONNECTION_FACTORY), J2EEResource.class.getName()));
628                    resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JDBC_RESOURCE), J2EEResource.class.getName()));
629                    resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JDBC_DRIVER), J2EEResource.class.getName()));
630                    resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JMS_RESOURCE), J2EEResource.class.getName()));
631                    resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JNDI_RESOURCE), J2EEResource.class.getName()));
632                    resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.JTA_RESOURCE), J2EEResource.class.getName()));
633                    resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.RMI_IIOP_RESOURCE), J2EEResource.class.getName()));
634                    resourcePatterns.add(new AbstractNameQuery(null, filter(thisApp, NameFactory.J2EE_TYPE, NameFactory.URL_RESOURCE), J2EEResource.class.getName()));
635                    gbeanData.setReferencePatterns("Resources", resourcePatterns);
636    
637                    gbeanData.setReferencePatterns("AppClientModules", new ReferencePatterns(new AbstractNameQuery(null, thisApp, org.apache.geronimo.management.AppClientModule.class.getName())));
638                    gbeanData.setReferencePatterns("EJBModules", new ReferencePatterns(new AbstractNameQuery(null, thisApp, org.apache.geronimo.management.EJBModule.class.getName())));
639                    gbeanData.setReferencePatterns("ResourceAdapterModules", new ReferencePatterns(new AbstractNameQuery(null, thisApp, org.apache.geronimo.management.geronimo.ResourceAdapterModule.class.getName())));
640                    gbeanData.setReferencePatterns("WebModules", new ReferencePatterns(new AbstractNameQuery(null, thisApp, org.apache.geronimo.management.geronimo.WebModule.class.getName())));
641                    earContext.addGBean(gbeanData);
642                }
643    
644                // each module can now add it's GBeans
645                for (Object module1 : modules) {
646                    Module module = (Module) module1;
647                    getBuilder(module).addGBeans(earContext, module, cl, repositories);
648                }
649    
650                // it's the caller's responsibility to close the context...
651                return earContext;
652            } catch (GBeanAlreadyExistsException e) {
653                cleanupContext(earContext, configurationDir);
654                throw new DeploymentException(e);
655            } catch (IOException e) {
656                cleanupContext(earContext, configurationDir);
657                throw e;
658            } catch (DeploymentException e) {
659                cleanupContext(earContext, configurationDir);
660                throw e;
661            } catch (RuntimeException e) {
662                cleanupContext(earContext, configurationDir);
663                throw e;
664            } catch (Error e) {
665                cleanupContext(earContext, configurationDir);
666                throw e;
667            } finally {
668                for (Object module1 : applicationInfo.getModules()) {
669                    Module module = (Module) module1;
670                    module.close();
671                }
672            }
673        }
674    
675        private String getLibraryDirectory(ApplicationType specDD) {
676            if (specDD == null || !specDD.isSetLibraryDirectory()) {
677                //value 'lib' is used if element not set or ear does not contain a dd
678                return "lib";
679            }
680    
681            //only set if not empty value, empty value implies no library directory
682            String value = specDD.getLibraryDirectory().getStringValue();
683            return value.trim().length() > 0 ? value : null;
684        }
685    
686        private void cleanupContext(EARContext earContext, File configurationDir) {
687            List<ConfigurationData> configurations = new ArrayList<ConfigurationData>();
688            if (earContext != null) {
689                configurations.addAll(earContext.getAdditionalDeployment());
690                try {
691                    earContext.close();
692                } catch (IOException ioe) {
693                    // ignore any cleanup problems
694                } catch (DeploymentException de) {
695                    // ignore any cleanup problems
696                }
697            }
698            // configurationDir is created before we create an EARContext
699            if (configurationDir != null) {
700                cleanupConfigurationDir(configurationDir);
701            }
702            // cleanup any other configurations generated (e.g. AppClient config dirs)
703            for (ConfigurationData configurationData : configurations) {
704                cleanupConfigurationDir(configurationData.getConfigurationDir());
705            }
706        }
707    
708        private boolean cleanupConfigurationDir(File configurationDir) {
709            LinkedList<String> cannotBeDeletedList = new LinkedList<String>();
710    
711            if (!DeploymentUtil.recursiveDelete(configurationDir, cannotBeDeletedList)) {
712                // Output a message to help user track down file problem
713                log.warn("Unable to delete " + cannotBeDeletedList.size() +
714                        " files while recursively deleting directory "
715                        + configurationDir.getAbsolutePath() + LINE_SEP +
716                        "The first file that could not be deleted was:" + LINE_SEP + "  " +
717                        (!cannotBeDeletedList.isEmpty() ? cannotBeDeletedList.getFirst() : ""));
718                return false;
719            }
720            return true;
721        }
722    
723        private static Map<String, String> filter(Map<String, String> original, String key, String value) {
724            LinkedHashMap<String, String> filter = new LinkedHashMap<String, String>(original);
725            filter.put(key, value);
726            return filter;
727        }
728    
729        private void addModules(JarFile earFile, ApplicationType application, GerApplicationType gerApplication, ModuleList moduleLocations, LinkedHashSet<Module> modules, Environment environment, AbstractName earName, ModuleIDBuilder idBuilder) throws DeploymentException {
730            Map<String, Object> altVendorDDs = new HashMap<String, Object>();
731            try {
732                mapVendorPlans(gerApplication, altVendorDDs, earFile);
733                if (earFile != null) {
734                    if (application != null) {
735                        ModuleType[] moduleTypes = application.getModuleArray();
736    
737                        //get a set containing all of the files in the ear that are actually modules
738                        for (ModuleType moduleXml : moduleTypes) {
739                            String modulePath;
740                            ModuleBuilder builder;
741    
742                            Object moduleContextInfo = null;
743                            String moduleTypeName;
744                            if (moduleXml.isSetEjb()) {
745                                modulePath = moduleXml.getEjb().getStringValue();
746                                builder = getEjbConfigBuilder();
747                                if (builder == null) {
748                                    throw new DeploymentException("Cannot deploy ejb application; No ejb deployer defined: " + modulePath);
749                                }
750                                moduleTypeName = "an EJB";
751                            } else if (moduleXml.isSetWeb()) {
752                                modulePath = moduleXml.getWeb().getWebUri().getStringValue();
753                                if (getWebConfigBuilder() == null) {
754                                    throw new DeploymentException("Cannot deploy web application; No war deployer defined: " + modulePath);
755                                }
756                                builder = getWebConfigBuilder();
757                                moduleTypeName = "a war";
758                                moduleContextInfo = moduleXml.getWeb().getContextRoot().getStringValue().trim();
759                            } else if (moduleXml.isSetConnector()) {
760                                modulePath = moduleXml.getConnector().getStringValue();
761                                if (getConnectorConfigBuilder() == null) {
762                                    throw new DeploymentException("Cannot deploy resource adapter; No rar deployer defined: " + modulePath);
763                                }
764                                builder = getConnectorConfigBuilder();
765                                moduleTypeName = "a connector";
766                            } else if (moduleXml.isSetJava()) {
767                                modulePath = moduleXml.getJava().getStringValue();
768                                if (getAppClientConfigBuilder() == null) {
769                                    throw new DeploymentException("Cannot deploy app client; No app client deployer defined: " + modulePath);
770                                }
771                                builder = getAppClientConfigBuilder();
772                                moduleTypeName = "an application client";
773                            } else {
774                                throw new DeploymentException("Could not find a module builder for module: " + moduleXml);
775                            }
776    
777                            moduleLocations.add(modulePath);
778    
779                            NestedJarFile moduleFile;
780                            try {
781                                moduleFile = new NestedJarFile(earFile, modulePath);
782                            } catch (IOException e) {
783                                throw new DeploymentException("Invalid moduleFile: " + modulePath, e);
784                            }
785    
786                            Module module = builder.createModule(altVendorDDs.get(modulePath),
787                                    moduleFile,
788                                    modulePath,
789                                    getAltSpecDDURL(earFile, moduleXml),
790                                    environment,
791                                    moduleContextInfo,
792                                    earName,
793                                    naming, idBuilder);
794    
795                            if (module == null) {
796                                throw new DeploymentException("Module was not " + moduleTypeName + ": " + modulePath);
797                            }
798    
799                            modules.add(module);
800                        }
801                    } else {
802                        //no application.xml available, must inspect ear to locate and process modules
803                        Enumeration<JarEntry> entries = earFile.entries();
804                        while (entries.hasMoreElements()) {
805                            ModuleBuilder builder;
806                            Object moduleContextInfo = null;
807                            String moduleTypeName;
808                            ZipEntry entry = entries.nextElement();
809                            if (entry.getName().endsWith(".war")) {
810                                if (getWebConfigBuilder() == null) {
811                                    throw new DeploymentException("Cannot deploy web application; No war deployer defined: " + entry.getName());
812                                }
813                                builder = getWebConfigBuilder();
814                                moduleTypeName = "a war";
815                                moduleContextInfo = entry.getName().split(".war")[0];
816                            } else if (entry.getName().endsWith(".rar")) {
817                                if (getConnectorConfigBuilder() == null) {
818                                    throw new DeploymentException("Cannot deploy resource adapter; No rar deployer defined: " + entry.getName());
819                                }
820                                builder = getConnectorConfigBuilder();
821                                moduleTypeName = "a connector";
822                            } else if (entry.getName().endsWith(".jar") && !isLibraryEntry(application, entry)) {
823                                try {
824                                    NestedJarFile moduleFile = new NestedJarFile(earFile, entry.getName());
825                                    Manifest mf = moduleFile.getManifest();
826    
827                                    if (mf.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS) != null) {
828                                        if (getAppClientConfigBuilder() == null) {
829                                            throw new DeploymentException("Cannot deploy app client; No app client deployer defined: " + entry.getName());
830                                        }
831                                        builder = getAppClientConfigBuilder();
832                                        moduleTypeName = "an application client";
833                                    } else {
834                                        //ask the ejb builder if its an ejb module
835                                        builder = getEjbConfigBuilder();
836                                        if (builder == null) {
837    //                                        throw new DeploymentException("Cannot deploy ejb application; No ejb deployer defined: " + entry.getName());
838                                            continue;
839                                        }
840    
841                                        Module module = builder.createModule(altVendorDDs.get(entry.getName()),
842                                                moduleFile,
843                                                entry.getName(),
844                                                null,
845                                                environment,
846                                                moduleContextInfo,
847                                                earName,
848                                                naming, idBuilder);
849    
850                                        if (module != null) {
851                                            moduleLocations.add(entry.getName());
852                                            modules.add(module);
853                                        }
854                                        continue;
855                                    }
856                                    //TODO if no ejb-jar.xml inspect classes for EJB component annotations to identify as EJBJar module
857                                } catch (IOException e) {
858                                    throw new DeploymentException("Invalid moduleFile: " + entry.getName(), e);
859                                }
860                            } else {
861                                continue;
862                            }
863    
864                            moduleLocations.add(entry.getName());
865    
866                            NestedJarFile moduleFile;
867                            try {
868                                moduleFile = new NestedJarFile(earFile, entry.getName());
869                            } catch (IOException e) {
870                                throw new DeploymentException("Invalid moduleFile: " + entry.getName(), e);
871                            }
872    
873                            Module module = builder.createModule(altVendorDDs.get(entry.getName()),
874                                    moduleFile,
875                                    entry.getName(),
876                                    null,
877                                    environment,
878                                    moduleContextInfo,
879                                    earName,
880                                    naming, idBuilder);
881    
882                            if (module == null) {
883                                throw new DeploymentException("Module was not " + moduleTypeName + ": " + entry.getName());
884                            }
885    
886                            modules.add(module);
887                        }
888                    }
889                }
890    
891                //all the modules in the geronimo plan should have been found by now.
892                if (!moduleLocations.containsAll(altVendorDDs.keySet())) {
893                    throw new DeploymentException("Geronimo ear plan contains modules that aren't in the ear: " + new HashSet<String>(moduleLocations).removeAll(altVendorDDs.keySet()));
894                }
895                //deploy the extension modules
896                for (GerExtModuleType gerExtModule : gerApplication.getExtModuleArray()) {
897                    String moduleName;
898                    ModuleBuilder builder;
899                    Object moduleContextInfo = null;
900                    String moduleTypeName;
901    
902                    if (gerExtModule.isSetEjb()) {
903                        moduleName = gerExtModule.getEjb().getStringValue();
904                        builder = getEjbConfigBuilder();
905                        if (builder == null) {
906                            throw new DeploymentException("Cannot deploy ejb application; No ejb deployer defined: " + moduleName);
907                        }
908                        moduleTypeName = "an EJB";
909                    } else if (gerExtModule.isSetWeb()) {
910                        moduleName = gerExtModule.getWeb().getStringValue();
911                        if (getWebConfigBuilder() == null) {
912                            throw new DeploymentException("Cannot deploy web application; No war deployer defined: " + moduleName);
913                        }
914                        builder = getWebConfigBuilder();
915                        moduleTypeName = "a war";
916                        //ext modules must use vendor plan to set context-root
917                    } else if (gerExtModule.isSetConnector()) {
918                        moduleName = gerExtModule.getConnector().getStringValue();
919                        if (getConnectorConfigBuilder() == null) {
920                            throw new DeploymentException("Cannot deploy resource adapter; No rar deployer defined: " + moduleName);
921                        }
922                        builder = getConnectorConfigBuilder();
923                        moduleTypeName = "a connector";
924                    } else if (gerExtModule.isSetJava()) {
925                        moduleName = gerExtModule.getJava().getStringValue();
926                        if (getAppClientConfigBuilder() == null) {
927                            throw new DeploymentException("Cannot deploy app client; No app client deployer defined: " + moduleName);
928                        }
929                        builder = getAppClientConfigBuilder();
930                        moduleTypeName = "an application client";
931                    } else {
932                        throw new DeploymentException("Could not find a module builder for module: " + gerExtModule);
933                    }
934                    //dd is included explicitly
935                    XmlObject[] anys = gerExtModule.selectChildren(GerExtModuleType.type.qnameSetForWildcardElements());
936                    if (anys.length != 1) {
937                        throw new DeploymentException("Unexpected count of xs:any elements in embedded vendor plan " + anys.length + " qnameset: " + GerExtModuleType.type.qnameSetForWildcardElements());
938                    }
939                    Object vendorDD = anys[0];
940    
941                    JarFile moduleFile;
942                    if (gerExtModule.isSetInternalPath()) {
943                        String modulePath = gerExtModule.getInternalPath().trim();
944                        moduleLocations.add(modulePath);
945                        try {
946                            moduleFile = new NestedJarFile(earFile, modulePath);
947                        } catch (IOException e) {
948                            throw new DeploymentException("Invalid moduleFile: " + modulePath, e);
949                        }
950                    } else {
951                        PatternType patternType = gerExtModule.getExternalPath();
952                        String groupId = trim(patternType.getGroupId());
953                        String artifactId = trim(patternType.getArtifactId());
954                        String version = trim(patternType.getVersion());
955                        String type = trim(patternType.getType());
956                        Artifact artifact = new Artifact(groupId, artifactId, version, type);
957                        try {
958                            artifact = getArtifactResolver().resolveInClassLoader(artifact);
959                        } catch (MissingDependencyException e) {
960                            throw new DeploymentException("Could not resolve external rar location in repository: " + artifact, e);
961                        }
962                        File location = null;
963                        for (Repository repository : repositories) {
964                            if (repository.contains(artifact)) {
965                                location = repository.getLocation(artifact);
966                                break;
967                            }
968                        }
969                        if (location == null) {
970                            throw new DeploymentException(moduleTypeName + " is missing in repositories: " + artifact);
971                        }
972                        try {
973                            moduleFile = new JarFile(location);
974                        } catch (IOException e) {
975                            throw new DeploymentException("Could not access contents of " + moduleTypeName, e);
976                        }
977    
978                    }
979    
980                    Module module = builder.createModule(vendorDD,
981                            moduleFile,
982                            moduleName,
983                            null, //TODO implement an alt-spec-dd element
984                            environment,
985                            moduleContextInfo,
986                            earName,
987                            naming, idBuilder);
988    
989                    if (module == null) {
990                        throw new DeploymentException("Module was not " + moduleTypeName + ": " + moduleName);
991                    }
992    
993                    modules.add(module);
994                }
995            } finally {
996                // delete all the temp files created for alt vendor dds
997                for (Object altVendorDD : altVendorDDs.values()) {
998                    if (altVendorDD instanceof File) {
999                        ((File) altVendorDD).delete();
1000                    }
1001                }
1002            }
1003        }
1004    
1005        private ArtifactResolver getArtifactResolver() throws DeploymentException {
1006            if (artifactResolvers == null || artifactResolvers.isEmpty()) {
1007                throw new DeploymentException("No artifact resolver supplied to resolve external module");
1008            }
1009            return artifactResolvers.iterator().next();
1010        }
1011    
1012        private String trim(String s) {
1013            if (s == null) {
1014                return null;
1015            }
1016            return s.trim();
1017        }
1018    
1019        private boolean isLibraryEntry(ApplicationType application, ZipEntry entry) {
1020            String libDir = getLibraryDirectory(application);
1021            return libDir != null && entry.getName().startsWith(libDir);
1022        }
1023    
1024        private void mapVendorPlans(GerApplicationType gerApplication, Map<String, Object> altVendorDDs, JarFile earFile) throws DeploymentException {
1025            //build map from module path to alt vendor dd
1026            for (GerModuleType gerModule : gerApplication.getModuleArray()) {
1027                String path = null;
1028                if (gerModule.isSetEjb()) {
1029                    path = gerModule.getEjb().getStringValue();
1030                } else if (gerModule.isSetWeb()) {
1031                    path = gerModule.getWeb().getStringValue();
1032                } else if (gerModule.isSetConnector()) {
1033                    path = gerModule.getConnector().getStringValue();
1034                } else if (gerModule.isSetJava()) {
1035                    path = gerModule.getJava().getStringValue();
1036                }
1037    //            if (!paths.contains(path)) {
1038    //                throw new DeploymentException("Geronimo deployment plan refers to module '" + path + "' but that was not defined in the META-INF/application.xml");
1039    //            }
1040    
1041                if (gerModule.isSetAltDd()) {
1042                    // the the url of the alt dd
1043                    try {
1044                        altVendorDDs.put(path, DeploymentUtil.toTempFile(earFile, gerModule.getAltDd().getStringValue()));
1045                    } catch (IOException e) {
1046                        throw new DeploymentException("Invalid alt vendor dd url: " + gerModule.getAltDd().getStringValue(), e);
1047                    }
1048                } else {
1049                    //dd is included explicitly
1050                    XmlObject[] anys = gerModule.selectChildren(GerModuleType.type.qnameSetForWildcardElements());
1051                    if (anys.length != 1) {
1052                        throw new DeploymentException("Unexpected count of xs:any elements in embedded vendor plan " + anys.length + " qnameset: " + GerModuleType.type.qnameSetForWildcardElements());
1053                    }
1054                    altVendorDDs.put(path, anys[0]);
1055                }
1056            }
1057        }
1058    
1059        private URL getAltSpecDDURL(JarFile earFile, ModuleType moduleXml) throws DeploymentException {
1060            if (moduleXml != null && moduleXml.isSetAltDd()) {
1061                try {
1062                    return DeploymentUtil.createJarURL(earFile, moduleXml.getAltDd().getStringValue());
1063                } catch (MalformedURLException e) {
1064                    throw new DeploymentException("Invalid alt sped dd url: " + moduleXml.getAltDd().getStringValue(), e);
1065                }
1066            }
1067            return null;
1068        }
1069    
1070        private ModuleBuilder getBuilder(Module module) throws DeploymentException {
1071            if (module instanceof EJBModule) {
1072                if (getEjbConfigBuilder() == null) {
1073                    throw new DeploymentException("Cannot deploy ejb application; No ejb deployer defined: " + module.getModuleURI());
1074                }
1075                return getEjbConfigBuilder();
1076            } else if (module instanceof WebModule) {
1077                if (getWebConfigBuilder() == null) {
1078                    throw new DeploymentException("Cannot deploy web application; No war deployer defined: " + module.getModuleURI());
1079                }
1080                return getWebConfigBuilder();
1081            } else if (module instanceof ConnectorModule) {
1082                if (getConnectorConfigBuilder() == null) {
1083                    throw new DeploymentException("Cannot deploy resource adapter; No rar deployer defined: " + module.getModuleURI());
1084                }
1085                return getConnectorConfigBuilder();
1086            } else if (module instanceof AppClientModule) {
1087                if (getAppClientConfigBuilder() == null) {
1088                    throw new DeploymentException("Cannot deploy app client; No app client deployer defined: " + module.getModuleURI());
1089                }
1090                return getAppClientConfigBuilder();
1091            }
1092            throw new IllegalArgumentException("Unknown module type: " + module.getClass().getName());
1093        }
1094    
1095        public static final GBeanInfo GBEAN_INFO;
1096    
1097        static {
1098            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(EARConfigBuilder.class, NameFactory.CONFIG_BUILDER);
1099            infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
1100            infoBuilder.addAttribute("transactionManagerAbstractName", AbstractNameQuery.class, true);
1101            infoBuilder.addAttribute("connectionTrackerAbstractName", AbstractNameQuery.class, true);
1102            infoBuilder.addAttribute("transactionalTimerAbstractName", AbstractNameQuery.class, true);
1103            infoBuilder.addAttribute("nonTransactionalTimerAbstractName", AbstractNameQuery.class, true);
1104            infoBuilder.addAttribute("corbaGBeanAbstractName", AbstractNameQuery.class, true);
1105            infoBuilder.addAttribute("serverName", AbstractNameQuery.class, true);
1106    
1107            infoBuilder.addReference("Repositories", Repository.class, "Repository");
1108            infoBuilder.addReference("EJBConfigBuilder", ModuleBuilder.class, NameFactory.MODULE_BUILDER);
1109            infoBuilder.addReference("WebConfigBuilder", ModuleBuilder.class, NameFactory.MODULE_BUILDER);
1110            infoBuilder.addReference("ConnectorConfigBuilder", ModuleBuilder.class, NameFactory.MODULE_BUILDER);
1111            infoBuilder.addReference("ActivationSpecInfoLocator", ActivationSpecInfoLocator.class, NameFactory.MODULE_BUILDER);
1112            infoBuilder.addReference("AppClientConfigBuilder", ModuleBuilder.class, NameFactory.MODULE_BUILDER);
1113            infoBuilder.addReference("SecurityBuilders", NamespaceDrivenBuilder.class, NameFactory.MODULE_BUILDER);
1114            infoBuilder.addReference("ServiceBuilders", NamespaceDrivenBuilder.class, NameFactory.MODULE_BUILDER);
1115            infoBuilder.addReference("PersistenceUnitBuilders", ModuleBuilderExtension.class, NameFactory.MODULE_BUILDER);
1116            infoBuilder.addReference("ArtifactResolvers", ArtifactResolver.class, "ArtifactResolver");
1117    
1118            infoBuilder.addAttribute("kernel", Kernel.class, false);
1119    
1120            infoBuilder.setConstructor(new String[]{
1121                    "defaultEnvironment",
1122                    "transactionManagerAbstractName",
1123                    "connectionTrackerAbstractName",
1124                    "transactionalTimerAbstractName",
1125                    "nonTransactionalTimerAbstractName",
1126                    "corbaGBeanAbstractName",
1127                    "serverName",
1128                    "Repositories",
1129                    "EJBConfigBuilder",
1130                    "WebConfigBuilder",
1131                    "ConnectorConfigBuilder",
1132                    "ActivationSpecInfoLocator",
1133                    "AppClientConfigBuilder",
1134                    "SecurityBuilders",
1135                    "ServiceBuilders",
1136                    "PersistenceUnitBuilders",
1137                    "ArtifactResolvers",
1138                    "kernel"
1139            });
1140    
1141            GBEAN_INFO = infoBuilder.getBeanInfo();
1142        }
1143    
1144        public static GBeanInfo getGBeanInfo() {
1145            return GBEAN_INFO;
1146        }
1147    
1148    }