001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.geronimo.naming.deployment; 020 021 import java.util.Collections; 022 import java.util.HashMap; 023 import java.util.HashSet; 024 import java.util.Iterator; 025 import java.util.Map; 026 import java.util.Set; 027 028 import javax.xml.namespace.QName; 029 030 import org.apache.geronimo.common.DeploymentException; 031 import org.apache.geronimo.deployment.service.EnvironmentBuilder; 032 import org.apache.geronimo.gbean.AbstractName; 033 import org.apache.geronimo.gbean.AbstractNameQuery; 034 import org.apache.geronimo.j2ee.deployment.Module; 035 import org.apache.geronimo.j2ee.deployment.NamingBuilder; 036 import org.apache.geronimo.kernel.config.Configuration; 037 import org.apache.geronimo.kernel.repository.Artifact; 038 import org.apache.geronimo.kernel.repository.Environment; 039 import org.apache.geronimo.schema.NamespaceElementConverter; 040 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType; 041 import org.apache.xmlbeans.QNameSet; 042 import org.apache.xmlbeans.SchemaType; 043 import org.apache.xmlbeans.XmlCursor; 044 import org.apache.xmlbeans.XmlObject; 045 046 /** 047 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $ 048 */ 049 public abstract class AbstractNamingBuilder implements NamingBuilder { 050 protected static final String J2EE_NAMESPACE = "http://java.sun.com/xml/ns/j2ee"; 051 protected static final String JEE_NAMESPACE = "http://java.sun.com/xml/ns/javaee"; 052 protected static final NamespaceElementConverter J2EE_CONVERTER = new NamespaceElementConverter(J2EE_NAMESPACE); 053 054 private final Environment defaultEnvironment; 055 056 protected AbstractNamingBuilder() { 057 defaultEnvironment = null; 058 } 059 060 protected AbstractNamingBuilder(Environment defaultEnvironment) { 061 this.defaultEnvironment = defaultEnvironment; 062 } 063 064 public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) { 065 if (willMergeEnvironment(specDD, plan)) { 066 EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment); 067 } 068 } 069 070 protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) { 071 return false; 072 } 073 074 public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException { 075 } 076 077 protected Map getJndiContextMap(Map sharedContext) { 078 return (Map)sharedContext.get(JNDI_KEY); 079 } 080 081 protected AbstractName getGBeanName(Map sharedContext) { 082 return (AbstractName)sharedContext.get(GBEAN_NAME_KEY); 083 } 084 085 protected static QNameSet buildQNameSet(String[] eeNamespaces, String localPart) { 086 Set qnames = new HashSet(eeNamespaces.length); 087 for (int i = 0; i < eeNamespaces.length; i++) { 088 String namespace = eeNamespaces[i]; 089 qnames.add(new QName(namespace, localPart)); 090 } 091 //xmlbeans 2.0 has a bug so forArray doesn't work. Don't know if it's fixed in later xmlbeans versions 092 //return QNameSet.forArray(qnames); 093 return QNameSet.forSets(null, Collections.EMPTY_SET, Collections.EMPTY_SET, qnames); 094 } 095 096 protected XmlObject[] convert(XmlObject[] xmlObjects, NamespaceElementConverter converter, SchemaType type) { 097 //bizarre ArrayStoreException if xmlObjects is loaded by the wrong classloader 098 XmlObject[] converted = new XmlObject[xmlObjects.length]; 099 for (int i = 0; i < xmlObjects.length; i++) { 100 XmlObject xmlObject = xmlObjects[i]; 101 if (xmlObject.schemaType() != type) { 102 xmlObject = xmlObject.copy(); 103 XmlCursor start =xmlObject.newCursor(); 104 XmlCursor end = xmlObject.newCursor(); 105 106 try { 107 converter.convertElement(start, end); 108 } finally { 109 start.dispose(); 110 end.dispose(); 111 } 112 converted[i] = xmlObject.changeType(type); 113 } else { 114 converted[i] = xmlObject; 115 } 116 } 117 return converted; 118 } 119 protected static String getStringValue(org.apache.geronimo.xbeans.j2ee.String string) { 120 if (string == null) { 121 return null; 122 } 123 String s = string.getStringValue(); 124 return s == null ? null : s.trim(); 125 } 126 127 public static AbstractNameQuery buildAbstractNameQuery(GerPatternType pattern, String type, String moduleType, Set interfaceTypes) { 128 String groupId = pattern.isSetGroupId() ? pattern.getGroupId().trim() : null; 129 String artifactid = pattern.isSetArtifactId() ? pattern.getArtifactId().trim() : null; 130 String version = pattern.isSetVersion() ? pattern.getVersion().trim() : null; 131 String module = pattern.isSetModule() ? pattern.getModule().trim() : null; 132 String name = pattern.getName().trim(); 133 134 Artifact artifact = artifactid != null ? new Artifact(groupId, artifactid, version, null) : null; 135 Map nameMap = new HashMap(); 136 nameMap.put("name", name); 137 if (type != null) { 138 nameMap.put("j2eeType", type); 139 } 140 if (module != null && moduleType != null) { 141 nameMap.put(moduleType, module); 142 } 143 if(interfaceTypes != null) { 144 Set trimmed = new HashSet(); 145 for (Iterator it = interfaceTypes.iterator(); it.hasNext();) { 146 String intf = (String) it.next(); 147 trimmed.add(intf == null ? null : intf.trim()); 148 } 149 interfaceTypes = trimmed; 150 } 151 return new AbstractNameQuery(artifact, nameMap, interfaceTypes); 152 } 153 154 public static AbstractNameQuery buildAbstractNameQuery(Artifact configId, String module, String name, String type, String moduleType) { 155 Map nameMap = new HashMap(); 156 nameMap.put("name", name); 157 if (type != null) { 158 nameMap.put("j2eeType", type); 159 } 160 if (module != null) { 161 nameMap.put(moduleType, module); 162 } 163 return new AbstractNameQuery(configId, nameMap); 164 } 165 166 public static Class assureInterface(String interfaceName, String superInterfaceName, String interfaceType, ClassLoader cl) throws DeploymentException { 167 if (interfaceName == null || interfaceName.equals("")) { 168 throw new DeploymentException("interface name cannot be blank"); 169 } 170 Class clazz; 171 try { 172 clazz = cl.loadClass(interfaceName); 173 } catch (ClassNotFoundException e) { 174 throw new DeploymentException(interfaceType + " interface class not found: " + interfaceName); 175 } 176 if (!clazz.isInterface()) { 177 throw new DeploymentException(interfaceType + " interface is not an interface: " + interfaceName); 178 } 179 Class superInterface; 180 try { 181 superInterface = cl.loadClass(superInterfaceName); 182 } catch (ClassNotFoundException e) { 183 throw new DeploymentException("Class " + superInterfaceName + " could not be loaded"); 184 } 185 if (!superInterface.isAssignableFrom(clazz)) { 186 throw new DeploymentException(interfaceType + " interface does not extend " + superInterfaceName + ": " + interfaceName); 187 } 188 return clazz; 189 } 190 191 192 }