001: /**
002: * EasyBeans
003: * Copyright (C) 2006 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: JettyENCInterceptor.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.naming.interceptors;
025:
026: import java.lang.reflect.Method;
027:
028: import javax.naming.Context;
029:
030: import org.ow2.easybeans.api.EasyBeansInvocationContext;
031: import org.ow2.easybeans.api.naming.NamingInterceptor;
032:
033: /**
034: * Interceptor used when EasyBeans is integrated in Jetty. As the java:
035: * namespace is managed by Jetty, EasyBeans needs to call Jetty objects in order
036: * to set java: context. But in Jetty, only java:comp can be configured. (comp
037: * is managed by a Factory class)
038: * @author Florent Benoit
039: */
040: public class JettyENCInterceptor extends AbsENCInterceptor implements
041: NamingInterceptor {
042:
043: /**
044: * Jetty's class for naming.
045: */
046: protected static final String JETTY_CONTEXT_FACTORY_CLASS = "org.mortbay.naming.ContextFactory";
047:
048: /**
049: * Method used to set the component context on the Jetty context factory.
050: */
051: private static Method setComponentContextMethod = null;
052:
053: /**
054: * Method used to reset the component context on the Jetty context factory.
055: */
056: private static Method resetComponentContextMethod = null;
057:
058: /**
059: * Default constructor. Gets a reference on the Jetty context factory and the
060: * methods that will be called
061: */
062: public JettyENCInterceptor() {
063: // Get reference on naming manager and its methods.
064: if (setComponentContextMethod == null
065: || resetComponentContextMethod == null) {
066: String errMsg = "Check that EasyBeans is embedded in Jetty server.";
067: Class namingClass = null;
068: try {
069: namingClass = Thread.currentThread()
070: .getContextClassLoader().loadClass(
071: JETTY_CONTEXT_FACTORY_CLASS);
072: } catch (ClassNotFoundException e) {
073: throw new IllegalStateException(
074: "Cannot load the Jetty context factory class '"
075: + JETTY_CONTEXT_FACTORY_CLASS + "'. "
076: + errMsg, e);
077: }
078:
079: // get methods
080: try {
081: setComponentContextMethod = namingClass.getMethod(
082: "setComponentContext",
083: new Class[] { Context.class });
084: } catch (SecurityException e) {
085: throw new IllegalStateException(
086: "Cannot get setComponentContext(Context) method on the JOnAS naming manager class '"
087: + JETTY_CONTEXT_FACTORY_CLASS
088: + "'. "
089: + errMsg, e);
090: } catch (NoSuchMethodException e) {
091: throw new IllegalStateException(
092: "Cannot get setComponentContext(Context) method on the JOnAS naming manager class '"
093: + JETTY_CONTEXT_FACTORY_CLASS
094: + "'. "
095: + errMsg, e);
096: }
097: try {
098: resetComponentContextMethod = namingClass.getMethod(
099: "resetComponentContext",
100: new Class[] { Context.class });
101: } catch (SecurityException e) {
102: throw new IllegalStateException(
103: "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '"
104: + JETTY_CONTEXT_FACTORY_CLASS
105: + "'. "
106: + errMsg, e);
107: } catch (NoSuchMethodException e) {
108: throw new IllegalStateException(
109: "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '"
110: + JETTY_CONTEXT_FACTORY_CLASS
111: + "'. "
112: + errMsg, e);
113: }
114: }
115: }
116:
117: /**
118: * Sets Jetty ENC context.
119: * @param invocationContext context with useful attributes on the current
120: * invocation.
121: * @return result of the next invocation (to chain interceptors).
122: * @throws Exception needs for signature of interceptor.
123: */
124: @Override
125: public Object intercept(
126: final EasyBeansInvocationContext invocationContext)
127: throws Exception {
128: // Get the subcontext (comp)
129: Context oldContext = (Context) setComponentContextMethod
130: .invoke(null, invocationContext.getFactory()
131: .getJavaContext().lookup("comp"));
132: try {
133: return invocationContext.proceed();
134: } finally {
135: resetComponentContextMethod.invoke(null, oldContext);
136: }
137: }
138:
139: }
|