JOnAS4ResourceAdapterFinder.java :  » J2EE » ow2-easybeans » org » ow2 » easybeans » container » mdb » helper » Java Open Source

Java Open Source » J2EE » ow2 easybeans 
ow2 easybeans » org » ow2 » easybeans » container » mdb » helper » JOnAS4ResourceAdapterFinder.java
/**
 * EasyBeans
 * Copyright (C) 2007 Bull S.A.S.
 * Contact: easybeans@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id: MDBResourceAdapterHelper.java 1970 2007-10-16 11:49:25Z benoitf $
 * --------------------------------------------------------------------------
 */

package org.ow2.easybeans.container.mdb.helper;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.resource.ResourceException;
import javax.resource.spi.ResourceAdapter;

/**
 * Gets the Resource Adapter Object when embeds in JOnAS 4.x.
 * @author Florent BENOIT
 */
public class JOnAS4ResourceAdapterFinder implements IResourceAdapterFinder {

    /**
     * JOnAS 4 Resource adapter class.
     */
    private static final String JONAS_4_RAR_CLASS = "org.objectweb.jonas.resource.Rar";

    /**
     * Gets the resource adapter object for the given jndi name (activation
     * spec) and the given embedded object.
     * @param jndiName the name of the activation spec bound in the registry
     * @return an instance of the resource adapter that provides the MDB
     *         activation spec.
     * @throws ResourceException if an error occurs while trying to get the
     *         resource adapter.
     */
    public ResourceAdapter getResourceAdapter(final String jndiName) throws ResourceException {
        Class<?> rarClass = null;
        // JOnAS 4 class
        try {
            rarClass = Thread.currentThread().getContextClassLoader().loadClass(JONAS_4_RAR_CLASS);
        } catch (ClassNotFoundException cnfe) {
            throw new ResourceException("Cannot find the JOnAS resource adapter class", cnfe);
        }

        // Get method for Rar.getRar(jndiName);
        Method getRarMethod = null;
        try {
            getRarMethod = rarClass.getMethod("getRar", new Class[] {String.class});
        } catch (SecurityException e) {
            throw new ResourceException("Cannot get the getRar method on the class '" + rarClass + "'.", e);
        } catch (NoSuchMethodException e) {
            throw new ResourceException("Cannot get the getRar method on the class '" + rarClass + "'.", e);
        }

        // invoke method (static method)
        Object rarObject = null;
        try {
            rarObject = getRarMethod.invoke(null, jndiName);
        } catch (IllegalArgumentException e) {
            throw new ResourceException("Cannot invoke method with jndiName '" + jndiName + "'.", e);
        } catch (IllegalAccessException e) {
            throw new ResourceException("Cannot invoke method with jndiName '" + jndiName + "'.", e);
        } catch (InvocationTargetException e) {
            throw new ResourceException("Cannot invoke method with jndiName '" + jndiName + "'.", e
                    .getTargetException());
        }

        // get the resource adapter on the given object
        Method getResourceAdapterMethod = null;
        try {
            getResourceAdapterMethod = rarObject.getClass().getMethod("getResourceAdapter", new Class[] {});
        } catch (SecurityException e) {
            throw new ResourceException("Cannot get the getResourceAdapter method on the class '"
                    + rarObject.getClass() + "'.", e);
        } catch (NoSuchMethodException e) {
            throw new ResourceException("Cannot get the getResourceAdapter method on the class '"
                    + rarObject.getClass() + "'.", e);
        }

        // invoke method
        Object resourceAdapterObj = null;
        try {
            resourceAdapterObj = getResourceAdapterMethod.invoke(rarObject, new Object[] {});
        } catch (IllegalArgumentException e) {
            throw new ResourceException("Cannot invoke method getResourceAdapter on the rar object.", e);
        } catch (IllegalAccessException e) {
            throw new ResourceException("Cannot invoke method getResourceAdapter on the rar object.", e);
        } catch (InvocationTargetException e) {
            throw new ResourceException("Cannot invoke method getResourceAdapter on the rar object.", e);
        }

        // cast object
        ResourceAdapter resourceAdapter = null;
        if (resourceAdapterObj instanceof ResourceAdapter) {
            resourceAdapter = (ResourceAdapter) resourceAdapterObj;
        } else {
            throw new ResourceException("Object found is not an instance of ResourceAdapter");
        }
        return resourceAdapter;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.