1 25 26 package org.objectweb.easybeans.naming.interceptors; 27 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 31 import javax.naming.Context ; 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 44 public class JOnASENCInterceptor implements EasyBeansInterceptor { 45 46 49 private static JLog logger = JLogFactory.getLog(JOnASENCInterceptor.class); 50 51 54 protected static final String JONAS_NAMING_MANAGER_CLASS = "org.objectweb.jonas.naming.NamingManager"; 55 56 59 private static Object jonasNamingManager = null; 60 61 64 private static Method setComponentContextMethod = null; 65 66 69 private static Method resetComponentContextMethod = null; 70 71 75 public JOnASENCInterceptor() { 76 if (jonasNamingManager == null) { 78 String errMsg = "Check that EasyBeans is embedded in JOnAS application server."; 79 Class namingClass = null; 80 try { 81 namingClass = Thread.currentThread().getContextClassLoader().loadClass(JONAS_NAMING_MANAGER_CLASS); 82 } catch (ClassNotFoundException e) { 83 throw new IllegalStateException ("Cannot load the JOnAS naming manager class '" 84 + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e); 85 } 86 Method getInstance; 88 try { 89 getInstance = namingClass.getMethod("getInstance"); 90 } catch (SecurityException e) { 91 throw new IllegalStateException ("Cannot get a method on the JOnAS naming manager class '" 92 + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e); 93 } catch (NoSuchMethodException e) { 94 throw new IllegalStateException ("Cannot get a method on the JOnAS naming manager class '" 95 + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e); 96 } 97 98 try { 100 jonasNamingManager = getInstance.invoke(null); 101 } catch (IllegalArgumentException e) { 102 throw new IllegalStateException ("Cannot get the the JOnAS naming manager instance'. " + errMsg, e); 103 } catch (IllegalAccessException e) { 104 throw new IllegalStateException ("Cannot get the the JOnAS naming manager instance'. " + errMsg, e); 105 } catch (InvocationTargetException e) { 106 throw new IllegalStateException ("Cannot get the the JOnAS naming manager instance'. " + errMsg, e); 107 } 108 109 try { 111 setComponentContextMethod = namingClass.getMethod("setComponentContext", new Class [] {Context .class}); 112 } catch (SecurityException e) { 113 throw new IllegalStateException ( 114 "Cannot get setComponentContext(Context) method on the JOnAS naming manager class '" 115 + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e); 116 } catch (NoSuchMethodException e) { 117 throw new IllegalStateException ( 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 [] {Context .class}); 124 } catch (SecurityException e) { 125 throw new IllegalStateException ( 126 "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '" 127 + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e); 128 } catch (NoSuchMethodException e) { 129 throw new IllegalStateException ( 130 "Cannot get resetComponentContext(Context) method on the JOnAS naming manager class '" 131 + JONAS_NAMING_MANAGER_CLASS + "'. " + errMsg, e); 132 } 133 } 134 } 135 136 143 public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception { 144 Context oldContext = (Context ) 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 |