1 17 18 package org.apache.geronimo.naming.deployment; 19 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.xml.namespace.QName ; 28 29 import org.apache.geronimo.common.DeploymentException; 30 import org.apache.geronimo.deployment.service.EnvironmentBuilder; 31 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil; 32 import org.apache.geronimo.gbean.AbstractName; 33 import org.apache.geronimo.gbean.AbstractNameQuery; 34 import org.apache.geronimo.j2ee.deployment.Module; 35 import org.apache.geronimo.j2ee.deployment.NamingBuilder; 36 import org.apache.geronimo.kernel.config.Configuration; 37 import org.apache.geronimo.kernel.repository.Artifact; 38 import org.apache.geronimo.kernel.repository.Environment; 39 import org.apache.geronimo.kernel.repository.Dependency; 40 import org.apache.geronimo.kernel.repository.ImportType; 41 import org.apache.geronimo.schema.NamespaceElementConverter; 42 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType; 43 import org.apache.geronimo.xbeans.geronimo.naming.GerAbstractNamingEntryDocument; 44 import org.apache.xmlbeans.QNameSet; 45 import org.apache.xmlbeans.SchemaType; 46 import org.apache.xmlbeans.XmlObject; 47 import org.apache.xmlbeans.XmlException; 48 49 52 public abstract class AbstractNamingBuilder implements NamingBuilder { 53 protected static final String J2EE_NAMESPACE = "http://java.sun.com/xml/ns/j2ee"; 54 protected static final String JEE_NAMESPACE = "http://java.sun.com/xml/ns/javaee"; 55 protected static final NamespaceElementConverter J2EE_CONVERTER = new NamespaceElementConverter(J2EE_NAMESPACE); 56 protected static final NamespaceElementConverter NAMING_CONVERTER = new NamespaceElementConverter(GerAbstractNamingEntryDocument.type.getDocumentElementName().getNamespaceURI()); 57 58 private final Environment defaultEnvironment; 59 60 protected AbstractNamingBuilder() { 61 defaultEnvironment = null; 62 } 63 64 protected AbstractNamingBuilder(Environment defaultEnvironment) { 65 this.defaultEnvironment = defaultEnvironment; 66 } 67 68 public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) throws DeploymentException { 69 if (willMergeEnvironment(specDD, plan)) { 70 EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment); 71 } 72 } 73 74 protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) throws DeploymentException { 75 return false; 76 } 77 78 protected boolean matchesDefaultEnvironment(Environment environment) { 79 for (Iterator iterator = defaultEnvironment.getDependencies().iterator(); iterator.hasNext();) { 80 Dependency defaultDependency = (Dependency) iterator.next(); 81 boolean matches = false; 82 for (Iterator iterator1 = environment.getDependencies().iterator(); iterator1.hasNext();) { 83 Dependency actualDependency = (Dependency) iterator1.next(); 84 if (matches(defaultDependency, actualDependency)) { 85 matches = true; 86 break; 87 } 88 } 89 if (!matches) { 90 return false; 91 } 92 } 93 return true; 94 } 95 96 private boolean matches(Dependency defaultDependency, Dependency actualDependency) { 97 if (defaultDependency.getArtifact().matches(actualDependency.getArtifact()) 98 || actualDependency.getArtifact().matches(defaultDependency.getArtifact())) { 99 return defaultDependency.getImportType() == actualDependency.getImportType() 100 || actualDependency.getImportType() == ImportType.ALL; 101 } 102 return false; 103 } 104 105 public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException { 106 } 107 108 protected Map getJndiContextMap(Map sharedContext) { 109 return (Map ) sharedContext.get(JNDI_KEY); 110 } 111 112 protected AbstractName getGBeanName(Map sharedContext) { 113 return (AbstractName) sharedContext.get(GBEAN_NAME_KEY); 114 } 115 116 protected static QNameSet buildQNameSet(String [] eeNamespaces, String localPart) { 117 Set qnames = new HashSet (eeNamespaces.length); 118 for (int i = 0; i < eeNamespaces.length; i++) { 119 String namespace = eeNamespaces[i]; 120 qnames.add(new QName (namespace, localPart)); 121 } 122 return QNameSet.forSets(null, Collections.EMPTY_SET, Collections.EMPTY_SET, qnames); 125 } 126 127 protected static XmlObject[] convert(XmlObject[] xmlObjects, NamespaceElementConverter converter, SchemaType type) throws DeploymentException { 128 XmlObject[] converted = new XmlObject[xmlObjects.length]; 130 for (int i = 0; i < xmlObjects.length; i++) { 131 XmlObject xmlObject = xmlObjects[i].copy(); 132 if (xmlObject.schemaType() != type) { 133 converter.convertElement(xmlObject); 134 converted[i] = xmlObject.changeType(type); 135 } else { 136 converted[i] = xmlObject; 137 } 138 try { 139 XmlBeansUtil.validateDD(converted[i]); 140 } catch (XmlException e) { 141 throw new DeploymentException("Could not validate xmlObject of type " + type, e); 142 } 143 } 144 return converted; 145 } 146 147 protected static String getStringValue(org.apache.geronimo.xbeans.j2ee.String string) { 148 if (string == null) { 149 return null; 150 } 151 String s = string.getStringValue(); 152 return s == null ? null : s.trim(); 153 } 154 155 public static AbstractNameQuery buildAbstractNameQuery(GerPatternType pattern, String type, String moduleType, Set interfaceTypes) { 156 String groupId = pattern.isSetGroupId() ? pattern.getGroupId().trim() : null; 157 String artifactid = pattern.isSetArtifactId() ? pattern.getArtifactId().trim() : null; 158 String version = pattern.isSetVersion() ? pattern.getVersion().trim() : null; 159 String module = pattern.isSetModule() ? pattern.getModule().trim() : null; 160 String name = pattern.getName().trim(); 161 162 Artifact artifact = artifactid != null ? new Artifact(groupId, artifactid, version, null) : null; 163 Map nameMap = new HashMap (); 164 nameMap.put("name", name); 165 if (type != null) { 166 nameMap.put("j2eeType", type); 167 } 168 if (module != null && moduleType != null) { 169 nameMap.put(moduleType, module); 170 } 171 if (interfaceTypes != null) { 172 Set trimmed = new HashSet (); 173 for (Iterator it = interfaceTypes.iterator(); it.hasNext();) { 174 String intf = (String ) it.next(); 175 trimmed.add(intf == null ? null : intf.trim()); 176 } 177 interfaceTypes = trimmed; 178 } 179 return new AbstractNameQuery(artifact, nameMap, interfaceTypes); 180 } 181 182 public static AbstractNameQuery buildAbstractNameQuery(Artifact configId, String module, String name, String type, String moduleType) { 183 Map nameMap = new HashMap (); 184 nameMap.put("name", name); 185 if (type != null) { 186 nameMap.put("j2eeType", type); 187 } 188 if (module != null) { 189 nameMap.put(moduleType, module); 190 } 191 return new AbstractNameQuery(configId, nameMap); 192 } 193 194 public static Class assureInterface(String interfaceName, String superInterfaceName, String interfaceType, ClassLoader cl) throws DeploymentException { 195 if (interfaceName == null || interfaceName.equals("")) { 196 throw new DeploymentException("interface name cannot be blank"); 197 } 198 Class clazz; 199 try { 200 clazz = cl.loadClass(interfaceName); 201 } catch (ClassNotFoundException e) { 202 throw new DeploymentException(interfaceType + " interface class not found: " + interfaceName); 203 } 204 if (!clazz.isInterface()) { 205 throw new DeploymentException(interfaceType + " interface is not an interface: " + interfaceName); 206 } 207 Class superInterface; 208 try { 209 superInterface = cl.loadClass(superInterfaceName); 210 } catch (ClassNotFoundException e) { 211 throw new DeploymentException("Class " + superInterfaceName + " could not be loaded"); 212 } 213 if (!superInterface.isAssignableFrom(clazz)) { 214 throw new DeploymentException(interfaceType + " interface does not extend " + superInterfaceName + ": " + interfaceName); 215 } 216 return clazz; 217 } 218 219 220 } 221 | Popular Tags |