KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > container > mdb > MDBResourceAdapterHelper


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: MDBResourceAdapterHelper.java 1022 2006-08-04 11:02:28Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.container.mdb;
27
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 import javax.resource.ResourceException JavaDoc;
32 import javax.resource.spi.ResourceAdapter JavaDoc;
33
34 import org.objectweb.easybeans.component.api.EZBComponent;
35 import org.objectweb.easybeans.component.itf.JMSComponent;
36 import org.objectweb.easybeans.log.JLog;
37 import org.objectweb.easybeans.log.JLogFactory;
38 import org.objectweb.easybeans.server.Embedded;
39
40 /**
41  * This class allow to get the ResourceAdapter object for a given destination
42  * (activation-spec object).
43  * @author Florent Benoit
44  */

45 public final class MDBResourceAdapterHelper {
46
47     /**
48      * Logger.
49      */

50     private static JLog logger = JLogFactory.getLog(MDBResourceAdapterHelper.class);
51
52     /**
53      * Utility class, no public constructor.
54      */

55     private MDBResourceAdapterHelper() {
56
57     }
58
59     /**
60      * Gets the resource adapter object for the given jndi name (activation
61      * spec) and the given embedded object.
62      * @param jndiName the nameof the activation spec bound in the registry
63      * @param embedded the embedded server
64      * @return an instance of the resource adapter that provides the MDB
65      * activation spec.
66      * @throws ResourceException if an error occurs while trying to get the
67      * resource adapter.
68      */

69     public static ResourceAdapter JavaDoc getResourceAdapter(final String JavaDoc jndiName, final Embedded embedded)
70             throws ResourceException JavaDoc {
71         // try to see if JMS mini-resource adapter service was started.
72
EZBComponent component = embedded.getComponent("org.objectweb.easybeans.component.joram.JoramComponent");
73         if (component != null) {
74             // get ResourceAdapter from the service
75
if (component instanceof JMSComponent) {
76                 return ((JMSComponent) component).getResourceAdapter();
77             }
78             throw new IllegalArgumentException JavaDoc("The 'jms' component doesn't implement JMSComponent interface.");
79         }
80
81         // else, maybe we are in JOnAS ? try to get the class with reflection
82
String JavaDoc jRoot = System.getProperty("install.root");
83         if (jRoot != null) {
84             logger.debug("JOnAS detected, trying with JOnAS Resource Adapter");
85             return getJOnASResourceAdapter(jndiName);
86         }
87
88         throw new ResourceException JavaDoc("MDB is used but no JMS resource service was started."
89                 + "Also, no resource service found for the server.");
90
91     }
92
93     /**
94      * Gets the ResourceAdapter by using JOnAS code (used when embedded into
95      * JOnAS application server).
96      * @param jndiName the name bound in the registry for activation spec
97      * object.
98      * @return an instance of the resource adapter.
99      * @throws ResourceException if there is a failure when retrieving the
100      * resource adapter object.
101      */

102     public static ResourceAdapter JavaDoc getJOnASResourceAdapter(final String JavaDoc jndiName) throws ResourceException JavaDoc {
103         Class JavaDoc rarClass = null;
104         try {
105             rarClass = Thread.currentThread().getContextClassLoader().loadClass("org.objectweb.jonas.resource.Rar");
106         } catch (ClassNotFoundException JavaDoc e) {
107             throw new ResourceException JavaDoc("Cannot find the JOnAS resource adapter class", e);
108         }
109
110         // Get method for Rar.getRar(jndiName);
111
Method JavaDoc getRarMethod = null;
112         try {
113             getRarMethod = rarClass.getMethod("getRar", new Class JavaDoc[] {String JavaDoc.class});
114         } catch (SecurityException JavaDoc e) {
115             throw new ResourceException JavaDoc("Cannot get the getRar method on the class '" + rarClass + "'.", e);
116         } catch (NoSuchMethodException JavaDoc e) {
117             throw new ResourceException JavaDoc("Cannot get the getRar method on the class '" + rarClass + "'.", e);
118         }
119
120         // invoke method (static method)
121
Object JavaDoc rarObject = null;
122         try {
123             rarObject = getRarMethod.invoke(null, jndiName);
124         } catch (IllegalArgumentException JavaDoc e) {
125             throw new ResourceException JavaDoc("Cannot invoke method with jndiName '" + jndiName + "'.", e);
126         } catch (IllegalAccessException JavaDoc e) {
127             throw new ResourceException JavaDoc("Cannot invoke method with jndiName '" + jndiName + "'.", e);
128         } catch (InvocationTargetException JavaDoc e) {
129             throw new ResourceException JavaDoc("Cannot invoke method with jndiName '" + jndiName + "'.", e
130                     .getTargetException());
131         }
132
133         // get the resource adapter on the given object
134
Method JavaDoc getResourceAdapterMethod = null;
135         try {
136             getResourceAdapterMethod = rarObject.getClass().getMethod("getResourceAdapter", new Class JavaDoc[] {});
137         } catch (SecurityException JavaDoc e) {
138             throw new ResourceException JavaDoc("Cannot get the getResourceAdapter method on the class '"
139                     + rarObject.getClass() + "'.", e);
140         } catch (NoSuchMethodException JavaDoc e) {
141             throw new ResourceException JavaDoc("Cannot get the getResourceAdapter method on the class '"
142                     + rarObject.getClass() + "'.", e);
143         }
144
145         // invoke method
146
Object JavaDoc resourceAdapterObj = null;
147         try {
148             resourceAdapterObj = getResourceAdapterMethod.invoke(rarObject, new Object JavaDoc[] {});
149         } catch (IllegalArgumentException JavaDoc e) {
150             throw new ResourceException JavaDoc("Cannot invoke method getResourceAdapter on the rar object.", e);
151         } catch (IllegalAccessException JavaDoc e) {
152             throw new ResourceException JavaDoc("Cannot invoke method getResourceAdapter on the rar object.", e);
153         } catch (InvocationTargetException JavaDoc e) {
154             throw new ResourceException JavaDoc("Cannot invoke method getResourceAdapter on the rar object.", e);
155         }
156
157         // cast object
158
ResourceAdapter JavaDoc resourceAdapter = null;
159         if (resourceAdapterObj instanceof ResourceAdapter JavaDoc) {
160             resourceAdapter = (ResourceAdapter JavaDoc) resourceAdapterObj;
161         } else {
162             throw new ResourceException JavaDoc("Object found is not an instance of ResourceAdapter");
163         }
164         return resourceAdapter;
165
166     }
167 }
168
Popular Tags