KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > naming > interceptors > JOnASENCInterceptor


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JOnASENCInterceptor.java 872 2006-07-13 17:18:15Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.naming.interceptors;
27
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 import javax.naming.Context JavaDoc;
32
33 import org.objectweb.easybeans.api.EasyBeansInterceptor;
34 import org.objectweb.easybeans.api.EasyBeansInvocationContext;
35 import org.objectweb.easybeans.log.JLog;
36 import org.objectweb.easybeans.log.JLogFactory;
37
38 /**
39  * Interceptor used when EasyBeans is integrated in JOnAS. As the java:
40  * namespace is managed by JOnAS, EasyBeans needs to call JOnAS objects to set
41  * java: context.
42  * @author Florent Benoit
43  */

44 public class JOnASENCInterceptor implements EasyBeansInterceptor {
45
46     /**
47      * Logger.
48      */

49     private static JLog logger = JLogFactory.getLog(JOnASENCInterceptor.class);
50
51     /**
52      * JOnAS's class for naming.
53      */

54     protected static final String JavaDoc JONAS_NAMING_MANAGER_CLASS = "org.objectweb.jonas.naming.NamingManager";
55
56     /**
57      * Reference on the JOnAS naming manager.
58      */

59     private static Object JavaDoc jonasNamingManager = null;
60
61     /**
62      * Method used to set the component context on the JOnAS naming manager.
63      */

64     private static Method JavaDoc setComponentContextMethod = null;
65
66     /**
67      * Method used to reset the component context on the JOnAS naming manager.
68      */

69     private static Method JavaDoc resetComponentContextMethod = null;
70
71     /**
72      * Default constructor. Gets a reference on the JOnAS naming manager and the
73      * methods that will be called
74      */

75     public JOnASENCInterceptor() {
76         // Get reference on naming manager and its methods.
77
if (jonasNamingManager == null) {
78             String JavaDoc errMsg = "Check that EasyBeans is embedded in JOnAS application server.";
79             Class JavaDoc namingClass = null;
80             try {
81                 namingClass = Thread.currentThread().getContextClassLoader().loadClass(JONAS_NAMING_MANAGER_CLASS);
82             } catch (ClassNotFoundException JavaDoc e) {
83                 throw new IllegalStateException JavaDoc("Cannot load the JOnAS naming manager class '"
84                         + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
85             }
86             // get method (getInstance)
87
Method JavaDoc getInstance;
88             try {
89                 getInstance = namingClass.getMethod("getInstance");
90             } catch (SecurityException JavaDoc e) {
91                 throw new IllegalStateException JavaDoc("Cannot get a method on the JOnAS naming manager class '"
92                         + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
93             } catch (NoSuchMethodException JavaDoc e) {
94                 throw new IllegalStateException JavaDoc("Cannot get a method on the JOnAS naming manager class '"
95                         + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
96             }
97
98             // call this method (null as it is a static method)
99
try {
100                 jonasNamingManager = getInstance.invoke(null);
101             } catch (IllegalArgumentException JavaDoc e) {
102                 throw new IllegalStateException JavaDoc("Cannot get the the JOnAS naming manager instance'. " + errMsg, e);
103             } catch (IllegalAccessException JavaDoc e) {
104                 throw new IllegalStateException JavaDoc("Cannot get the the JOnAS naming manager instance'. " + errMsg, e);
105             } catch (InvocationTargetException JavaDoc e) {
106                 throw new IllegalStateException JavaDoc("Cannot get the the JOnAS naming manager instance'. " + errMsg, e);
107             }
108
109             // get methods
110
try {
111                 setComponentContextMethod = namingClass.getMethod("setComponentContext", new Class JavaDoc[] {Context JavaDoc.class});
112             } catch (SecurityException JavaDoc e) {
113                 throw new IllegalStateException JavaDoc(
114                         "Cannot get setComponentContext(Context) method on the JOnAS naming manager class '"
115                                 + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
116             } catch (NoSuchMethodException JavaDoc e) {
117                 throw new IllegalStateException JavaDoc(
118                         "Cannot get setComponentContext(Context) method on the JOnAS naming manager class '"
119                                 + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
120             }
121             try {
122                 resetComponentContextMethod = namingClass.getMethod("resetComponentContext",
123                         new Class JavaDoc[] {Context JavaDoc.class});
124             } catch (SecurityException JavaDoc e) {
125                 throw new IllegalStateException JavaDoc(
126                         "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '"
127                                 + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
128             } catch (NoSuchMethodException JavaDoc e) {
129                 throw new IllegalStateException JavaDoc(
130                         "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '"
131                                 + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e);
132             }
133         }
134     }
135
136     /**
137      * Sets JOnAS ENC context.
138      * @param invocationContext context with useful attributes on the current
139      * invocation.
140      * @return result of the next invocation (to chain interceptors).
141      * @throws Exception needs for signature of interceptor.
142      */

143     public Object JavaDoc intercept(final EasyBeansInvocationContext invocationContext) throws Exception JavaDoc {
144         Context JavaDoc oldContext = (Context JavaDoc) setComponentContextMethod.invoke(jonasNamingManager, invocationContext
145                 .getFactory().getJavaContext());
146         try {
147             return invocationContext.proceed();
148         } finally {
149             logger.debug("Unset ENC environment");
150             resetComponentContextMethod.invoke(jonasNamingManager, oldContext);
151         }
152     }
153
154 }
155
Popular Tags