KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > tomcat > TomcatJndiSupport


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact info@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: TomcatJndiSupport.java 2106 2005-08-26 21:04:51Z dblevins $
44  */

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 JavaDoc;
54 import java.lang.reflect.Method JavaDoc;
55 import java.lang.reflect.InvocationTargetException JavaDoc;
56 import java.util.Collection JavaDoc;
57 import java.util.HashMap JavaDoc;
58 import java.util.Iterator JavaDoc;
59 import java.util.Properties JavaDoc;
60 import java.util.Map JavaDoc;
61
62 /**
63  * @version $Revision: 2106 $ $Date: 2005-08-26 14:04:51 -0700 (Fri, 26 Aug 2005) $
64  */

65 public class TomcatJndiSupport extends RpcContainerWrapper {
66     private final Class JavaDoc contextBindings;
67     private final Method JavaDoc bindContext;
68     private final Method JavaDoc bindThread;
69     private final Method JavaDoc unbindThread;
70
71     public TomcatJndiSupport(RpcContainer container) throws OpenEJBException {
72         super(container);
73         try {
74             ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
75             contextBindings = classLoader.loadClass("org.apache.naming.ContextBindings");
76             bindContext = contextBindings.getMethod("bindContext", new Class JavaDoc[]{Object JavaDoc.class, Context JavaDoc.class, Object JavaDoc.class});
77             bindThread = contextBindings.getMethod("bindThread", new Class JavaDoc[]{Object JavaDoc.class, Object JavaDoc.class});
78             unbindThread = contextBindings.getMethod("unbindThread", new Class JavaDoc[]{Object JavaDoc.class, Object JavaDoc.class});
79         } catch (ClassNotFoundException JavaDoc 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 JavaDoc 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 JavaDoc containerId, HashMap JavaDoc deployments, Properties JavaDoc properties) throws OpenEJBException {
87         super.init(containerId, deployments, properties);
88         Collection JavaDoc collection = deployments.values();
89         for (Iterator JavaDoc iterator = collection.iterator(); iterator.hasNext();) {
90             DeploymentInfo deployment = (DeploymentInfo) iterator.next();
91
92             setupDeployment(deployment);
93         }
94     }
95
96     public void deploy(Object JavaDoc deploymentID, org.openejb.DeploymentInfo info) throws OpenEJBException {
97         super.deploy(deploymentID, info);
98         setupDeployment((DeploymentInfo)info);
99     }
100
101     public static Map JavaDoc contexts = new HashMap JavaDoc();
102
103     private void setupDeployment(DeploymentInfo deployment) {
104         // Set this container as the deployment's container
105
// so calls from proxies created by the deploymentinfo
106
// class come here first.
107
deployment.setContainer(this);
108
109         // Register this deployment's JNDI namespace in
110
// Tomcat's list of context objects
111
Object JavaDoc deploymentID = deployment.getDeploymentID();
112         Context JavaDoc jndiEnc = deployment.getJndiEnc();
113         bindContext(deploymentID, jndiEnc);
114         contexts.put(deploymentID, jndiEnc);
115     }
116
117     public Object JavaDoc invoke(Object JavaDoc deployID, Method JavaDoc callMethod, Object JavaDoc[] args, Object JavaDoc primKey, Object JavaDoc securityIdentity) throws OpenEJBException {
118         try {
119             // Tell tomcat that this deployment's context should
120
// be used for this call
121
bindThread(deployID);
122             return super.invoke(deployID, callMethod, args, primKey, securityIdentity);
123         } finally {
124             unbindThread(deployID);
125         }
126     }
127
128     public void bindContext(Object JavaDoc name, Context JavaDoc context) {
129         try {
130             bindContext.invoke(null, new Object JavaDoc[]{name, context, name});
131         } catch (Throwable JavaDoc e) {
132             throw convertToRuntimeException(e, "bindContext");
133         }
134     }
135
136     public void bindThread(Object JavaDoc name) {
137         try {
138             bindThread.invoke(null, new Object JavaDoc[]{name, name});
139         } catch (Throwable JavaDoc e) {
140             throw convertToRuntimeException(e, "bindThread");
141         }
142     }
143
144     public void unbindThread(Object JavaDoc name) {
145         try {
146             unbindThread.invoke(null, new Object JavaDoc[]{name, name});
147         } catch (Throwable JavaDoc e) {
148             throw convertToRuntimeException(e, "unbindThread");
149         }
150     }
151
152     private RuntimeException JavaDoc convertToRuntimeException(Throwable JavaDoc e, String JavaDoc methodName) {
153         if (e instanceof InvocationTargetException JavaDoc){
154             Throwable JavaDoc cause = e.getCause();
155             if (cause instanceof RuntimeException JavaDoc){
156                 return (RuntimeException JavaDoc) cause;
157             } else {
158                 e = cause;
159             }
160         }
161         return new RuntimeException JavaDoc("ContextBindings."+methodName, e);
162     }
163 }
164
Popular Tags