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.naming.deployment;
017:
018: import java.util.ArrayList;
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import javax.xml.namespace.QName;
028:
029: import org.apache.geronimo.common.DeploymentException;
030: import org.apache.geronimo.deployment.service.EnvironmentBuilder;
031: import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
032: import org.apache.geronimo.gbean.AbstractName;
033: import org.apache.geronimo.gbean.AbstractNameQuery;
034: import org.apache.geronimo.j2ee.annotation.Holder;
035: import org.apache.geronimo.j2ee.annotation.Injection;
036: import org.apache.geronimo.j2ee.deployment.Module;
037: import org.apache.geronimo.j2ee.deployment.NamingBuilder;
038: import org.apache.geronimo.kernel.config.Configuration;
039: import org.apache.geronimo.kernel.repository.Artifact;
040: import org.apache.geronimo.kernel.repository.Dependency;
041: import org.apache.geronimo.kernel.repository.Environment;
042: import org.apache.geronimo.kernel.repository.ImportType;
043: import org.apache.geronimo.schema.NamespaceElementConverter;
044: import org.apache.geronimo.xbeans.geronimo.naming.GerAbstractNamingEntryDocument;
045: import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
046: import org.apache.geronimo.xbeans.javaee.InjectionTargetType;
047: import org.apache.geronimo.xbeans.javaee.XsdStringType;
048: import org.apache.xmlbeans.QNameSet;
049: import org.apache.xmlbeans.SchemaType;
050: import org.apache.xmlbeans.XmlCursor;
051: import org.apache.xmlbeans.XmlException;
052: import org.apache.xmlbeans.XmlObject;
053:
054: /**
055: * @version $Rev: 608606 $ $Date: 2008-01-03 11:35:04 -0800 (Thu, 03 Jan 2008) $
056: */
057: public abstract class AbstractNamingBuilder implements NamingBuilder {
058: protected static final String J2EE_NAMESPACE = "http://java.sun.com/xml/ns/j2ee";
059: protected static final String JEE_NAMESPACE = "http://java.sun.com/xml/ns/javaee";
060: protected static final NamespaceElementConverter J2EE_CONVERTER = new NamespaceElementConverter(
061: J2EE_NAMESPACE);
062: protected static final NamespaceElementConverter JEE_CONVERTER = new NamespaceElementConverter(
063: JEE_NAMESPACE);
064: protected static final NamespaceElementConverter NAMING_CONVERTER = new NamespaceElementConverter(
065: GerAbstractNamingEntryDocument.type
066: .getDocumentElementName().getNamespaceURI());
067:
068: private final Environment defaultEnvironment;
069:
070: protected AbstractNamingBuilder() {
071: defaultEnvironment = null;
072: }
073:
074: protected AbstractNamingBuilder(Environment defaultEnvironment) {
075: this .defaultEnvironment = defaultEnvironment;
076: }
077:
078: public Environment getEnvironment() {
079: return this .defaultEnvironment;
080: }
081:
082: public void buildEnvironment(XmlObject specDD, XmlObject plan,
083: Environment environment) throws DeploymentException {
084: // TODO Currently this method is called before the xml is metadata complete, so will not contain all refs
085: // Just always call mergeEnvironment until this is fixed
086: //
087: // if (willMergeEnvironment(specDD, plan)) {
088: EnvironmentBuilder.mergeEnvironments(environment,
089: defaultEnvironment);
090: // }
091: }
092:
093: protected boolean willMergeEnvironment(XmlObject specDD,
094: XmlObject plan) throws DeploymentException {
095: return false;
096: }
097:
098: protected boolean matchesDefaultEnvironment(Environment environment) {
099: for (Iterator iterator = defaultEnvironment.getDependencies()
100: .iterator(); iterator.hasNext();) {
101: Dependency defaultDependency = (Dependency) iterator.next();
102: boolean matches = false;
103: for (Iterator iterator1 = environment.getDependencies()
104: .iterator(); iterator1.hasNext();) {
105: Dependency actualDependency = (Dependency) iterator1
106: .next();
107: if (matches(defaultDependency, actualDependency)) {
108: matches = true;
109: break;
110: }
111: }
112: if (!matches) {
113: return false;
114: }
115: }
116: return true;
117: }
118:
119: private boolean matches(Dependency defaultDependency,
120: Dependency actualDependency) {
121: if (defaultDependency.getArtifact().matches(
122: actualDependency.getArtifact())
123: || actualDependency.getArtifact().matches(
124: defaultDependency.getArtifact())) {
125: return defaultDependency.getImportType() == actualDependency
126: .getImportType()
127: || actualDependency.getImportType() == ImportType.ALL;
128: }
129: return false;
130: }
131:
132: public void initContext(XmlObject specDD, XmlObject plan,
133: Module module) throws DeploymentException {
134: }
135:
136: protected Map<String, Object> getJndiContextMap(Map sharedContext) {
137: return NamingBuilder.JNDI_KEY.get(sharedContext);
138: }
139:
140: protected AbstractName getGBeanName(Map sharedContext) {
141: return GBEAN_NAME_KEY.get(sharedContext);
142: }
143:
144: protected static QNameSet buildQNameSet(String[] eeNamespaces,
145: String localPart) {
146: Set qnames = new HashSet(eeNamespaces.length);
147: for (int i = 0; i < eeNamespaces.length; i++) {
148: String namespace = eeNamespaces[i];
149: qnames.add(new QName(namespace, localPart));
150: }
151: //xmlbeans 2.0 has a bug so forArray doesn't work. Don't know if it's fixed in later xmlbeans versions
152: //return QNameSet.forArray(qnames);
153: return QNameSet.forSets(null, Collections.EMPTY_SET,
154: Collections.EMPTY_SET, qnames);
155: }
156:
157: /**
158: * @param xmlObjects
159: * @param converter
160: * @param type
161: * @return
162: * @throws DeploymentException
163: * @deprecated
164: */
165: protected static XmlObject[] convert(XmlObject[] xmlObjects,
166: NamespaceElementConverter converter, SchemaType type)
167: throws DeploymentException {
168: //bizarre ArrayStoreException if xmlObjects is loaded by the wrong classloader
169: XmlObject[] converted = new XmlObject[xmlObjects.length];
170: for (int i = 0; i < xmlObjects.length; i++) {
171: XmlObject xmlObject = xmlObjects[i].copy();
172: if (xmlObject.schemaType() != type) {
173: converter.convertElement(xmlObject);
174: converted[i] = xmlObject.changeType(type);
175: } else {
176: converted[i] = xmlObject;
177: }
178: try {
179: XmlBeansUtil.validateDD(converted[i]);
180: } catch (XmlException e) {
181: throw new DeploymentException(
182: "Could not validate xmlObject of type " + type,
183: e);
184: }
185: }
186: return converted;
187:
188: }
189:
190: protected static <T extends XmlObject> List<T> convert(
191: XmlObject[] xmlObjects,
192: NamespaceElementConverter converter, Class<T> c,
193: SchemaType type) throws DeploymentException {
194: //there's probably a better way to say T extends XmlObject and get the type from that
195: List<T> result = new ArrayList<T>(xmlObjects.length);
196: for (XmlObject xmlObject : xmlObjects) {
197: xmlObject = convert(xmlObject, converter, type);
198: result.add((T) xmlObject);
199: }
200: return result;
201: }
202:
203: protected static XmlObject convert(XmlObject xmlObject,
204: NamespaceElementConverter converter, SchemaType type)
205: throws DeploymentException {
206: Map ns = new HashMap();
207: XmlCursor cursor = xmlObject.newCursor();
208: try {
209: cursor.getAllNamespaces(ns);
210: } finally {
211: cursor.dispose();
212: }
213: xmlObject = xmlObject.copy();
214: cursor = xmlObject.newCursor();
215: cursor.toNextToken();
216: try {
217: for (Object o : ns.entrySet()) {
218: Map.Entry entry = (Map.Entry) o;
219: cursor.insertNamespace((String) entry.getKey(),
220: (String) entry.getValue());
221: }
222: } finally {
223: cursor.dispose();
224: }
225:
226: if (xmlObject.schemaType() != type) {
227: converter.convertElement(xmlObject);
228: xmlObject = xmlObject.changeType(type);
229: }
230: try {
231: XmlBeansUtil.validateDD(xmlObject);
232: } catch (XmlException e) {
233: throw new DeploymentException(
234: "Could not validate xmlObject of type " + type, e);
235: }
236: return xmlObject;
237: }
238:
239: protected static String getStringValue(
240: org.apache.geronimo.xbeans.javaee.String string) {
241: if (string == null) {
242: return null;
243: }
244: String s = string.getStringValue();
245: return s == null ? null : s.trim();
246: }
247:
248: protected static String getStringValue(XsdStringType string) {
249: if (string == null) {
250: return null;
251: }
252: String s = string.getStringValue();
253: return s == null ? null : s.trim();
254: }
255:
256: public static AbstractNameQuery buildAbstractNameQuery(
257: GerPatternType pattern, String type, String moduleType,
258: Set interfaceTypes) {
259: return ENCConfigBuilder.buildAbstractNameQueryFromPattern(
260: pattern, null, type, moduleType, interfaceTypes);
261: }
262:
263: public static AbstractNameQuery buildAbstractNameQuery(
264: Artifact configId, String module, String name, String type,
265: String moduleType) {
266: return ENCConfigBuilder.buildAbstractNameQuery(configId,
267: module, name, type, moduleType);
268: }
269:
270: public static Class assureInterface(String interfaceName,
271: String super InterfaceName, String interfaceType,
272: ClassLoader cl) throws DeploymentException {
273: if (interfaceName == null || interfaceName.equals("")) {
274: throw new DeploymentException(
275: "interface name cannot be blank");
276: }
277: Class clazz;
278: try {
279: clazz = cl.loadClass(interfaceName);
280: } catch (ClassNotFoundException e) {
281: throw new DeploymentException(interfaceType
282: + " interface class not found: " + interfaceName, e);
283: }
284: if (!clazz.isInterface()) {
285: throw new DeploymentException(interfaceType
286: + " interface is not an interface: "
287: + interfaceName);
288: }
289: Class super Interface;
290: try {
291: super Interface = cl.loadClass(super InterfaceName);
292: } catch (ClassNotFoundException e) {
293: throw new DeploymentException("Class " + super InterfaceName
294: + " could not be loaded", e);
295: }
296: if (!super Interface.isAssignableFrom(clazz)) {
297: throw new DeploymentException(interfaceType
298: + " interface does not extend "
299: + super InterfaceName + ": " + interfaceName);
300: }
301: return clazz;
302: }
303:
304: protected void addInjections(String jndiName,
305: InjectionTargetType[] injectionTargetArray,
306: Map sharedContext) {
307: Holder holder = NamingBuilder.INJECTION_KEY.get(sharedContext);
308: for (InjectionTargetType injectionTarget : injectionTargetArray) {
309: String targetName = injectionTarget
310: .getInjectionTargetName().getStringValue().trim();
311: String targetClassName = injectionTarget
312: .getInjectionTargetClass().getStringValue().trim();
313: holder.addInjection(targetClassName, new Injection(
314: targetClassName, targetName, jndiName));
315: }
316: }
317:
318: protected static Artifact[] getConfigId(
319: Configuration localConfiguration,
320: Configuration earConfiguration) {
321: if (localConfiguration == earConfiguration) {
322: return new Artifact[] { earConfiguration.getId() };
323: }
324: return new Artifact[] { earConfiguration.getId(),
325: localConfiguration.getId() };
326: }
327:
328: }
|