Package org.ow2.easybeans.naming.interceptors

Source Code of org.ow2.easybeans.naming.interceptors.JOnASENCInterceptor

/**
* EasyBeans
* Copyright (C) 2006 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: JOnASENCInterceptor.java 5369 2010-02-24 14:58:19Z benoitf $
* --------------------------------------------------------------------------
*/

package org.ow2.easybeans.naming.interceptors;

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

import javax.naming.Context;

import org.ow2.easybeans.api.EasyBeansInvocationContext;
import org.ow2.easybeans.api.naming.NamingInterceptor;

/**
* Interceptor used when EasyBeans is integrated in JOnAS. As the java:
* namespace is managed by JOnAS, EasyBeans needs to call JOnAS objects to set
* java: context.
* @author Florent Benoit
*/
public class JOnASENCInterceptor extends AbsENCInterceptor implements NamingInterceptor {

    /**
     * JOnAS's class for naming.
     */
    protected static final String JONAS_NAMING_MANAGER_CLASS = "org.objectweb.jonas.naming.NamingManager";

    /**
     * Reference on the JOnAS naming manager.
     */
    private Object jonasNamingManager = null;

    /**
     * Method used to set the component context on the JOnAS naming manager.
     */
    private Method setComponentContextMethod = null;

    /**
     * Method used to reset the component context on the JOnAS naming manager.
     */
    private Method resetComponentContextMethod = null;

    /**
     * Default constructor. Gets a reference on the JOnAS naming manager and the
     * methods that will be called
     */
    public JOnASENCInterceptor() {
        // Get reference on naming manager and its methods.
        if (this.jonasNamingManager == null) {
            String errMsg = "Check that EasyBeans is embedded in JOnAS application server.";
            Class<?> namingClass = null;
            try {
                namingClass = Thread.currentThread().getContextClassLoader().loadClass(JONAS_NAMING_MANAGER_CLASS);
            } catch (ClassNotFoundException e) {
                throw new IllegalStateException("Cannot load the JOnAS naming manager class '"
                        + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
            }
            // get method (getInstance)
            Method getInstance;
            try {
                getInstance = namingClass.getMethod("getInstance");
            } catch (SecurityException e) {
                throw new IllegalStateException("Cannot get a method on the JOnAS naming manager class '"
                        + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException("Cannot get a method on the JOnAS naming manager class '"
                        + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
            }

            // call this method (null as it is a static method)
            try {
                this.jonasNamingManager = getInstance.invoke(null);
            } catch (IllegalArgumentException e) {
                throw new IllegalStateException("Cannot get the the JOnAS naming manager instance'. " + errMsg, e);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException("Cannot get the the JOnAS naming manager instance'. " + errMsg, e);
            } catch (InvocationTargetException e) {
                throw new IllegalStateException("Cannot get the the JOnAS naming manager instance'. " + errMsg, e);
            }

            // get methods
            try {
                this.setComponentContextMethod = namingClass.getMethod("setComponentContext", new Class[] {Context.class});
            } catch (SecurityException e) {
                throw new IllegalStateException(
                        "Cannot get setComponentContext(Context) method on the JOnAS naming manager class '"
                                + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException(
                        "Cannot get setComponentContext(Context) method on the JOnAS naming manager class '"
                                + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
            }
            try {
                this.resetComponentContextMethod = namingClass.getMethod("resetComponentContext",
                        new Class[] {Context.class});
            } catch (SecurityException e) {
                throw new IllegalStateException(
                        "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '"
                                + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException(
                        "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '"
                                + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
            }
        }
    }

    /**
     * Sets JOnAS ENC context.
     * @param invocationContext context with useful attributes on the current
     *        invocation.
     * @return result of the next invocation (to chain interceptors).
     * @throws Exception needs for signature of interceptor.
     */
    @Override
    public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
        Context oldContext = (Context) this.setComponentContextMethod.invoke(this.jonasNamingManager, invocationContext
                .getFactory().getJavaContext());
        try {
            return invocationContext.proceed();
        } finally {
            this.resetComponentContextMethod.invoke(this.jonasNamingManager, oldContext);
        }
    }


}
TOP

Related Classes of org.ow2.easybeans.naming.interceptors.JOnASENCInterceptor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.