KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > deployment > AbstractNamingBuilder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.naming.deployment;
19
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
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 /**
50  * @version $Rev: 486273 $ $Date: 2006-12-12 13:45:57 -0500 (Tue, 12 Dec 2006) $
51  */

52 public abstract class AbstractNamingBuilder implements NamingBuilder {
53     protected static final String JavaDoc J2EE_NAMESPACE = "http://java.sun.com/xml/ns/j2ee";
54     protected static final String JavaDoc 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 JavaDoc iterator = defaultEnvironment.getDependencies().iterator(); iterator.hasNext();) {
80             Dependency defaultDependency = (Dependency) iterator.next();
81             boolean matches = false;
82             for (Iterator JavaDoc 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 JavaDoc getJndiContextMap(Map JavaDoc sharedContext) {
109         return (Map JavaDoc) sharedContext.get(JNDI_KEY);
110     }
111
112     protected AbstractName getGBeanName(Map JavaDoc sharedContext) {
113         return (AbstractName) sharedContext.get(GBEAN_NAME_KEY);
114     }
115
116     protected static QNameSet buildQNameSet(String JavaDoc[] eeNamespaces, String JavaDoc localPart) {
117         Set JavaDoc qnames = new HashSet JavaDoc(eeNamespaces.length);
118         for (int i = 0; i < eeNamespaces.length; i++) {
119             String JavaDoc namespace = eeNamespaces[i];
120             qnames.add(new QName JavaDoc(namespace, localPart));
121         }
122         //xmlbeans 2.0 has a bug so forArray doesn't work. Don't know if it's fixed in later xmlbeans versions
123
//return QNameSet.forArray(qnames);
124
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         //bizarre ArrayStoreException if xmlObjects is loaded by the wrong classloader
129
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 JavaDoc getStringValue(org.apache.geronimo.xbeans.j2ee.String string) {
148         if (string == null) {
149             return null;
150         }
151         String JavaDoc s = string.getStringValue();
152         return s == null ? null : s.trim();
153     }
154
155     public static AbstractNameQuery buildAbstractNameQuery(GerPatternType pattern, String JavaDoc type, String JavaDoc moduleType, Set JavaDoc interfaceTypes) {
156         String JavaDoc groupId = pattern.isSetGroupId() ? pattern.getGroupId().trim() : null;
157         String JavaDoc artifactid = pattern.isSetArtifactId() ? pattern.getArtifactId().trim() : null;
158         String JavaDoc version = pattern.isSetVersion() ? pattern.getVersion().trim() : null;
159         String JavaDoc module = pattern.isSetModule() ? pattern.getModule().trim() : null;
160         String JavaDoc name = pattern.getName().trim();
161
162         Artifact artifact = artifactid != null ? new Artifact(groupId, artifactid, version, null) : null;
163         Map JavaDoc nameMap = new HashMap JavaDoc();
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 JavaDoc trimmed = new HashSet JavaDoc();
173             for (Iterator JavaDoc it = interfaceTypes.iterator(); it.hasNext();) {
174                 String JavaDoc intf = (String JavaDoc) 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 JavaDoc module, String JavaDoc name, String JavaDoc type, String JavaDoc moduleType) {
183         Map JavaDoc nameMap = new HashMap JavaDoc();
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 JavaDoc assureInterface(String JavaDoc interfaceName, String JavaDoc superInterfaceName, String JavaDoc interfaceType, ClassLoader JavaDoc cl) throws DeploymentException {
195         if (interfaceName == null || interfaceName.equals("")) {
196             throw new DeploymentException("interface name cannot be blank");
197         }
198         Class JavaDoc clazz;
199         try {
200             clazz = cl.loadClass(interfaceName);
201         } catch (ClassNotFoundException JavaDoc 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 JavaDoc superInterface;
208         try {
209             superInterface = cl.loadClass(superInterfaceName);
210         } catch (ClassNotFoundException JavaDoc 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