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
19 package org.apache.geronimo.naming.deployment;
20
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Set;
27
28 import javax.xml.namespace.QName;
29
30 import org.apache.geronimo.common.DeploymentException;
31 import org.apache.geronimo.deployment.service.EnvironmentBuilder;
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.schema.NamespaceElementConverter;
40 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
41 import org.apache.xmlbeans.QNameSet;
42 import org.apache.xmlbeans.SchemaType;
43 import org.apache.xmlbeans.XmlCursor;
44 import org.apache.xmlbeans.XmlObject;
45
46 /**
47 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
48 */
49 public abstract class AbstractNamingBuilder implements NamingBuilder {
50 protected static final String J2EE_NAMESPACE = "http://java.sun.com/xml/ns/j2ee";
51 protected static final String JEE_NAMESPACE = "http://java.sun.com/xml/ns/javaee";
52 protected static final NamespaceElementConverter J2EE_CONVERTER = new NamespaceElementConverter(J2EE_NAMESPACE);
53
54 private final Environment defaultEnvironment;
55
56 protected AbstractNamingBuilder() {
57 defaultEnvironment = null;
58 }
59
60 protected AbstractNamingBuilder(Environment defaultEnvironment) {
61 this.defaultEnvironment = defaultEnvironment;
62 }
63
64 public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) {
65 if (willMergeEnvironment(specDD, plan)) {
66 EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment);
67 }
68 }
69
70 protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) {
71 return false;
72 }
73
74 public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException {
75 }
76
77 protected Map getJndiContextMap(Map sharedContext) {
78 return (Map)sharedContext.get(JNDI_KEY);
79 }
80
81 protected AbstractName getGBeanName(Map sharedContext) {
82 return (AbstractName)sharedContext.get(GBEAN_NAME_KEY);
83 }
84
85 protected static QNameSet buildQNameSet(String[] eeNamespaces, String localPart) {
86 Set qnames = new HashSet(eeNamespaces.length);
87 for (int i = 0; i < eeNamespaces.length; i++) {
88 String namespace = eeNamespaces[i];
89 qnames.add(new QName(namespace, localPart));
90 }
91
92
93 return QNameSet.forSets(null, Collections.EMPTY_SET, Collections.EMPTY_SET, qnames);
94 }
95
96 protected XmlObject[] convert(XmlObject[] xmlObjects, NamespaceElementConverter converter, SchemaType type) {
97
98 XmlObject[] converted = new XmlObject[xmlObjects.length];
99 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 }