001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: MDBResourceAdapterHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.container.mdb.helper;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028:
029: import javax.resource.ResourceException;
030: import javax.resource.spi.ResourceAdapter;
031:
032: /**
033: * Gets the Resource Adapter Object when embeds in JOnAS 4.x.
034: * @author Florent BENOIT
035: */
036: public class JOnAS4ResourceAdapterFinder implements
037: IResourceAdapterFinder {
038:
039: /**
040: * JOnAS 4 Resource adapter class.
041: */
042: private static final String JONAS_4_RAR_CLASS = "org.objectweb.jonas.resource.Rar";
043:
044: /**
045: * Gets the resource adapter object for the given jndi name (activation
046: * spec) and the given embedded object.
047: * @param jndiName the name of the activation spec bound in the registry
048: * @return an instance of the resource adapter that provides the MDB
049: * activation spec.
050: * @throws ResourceException if an error occurs while trying to get the
051: * resource adapter.
052: */
053: public ResourceAdapter getResourceAdapter(final String jndiName)
054: throws ResourceException {
055: Class<?> rarClass = null;
056: // JOnAS 4 class
057: try {
058: rarClass = Thread.currentThread().getContextClassLoader()
059: .loadClass(JONAS_4_RAR_CLASS);
060: } catch (ClassNotFoundException cnfe) {
061: throw new ResourceException(
062: "Cannot find the JOnAS resource adapter class",
063: cnfe);
064: }
065:
066: // Get method for Rar.getRar(jndiName);
067: Method getRarMethod = null;
068: try {
069: getRarMethod = rarClass.getMethod("getRar",
070: new Class[] { String.class });
071: } catch (SecurityException e) {
072: throw new ResourceException(
073: "Cannot get the getRar method on the class '"
074: + rarClass + "'.", e);
075: } catch (NoSuchMethodException e) {
076: throw new ResourceException(
077: "Cannot get the getRar method on the class '"
078: + rarClass + "'.", e);
079: }
080:
081: // invoke method (static method)
082: Object rarObject = null;
083: try {
084: rarObject = getRarMethod.invoke(null, jndiName);
085: } catch (IllegalArgumentException e) {
086: throw new ResourceException(
087: "Cannot invoke method with jndiName '" + jndiName
088: + "'.", e);
089: } catch (IllegalAccessException e) {
090: throw new ResourceException(
091: "Cannot invoke method with jndiName '" + jndiName
092: + "'.", e);
093: } catch (InvocationTargetException e) {
094: throw new ResourceException(
095: "Cannot invoke method with jndiName '" + jndiName
096: + "'.", e.getTargetException());
097: }
098:
099: // get the resource adapter on the given object
100: Method getResourceAdapterMethod = null;
101: try {
102: getResourceAdapterMethod = rarObject.getClass().getMethod(
103: "getResourceAdapter", new Class[] {});
104: } catch (SecurityException e) {
105: throw new ResourceException(
106: "Cannot get the getResourceAdapter method on the class '"
107: + rarObject.getClass() + "'.", e);
108: } catch (NoSuchMethodException e) {
109: throw new ResourceException(
110: "Cannot get the getResourceAdapter method on the class '"
111: + rarObject.getClass() + "'.", e);
112: }
113:
114: // invoke method
115: Object resourceAdapterObj = null;
116: try {
117: resourceAdapterObj = getResourceAdapterMethod.invoke(
118: rarObject, new Object[] {});
119: } catch (IllegalArgumentException e) {
120: throw new ResourceException(
121: "Cannot invoke method getResourceAdapter on the rar object.",
122: e);
123: } catch (IllegalAccessException e) {
124: throw new ResourceException(
125: "Cannot invoke method getResourceAdapter on the rar object.",
126: e);
127: } catch (InvocationTargetException e) {
128: throw new ResourceException(
129: "Cannot invoke method getResourceAdapter on the rar object.",
130: e);
131: }
132:
133: // cast object
134: ResourceAdapter resourceAdapter = null;
135: if (resourceAdapterObj instanceof ResourceAdapter) {
136: resourceAdapter = (ResourceAdapter) resourceAdapterObj;
137: } else {
138: throw new ResourceException(
139: "Object found is not an instance of ResourceAdapter");
140: }
141: return resourceAdapter;
142: }
143:
144: }
|