KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > ManagedConnectionFactoryWrapper


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.outbound;
19
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.LinkedHashSet JavaDoc;
24
25 import javax.management.ObjectName JavaDoc;
26 import javax.resource.ResourceException JavaDoc;
27 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
28 import javax.resource.spi.ResourceAdapterAssociation JavaDoc;
29 import javax.transaction.SystemException JavaDoc;
30
31 import net.sf.cglib.proxy.Callback;
32 import net.sf.cglib.proxy.Enhancer;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.geronimo.connector.ConnectorMethodInterceptor;
36 import org.apache.geronimo.connector.ResourceAdapterWrapper;
37 import org.apache.geronimo.gbean.DynamicGBean;
38 import org.apache.geronimo.gbean.DynamicGBeanDelegate;
39 import org.apache.geronimo.gbean.GBeanLifecycle;
40 import org.apache.geronimo.gbean.AbstractName;
41 import org.apache.geronimo.kernel.Kernel;
42 import org.apache.geronimo.management.geronimo.JCAManagedConnectionFactory;
43 import org.apache.geronimo.transaction.manager.NamedXAResource;
44 import org.apache.geronimo.transaction.manager.ResourceManager;
45
46 /**
47  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
48  */

49 public class ManagedConnectionFactoryWrapper implements GBeanLifecycle, DynamicGBean, ResourceManager, JCAManagedConnectionFactory, ConnectionFactorySource {
50
51     private static final Log log = LogFactory.getLog(ManagedConnectionFactoryWrapper.class);
52
53     private final String JavaDoc managedConnectionFactoryClass;
54     private final String JavaDoc connectionFactoryInterface;
55     private final String JavaDoc[] implementedInterfaces;
56     private final String JavaDoc connectionFactoryImplClass;
57     private final String JavaDoc connectionInterface;
58     private final String JavaDoc connectionImplClass;
59
60     private final Class JavaDoc[] allImplementedInterfaces;
61
62     private final ResourceAdapterWrapper resourceAdapterWrapper;
63     private final ConnectionManagerContainer connectionManagerContainer;
64
65     private ManagedConnectionFactory JavaDoc managedConnectionFactory;
66
67     private Object JavaDoc connectionFactory;
68
69     private DynamicGBeanDelegate delegate;
70
71
72     private boolean registered = false;
73     private Object JavaDoc proxy;
74     private ConnectorMethodInterceptor interceptor;
75     private final Kernel kernel;
76     private final AbstractName abstractName;
77     private final String JavaDoc objectName;
78     private final boolean isProxyable;
79     private final ClassLoader JavaDoc classLoader;
80
81     //default constructor for enhancement proxy endpoint
82
public ManagedConnectionFactoryWrapper() {
83         managedConnectionFactoryClass = null;
84         connectionFactoryInterface = null;
85         implementedInterfaces = null;
86         connectionFactoryImplClass = null;
87         connectionInterface = null;
88         connectionImplClass = null;
89         kernel = null;
90         abstractName = null;
91         objectName = null;
92         allImplementedInterfaces = null;
93         isProxyable = false;
94         classLoader = null;
95         resourceAdapterWrapper = null;
96         connectionManagerContainer = null;
97     }
98
99     public ManagedConnectionFactoryWrapper(String JavaDoc managedConnectionFactoryClass,
100                                            String JavaDoc connectionFactoryInterface,
101                                            String JavaDoc[] implementedInterfaces,
102                                            String JavaDoc connectionFactoryImplClass,
103                                            String JavaDoc connectionInterface,
104                                            String JavaDoc connectionImplClass,
105                                            ResourceAdapterWrapper resourceAdapterWrapper,
106                                            ConnectionManagerContainer connectionManagerContainer,
107                                            Kernel kernel,
108                                            AbstractName abstractName,
109                                            String JavaDoc objectName,
110                                            ClassLoader JavaDoc cl) throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, ClassNotFoundException JavaDoc {
111         this.managedConnectionFactoryClass = managedConnectionFactoryClass;
112         this.connectionFactoryInterface = connectionFactoryInterface;
113         this.implementedInterfaces = implementedInterfaces;
114         this.connectionFactoryImplClass = connectionFactoryImplClass;
115         this.connectionInterface = connectionInterface;
116         this.connectionImplClass = connectionImplClass;
117
118         LinkedHashSet JavaDoc allInterfaceSet = new LinkedHashSet JavaDoc();
119         allInterfaceSet.add(cl.loadClass(connectionFactoryInterface));
120         for (int i = 0; i < implementedInterfaces.length; i++) {
121             allInterfaceSet.add(cl.loadClass(implementedInterfaces[i]));
122         }
123         allImplementedInterfaces = (Class JavaDoc[])allInterfaceSet.toArray(new Class JavaDoc[allInterfaceSet.size()]);
124         
125         boolean mightBeProxyable = true;
126         for (int i = 0; i < allImplementedInterfaces.length; i++) {
127             Class JavaDoc implementedInterface = allImplementedInterfaces[i];
128             if (!implementedInterface.isInterface()) {
129                 mightBeProxyable = false;
130                 break;
131             }
132         }
133         isProxyable = mightBeProxyable;
134
135         this.resourceAdapterWrapper = resourceAdapterWrapper;
136         this.connectionManagerContainer = connectionManagerContainer;
137
138         //set up that must be done before start
139
classLoader = cl;
140         Class JavaDoc clazz = cl.loadClass(managedConnectionFactoryClass);
141         managedConnectionFactory = (ManagedConnectionFactory JavaDoc) clazz.newInstance();
142         delegate = new DynamicGBeanDelegate();
143         delegate.addAll(managedConnectionFactory);
144         this.kernel = kernel;
145         this.abstractName = abstractName;
146         this.objectName = objectName;
147     }
148
149     public String JavaDoc getManagedConnectionFactoryClass() {
150         return managedConnectionFactoryClass;
151     }
152
153     public String JavaDoc getConnectionFactoryInterface() {
154         return connectionFactoryInterface;
155     }
156
157     public String JavaDoc[] getImplementedInterfaces() {
158         return implementedInterfaces;
159     }
160
161     public String JavaDoc getConnectionFactoryImplClass() {
162         return connectionFactoryImplClass;
163     }
164
165     public String JavaDoc getConnectionInterface() {
166         return connectionInterface;
167     }
168
169     public String JavaDoc getConnectionImplClass() {
170         return connectionImplClass;
171     }
172
173     public ResourceAdapterWrapper getResourceAdapterWrapper() {
174         return resourceAdapterWrapper;
175     }
176
177     public ConnectionManagerContainer getConnectionManagerFactory() {
178         return connectionManagerContainer;
179     }
180
181     public Object JavaDoc getConnectionManager() {
182         return connectionManagerContainer.getConnectionManager();
183     }
184
185     public void doStart() throws Exception JavaDoc {
186         //register with resource adapter if not yet done
187
if (!registered && (managedConnectionFactory instanceof ResourceAdapterAssociation JavaDoc)) {
188             if (resourceAdapterWrapper == null) {
189                 throw new IllegalStateException JavaDoc("Managed connection factory expects to be registered with a ResourceAdapter, but there is no ResourceAdapter");
190             }
191             resourceAdapterWrapper.registerResourceAdapterAssociation((ResourceAdapterAssociation JavaDoc) managedConnectionFactory);
192             registered = true;
193             log.debug("Registered managedConnectionFactory with ResourceAdapter " + resourceAdapterWrapper.toString());
194         }
195
196         //create a new ConnectionFactory
197
connectionFactory = connectionManagerContainer.createConnectionFactory(managedConnectionFactory);
198
199         //build proxy
200
if (isProxyable) {
201             Enhancer enhancer = new Enhancer();
202             enhancer.setInterfaces(allImplementedInterfaces);
203             enhancer.setCallbackType(net.sf.cglib.proxy.MethodInterceptor.class);
204             enhancer.setUseFactory(false);//????
205
interceptor = new ConnectorMethodInterceptor(kernel.getKernelName(), abstractName);
206             enhancer.setCallbacks(new Callback[]{interceptor});
207             proxy = enhancer.create(new Class JavaDoc[0], new Object JavaDoc[0]);
208         } else {
209             proxy = connectionFactory;
210         }
211
212         //connect proxy
213
if (interceptor != null) {
214             interceptor.setInternalProxy(connectionFactory);
215         }
216     }
217
218     public void doStop() {
219         if (interceptor != null) {
220             interceptor.setInternalProxy(null);
221         }
222         connectionFactory = null;
223     }
224
225     public void doFail() {
226         doStop();
227     }
228
229     //DynamicGBean implementation
230
public Object JavaDoc getAttribute(String JavaDoc name) throws Exception JavaDoc {
231         Thread JavaDoc thread = Thread.currentThread();
232         ClassLoader JavaDoc oldTCL = thread.getContextClassLoader();
233         thread.setContextClassLoader(classLoader);
234         try {
235             return delegate.getAttribute(name);
236         } finally {
237             thread.setContextClassLoader(oldTCL);
238         }
239     }
240
241     public void setAttribute(String JavaDoc name, Object JavaDoc value) throws Exception JavaDoc {
242         Thread JavaDoc thread = Thread.currentThread();
243         ClassLoader JavaDoc oldTCL = thread.getContextClassLoader();
244         thread.setContextClassLoader(classLoader);
245         try {
246             delegate.setAttribute(name, value);
247         } finally {
248             thread.setContextClassLoader(oldTCL);
249         }
250     }
251
252     public Object JavaDoc invoke(String JavaDoc name, Object JavaDoc[] arguments, String JavaDoc[] types) throws Exception JavaDoc {
253         //we have no dynamic operations.
254
return null;
255     }
256
257     public Object JavaDoc getConnectionFactory() {
258         return $getResource();
259     }
260
261     public Object JavaDoc $getResource() {
262         return proxy;
263     }
264
265     public Object JavaDoc $getConnectionFactory() {
266         return connectionFactory;
267     }
268
269     public ManagedConnectionFactory JavaDoc $getManagedConnectionFactory() {
270         return managedConnectionFactory;
271     }
272
273     /**
274      * Gets the config properties in the form of a map where the key is the
275      * property name and the value is property type (as a String not a Class).
276      */

277     public Map JavaDoc getConfigProperties() {
278         String JavaDoc[] props = delegate.getProperties();
279         Map JavaDoc map = new HashMap JavaDoc();
280         for (int i = 0; i < props.length; i++) {
281             String JavaDoc prop = props[i];
282             if(prop.equals("logWriter")) {
283                 continue;
284             }
285             map.put(prop, delegate.getPropertyType(prop));
286         }
287         return map;
288     }
289
290     public void setConfigProperty(String JavaDoc property, Object JavaDoc value) throws Exception JavaDoc {
291         Class JavaDoc cls = delegate.getPropertyType(property);
292         if(value != null && value instanceof String JavaDoc && !cls.getName().equals("java.lang.String")) {
293             if(cls.isPrimitive()) {
294                 if(cls.equals(int.class)) {
295                     cls = Integer JavaDoc.class;
296                 } else if(cls.equals(boolean.class)) {
297                     cls = Boolean JavaDoc.class;
298                 } else if(cls.equals(float.class)) {
299                     cls = Float JavaDoc.class;
300                 } else if(cls.equals(double.class)) {
301                     cls = Double JavaDoc.class;
302                 } else if(cls.equals(long.class)) {
303                     cls = Long JavaDoc.class;
304                 } else if(cls.equals(short.class)) {
305                     cls = Short JavaDoc.class;
306                 } else if(cls.equals(byte.class)) {
307                     cls = Byte JavaDoc.class;
308                 } else if(cls.equals(char.class)) {
309                     cls = Character JavaDoc.class;
310                 }
311             }
312             Constructor JavaDoc con = cls.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
313             value = con.newInstance(new Object JavaDoc[]{value});
314         }
315         kernel.setAttribute(abstractName, property, value);
316     }
317
318     public Object JavaDoc getConfigProperty(String JavaDoc property) throws Exception JavaDoc {
319         return delegate.getAttribute(property);
320     }
321
322     //ResourceManager implementation
323
public NamedXAResource getRecoveryXAResources() throws SystemException JavaDoc {
324         try {
325             return connectionManagerContainer.getRecoveryXAResource(managedConnectionFactory);
326         } catch (ResourceException JavaDoc e) {
327             throw (SystemException JavaDoc) new SystemException JavaDoc("Could not obtain recovery XAResource for managedConnectionFactory " + objectName).initCause(e);
328         }
329     }
330
331     public void returnResource(NamedXAResource xaResource) {
332         ((ConnectionManagerContainer.ReturnableXAResource) xaResource).returnConnection();
333     }
334
335     public String JavaDoc getObjectName() {
336         return objectName;
337     }
338
339     public boolean isStateManageable() {
340         return false;
341     }
342
343     public boolean isStatisticsProvider() {
344         return false;
345     }
346
347     public boolean isEventProvider() {
348         return false;
349     }
350 }
351
Popular Tags