Package org.ow2.easybeans.naming.interceptors

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

/**
* 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: Jetty6ENCInterceptor.java 5369 2010-02-24 14:58:19Z benoitf $
* --------------------------------------------------------------------------
*/

package org.ow2.easybeans.naming.interceptors;

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 Jetty. As the java:
* namespace is managed by Jetty, EasyBeans needs to call Jetty objects in order
* to set java: context. But in Jetty, only java:comp can be configured. (comp
* is managed by a Factory class)
* @author Florent Benoit
*/
public class Jetty6ENCInterceptor extends AbsENCInterceptor implements NamingInterceptor {

    /**
     * Jetty's class for naming.
     */
    protected static final String JETTY_CONTEXT_FACTORY_CLASS = "org.mortbay.naming.ContextFactory";

    /**
     * Method used to set the component context on the Jetty context factory.
     */
    private static Method setComponentContextMethod = null;

    /**
     * Method used to reset the component context on the Jetty context factory.
     */
    private static Method resetComponentContextMethod = null;

    /**
     * Default constructor. Gets a reference on the Jetty context factory and the
     * methods that will be called
     */
    public Jetty6ENCInterceptor() {
        // Get reference on naming manager and its methods.
        if (setComponentContextMethod == null || resetComponentContextMethod == null) {
            String errMsg = "Check that EasyBeans is embedded in Jetty server.";
            Class namingClass = null;
            try {
                namingClass = Thread.currentThread().getContextClassLoader().loadClass(JETTY_CONTEXT_FACTORY_CLASS);
            } catch (ClassNotFoundException e) {
                throw new IllegalStateException("Cannot load the Jetty context factory class '"
                        + JETTY_CONTEXT_FACTORY_CLASS + "'. " + errMsg, e);
            }

            // get methods
            try {
                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 '"
                                + JETTY_CONTEXT_FACTORY_CLASS + "'. " + errMsg, e);
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException(
                        "Cannot get setComponentContext(Context) method on the JOnAS naming manager class '"
                                + JETTY_CONTEXT_FACTORY_CLASS + "'. " + errMsg, e);
            }
            try {
                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 '"
                                + JETTY_CONTEXT_FACTORY_CLASS + "'. " + errMsg, e);
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException(
                        "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '"
                                + JETTY_CONTEXT_FACTORY_CLASS + "'. " + errMsg, e);
            }
        }
    }

    /**
     * Sets Jetty 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 {
        // Get the subcontext (comp)
        Context oldContext = (Context) setComponentContextMethod.invoke(null, invocationContext
                .getFactory().getJavaContext().lookup("comp"));
        try {
            return invocationContext.proceed();
        } finally {
            resetComponentContextMethod.invoke(null, oldContext);
        }
    }


}
TOP

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

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.