1 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 ; 37 import java.io.IOException ; 38 import java.net.URL ; 39 import java.util.Collection ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.Map ; 43 import java.util.jar.JarFile ; 44 45 48 public class SwitchingModuleBuilder implements ModuleBuilder { 49 50 private final Map namespaceToBuilderMap = new HashMap (); 51 52 private String 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 namespace = builder.getSchemaNamespace(); 60 namespaceToBuilderMap.put(namespace, builder); 61 } 62 63 public void memberRemoved(ReferenceCollectionEvent event) { 64 ModuleBuilder builder = (ModuleBuilder) event.getMember(); 65 String namespace = builder.getSchemaNamespace(); 66 namespaceToBuilderMap.remove(namespace); 67 } 68 }); 69 for (Iterator iterator = builders.iterator(); iterator.hasNext();) { 70 ModuleBuilder builder = (ModuleBuilder) iterator.next(); 71 String namespace = builder.getSchemaNamespace(); 72 namespaceToBuilderMap.put(namespace, builder); 73 } 74 75 } 76 77 public String getDefaultNamespace() { 78 return defaultNamespace; 79 } 80 81 public void setDefaultNamespace(String defaultNamespace) { 82 this.defaultNamespace = defaultNamespace; 83 } 84 85 public Module createModule(File plan, JarFile moduleFile, Naming naming, ModuleIDBuilder idBuilder) throws DeploymentException { 86 String 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 getNamespaceFromPlan(Object plan) throws DeploymentException { 101 XmlObject xmlObject; 102 if (plan instanceof File ) { 103 try { 104 xmlObject = XmlBeansUtil.parse(((File ) plan).toURL(), getClass().getClassLoader()); 105 } catch (IOException 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 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 plan, JarFile moduleFile, String targetPath, URL specDDUrl, Environment environment, Object moduleContextInfo, AbstractName earName, Naming naming, ModuleIDBuilder idBuilder) throws DeploymentException { 141 String 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 earFile, EARContext earContext, Module module, Collection configurationStores, ConfigurationStore targetConfigurationStore, Collection repositories) throws DeploymentException { 151 String 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 cl) throws DeploymentException { 157 String 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 cl, Collection repositories) throws DeploymentException { 163 String namespace = module.getNamespace(); 164 ModuleBuilder builder = getBuilderFromNamespace(namespace); 165 builder.addGBeans(earContext, module, cl, repositories); 166 } 167 168 public String 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 .class, true, true); 177 infoBuilder.addReference("ModuleBuilders", ModuleBuilder.class, NameFactory.MODULE_BUILDER); 178 infoBuilder.addInterface(ModuleBuilder.class); 179 180 infoBuilder.setConstructor(new String [] {"ModuleBuilders"}); 181 GBEAN_INFO = infoBuilder.getBeanInfo(); 182 } 183 184 public static GBeanInfo getGBeanInfo() { 185 return GBEAN_INFO; 186 } 187 } 188 | Popular Tags |