1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.geronimo.j2ee.deployment;
19
20 import org.apache.geronimo.common.DeploymentException;
21 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
22 import org.apache.geronimo.deployment.ModuleIDBuilder;
23 import org.apache.geronimo.gbean.GBeanInfo;
24 import org.apache.geronimo.gbean.GBeanInfoBuilder;
25 import org.apache.geronimo.gbean.ReferenceCollection;
26 import org.apache.geronimo.gbean.ReferenceCollectionEvent;
27 import org.apache.geronimo.gbean.ReferenceCollectionListener;
28 import org.apache.geronimo.gbean.AbstractName;
29 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
30 import org.apache.geronimo.kernel.repository.Environment;
31 import org.apache.geronimo.kernel.Naming;
32 import org.apache.geronimo.kernel.config.ConfigurationStore;
33 import org.apache.xmlbeans.XmlCursor;
34 import org.apache.xmlbeans.XmlException;
35 import org.apache.xmlbeans.XmlObject;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.net.URL;
40 import java.util.Collection;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.Map;
44 import java.util.jar.JarFile;
45
46 /**
47 * @version $Rev:386276 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
48 */
49 public class SwitchingModuleBuilder implements ModuleBuilder {
50
51 private final Map namespaceToBuilderMap = new HashMap();
52
53 private String defaultNamespace;
54
55 public SwitchingModuleBuilder(Collection builders) {
56 ReferenceCollection buildersCollection = (ReferenceCollection) builders;
57 buildersCollection.addReferenceCollectionListener(new ReferenceCollectionListener() {
58 public void memberAdded(ReferenceCollectionEvent event) {
59 ModuleBuilder builder = (ModuleBuilder) event.getMember();
60 String namespace = builder.getSchemaNamespace();
61 namespaceToBuilderMap.put(namespace, builder);
62 }
63
64 public void memberRemoved(ReferenceCollectionEvent event) {
65 ModuleBuilder builder = (ModuleBuilder) event.getMember();
66 String namespace = builder.getSchemaNamespace();
67 namespaceToBuilderMap.remove(namespace);
68 }
69 });
70 for (Iterator iterator = builders.iterator(); iterator.hasNext();) {
71 ModuleBuilder builder = (ModuleBuilder) iterator.next();
72 String namespace = builder.getSchemaNamespace();
73 namespaceToBuilderMap.put(namespace, builder);
74 }
75
76 }
77
78 public String getDefaultNamespace() {
79 return defaultNamespace;
80 }
81
82 public void setDefaultNamespace(String defaultNamespace) {
83 this.defaultNamespace = defaultNamespace;
84 }
85
86 public Module createModule(File plan, JarFile moduleFile, Naming naming, ModuleIDBuilder idBuilder) throws DeploymentException {
87 String namespace;
88 if (plan == null) {
89 namespace = defaultNamespace;
90 } else {
91 namespace = getNamespaceFromPlan(plan);
92 }
93 ModuleBuilder builder = getBuilderFromNamespace(namespace);
94 if (builder != null) {
95 return builder.createModule(plan, moduleFile, naming, idBuilder);
96 } else {
97 return null;
98 }
99 }
100
101 private String getNamespaceFromPlan(Object plan) throws DeploymentException {
102 XmlObject xmlObject;
103 if (plan instanceof File) {
104 try {
105 xmlObject = XmlBeansUtil.parse(((File) plan).toURL(), getClass().getClassLoader());
106 } catch (IOException e) {
107 throw new DeploymentException("Could not read plan file", e);
108 } catch (XmlException e) {
109 throw new DeploymentException("Plan file does not contain well formed xml", e);
110 }
111 } else if (plan instanceof XmlObject) {
112 xmlObject = (XmlObject) plan;
113 } else {
114 return defaultNamespace;
115 }
116 XmlCursor cursor = xmlObject.newCursor();
117 try {
118 while (cursor.hasNextToken()){
119 if (cursor.isStart()) {
120 return cursor.getName().getNamespaceURI();
121 }
122 cursor.toNextToken();
123 }
124 } finally {
125 cursor.dispose();
126 }
127 throw new DeploymentException("Cannot find namespace in xmlObject: " + xmlObject.xmlText());
128 }
129
130 private ModuleBuilder getBuilderFromNamespace(String namespace) {
131 ModuleBuilder builder = (ModuleBuilder) namespaceToBuilderMap.get(namespace);
132 if (builder == null) {
133 builder = (ModuleBuilder) namespaceToBuilderMap.get(defaultNamespace);
134 }
135 return builder;
136 }
137
138 public Module createModule(Object plan, JarFile moduleFile, String targetPath, URL specDDUrl, Environment environment, Object moduleContextInfo, AbstractName earName, Naming naming, ModuleIDBuilder idBuilder) throws DeploymentException {
139 String namespace = getNamespaceFromPlan(plan);
140 ModuleBuilder builder = getBuilderFromNamespace(namespace);
141 if (builder != null) {
142 return builder.createModule(plan, moduleFile, targetPath, specDDUrl, environment, moduleContextInfo, earName, naming, idBuilder);
143 } else {
144 return null;
145 }
146 }
147
148 public void installModule(JarFile earFile, EARContext earContext, Module module, Collection configurationStores, ConfigurationStore targetConfigurationStore, Collection repositories) throws DeploymentException {
149 String namespace = module.getNamespace();
150 ModuleBuilder builder = getBuilderFromNamespace(namespace);
151 builder.installModule(earFile, earContext, module, configurationStores, targetConfigurationStore, repositories);
152 }
153
154 public void initContext(EARContext earContext, Module module, ClassLoader cl) throws DeploymentException {
155 String namespace = module.getNamespace();
156 ModuleBuilder builder = getBuilderFromNamespace(namespace);
157 builder.initContext(earContext, module, cl);
158 }
159
160 public void addGBeans(EARContext earContext, Module module, ClassLoader cl, Collection repositories) throws DeploymentException {
161 String namespace = module.getNamespace();
162 ModuleBuilder builder = getBuilderFromNamespace(namespace);
163 builder.addGBeans(earContext, module, cl, repositories);
164 }
165
166 public String getSchemaNamespace() {
167 return null;
168 }
169
170 public static final GBeanInfo GBEAN_INFO;
171
172 static {
173 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(SwitchingModuleBuilder.class, NameFactory.MODULE_BUILDER);
174 infoBuilder.addAttribute("defaultNamespace", String.class, true, true);
175 infoBuilder.addReference("ModuleBuilders", ModuleBuilder.class, NameFactory.MODULE_BUILDER);
176 infoBuilder.addInterface(ModuleBuilder.class);
177
178 infoBuilder.setConstructor(new String[] {"ModuleBuilders"});
179 GBEAN_INFO = infoBuilder.getBeanInfo();
180 }
181
182 public static GBeanInfo getGBeanInfo() {
183 return GBEAN_INFO;
184 }
185 }