1 45 46 package org.openejb.tomcat; 47 48 import org.openejb.OpenEJBException; 49 import org.openejb.RpcContainer; 50 import org.openejb.core.RpcContainerWrapper; 51 import org.openejb.core.DeploymentInfo; 52 53 import javax.naming.Context ; 54 import java.lang.reflect.Method ; 55 import java.lang.reflect.InvocationTargetException ; 56 import java.util.Collection ; 57 import java.util.HashMap ; 58 import java.util.Iterator ; 59 import java.util.Properties ; 60 import java.util.Map ; 61 62 65 public class TomcatJndiSupport extends RpcContainerWrapper { 66 private final Class contextBindings; 67 private final Method bindContext; 68 private final Method bindThread; 69 private final Method unbindThread; 70 71 public TomcatJndiSupport(RpcContainer container) throws OpenEJBException { 72 super(container); 73 try { 74 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 75 contextBindings = classLoader.loadClass("org.apache.naming.ContextBindings"); 76 bindContext = contextBindings.getMethod("bindContext", new Class []{Object .class, Context .class, Object .class}); 77 bindThread = contextBindings.getMethod("bindThread", new Class []{Object .class, Object .class}); 78 unbindThread = contextBindings.getMethod("unbindThread", new Class []{Object .class, Object .class}); 79 } catch (ClassNotFoundException e) { 80 throw new OpenEJBException("Unable to setup Tomcat JNDI support. Support requires the org.apache.naming.ContextBindings class to be available."); 81 } catch (NoSuchMethodException e) { 82 throw new OpenEJBException("Unable to setup Tomcat JNDI support. Method of org.apache.naming.ContextBindings was not found:"+e.getMessage()); 83 } 84 } 85 86 public void init(Object containerId, HashMap deployments, Properties properties) throws OpenEJBException { 87 super.init(containerId, deployments, properties); 88 Collection collection = deployments.values(); 89 for (Iterator iterator = collection.iterator(); iterator.hasNext();) { 90 DeploymentInfo deployment = (DeploymentInfo) iterator.next(); 91 92 setupDeployment(deployment); 93 } 94 } 95 96 public void deploy(Object deploymentID, org.openejb.DeploymentInfo info) throws OpenEJBException { 97 super.deploy(deploymentID, info); 98 setupDeployment((DeploymentInfo)info); 99 } 100 101 public static Map contexts = new HashMap (); 102 103 private void setupDeployment(DeploymentInfo deployment) { 104 deployment.setContainer(this); 108 109 Object deploymentID = deployment.getDeploymentID(); 112 Context jndiEnc = deployment.getJndiEnc(); 113 bindContext(deploymentID, jndiEnc); 114 contexts.put(deploymentID, jndiEnc); 115 } 116 117 public Object invoke(Object deployID, Method callMethod, Object [] args, Object primKey, Object securityIdentity) throws OpenEJBException { 118 try { 119 bindThread(deployID); 122 return super.invoke(deployID, callMethod, args, primKey, securityIdentity); 123 } finally { 124 unbindThread(deployID); 125 } 126 } 127 128 public void bindContext(Object name, Context context) { 129 try { 130 bindContext.invoke(null, new Object []{name, context, name}); 131 } catch (Throwable e) { 132 throw convertToRuntimeException(e, "bindContext"); 133 } 134 } 135 136 public void bindThread(Object name) { 137 try { 138 bindThread.invoke(null, new Object []{name, name}); 139 } catch (Throwable e) { 140 throw convertToRuntimeException(e, "bindThread"); 141 } 142 } 143 144 public void unbindThread(Object name) { 145 try { 146 unbindThread.invoke(null, new Object []{name, name}); 147 } catch (Throwable e) { 148 throw convertToRuntimeException(e, "unbindThread"); 149 } 150 } 151 152 private RuntimeException convertToRuntimeException(Throwable e, String methodName) { 153 if (e instanceof InvocationTargetException ){ 154 Throwable cause = e.getCause(); 155 if (cause instanceof RuntimeException ){ 156 return (RuntimeException ) cause; 157 } else { 158 e = cause; 159 } 160 } 161 return new RuntimeException ("ContextBindings."+methodName, e); 162 } 163 } 164 | Popular Tags |