KickJava   Java API By Example, From Geeks To Geeks.

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


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: TomcatENCInterceptor.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.Method JavaDoc;
29
30 import javax.naming.Context JavaDoc;
31
32 import org.objectweb.easybeans.api.EasyBeansInterceptor;
33 import org.objectweb.easybeans.api.EasyBeansInvocationContext;
34 import org.objectweb.easybeans.log.JLog;
35 import org.objectweb.easybeans.log.JLogFactory;
36
37 /**
38  * Interceptor used when EasyBeans is integrated in Tomcat. As the java:
39  * namespace is managed by Tomcat, EasyBeans needs to call Tomcat objects in
40  * order to set java: context.
41  * @author Florent Benoit
42  */

43 public class TomcatENCInterceptor implements EasyBeansInterceptor {
44
45     /**
46      * Logger.
47      */

48     private static JLog logger = JLogFactory.getLog(TomcatENCInterceptor.class);
49
50     /**
51      * Tomcat's class for naming.
52      */

53     protected static final String JavaDoc TOMCAT_NAMING_CLASS = "org.apache.naming.ContextBindings";
54
55     /**
56      * Method used to bind the name associated to a component context for the current thread on the Tomcat naming.
57      */

58     private static Method JavaDoc bindThreadMethod = null;
59
60     /**
61      * Method used to unbind the name associated to a context for the current thread on the Tomcat naming.
62      */

63     private static Method JavaDoc unbindThreadMethod = null;
64
65     /**
66      * Method used to bind the component context for the current thread on the Tomcat naming.
67      */

68     private static Method JavaDoc bindContextMethod = null;
69
70     /**
71      * Method used to unbind the component context for the current thread on the Tomcat naming.
72      */

73     private static Method JavaDoc unbindContextMethod = null;
74
75
76     /**
77      * Default constructor. Gets a reference on the tomcat's naming methods
78      */

79     public TomcatENCInterceptor() {
80         // Get reference on naming manager and its methods.
81
if (bindThreadMethod == null) {
82             String JavaDoc errMsg = "Check that EasyBeans is embedded in Tomcat web server.";
83             Class JavaDoc namingClass = null;
84             try {
85                 namingClass = Thread.currentThread().getContextClassLoader().loadClass(TOMCAT_NAMING_CLASS);
86             } catch (ClassNotFoundException JavaDoc e) {
87                 throw new IllegalStateException JavaDoc("Cannot load the Tomcat naming class '"
88                         + TOMCAT_NAMING_CLASS + "'. " + errMsg, e);
89             }
90             // get method (bindThread)
91
try {
92                 bindThreadMethod = namingClass.getMethod("bindThread", new Class JavaDoc[] {Object JavaDoc.class});
93             } catch (SecurityException JavaDoc e) {
94                 throw new IllegalStateException JavaDoc("Cannot get bindThread() method on the Tomcat naming class '"
95                         + TOMCAT_NAMING_CLASS + "'. " + errMsg, e);
96             } catch (NoSuchMethodException JavaDoc e) {
97                 throw new IllegalStateException JavaDoc("Cannot get bindThread() method on the Tomcat naming class '"
98                         + TOMCAT_NAMING_CLASS + "'. " + errMsg, e);
99             }
100
101             // get method (unbindThread)
102
try {
103                 unbindThreadMethod = namingClass.getMethod("unbindThread", new Class JavaDoc[] {Object JavaDoc.class});
104             } catch (SecurityException JavaDoc e) {
105                 throw new IllegalStateException JavaDoc("Cannot get unbindThread() method on the Tomcat naming class '"
106                         + TOMCAT_NAMING_CLASS + "'. " + errMsg, e);
107             } catch (NoSuchMethodException JavaDoc e) {
108                 throw new IllegalStateException JavaDoc("Cannot get unbindThread() method on the Tomcat naming class '"
109                         + TOMCAT_NAMING_CLASS + "'. " + errMsg, e);
110             }
111
112             // get method (bindContext)
113
try {
114                 bindContextMethod = namingClass.getMethod("bindContext", new Class JavaDoc[] {Object JavaDoc.class, Context JavaDoc.class});
115             } catch (SecurityException JavaDoc e) {
116                 throw new IllegalStateException JavaDoc("Cannot get bindContext() method on the Tomcat naming class '"
117                         + TOMCAT_NAMING_CLASS + "'. " + errMsg, e);
118             } catch (NoSuchMethodException JavaDoc e) {
119                 throw new IllegalStateException JavaDoc("Cannot get bindContext() method on the Tomcat naming class '"
120                         + TOMCAT_NAMING_CLASS + "'. " + errMsg, e);
121             }
122
123             // get method (unbindContext)
124
try {
125                 unbindContextMethod = namingClass.getMethod("unbindContext", new Class JavaDoc[] {Object JavaDoc.class});
126             } catch (SecurityException JavaDoc e) {
127                 throw new IllegalStateException JavaDoc("Cannot get unbindContext() method on the Tomcat naming class '"
128                         + TOMCAT_NAMING_CLASS + "'. " + errMsg, e);
129             } catch (NoSuchMethodException JavaDoc e) {
130                 throw new IllegalStateException JavaDoc("Cannot get unbindContext() method on the Tomcat naming class '"
131                         + TOMCAT_NAMING_CLASS + "'. " + errMsg, e);
132             }
133
134         }
135     }
136
137     /**
138      * Sets JOnAS ENC context.
139      * @param invocationContext context with useful attributes on the current
140      * invocation.
141      * @return result of the next invocation (to chain interceptors).
142      * @throws Exception needs for signature of interceptor.
143      */

144     public Object JavaDoc intercept(final EasyBeansInvocationContext invocationContext) throws Exception JavaDoc {
145         bindContextMethod.invoke(null, "EZBContext", invocationContext.getFactory().getJavaContext());
146         bindThreadMethod.invoke(null, "EZBContext");
147         try {
148             return invocationContext.proceed();
149         } finally {
150             logger.debug("Unset ENC environment");
151             unbindThreadMethod.invoke(null, "EZBContext");
152             unbindContextMethod.invoke(null, "EZBContext");
153         }
154     }
155
156 }
157
Popular Tags