KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.geronimo.common.DeploymentException;
20 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
21 import org.apache.geronimo.deployment.ModuleIDBuilder;
22 import org.apache.geronimo.gbean.GBeanInfo;
23 import org.apache.geronimo.gbean.GBeanInfoBuilder;
24 import org.apache.geronimo.gbean.ReferenceCollection;
25 import org.apache.geronimo.gbean.ReferenceCollectionEvent;
26 import org.apache.geronimo.gbean.ReferenceCollectionListener;
27 import org.apache.geronimo.gbean.AbstractName;
28 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
29 import org.apache.geronimo.kernel.repository.Environment;
30 import org.apache.geronimo.kernel.Naming;
31 import org.apache.geronimo.kernel.config.ConfigurationStore;
32 import org.apache.xmlbeans.XmlCursor;
33 import org.apache.xmlbeans.XmlException;
34 import org.apache.xmlbeans.XmlObject;
35
36 import java.io.File JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.jar.JarFile JavaDoc;
44
45 /**
46  * @version $Rev:386276 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
47  */

48 public class SwitchingModuleBuilder implements ModuleBuilder {
49
50     private final Map JavaDoc namespaceToBuilderMap = new HashMap JavaDoc();
51
52     private String JavaDoc defaultNamespace;
53
54     public SwitchingModuleBuilder(Collection builders) {
55         ReferenceCollection buildersCollection = (ReferenceCollection) builders;
56         buildersCollection.addReferenceCollectionListener(new ReferenceCollectionListener() {
57             public void memberAdded(ReferenceCollectionEvent event) {
58                 ModuleBuilder builder = (ModuleBuilder) event.getMember();
59                 String JavaDoc namespace = builder.getSchemaNamespace();
60                 namespaceToBuilderMap.put(namespace, builder);
61             }
62
63             public void memberRemoved(ReferenceCollectionEvent event) {
64                 ModuleBuilder builder = (ModuleBuilder) event.getMember();
65                 String JavaDoc namespace = builder.getSchemaNamespace();
66                 namespaceToBuilderMap.remove(namespace);
67             }
68         });
69         for (Iterator JavaDoc iterator = builders.iterator(); iterator.hasNext();) {
70             ModuleBuilder builder = (ModuleBuilder) iterator.next();
71             String JavaDoc namespace = builder.getSchemaNamespace();
72             namespaceToBuilderMap.put(namespace, builder);
73         }
74
75     }
76
77     public String JavaDoc getDefaultNamespace() {
78         return defaultNamespace;
79     }
80
81     public void setDefaultNamespace(String JavaDoc defaultNamespace) {
82         this.defaultNamespace = defaultNamespace;
83     }
84
85     public Module createModule(File JavaDoc plan, JarFile JavaDoc moduleFile, Naming naming, ModuleIDBuilder idBuilder) throws DeploymentException {
86         String JavaDoc namespace;
87         if (plan == null) {
88             namespace = defaultNamespace;
89         } else {
90             namespace = getNamespaceFromPlan(plan);
91         }
92         ModuleBuilder builder = getBuilderFromNamespace(namespace);
93         if (builder != null) {
94             return builder.createModule(plan, moduleFile, naming, idBuilder);
95         } else {
96             return null;
97         }
98     }
99
100     private String JavaDoc getNamespaceFromPlan(Object JavaDoc plan) throws DeploymentException {
101         XmlObject xmlObject;
102         if (plan instanceof File JavaDoc) {
103             try {
104                 xmlObject = XmlBeansUtil.parse(((File JavaDoc) plan).toURL(), getClass().getClassLoader());
105             } catch (IOException JavaDoc e) {
106                 throw new DeploymentException("Could not read plan file", e);
107             } catch (XmlException e) {
108                 throw new DeploymentException("Plan file does not contain well formed xml", e);
109             }
110         } else if (plan instanceof XmlObject) {
111             xmlObject = (XmlObject) plan;
112         } else {
113             return defaultNamespace;
114         }
115         XmlCursor cursor = xmlObject.newCursor();
116         try {
117             while (cursor.hasNextToken()){
118                 if (cursor.isStart()) {
119                     return cursor.getName().getNamespaceURI();
120                 }
121                 cursor.toNextToken();
122             }
123         } finally {
124             cursor.dispose();
125         }
126         throw new DeploymentException("Cannot find namespace in xmlObject: " + xmlObject.xmlText());
127     }
128
129     private ModuleBuilder getBuilderFromNamespace(String JavaDoc namespace) {
130         ModuleBuilder builder = (ModuleBuilder) namespaceToBuilderMap.get(namespace);
131         if (builder == null) {
132             builder = (ModuleBuilder) namespaceToBuilderMap.get(defaultNamespace);
133         }
134         if (builder == null && namespaceToBuilderMap.size() == 1) {
135             builder = (ModuleBuilder) namespaceToBuilderMap.values().iterator().next();
136         }
137         return builder;
138     }
139
140     public Module createModule(Object JavaDoc plan, JarFile JavaDoc moduleFile, String JavaDoc targetPath, URL JavaDoc specDDUrl, Environment environment, Object JavaDoc moduleContextInfo, AbstractName earName, Naming naming, ModuleIDBuilder idBuilder) throws DeploymentException {
141         String JavaDoc namespace = getNamespaceFromPlan(plan);
142         ModuleBuilder builder = getBuilderFromNamespace(namespace);
143         if (builder != null) {
144             return builder.createModule(plan, moduleFile, targetPath, specDDUrl, environment, moduleContextInfo, earName, naming, idBuilder);
145         } else {
146             return null;
147         }
148     }
149
150     public void installModule(JarFile JavaDoc earFile, EARContext earContext, Module module, Collection configurationStores, ConfigurationStore targetConfigurationStore, Collection repositories) throws DeploymentException {
151         String JavaDoc namespace = module.getNamespace();
152         ModuleBuilder builder = getBuilderFromNamespace(namespace);
153         builder.installModule(earFile, earContext, module, configurationStores, targetConfigurationStore, repositories);
154     }
155
156     public void initContext(EARContext earContext, Module module, ClassLoader JavaDoc cl) throws DeploymentException {
157         String JavaDoc namespace = module.getNamespace();
158         ModuleBuilder builder = getBuilderFromNamespace(namespace);
159         builder.initContext(earContext, module, cl);
160     }
161
162     public void addGBeans(EARContext earContext, Module module, ClassLoader JavaDoc cl, Collection repositories) throws DeploymentException {
163         String JavaDoc namespace = module.getNamespace();
164         ModuleBuilder builder = getBuilderFromNamespace(namespace);
165         builder.addGBeans(earContext, module, cl, repositories);
166     }
167
168     public String JavaDoc getSchemaNamespace() {
169         return null;
170     }
171
172     public static final GBeanInfo GBEAN_INFO;
173
174     static {
175         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(SwitchingModuleBuilder.class, NameFactory.MODULE_BUILDER);
176         infoBuilder.addAttribute("defaultNamespace", String JavaDoc.class, true, true);
177         infoBuilder.addReference("ModuleBuilders", ModuleBuilder.class, NameFactory.MODULE_BUILDER);
178         infoBuilder.addInterface(ModuleBuilder.class);
179
180         infoBuilder.setConstructor(new String JavaDoc[] {"ModuleBuilders"});
181         GBEAN_INFO = infoBuilder.getBeanInfo();
182     }
183
184     public static GBeanInfo getGBeanInfo() {
185         return GBEAN_INFO;
186     }
187 }
188
Popular Tags