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.catalina.Wrapper;
019: import org.apache.catalina.deploy.NamingResources;
020: import org.apache.catalina.core.NamingContextListener;
021: import org.apache.catalina.core.StandardContext;
022: import org.apache.catalina.core.StandardWrapper;
023: import org.apache.catalina.deploy.ContextService;
024: import org.apache.openejb.tomcat.common.TomcatVersion;
025:
026: import javax.servlet.Servlet;
027: import java.lang.reflect.Field;
028: import java.security.AccessController;
029: import java.security.PrivilegedAction;
030:
031: /**
032: * @version $Rev$ $Date$
033: */
034: public class BackportUtil {
035:
036: private static API api;
037:
038: static {
039: if (TomcatVersion.v6.isTheVersion()) {
040: api = new Tomcat6();
041: } else {
042: api = new Tomcat55();
043: }
044: }
045:
046: private static API getAPI() {
047: return api;
048: }
049:
050: public static Servlet getServlet(Wrapper wrapper) {
051: return getAPI().getServlet(wrapper);
052: }
053:
054: public static NamingContextListener getNamingContextListener(
055: StandardContext standardContext) {
056: return getAPI().getNamingContextListener(standardContext);
057: }
058:
059: public static String findServiceName(NamingResources naming,
060: String refName) {
061: return getAPI().findServiceName(naming, refName);
062: }
063:
064: public static void removeService(
065: NamingContextListener namingContextListener,
066: String serviceName) {
067: getAPI().removeService(namingContextListener, serviceName);
068: }
069:
070: public static interface API {
071: Servlet getServlet(Wrapper wrapper);
072:
073: String findServiceName(NamingResources naming, String refName);
074:
075: NamingContextListener getNamingContextListener(
076: StandardContext standardContext);
077:
078: void removeService(NamingContextListener namingContextListener,
079: String serviceName);
080: }
081:
082: public static class Tomcat6 implements API {
083: public Servlet getServlet(Wrapper wrapper) {
084: return wrapper.getServlet();
085: }
086:
087: public NamingContextListener getNamingContextListener(
088: StandardContext standardContext) {
089: return standardContext.getNamingContextListener();
090: }
091:
092: public String findServiceName(NamingResources naming,
093: String referenceName) {
094: ContextService service = naming.findService(referenceName);
095: return (service != null) ? service.getName() : null;
096: }
097:
098: public void removeService(
099: NamingContextListener namingContextListener,
100: String serviceName) {
101: namingContextListener.removeService(serviceName);
102: }
103: }
104:
105: public static class Tomcat55 implements API {
106: private final Field namingContextListener;
107: private final Field instance;
108:
109: public Tomcat55() {
110: namingContextListener = getField(StandardContext.class,
111: "namingContextListener");
112: instance = getField(StandardWrapper.class, "instance");
113: }
114:
115: public Servlet getServlet(Wrapper wrapper) {
116: try {
117: return (Servlet) instance.get(wrapper);
118: } catch (IllegalAccessException e) {
119: throw new IllegalStateException(e);
120: }
121: }
122:
123: public NamingContextListener getNamingContextListener(
124: StandardContext standardContext) {
125: try {
126: return (NamingContextListener) namingContextListener
127: .get(standardContext);
128: } catch (IllegalAccessException e) {
129: throw new IllegalStateException(e);
130: }
131: }
132:
133: public String findServiceName(NamingResources naming,
134: String refName) {
135: return null;
136: }
137:
138: public void removeService(
139: NamingContextListener namingContextListener,
140: String serviceName) {
141: }
142:
143: private Field getField(final Class clazz, final String name) {
144: return AccessController
145: .doPrivileged(new PrivilegedAction<Field>() {
146: public Field run() {
147: try {
148: Field field = clazz
149: .getDeclaredField(name);
150: field.setAccessible(true);
151: return field;
152: } catch (Exception e2) {
153: throw (IllegalStateException) new IllegalStateException(
154: "Unable to find or access the '"
155: + name + "' field in "
156: + clazz.getName())
157: .initCause(e2);
158: }
159: }
160: });
161: }
162:
163: }
164:
165: }
|