001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.tomcat.catalina;
017:
018: import org.apache.openejb.OpenEJBException;
019: import org.apache.openejb.RpcContainer;
020: import org.apache.openejb.DeploymentInfo;
021: import org.apache.openejb.core.RpcContainerWrapper;
022: import org.apache.openejb.core.CoreDeploymentInfo;
023:
024: import javax.naming.Context;
025: import java.lang.reflect.InvocationTargetException;
026: import java.lang.reflect.Method;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.util.Properties;
030:
031: public class TomcatJndiSupport extends RpcContainerWrapper {
032: private final Method bindContext;
033: private final Method bindThread;
034: private final Method unbindThread;
035:
036: public TomcatJndiSupport(RpcContainer container)
037: throws OpenEJBException {
038: super (container);
039: try {
040: ClassLoader classLoader = Thread.currentThread()
041: .getContextClassLoader();
042: Class contextBindings = classLoader
043: .loadClass("org.apache.naming.ContextBindings");
044: bindContext = contextBindings.getMethod("bindContext",
045: Object.class, Context.class, Object.class);
046: bindThread = contextBindings.getMethod("bindThread",
047: Object.class, Object.class);
048: unbindThread = contextBindings.getMethod("unbindThread",
049: Object.class, Object.class);
050: } catch (ClassNotFoundException e) {
051: throw new OpenEJBException(
052: "Unable to setup Tomcat JNDI support. Support requires the org.apache.naming.ContextBindings class to be available.");
053: } catch (NoSuchMethodException e) {
054: throw new OpenEJBException(
055: "Unable to setup Tomcat JNDI support. Method of org.apache.naming.ContextBindings was not found:"
056: + e.getMessage());
057: }
058: DeploymentInfo[] deploymentInfos = container.deployments();
059: for (DeploymentInfo deploymentInfo : deploymentInfos) {
060: CoreDeploymentInfo deployment = (CoreDeploymentInfo) deploymentInfo;
061: setupDeployment(deployment);
062: }
063: }
064:
065: @SuppressWarnings({"UnusedDeclaration"})
066: public void init(Object containerId, HashMap deployments,
067: Properties properties) throws OpenEJBException {
068: }
069:
070: public void deploy(DeploymentInfo info) throws OpenEJBException {
071: super .deploy(info);
072: setupDeployment((org.apache.openejb.core.CoreDeploymentInfo) info);
073: }
074:
075: public static Map<Object, Context> contexts = new HashMap<Object, Context>();
076:
077: private void setupDeployment(
078: org.apache.openejb.core.CoreDeploymentInfo deployment) {
079:
080: deployment.setContainer(this );
081:
082: Object deploymentID = deployment.getDeploymentID();
083: Context jndiEnc = deployment.getJndiEnc();
084: bindContext(deploymentID, jndiEnc);
085: contexts.put(deploymentID, jndiEnc);
086: }
087:
088: public Object invoke(Object deployID, Method callMethod,
089: Object[] args, Object primKey, Object securityIdentity)
090: throws OpenEJBException {
091: try {
092:
093: bindThread(deployID);
094: return super .invoke(deployID, callMethod, args, primKey,
095: securityIdentity);
096: } finally {
097: unbindThread(deployID);
098: }
099: }
100:
101: public void bindContext(Object name, Context context) {
102: try {
103: bindContext.invoke(null, name, context, name);
104: } catch (Throwable e) {
105: throw convertToRuntimeException(e, "bindContext");
106: }
107: }
108:
109: public void bindThread(Object name) {
110: try {
111: bindThread.invoke(null, name, name);
112: } catch (Throwable e) {
113: throw convertToRuntimeException(e, "bindThread");
114: }
115: }
116:
117: public void unbindThread(Object name) {
118: try {
119: unbindThread.invoke(null, name, name);
120: } catch (Throwable e) {
121: throw convertToRuntimeException(e, "unbindThread");
122: }
123: }
124:
125: private RuntimeException convertToRuntimeException(Throwable e,
126: String methodName) {
127: if (e instanceof InvocationTargetException) {
128: Throwable cause = e.getCause();
129: if (cause instanceof RuntimeException) {
130: return (RuntimeException) cause;
131: } else {
132: e = cause;
133: }
134: }
135: return new RuntimeException("ContextBindings." + methodName, e);
136: }
137: }
|