1 25 26 package org.objectweb.easybeans.naming.interceptors; 27 28 import java.lang.reflect.Method ; 29 30 import javax.naming.Context ; 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 43 public class TomcatENCInterceptor implements EasyBeansInterceptor { 44 45 48 private static JLog logger = JLogFactory.getLog(TomcatENCInterceptor.class); 49 50 53 protected static final String TOMCAT_NAMING_CLASS = "org.apache.naming.ContextBindings"; 54 55 58 private static Method bindThreadMethod = null; 59 60 63 private static Method unbindThreadMethod = null; 64 65 68 private static Method bindContextMethod = null; 69 70 73 private static Method unbindContextMethod = null; 74 75 76 79 public TomcatENCInterceptor() { 80 if (bindThreadMethod == null) { 82 String errMsg = "Check that EasyBeans is embedded in Tomcat web server."; 83 Class namingClass = null; 84 try { 85 namingClass = Thread.currentThread().getContextClassLoader().loadClass(TOMCAT_NAMING_CLASS); 86 } catch (ClassNotFoundException e) { 87 throw new IllegalStateException ("Cannot load the Tomcat naming class '" 88 + TOMCAT_NAMING_CLASS + "'. " + errMsg, e); 89 } 90 try { 92 bindThreadMethod = namingClass.getMethod("bindThread", new Class [] {Object .class}); 93 } catch (SecurityException e) { 94 throw new IllegalStateException ("Cannot get bindThread() method on the Tomcat naming class '" 95 + TOMCAT_NAMING_CLASS + "'. " + errMsg, e); 96 } catch (NoSuchMethodException e) { 97 throw new IllegalStateException ("Cannot get bindThread() method on the Tomcat naming class '" 98 + TOMCAT_NAMING_CLASS + "'. " + errMsg, e); 99 } 100 101 try { 103 unbindThreadMethod = namingClass.getMethod("unbindThread", new Class [] {Object .class}); 104 } catch (SecurityException e) { 105 throw new IllegalStateException ("Cannot get unbindThread() method on the Tomcat naming class '" 106 + TOMCAT_NAMING_CLASS + "'. " + errMsg, e); 107 } catch (NoSuchMethodException e) { 108 throw new IllegalStateException ("Cannot get unbindThread() method on the Tomcat naming class '" 109 + TOMCAT_NAMING_CLASS + "'. " + errMsg, e); 110 } 111 112 try { 114 bindContextMethod = namingClass.getMethod("bindContext", new Class [] {Object .class, Context .class}); 115 } catch (SecurityException e) { 116 throw new IllegalStateException ("Cannot get bindContext() method on the Tomcat naming class '" 117 + TOMCAT_NAMING_CLASS + "'. " + errMsg, e); 118 } catch (NoSuchMethodException e) { 119 throw new IllegalStateException ("Cannot get bindContext() method on the Tomcat naming class '" 120 + TOMCAT_NAMING_CLASS + "'. " + errMsg, e); 121 } 122 123 try { 125 unbindContextMethod = namingClass.getMethod("unbindContext", new Class [] {Object .class}); 126 } catch (SecurityException e) { 127 throw new IllegalStateException ("Cannot get unbindContext() method on the Tomcat naming class '" 128 + TOMCAT_NAMING_CLASS + "'. " + errMsg, e); 129 } catch (NoSuchMethodException e) { 130 throw new IllegalStateException ("Cannot get unbindContext() method on the Tomcat naming class '" 131 + TOMCAT_NAMING_CLASS + "'. " + errMsg, e); 132 } 133 134 } 135 } 136 137 144 public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception { 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 |