KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > deployment > ResourceRefBuilder


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.connector.deployment;
19
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27
28 import javax.naming.Reference JavaDoc;
29 import javax.xml.namespace.QName JavaDoc;
30
31 import org.apache.geronimo.common.DeploymentException;
32 import org.apache.geronimo.common.UnresolvedReferenceException;
33 import org.apache.geronimo.gbean.AbstractNameQuery;
34 import org.apache.geronimo.gbean.GBeanInfo;
35 import org.apache.geronimo.gbean.GBeanInfoBuilder;
36 import org.apache.geronimo.j2ee.deployment.Module;
37 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
38 import org.apache.geronimo.kernel.GBeanNotFoundException;
39 import org.apache.geronimo.kernel.config.Configuration;
40 import org.apache.geronimo.kernel.repository.Environment;
41 import org.apache.geronimo.naming.deployment.AbstractNamingBuilder;
42 import org.apache.geronimo.naming.deployment.ResourceEnvironmentBuilder;
43 import org.apache.geronimo.naming.deployment.ResourceEnvironmentSetter;
44 import org.apache.geronimo.naming.reference.ResourceReference;
45 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
46 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceRefDocument;
47 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceRefType;
48 import org.apache.geronimo.xbeans.j2ee.ResourceRefType;
49 import org.apache.xmlbeans.QNameSet;
50 import org.apache.xmlbeans.XmlObject;
51
52 /**
53  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
54  */

