All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.easybeans.deployable.DeployerFactory Maven / Gradle / Ivy

The newest version!
/**
 * EasyBeans
 * Copyright (C) 2007 Bull S.A.S.
 * Contact: [email protected]
 *
 * 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: DeployerFactory.java 5602 2010-10-01 15:49:31Z sauthieg $
 * --------------------------------------------------------------------------
 */

package org.ow2.easybeans.deployable;

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

import org.ow2.easybeans.api.EZBServer;
import org.ow2.util.ee.deploy.api.deployer.DeployerException;
import org.ow2.util.ee.deploy.api.deployer.IDeployer;
import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;

/**
 * Allow to get a deployer. When EasyBeans is integrated into JOnAS, it will
 * return a JOnASDeployer, for EasyBeans in standalone mode an
 * EasyBeansDeployer, etc.
 * @author Florent Benoit
 */
public final class DeployerFactory {

    /**
     * Name of the property thats defines the deployer's class. (optional)
     */
    public static final String DEPLOYER_FACTORY_CLASS_NAME = DeployerFactory.class.getName();

    /**
     * Default deployer.
     */
    private static final String DEFAULT_DEPLOYER = "org.ow2.easybeans.deployer.EasyBeansDeployer";

    /**
     * Logger.
     */
    private static Log logger = LogFactory.getLog(DeployerFactory.class);

    /**
     * Name of the class to use for the deployer.
     */
    private static String className = null;

    /**
     * Reference to the deployer.
     */
    private static IDeployer deployer = null;


    /**
     * Utility class, no public constructor.
     */
    private DeployerFactory() {

    }


    /**
     * Gets a deployer depending of the property or the class that has been
     * registered.
     * @param embedded the instance of the server.
     * @return an instance of the deployer.
     * @throws DeployerException if the deployer cannot be returned.
     */
    @SuppressWarnings("unchecked")
    public static IDeployer getDeployer(final EZBServer embedded) throws DeployerException {
        if (deployer != null) {
            return deployer;
        }

        // If class name is empty, set to the default value
        if (className == null) {
            className = DEFAULT_DEPLOYER;
        }

        // Use the system property of the class name if the sys property is not
        // set.
        String clName = System.getProperty(DEPLOYER_FACTORY_CLASS_NAME, className);

        logger.debug("Using ''{0}'' as deployer class", clName);

        // load the class
        Class clazz;
        try {
            clazz = Thread.currentThread().getContextClassLoader().loadClass(clName);
        } catch (ClassNotFoundException e) {
            throw new DeployerException("Cannot load the deployer class named '" + clName + "'.", e);
        }

        // Cast
        Class depClass = null;
        if (IDeployer.class.isAssignableFrom(clazz)) {
            depClass = (Class) clazz;
        } else {
            throw new DeployerException("The class '" + clName + "' is not an EZBDeployer class");
        }


        // Build new instance
        try {
            deployer = depClass.newInstance();
        } catch (InstantiationException e) {
            throw new DeployerException("Cannot instantiate the class '" + clName + "'.");
        } catch (IllegalAccessException e) {
            throw new DeployerException("Cannot instantiate the class '" + clName + "'.");
        }

        // Set the embedded if exists
        Method m = null;
        try {
           m = depClass.getMethod("setEmbedded", EZBServer.class);
        } catch (SecurityException e) {
            logger.debug("Cannot get the method setEmbedded", e);
        } catch (NoSuchMethodException e) {
            logger.debug("Cannot get the method setEmbedded", e);
        }

        if (m != null) {
            try {
                m.invoke(deployer, embedded);
            } catch (IllegalArgumentException e) {
                logger.error("Cannot call setEmbedded", e);
            } catch (IllegalAccessException e) {
                logger.error("Cannot call setEmbedded", e);
            } catch (InvocationTargetException e) {
                logger.error("Cannot call setEmbedded", e);
            }
        }

        // return
        return deployer;
    }


    /**
     * Set the name of the deployer class to use.
     * @param classNameStr the name of the class to use.
     */
    public static void setClassName(final String classNameStr) {
        DeployerFactory.className = classNameStr;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy