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: */package org.apache.geronimo.j2ee.deployment;
017:
018: import org.apache.geronimo.common.DeploymentException;
019: import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
020: import org.apache.geronimo.deployment.ModuleIDBuilder;
021: import org.apache.geronimo.gbean.GBeanInfo;
022: import org.apache.geronimo.gbean.GBeanInfoBuilder;
023: import org.apache.geronimo.gbean.ReferenceCollection;
024: import org.apache.geronimo.gbean.ReferenceCollectionEvent;
025: import org.apache.geronimo.gbean.ReferenceCollectionListener;
026: import org.apache.geronimo.gbean.AbstractName;
027: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
028: import org.apache.geronimo.kernel.repository.Environment;
029: import org.apache.geronimo.kernel.Naming;
030: import org.apache.geronimo.kernel.config.ConfigurationStore;
031: import org.apache.xmlbeans.XmlCursor;
032: import org.apache.xmlbeans.XmlException;
033: import org.apache.xmlbeans.XmlObject;
034:
035: import java.io.File;
036: import java.io.IOException;
037: import java.net.URL;
038: import java.util.Collection;
039: import java.util.HashMap;
040: import java.util.Iterator;
041: import java.util.Map;
042: import java.util.jar.JarFile;
043:
044: /**
045: * @version $Rev:386276 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
046: */
047: public class SwitchingModuleBuilder implements ModuleBuilder {
048:
049: private final Map namespaceToBuilderMap = new HashMap();
050:
051: private String defaultNamespace;
052:
053: public SwitchingModuleBuilder(Collection builders) {
054: ReferenceCollection buildersCollection = (ReferenceCollection) builders;
055: buildersCollection
056: .addReferenceCollectionListener(new ReferenceCollectionListener() {
057: public void memberAdded(
058: ReferenceCollectionEvent event) {
059: ModuleBuilder builder = (ModuleBuilder) event
060: .getMember();
061: String namespace = builder.getSchemaNamespace();
062: namespaceToBuilderMap.put(namespace, builder);
063: }
064:
065: public void memberRemoved(
066: ReferenceCollectionEvent event) {
067: ModuleBuilder builder = (ModuleBuilder) event
068: .getMember();
069: String namespace = builder.getSchemaNamespace();
070: namespaceToBuilderMap.remove(namespace);
071: }
072: });
073: for (Iterator iterator = builders.iterator(); iterator
074: .hasNext();) {
075: ModuleBuilder builder = (ModuleBuilder) iterator.next();
076: String namespace = builder.getSchemaNamespace();
077: namespaceToBuilderMap.put(namespace, builder);
078: }
079:
080: }
081:
082: public String getDefaultNamespace() {
083: return defaultNamespace;
084: }
085:
086: public void setDefaultNamespace(String defaultNamespace) {
087: this .defaultNamespace = defaultNamespace;
088: }
089:
090: public Module createModule(File plan, JarFile moduleFile,
091: Naming naming, ModuleIDBuilder idBuilder)
092: throws DeploymentException {
093: String namespace;
094: if (plan == null) {
095: namespace = defaultNamespace;
096: } else {
097: namespace = getNamespaceFromPlan(plan);
098: }
099: ModuleBuilder builder = getBuilderFromNamespace(namespace);
100: if (builder != null) {
101: return builder.createModule(plan, moduleFile, naming,
102: idBuilder);
103: } else {
104: return null;
105: }
106: }
107:
108: private String getNamespaceFromPlan(Object plan)
109: throws DeploymentException {
110: XmlObject xmlObject;
111: if (plan instanceof File) {
112: try {
113: xmlObject = XmlBeansUtil.parse(((File) plan).toURL(),
114: getClass().getClassLoader());
115: } catch (IOException e) {
116: throw new DeploymentException(
117: "Could not read plan file", e);
118: } catch (XmlException e) {
119: throw new DeploymentException(
120: "Plan file does not contain well formed xml", e);
121: }
122: } else if (plan instanceof XmlObject) {
123: xmlObject = (XmlObject) plan;
124: } else {
125: return defaultNamespace;
126: }
127: XmlCursor cursor = xmlObject.newCursor();
128: try {
129: while (cursor.hasNextToken()) {
130: if (cursor.isStart()) {
131: return cursor.getName().getNamespaceURI();
132: }
133: cursor.toNextToken();
134: }
135: } finally {
136: cursor.dispose();
137: }
138: throw new DeploymentException(
139: "Cannot find namespace in xmlObject: "
140: + xmlObject.xmlText());
141: }
142:
143: private ModuleBuilder getBuilderFromNamespace(String namespace) {
144: ModuleBuilder builder = (ModuleBuilder) namespaceToBuilderMap
145: .get(namespace);
146: if (builder == null) {
147: builder = (ModuleBuilder) namespaceToBuilderMap
148: .get(defaultNamespace);
149: }
150: if (builder == null && namespaceToBuilderMap.size() == 1) {
151: builder = (ModuleBuilder) namespaceToBuilderMap.values()
152: .iterator().next();
153: }
154: return builder;
155: }
156:
157: public Module createModule(Object plan, JarFile moduleFile,
158: String targetPath, URL specDDUrl, Environment environment,
159: Object moduleContextInfo, AbstractName earName,
160: Naming naming, ModuleIDBuilder idBuilder)
161: throws DeploymentException {
162: String namespace = getNamespaceFromPlan(plan);
163: ModuleBuilder builder = getBuilderFromNamespace(namespace);
164: if (builder != null) {
165: return builder.createModule(plan, moduleFile, targetPath,
166: specDDUrl, environment, moduleContextInfo, earName,
167: naming, idBuilder);
168: } else {
169: return null;
170: }
171: }
172:
173: public void installModule(JarFile earFile, EARContext earContext,
174: Module module, Collection configurationStores,
175: ConfigurationStore targetConfigurationStore,
176: Collection repositories) throws DeploymentException {
177: String namespace = module.getNamespace();
178: ModuleBuilder builder = getBuilderFromNamespace(namespace);
179: builder.installModule(earFile, earContext, module,
180: configurationStores, targetConfigurationStore,
181: repositories);
182: }
183:
184: public void initContext(EARContext earContext, Module module,
185: ClassLoader cl) throws DeploymentException {
186: String namespace = module.getNamespace();
187: ModuleBuilder builder = getBuilderFromNamespace(namespace);
188: builder.initContext(earContext, module, cl);
189: }
190:
191: public void addGBeans(EARContext earContext, Module module,
192: ClassLoader cl, Collection repositories)
193: throws DeploymentException {
194: String namespace = module.getNamespace();
195: ModuleBuilder builder = getBuilderFromNamespace(namespace);
196: builder.addGBeans(earContext, module, cl, repositories);
197: }
198:
199: public String getSchemaNamespace() {
200: return null;
201: }
202:
203: public static final GBeanInfo GBEAN_INFO;
204:
205: static {
206: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
207: SwitchingModuleBuilder.class,
208: NameFactory.MODULE_BUILDER);
209: infoBuilder.addAttribute("defaultNamespace", String.class,
210: true, true);
211: infoBuilder.addReference("ModuleBuilders", ModuleBuilder.class,
212: NameFactory.MODULE_BUILDER);
213: infoBuilder.addInterface(ModuleBuilder.class);
214:
215: infoBuilder.setConstructor(new String[] { "ModuleBuilders" });
216: GBEAN_INFO = infoBuilder.getBeanInfo();
217: }
218:
219: public static GBeanInfo getGBeanInfo() {
220: return GBEAN_INFO;
221: }
222: }
|