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: JOnASENCInterceptor.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.naming.interceptors;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028:
029: import javax.naming.Context;
030:
031: import org.ow2.easybeans.api.EasyBeansInvocationContext;
032: import org.ow2.easybeans.api.naming.NamingInterceptor;
033:
034: /**
035: * Interceptor used when EasyBeans is integrated in JOnAS. As the java:
036: * namespace is managed by JOnAS, EasyBeans needs to call JOnAS objects to set
037: * java: context.
038: * @author Florent Benoit
039: */
040: public class JOnASENCInterceptor extends AbsENCInterceptor implements
041: NamingInterceptor {
042:
043: /**
044: * JOnAS's class for naming.
045: */
046: protected static final String JONAS_NAMING_MANAGER_CLASS = "org.objectweb.jonas.naming.NamingManager";
047:
048: /**
049: * Reference on the JOnAS naming manager.
050: */
051: private static Object jonasNamingManager = null;
052:
053: /**
054: * Method used to set the component context on the JOnAS naming manager.
055: */
056: private static Method setComponentContextMethod = null;
057:
058: /**
059: * Method used to reset the component context on the JOnAS naming manager.
060: */
061: private static Method resetComponentContextMethod = null;
062:
063: /**
064: * Default constructor. Gets a reference on the JOnAS naming manager and the
065: * methods that will be called
066: */
067: public JOnASENCInterceptor() {
068: // Get reference on naming manager and its methods.
069: if (jonasNamingManager == null) {
070: String errMsg = "Check that EasyBeans is embedded in JOnAS application server.";
071: Class namingClass = null;
072: try {
073: namingClass = Thread.currentThread()
074: .getContextClassLoader().loadClass(
075: JONAS_NAMING_MANAGER_CLASS);
076: } catch (ClassNotFoundException e) {
077: throw new IllegalStateException(
078: "Cannot load the JOnAS naming manager class '"
079: + JONAS_NAMING_MANAGER_CLASS + "'. "
080: + errMsg, e);
081: }
082: // get method (getInstance)
083: Method getInstance;
084: try {
085: getInstance = namingClass.getMethod("getInstance");
086: } catch (SecurityException e) {
087: throw new IllegalStateException(
088: "Cannot get a method on the JOnAS naming manager class '"
089: + JONAS_NAMING_MANAGER_CLASS + "'. "
090: + errMsg, e);
091: } catch (NoSuchMethodException e) {
092: throw new IllegalStateException(
093: "Cannot get a method on the JOnAS naming manager class '"
094: + JONAS_NAMING_MANAGER_CLASS + "'. "
095: + errMsg, e);
096: }
097:
098: // call this method (null as it is a static method)
099: try {
100: jonasNamingManager = getInstance.invoke(null);
101: } catch (IllegalArgumentException e) {
102: throw new IllegalStateException(
103: "Cannot get the the JOnAS naming manager instance'. "
104: + errMsg, e);
105: } catch (IllegalAccessException e) {
106: throw new IllegalStateException(
107: "Cannot get the the JOnAS naming manager instance'. "
108: + errMsg, e);
109: } catch (InvocationTargetException e) {
110: throw new IllegalStateException(
111: "Cannot get the the JOnAS naming manager instance'. "
112: + errMsg, e);
113: }
114:
115: // get methods
116: try {
117: setComponentContextMethod = namingClass.getMethod(
118: "setComponentContext",
119: new Class[] { Context.class });
120: } catch (SecurityException e) {
121: throw new IllegalStateException(
122: "Cannot get setComponentContext(Context) method on the JOnAS naming manager class '"
123: + JONAS_NAMING_MANAGER_CLASS
124: + "'. "
125: + errMsg, e);
126: } catch (NoSuchMethodException e) {
127: throw new IllegalStateException(
128: "Cannot get setComponentContext(Context) method on the JOnAS naming manager class '"
129: + JONAS_NAMING_MANAGER_CLASS
130: + "'. "
131: + errMsg, e);
132: }
133: try {
134: resetComponentContextMethod = namingClass.getMethod(
135: "resetComponentContext",
136: new Class[] { Context.class });
137: } catch (SecurityException e) {
138: throw new IllegalStateException(
139: "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '"
140: + JONAS_NAMING_MANAGER_CLASS
141: + "'. "
142: + errMsg, e);
143: } catch (NoSuchMethodException e) {
144: throw new IllegalStateException(
145: "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '"
146: + JONAS_NAMING_MANAGER_CLASS
147: + "'. "
148: + errMsg, e);
149: }
150: }
151: }
152:
153: /**
154: * Sets JOnAS ENC context.
155: * @param invocationContext context with useful attributes on the current
156: * invocation.
157: * @return result of the next invocation (to chain interceptors).
158: * @throws Exception needs for signature of interceptor.
159: */
160: @Override
161: public Object intercept(
162: final EasyBeansInvocationContext invocationContext)
163: throws Exception {
164: Context oldContext = (Context) setComponentContextMethod
165: .invoke(jonasNamingManager, invocationContext
166: .getFactory().getJavaContext());
167: try {
168: return invocationContext.proceed();
169: } finally {
170: resetComponentContextMethod.invoke(jonasNamingManager,
171: oldContext);
172: }
173: }
174:
175: }
|