KickJava   Java API By Example, From Geeks To Geeks.

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


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

53 public class AdminObjectRefBuilder extends AbstractNamingBuilder {
54     private final QNameSet adminOjbectRefQNameSet;
55     private final QNameSet messageDestinationQNameSet;
56     private final QNameSet messageDestinationRefQNameSet;
57
58     private static final QName JavaDoc GER_ADMIN_OBJECT_REF_QNAME = GerResourceEnvRefDocument.type.getDocumentElementName();
59     private static final QNameSet GER_ADMIN_OBJECT_REF_QNAME_SET = QNameSet.singleton(GER_ADMIN_OBJECT_REF_QNAME);
60     private static final QName JavaDoc GER_MESSAGE_DESTINATION_QNAME = GerMessageDestinationDocument.type.getDocumentElementName();
61     private static final QNameSet GER_MESSAGE_DESTINATION_QNAME_SET = QNameSet.singleton(GER_MESSAGE_DESTINATION_QNAME);
62
63     public AdminObjectRefBuilder(Environment defaultEnvironment, String JavaDoc[] eeNamespaces) {
64         super(defaultEnvironment);
65         adminOjbectRefQNameSet = buildQNameSet(eeNamespaces, "resource-env-ref");
66         messageDestinationQNameSet = buildQNameSet(eeNamespaces, "message-destination");
67         messageDestinationRefQNameSet = buildQNameSet(eeNamespaces, "message-destination-ref");
68     }
69
70     protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) {
71         return specDD.selectChildren(adminOjbectRefQNameSet).length > 0 || specDD.selectChildren(messageDestinationRefQNameSet).length > 0;
72     }
73
74     public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException {
75         XmlObject[] specDestinations = convert(specDD.selectChildren(messageDestinationQNameSet), J2EE_CONVERTER, MessageDestinationType.type);
76         XmlObject[] gerDestinations = plan.selectChildren(GER_MESSAGE_DESTINATION_QNAME_SET);
77             Map JavaDoc nameMap = new HashMap JavaDoc();
78             for (int i = 0; i < gerDestinations.length; i++) {
79                 GerMessageDestinationType destination = (GerMessageDestinationType) gerDestinations[i].copy().changeType(GerMessageDestinationType.type);
80                 String JavaDoc name = destination.getMessageDestinationName().trim();
81                 nameMap.put(name, destination);
82                 boolean found = false;
83                 for (int j = 0; j < specDestinations.length; j++) {
84                     MessageDestinationType specDestination = (MessageDestinationType) specDestinations[j];
85                     if (specDestination.getMessageDestinationName().getStringValue().trim().equals(name)) {
86                         found = true;
87                         break;
88                     }
89                 }
90                 if (!found) {
91                     throw new DeploymentException("No spec DD message-destination for " + name);
92                 }
93             }
94             module.getRootEarContext().registerMessageDestionations(module.getName(), nameMap);
95     }
96
97
98     public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map JavaDoc componentContext) throws DeploymentException {
99         XmlObject[] resourceEnvRefsUntyped = convert(specDD.selectChildren(adminOjbectRefQNameSet), J2EE_CONVERTER, ResourceEnvRefType.type);
100         ClassLoader JavaDoc cl = module.getEarContext().getClassLoader();
101         XmlObject[] gerResourceEnvRefsUntyped = plan == null? NO_REFS: plan.selectChildren(GER_ADMIN_OBJECT_REF_QNAME_SET);
102         Map JavaDoc refMap = mapResourceEnvRefs(gerResourceEnvRefsUntyped);
103         for (int i = 0; i < resourceEnvRefsUntyped.length; i++) {
104             ResourceEnvRefType resourceEnvRef = (ResourceEnvRefType) resourceEnvRefsUntyped[i];
105             String JavaDoc name = resourceEnvRef.getResourceEnvRefName().getStringValue().trim();
106             String JavaDoc type = resourceEnvRef.getResourceEnvRefType().getStringValue().trim();
107             Class JavaDoc iface;
108             try {
109                 iface = cl.loadClass(type);
110             } catch (ClassNotFoundException JavaDoc e) {
111                 throw new DeploymentException("could not load class " + type, e);
112             }
113             GerResourceEnvRefType gerResourceEnvRef = (GerResourceEnvRefType) refMap.get(name);
114             try {
115                 AbstractNameQuery containerId = getAdminObjectContainerId(name, gerResourceEnvRef);
116                 Reference JavaDoc ref = buildAdminObjectReference(localConfiguration, containerId, iface);
117                 getJndiContextMap(componentContext).put(ENV + name, ref);
118             } catch (UnresolvedReferenceException e) {
119                 throw new DeploymentException("Unable to resolve resource env reference '" + name + "' (" + (e.isMultiple() ? "found multiple matching resources" : "no matching resources found") + ")");
120             }
121         }
122
123         //message-destination-refs
124
XmlObject[] messageDestinationRefsUntyped = convert(specDD.selectChildren(messageDestinationRefQNameSet), J2EE_CONVERTER, MessageDestinationRefType.type);
125
126         for (int i = 0; i < messageDestinationRefsUntyped.length; i++) {
127             MessageDestinationRefType messageDestinationRef = (MessageDestinationRefType) messageDestinationRefsUntyped[i];
128             String JavaDoc name = getStringValue(messageDestinationRef.getMessageDestinationRefName());
129             String JavaDoc linkName = getStringValue(messageDestinationRef.getMessageDestinationLink());
130             String JavaDoc type = getStringValue(messageDestinationRef.getMessageDestinationType());
131             Class JavaDoc iface;
132             try {
133                 iface = cl.loadClass(type);
134             } catch (ClassNotFoundException JavaDoc e) {
135                 throw new DeploymentException("could not load class " + type, e);
136             }
137             String JavaDoc moduleURI = null;
138             Map JavaDoc messageDestinations = module.getRootEarContext().getMessageDestinations();
139             GerMessageDestinationType destination = getMessageDestination(linkName, messageDestinations);
140             if (destination != null) {
141                 if (destination.isSetAdminObjectLink()) {
142                     if (destination.isSetAdminObjectModule()) {
143                         moduleURI = destination.getAdminObjectModule().trim();
144                     }
145                     linkName = destination.getAdminObjectLink().trim();
146                 }
147             } else {
148                 //well, we know for sure an admin object is not going to be defined in a modules that can have a message-destination
149
int pos = linkName.indexOf('#');
150                 if (pos > -1) {
151                     //AMM -- the following line causes blowups; e.g. to look in DayTrader EJB module for a RA -- why is that?!?
152
//moduleURI = linkName.substring(0, pos);
153
linkName = linkName.substring(pos + 1);
154                 }
155             }
156
157             //try to resolve ref based only matching resource-ref-name
158
//throws exception if it can't locate ref.
159
AbstractNameQuery containerId = buildAbstractNameQuery(null, moduleURI, linkName, NameFactory.JCA_ADMIN_OBJECT, NameFactory.RESOURCE_ADAPTER_MODULE);
160             Reference JavaDoc ref = buildAdminObjectReference(localConfiguration, containerId, iface);
161             getJndiContextMap(componentContext).put(ENV + name, ref);
162
163         }
164
165     }
166
167     public static GerMessageDestinationType getMessageDestination(String JavaDoc messageDestinationLink, Map JavaDoc messageDestinations) throws DeploymentException {
168         GerMessageDestinationType destination = null;
169         int pos = messageDestinationLink.indexOf('#');
170         if (pos > -1) {
171             String JavaDoc targetModule = messageDestinationLink.substring(0, pos);
172             Map JavaDoc destinations = (Map JavaDoc) messageDestinations.get(targetModule);
173             // Hmmm...if we don't find the module then something is wrong in the deployment.
174
if (destinations == null) {
175                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
176                 for (Iterator JavaDoc mapIterator = messageDestinations.keySet().iterator(); mapIterator.hasNext();) {
177                     sb.append(mapIterator.next()).append("\n");
178                 }
179                 throw new DeploymentException("Unknown module " + targetModule + " when processing message destination " + messageDestinationLink +
180                         "\nKnown modules in deployable unit are:\n" + sb.toString());
181             }
182             messageDestinationLink = messageDestinationLink.substring(pos + 1);
183             destination = (GerMessageDestinationType) destinations.get(messageDestinationLink);
184         } else {
185             for (Iterator JavaDoc iterator = messageDestinations.values().iterator(); iterator.hasNext();) {
186                 Map JavaDoc destinations = (Map JavaDoc) iterator.next();
187                 GerMessageDestinationType destinationTest = (GerMessageDestinationType) destinations.get(messageDestinationLink);
188                 if (destinationTest != null) {
189                     if (destination != null) {
190                         throw new DeploymentException("Duplicate message destination " + messageDestinationLink + " accessed from a message-destination-link without a module");
191                     }
192                     destination = destinationTest;
193                 }
194             }
195         }
196         return destination;
197     }
198
199
200     private Reference JavaDoc buildAdminObjectReference(Configuration localConfiguration, AbstractNameQuery containerId, Class JavaDoc iface) throws DeploymentException {
201         try {
202             localConfiguration.findGBean(containerId);
203         } catch (GBeanNotFoundException e) {
204             throw new DeploymentException("Can not resolve admin object ref " + containerId + " in configuration " + localConfiguration.getId());
205         }
206         return new ResourceReference(localConfiguration.getId(), containerId, iface);
207     }
208
209     private static AbstractNameQuery getAdminObjectContainerId(String JavaDoc name, GerResourceEnvRefType gerResourceEnvRef) {
210         AbstractNameQuery containerId;
211         if (gerResourceEnvRef == null) {
212             containerId = buildAbstractNameQuery(null, null, name, NameFactory.JCA_ADMIN_OBJECT, NameFactory.RESOURCE_ADAPTER_MODULE);
213         } else if (gerResourceEnvRef.isSetMessageDestinationLink()) {
214             containerId = buildAbstractNameQuery(null, null, gerResourceEnvRef.getMessageDestinationLink().trim(), NameFactory.JCA_ADMIN_OBJECT, NameFactory.RESOURCE_ADAPTER_MODULE);
215         } else if (gerResourceEnvRef.isSetAdminObjectLink()) {
216             String JavaDoc moduleURI = null;
217             if (gerResourceEnvRef.isSetAdminObjectModule()) {
218                 moduleURI = gerResourceEnvRef.getAdminObjectModule().trim();
219             }
220             containerId = buildAbstractNameQuery(null, moduleURI, gerResourceEnvRef.getAdminObjectLink().trim(), NameFactory.JCA_ADMIN_OBJECT, NameFactory.RESOURCE_ADAPTER_MODULE);
221         } else {
222             //construct name from components
223
GerPatternType patternType = gerResourceEnvRef.getPattern();
224             containerId = buildAbstractNameQuery(patternType, NameFactory.JCA_ADMIN_OBJECT, NameFactory.RESOURCE_ADAPTER_MODULE, null);
225         }
226         return containerId;
227     }
228
229     private static Map JavaDoc mapResourceEnvRefs(XmlObject[] refs) {
230         Map JavaDoc refMap = new HashMap JavaDoc();
231         if (refs != null) {
232             for (int i = 0; i < refs.length; i++) {
233                 GerResourceEnvRefType ref = (GerResourceEnvRefType) refs[i].copy().changeType(GerResourceEnvRefType.type);
234                 refMap.put(ref.getRefName().trim(), ref);
235             }
236         }
237         return refMap;
238     }
239
240     public QNameSet getSpecQNameSet() {
241         return adminOjbectRefQNameSet;
242     }
243
244     public QNameSet getPlanQNameSet() {
245         return GER_ADMIN_OBJECT_REF_QNAME_SET;
246     }
247
248     public static final GBeanInfo GBEAN_INFO;
249
250     static {
251         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(AdminObjectRefBuilder.class, NameFactory.MODULE_BUILDER);
252         infoBuilder.addAttribute("eeNamespaces", String JavaDoc[].class, true, true);
253         infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
254
255         infoBuilder.setConstructor(new String JavaDoc[] {"defaultEnvironment", "eeNamespaces"});
256
257         GBEAN_INFO = infoBuilder.getBeanInfo();
258     }
259
260     public static GBeanInfo getGBeanInfo() {
261         return GBEAN_INFO;
262     }
263
264 }
265
Popular Tags