55 public class ResourceRefBuilder extends AbstractNamingBuilder implements ResourceEnvironmentSetter {
56     private static final QName JavaDoc GER_RESOURCE_REF_QNAME = GerResourceRefDocument.type.getDocumentElementName();
57     private static final QNameSet GER_RESOURCE_REF_QNAME_SET = QNameSet.singleton(GER_RESOURCE_REF_QNAME);
58
59     private final QNameSet resourceRefQNameSet;
60
61     private static final String JavaDoc JAXR_CONNECTION_FACTORY_CLASS = "javax.xml.registry.ConnectionFactory";
62     private static final String JavaDoc JAVAX_MAIL_SESSION_CLASS = "javax.mail.Session";
63
64     public ResourceRefBuilder(Environment defaultEnvironment, String JavaDoc[] eeNamespaces) {
65         super(defaultEnvironment);
66
67         resourceRefQNameSet = buildQNameSet(eeNamespaces, "resource-ref");
68     }
69
70     protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) {
71         return specDD.selectChildren(resourceRefQNameSet).length > 0;
72     }
73
74     public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map JavaDoc componentContext) throws DeploymentException {
75         XmlObject[] resourceRefsUntyped = convert(specDD.selectChildren(resourceRefQNameSet), J2EE_CONVERTER, ResourceRefType.type);
76         XmlObject[] gerResourceRefsUntyped = plan == null? NO_REFS: plan.selectChildren(GER_RESOURCE_REF_QNAME_SET);
77         Map JavaDoc refMap = mapResourceRefs(gerResourceRefsUntyped);
78         ClassLoader JavaDoc cl = module.getEarContext().getClassLoader();
79
80         for (int i = 0; i < resourceRefsUntyped.length; i++) {
81             ResourceRefType resourceRef = (ResourceRefType) resourceRefsUntyped[i];
82             String JavaDoc name = resourceRef.getResRefName().getStringValue().trim();
83             String JavaDoc type = resourceRef.getResType().getStringValue().trim();
84             GerResourceRefType gerResourceRef = (GerResourceRefType) refMap.get(name);
85             Class JavaDoc iface;
86             try {
87                 iface = cl.loadClass(type);
88             } catch (ClassNotFoundException JavaDoc e) {
89                 throw new DeploymentException("could not load class " + type, e);
90             }
91             if (iface == URL JavaDoc.class) {
92                 if (gerResourceRef == null || !gerResourceRef.isSetUrl()) {
93                     throw new DeploymentException("No url supplied to resolve: " + name);
94                 }
95                 try {
96                     //TODO expose jsr-77 objects for these guys
97
getJndiContextMap(componentContext).put(ENV + name, new URL JavaDoc(gerResourceRef.getUrl()));
98                 } catch (MalformedURLException JavaDoc e) {
99                     throw new DeploymentException("Could not convert " + gerResourceRef.getUrl() + " to URL", e);
100                 }
101             } else {
102                 //determine jsr-77 type from interface
103
String JavaDoc j2eeType;
104
105
106                 if (JAVAX_MAIL_SESSION_CLASS.equals(type)) {
107                     j2eeType = NameFactory.JAVA_MAIL_RESOURCE;
108                 } else if (JAXR_CONNECTION_FACTORY_CLASS.equals(type)) {
109                     j2eeType = NameFactory.JAXR_CONNECTION_FACTORY;
110                 } else {
111                     j2eeType = NameFactory.JCA_MANAGED_CONNECTION_FACTORY;
112                 }
113                 try {
114                     AbstractNameQuery containerId = getResourceContainerId(name, j2eeType, null, gerResourceRef);
115
116                     try {
117                         localConfiguration.findGBean(containerId);
118                     } catch (GBeanNotFoundException e) {
119                         throw new UnresolvedReferenceException("Resource", false, containerId.toString(), localConfiguration.getId().toString());
120                     }
121
122                     Reference JavaDoc ref = new ResourceReference(localConfiguration.getId(), containerId, iface);
123                     getJndiContextMap(componentContext).put(ENV + name, ref);
124                 } catch (UnresolvedReferenceException e) {
125
126                     StringBuffer JavaDoc errorMessage = new StringBuffer JavaDoc("Unable to resolve resource reference '");
127                     errorMessage.append(name);
128                     errorMessage.append("' (");
129                     if (e.isMultiple()) {
130                         errorMessage.append("Found multiple matching resources. Try being more specific in a resource-ref mapping in your Geronimo deployment plan.");
131                     } else if (gerResourceRef == null) {
132                         errorMessage.append("Could not auto-map to resource. Try adding a resource-ref mapping to your Geronimo deployment plan.");
133                     } else if (gerResourceRef.isSetResourceLink()) {
134                         errorMessage.append("Could not find resource '");
135                         errorMessage.append(gerResourceRef.getResourceLink());
136                         errorMessage.append("'. Perhaps it has not yet been configured, or your application does not have a dependency declared for that resource module?");
137                     } else {
138                         errorMessage.append("Could not find the resource specified in your Geronimo deployment plan:");
139                         errorMessage.append(gerResourceRef.getPattern());
140                     }
141                     errorMessage.append(")");
142
143                     throw new DeploymentException(errorMessage.toString());
144                 }
145             }
146         }
147
148     }
149
150     public void setResourceEnvironment(ResourceEnvironmentBuilder builder, XmlObject[] resourceRefs, GerResourceRefType[] gerResourceRefs) throws DeploymentException {
151         resourceRefs = convert(resourceRefs, J2EE_CONVERTER, ResourceRefType.type);
152         Map JavaDoc refMap = mapResourceRefs(gerResourceRefs);
153         Set JavaDoc unshareableResources = new HashSet JavaDoc();
154         Set JavaDoc applicationManagedSecurityResources = new HashSet JavaDoc();
155         for (int i = 0; i < resourceRefs.length; i++) {
156             ResourceRefType resourceRefType = (ResourceRefType) resourceRefs[i];
157
158             String JavaDoc type = resourceRefType.getResType().getStringValue().trim();
159
160             if (!URL JavaDoc.class.getName().equals(type)
161                     && !"javax.mail.Session".equals(type)
162                     && !JAXR_CONNECTION_FACTORY_CLASS.equals(type)) {
163
164                 GerResourceRefType gerResourceRef = (GerResourceRefType) refMap.get(resourceRefType.getResRefName().getStringValue());
165                 AbstractNameQuery containerId = getResourceContainerId(getStringValue(resourceRefType.getResRefName()), NameFactory.JCA_MANAGED_CONNECTION_FACTORY, null, gerResourceRef);
166
167                 if ("Unshareable".equals(getStringValue(resourceRefType.getResSharingScope()))) {
168                     unshareableResources.add(containerId);
169                 }
170                 if ("Application".equals(getStringValue(resourceRefType.getResAuth()))) {
171                     applicationManagedSecurityResources.add(containerId);
172                 }
173             }
174         }
175         builder.setUnshareableResources(unshareableResources);
176         builder.setApplicationManagedSecurityResources(applicationManagedSecurityResources);
177     }
178
179     private Map JavaDoc mapResourceRefs(XmlObject[] refs) {
180         Map JavaDoc refMap = new HashMap JavaDoc();
181         if (refs != null) {
182             for (int i = 0; i < refs.length; i++) {
183                 GerResourceRefType ref = (GerResourceRefType) refs[i].copy().changeType(GerResourceRefType.type);
184                 refMap.put(ref.getRefName().trim(), ref);
185             }
186         }
187         return refMap;
188     }
189
190     private AbstractNameQuery getResourceContainerId(String JavaDoc name, String JavaDoc type, URI JavaDoc moduleURI, GerResourceRefType gerResourceRef) {
191         AbstractNameQuery containerId;
192         String JavaDoc module = moduleURI == null ? null : moduleURI.toString();
193         if (gerResourceRef == null) {
194             containerId = buildAbstractNameQuery(null, module, name, type, NameFactory.RESOURCE_ADAPTER_MODULE);
195         } else if (gerResourceRef.isSetResourceLink()) {
196             containerId = buildAbstractNameQuery(null, module, gerResourceRef.getResourceLink().trim(), type, NameFactory.RESOURCE_ADAPTER_MODULE);
197         } else {
198             //construct name from components
199
GerPatternType patternType = gerResourceRef.getPattern();
200             containerId = buildAbstractNameQuery(patternType, type, NameFactory.RESOURCE_ADAPTER_MODULE, null);
201         }
202         return containerId;
203     }
204
205     public QNameSet getSpecQNameSet() {
206         return resourceRefQNameSet;
207     }
208
209     public QNameSet getPlanQNameSet() {
210         return GER_RESOURCE_REF_QNAME_SET;
211     }
212
213     public static final GBeanInfo GBEAN_INFO;
214
215     static {
216         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(ResourceRefBuilder.class, NameFactory.MODULE_BUILDER);
217         infoBuilder.addAttribute("eeNamespaces", String JavaDoc[].class, true, true);
218         infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
219
220         infoBuilder.setConstructor(new String JavaDoc[] {"defaultEnvironment", "eeNamespaces"});
221
222         GBEAN_INFO = infoBuilder.getBeanInfo();
223     }
224
225     public static GBeanInfo getGBeanInfo() {
226         return GBEAN_INFO;
227     }
228 }
229
Popular Tags