diff options
| author | Simon Archer | 2011-10-19 20:53:36 +0000 |
|---|---|---|
| committer | Thomas Watson | 2011-10-19 20:57:08 +0000 |
| commit | bcb49f062fc476128f34aed1f6f04808a35147d7 (patch) | |
| tree | 6651ba0512e47717d3f737ea48c9e9fee748d8e9 | |
| parent | 45c0274c3aeb92f58cd31b45b417a0b3ac962ecb (diff) | |
| download | rt.equinox.bundles-bcb49f062fc476128f34aed1f6f04808a35147d7.tar.gz rt.equinox.bundles-bcb49f062fc476128f34aed1f6f04808a35147d7.tar.xz rt.equinox.bundles-bcb49f062fc476128f34aed1f6f04808a35147d7.zip | |
Bug 341643 - Simplify proxy implementation.
7 files changed, 94 insertions, 374 deletions
diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/MethodAdvisor.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/MethodAdvisor.java deleted file mode 100644 index f29df9ec5..000000000 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/MethodAdvisor.java +++ /dev/null @@ -1,124 +0,0 @@ -/*******************************************************************************
- * Copyright (c) 2011, IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *******************************************************************************/
-package org.eclipse.equinox.http.servlet.internal;
-
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.HashSet;
-import java.util.Set;
-
-class MethodAdvisor extends Object {
- private final Class subject;
- private final Set methodsCache;
- private final boolean methodCacheEnabled;
-
- // Property to turn off method caching.
- private static final String DISABLE_METHOD_CACHE = "org.eclipse.equinox.http.servlet.internal.disable.method.cache"; //$NON-NLS-1$
-
- private static boolean isMethodCacheEnabled() {
- return Boolean.getBoolean(MethodAdvisor.DISABLE_METHOD_CACHE) == false;
- }
-
- MethodAdvisor(Class subject) {
- super();
- if (subject == null)
- throw new IllegalArgumentException("subject must not be null"); //$NON-NLS-1$
- if (subject.isInterface())
- throw new IllegalArgumentException("subject must not be an interface"); //$NON-NLS-1$
- this.subject = subject;
- this.methodsCache = new HashSet(17);
- this.methodCacheEnabled = MethodAdvisor.isMethodCacheEnabled();
- }
-
- private boolean equals(Method left, Method right) {
- boolean match;
- match = hasEqualMethodNames(left, right);
- if (match == false)
- return false;
- match = hasEqualReturnTypes(left, right);
- if (match == false)
- return false;
- match = hasEqualParameterTypes(left, right);
- if (match == false)
- return false;
- return true;
- }
-
- private boolean hasEqualMethodNames(Method left, Method right) {
- String leftName = left.getName();
- String rightName = right.getName();
- boolean equal = leftName.equals(rightName);
- return equal;
- }
-
- private boolean hasEqualParameterTypes(Method left, Method right) {
- Class[] leftParameterTypes = left.getParameterTypes();
- Class[] rightMethodParameterTypes = right.getParameterTypes();
- boolean equal = leftParameterTypes.length == rightMethodParameterTypes.length;
- int i = 0;
- int count = leftParameterTypes.length - 1;
- while (equal && i <= count) {
- Class leftClass = leftParameterTypes[i];
- Class rightClass = rightMethodParameterTypes[i];
- equal = leftClass.equals(rightClass);
- i++;
- }
- return equal;
- }
-
- private boolean hasEqualReturnTypes(Method left, Method right) {
- Class leftClass = left.getReturnType();
- Class rightClass = right.getReturnType();
- boolean equal = leftClass.equals(rightClass);
- return equal;
- }
-
- private boolean hasValidModifiers(Method declaredMethod) {
- int modifiers = declaredMethod.getModifiers();
- boolean valid;
- valid = Modifier.isPublic(modifiers);
- if (valid == false)
- return false;
- valid = Modifier.isAbstract(modifiers) == false;
- if (valid == false)
- return false;
- return true;
- }
-
- private boolean isImplemented(Class clazz, Method method) {
- if (clazz == null)
- return false;
- Method[] declaredMethods = clazz.getDeclaredMethods();
- for (int i = 0; i < declaredMethods.length; i++) {
- Method declaredMethod = declaredMethods[i];
- boolean valid = hasValidModifiers(declaredMethod);
- if (valid == false)
- continue;
- boolean match = equals(method, declaredMethod);
- if (match == false)
- continue;
- methodsCache.add(method);
- return true; // Implemented and added to cache.
- }
- Class parent = clazz.getSuperclass();
- return isImplemented(parent, method);
- }
-
- boolean isImplemented(Method method) {
- if (method == null)
- throw new IllegalArgumentException("method must not be null"); //$NON-NLS-1$
- synchronized (methodsCache) {
- if (methodCacheEnabled) {
- boolean exists = methodsCache.contains(method);
- if (exists)
- return true; // Implemented and exists in cache.
- }
- return isImplemented(subject, method);
- }
- }
-}
diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ProxyServlet.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ProxyServlet.java index 933c6cb60..79cfaea89 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ProxyServlet.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ProxyServlet.java @@ -251,8 +251,8 @@ public class ProxyServlet extends HttpServlet { } private ServletContext createServletContextProxy(HttpContext httpContext) { - ServletContextAdaptor adapter = new ServletContextAdaptor(proxyContext, getServletContext(), httpContext, AccessController.getContext()); - return ServletContextProxyFactory.create(adapter); + ServletContextAdaptor handler = new ServletContextAdaptor(proxyContext, getServletContext(), httpContext, AccessController.getContext()); + return ServletContextProxyFactory.create(handler); } private int findFilterPriority(Dictionary initparams) { diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ServletContextAdaptor.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ServletContextAdaptor.java index dac165006..7923fde5b 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ServletContextAdaptor.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ServletContextAdaptor.java @@ -1,33 +1,40 @@ -/******************************************************************************* - * Copyright (c) 2005, 2011 Cognos Incorporated, IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Cognos Incorporated - initial API and implementation - * IBM Corporation - bug fixes and enhancements - *******************************************************************************/ package org.eclipse.equinox.http.servlet.internal; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.net.URL; import java.security.*; import java.util.*; -import javax.servlet.*; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; import org.osgi.service.http.HttpContext; -public class ServletContextAdaptor implements ServletContext { +public class ServletContextAdaptor implements InvocationHandler { - private ServletContext servletContext; - HttpContext httpContext; - private AccessControlContext acc; - private ProxyContext proxyContext; + private final static Map contextToHandlerMethods; + static { + Map methods = new HashMap(); + Class servletContextClazz = ServletContext.class; + Class handlerClazz = ServletContextAdaptor.class; + Method[] handlerMethods = handlerClazz.getDeclaredMethods(); + for (int i = 0; i < handlerMethods.length; i++) { + try { + Method m = servletContextClazz.getMethod(handlerMethods[i].getName(), handlerMethods[i].getParameterTypes()); + methods.put(m, handlerMethods[i]); + } catch (NoSuchMethodException e) { + // do nothing + } + } + contextToHandlerMethods = methods; + } + final private ServletContext servletContext; + final HttpContext httpContext; + final private AccessControlContext acc; + final private ProxyContext proxyContext; - ServletContextAdaptor(ProxyContext proxyContext, ServletContext servletContext, HttpContext httpContext, AccessControlContext acc) { + public ServletContextAdaptor(ProxyContext proxyContext, ServletContext servletContext, HttpContext httpContext, AccessControlContext acc) { this.servletContext = servletContext; this.httpContext = httpContext; this.acc = acc; @@ -89,7 +96,7 @@ public class ServletContextAdaptor implements ServletContext { } }, acc); } catch (PrivilegedActionException e) { - log(e.getException().getMessage(), e.getException()); + servletContext.log(e.getException().getMessage(), e.getException()); } return null; } @@ -100,92 +107,25 @@ public class ServletContextAdaptor implements ServletContext { try { return url.openStream(); } catch (IOException e) { - log("Error opening stream for resource '" + name + "'", e); //$NON-NLS-1$ //$NON-NLS-2$ + servletContext.log("Error opening stream for resource '" + name + "'", e); //$NON-NLS-1$ //$NON-NLS-2$ } } return null; } - public ServletContext getContext(String arg0) { - return servletContext.getContext(arg0); - } - - public String getInitParameter(String arg0) { - return servletContext.getInitParameter(arg0); - } - - public Enumeration getInitParameterNames() { - return servletContext.getInitParameterNames(); - } - - public int getMajorVersion() { - return servletContext.getMajorVersion(); - } - - public int getMinorVersion() { - return servletContext.getMinorVersion(); - } - public RequestDispatcher getNamedDispatcher(String arg0) { return new RequestDispatcherAdaptor(servletContext.getNamedDispatcher(arg0)); } - public String getRealPath(String arg0) { - return servletContext.getRealPath(arg0); - } - public RequestDispatcher getRequestDispatcher(String arg0) { return new RequestDispatcherAdaptor(servletContext.getRequestDispatcher(proxyContext.getServletPath() + arg0)); } - public String getServerInfo() { - return servletContext.getServerInfo(); - } - - /**@deprecated*/ - public Servlet getServlet(String arg0) throws ServletException { - return servletContext.getServlet(arg0); - } - - public String getServletContextName() { - return servletContext.getServletContextName(); - } - - /**@deprecated*/ - public Enumeration getServletNames() { - return servletContext.getServletNames(); - } - - /**@deprecated*/ - public Enumeration getServlets() { - return servletContext.getServlets(); - } - - /**@deprecated*/ - public void log(Exception arg0, String arg1) { - servletContext.log(arg0, arg1); - } - - public void log(String arg0, Throwable arg1) { - servletContext.log(arg0, arg1); - } - - public void log(String arg0) { - servletContext.log(arg0); - } - - // Added in Servlet 2.5 - public String getContextPath() { - try { - Method getContextPathMethod = servletContext.getClass().getMethod("getContextPath", null); //$NON-NLS-1$ - return (String) getContextPathMethod.invoke(servletContext, null) + proxyContext.getServletPath(); - } catch (Exception e) { - // ignore + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + Method m = (Method) contextToHandlerMethods.get(method); + if (m != null) { + return m.invoke(this, args); } - return null; - } - - Object getSubject() { - return servletContext; + return method.invoke(servletContext, args); } } diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ServletContextProxyFactory.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ServletContextProxyFactory.java index 8a771abcb..7893c6ef7 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ServletContextProxyFactory.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/ServletContextProxyFactory.java @@ -1,54 +1,23 @@ -/*******************************************************************************
- * Copyright (c) 2011, IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *******************************************************************************/
-package org.eclipse.equinox.http.servlet.internal;
-
-import java.lang.reflect.*;
-import javax.servlet.ServletContext;
-
-class ServletContextProxyFactory extends Object {
- private static final String PROXY_METHOD_TRACING_PROPERTY = "org.eclipse.equinox.http.servlet.internal.proxy.method.tracing"; //$NON-NLS-1$
- private static final boolean PROXY_METHOD_TRACING = Boolean.getBoolean(ServletContextProxyFactory.PROXY_METHOD_TRACING_PROPERTY);
-
- private MethodAdvisor methodAdvisor;
-
- static ServletContext create(ServletContextAdaptor adapter) {
- ServletContextProxyFactory factory = new ServletContextProxyFactory();
- return factory.createServletContext(adapter);
- }
-
- private ServletContextProxyFactory() {
- super();
- this.methodAdvisor = new MethodAdvisor(ServletContextAdaptor.class);
- }
-
- private ServletContext createServletContext(ServletContextAdaptor adapter) {
- if (adapter == null)
- throw new IllegalArgumentException("adapter must not be null"); //$NON-NLS-1$
- ClassLoader loader = ServletContextAdaptor.class.getClassLoader();
- InvocationHandler handler = createServletContextInvocationHandler(adapter);
- Class[] interfaces = new Class[] {ServletContext.class};
- return (ServletContext) Proxy.newProxyInstance(loader, interfaces, handler);
- }
-
- private InvocationHandler createServletContextInvocationHandler(final ServletContextAdaptor adapter) {
- return new InvocationHandler() {
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- return ServletContextProxyFactory.this.invoke(adapter, proxy, method, args);
- }
- };
- }
-
- private Object invoke(ServletContextAdaptor adapter, Object proxy, Method method, Object[] args) throws Throwable {
- if (ServletContextProxyFactory.PROXY_METHOD_TRACING) {
- System.out.println("TRACE-invoking: " + method.getName()); //$NON-NLS-1$
- }
- boolean match = methodAdvisor.isImplemented(method);
- Object object = match ? adapter : adapter.getSubject();
- return method.invoke(object, args);
- }
-}
+/******************************************************************************* + * Copyright (c) 2011, IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.equinox.http.servlet.internal; + +import java.lang.reflect.Proxy; +import javax.servlet.ServletContext; + +class ServletContextProxyFactory extends Object { + private static ClassLoader cl = ServletContextProxyFactory.class.getClassLoader(); + + static ServletContext create(ServletContextAdaptor handler) { + if (handler == null) + throw new IllegalArgumentException("adapter must not be null"); //$NON-NLS-1$ + Class[] interfaces = new Class[] {ServletContext.class}; + return (ServletContext) Proxy.newProxyInstance(cl, interfaces, handler); + } + +} diff --git a/bundles/org.eclipse.equinox.jsp.jasper/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.jsp.jasper/META-INF/MANIFEST.MF index 2e46923ad..0c59656dc 100644 --- a/bundles/org.eclipse.equinox.jsp.jasper/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.jsp.jasper/META-INF/MANIFEST.MF @@ -6,9 +6,9 @@ Bundle-Localization: plugin Bundle-SymbolicName: org.eclipse.equinox.jsp.jasper Bundle-Version: 1.0.300.qualifier Bundle-Activator: org.eclipse.equinox.internal.jsp.jasper.Activator -Import-Package: javax.servlet;version="[2.4, 3.0)", - javax.servlet.http;version="[2.4, 3.0)", - javax.servlet.jsp;version="[2.0, 2.1)", +Import-Package: javax.servlet;version="[2.4, 3.1)", + javax.servlet.http;version="[2.4, 3.1)", + javax.servlet.jsp;version="[2.0, 2.3)", org.apache.jasper.servlet;version="[0, 6)", org.osgi.framework;version="1.3.0", org.osgi.service.http;version="1.2.0", diff --git a/bundles/org.eclipse.equinox.jsp.jasper/src/org/eclipse/equinox/internal/jsp/jasper/Activator.java b/bundles/org.eclipse.equinox.jsp.jasper/src/org/eclipse/equinox/internal/jsp/jasper/Activator.java index 483ad5998..a7ebe116d 100644 --- a/bundles/org.eclipse.equinox.jsp.jasper/src/org/eclipse/equinox/internal/jsp/jasper/Activator.java +++ b/bundles/org.eclipse.equinox.jsp.jasper/src/org/eclipse/equinox/internal/jsp/jasper/Activator.java @@ -28,6 +28,9 @@ public class Activator implements BundleActivator, ServiceTrackerCustomizer { private BundleContext context; public void start(BundleContext context) throws Exception { + //disable the JSR99 compiler that does not work in OSGi; + //This will convince jasper to use the JDTCompiler that invokes ecj (see JSP-21 on the glassfish bug-tracker) + System.setProperty("org.apache.jasper.compiler.disablejsr199", Boolean.TRUE.toString()); this.context = context; thisBundle = context.getBundle(); packageAdminTracker = new ServiceTracker(context, PackageAdmin.class.getName(), this); diff --git a/bundles/org.eclipse.equinox.jsp.jasper/src/org/eclipse/equinox/jsp/jasper/JspServlet.java b/bundles/org.eclipse.equinox.jsp.jasper/src/org/eclipse/equinox/jsp/jasper/JspServlet.java index 0ffb1e295..17cc259d5 100644 --- a/bundles/org.eclipse.equinox.jsp.jasper/src/org/eclipse/equinox/jsp/jasper/JspServlet.java +++ b/bundles/org.eclipse.equinox.jsp.jasper/src/org/eclipse/equinox/jsp/jasper/JspServlet.java @@ -14,7 +14,9 @@ package org.eclipse.equinox.jsp.jasper; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; @@ -22,10 +24,11 @@ import java.security.Permission; import java.security.PermissionCollection; import java.util.Collections; import java.util.Enumeration; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; -import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; @@ -65,6 +68,7 @@ import org.osgi.framework.Bundle; */ public class JspServlet extends HttpServlet { + private static ClassLoader cl = JspServlet.class.getClassLoader(); private static class BundlePermissionCollection extends PermissionCollection { private static final long serialVersionUID = -6365478608043900677L; @@ -170,7 +174,8 @@ public class JspServlet extends HttpServlet { public ServletConfigAdaptor(ServletConfig config) { this.config = config; - this.context = new ServletContextAdaptor(config.getServletContext()); + Class[] interfaces = new Class[] {ServletContext.class}; + this.context = (ServletContext) Proxy.newProxyInstance(cl, interfaces, new ServletContextAdaptor(config.getServletContext())); } public String getInitParameter(String arg0) { @@ -190,7 +195,25 @@ public class JspServlet extends HttpServlet { } } - private class ServletContextAdaptor implements ServletContext { + private final static Map contextToHandlerMethods; + static { + HashMap methods = new HashMap(); + Class servletContextClazz = ServletContext.class; + Class handlerClazz = ServletContextAdaptor.class; + Method[] handlerMethods = handlerClazz.getDeclaredMethods(); + for (int i = 0; i < handlerMethods.length; i++) { + try { + Method m = servletContextClazz.getMethod(handlerMethods[i].getName(), handlerMethods[i].getParameterTypes()); + methods.put(m, handlerMethods[i]); + } catch (NoSuchMethodException e) { + // do nothing + } + } + contextToHandlerMethods = methods; + } + + class ServletContextAdaptor implements InvocationHandler { + private ServletContext delegate; public ServletContextAdaptor(ServletContext delegate) { @@ -265,103 +288,12 @@ public class JspServlet extends HttpServlet { return result; } - public RequestDispatcher getRequestDispatcher(String arg0) { - return delegate.getRequestDispatcher(arg0); - } - - public Object getAttribute(String arg0) { - return delegate.getAttribute(arg0); - } - - public Enumeration getAttributeNames() { - return delegate.getAttributeNames(); - } - - public ServletContext getContext(String arg0) { - return delegate.getContext(arg0); - } - - public String getInitParameter(String arg0) { - return delegate.getInitParameter(arg0); - } - - public Enumeration getInitParameterNames() { - return delegate.getInitParameterNames(); - } - - public int getMajorVersion() { - return delegate.getMajorVersion(); - } - - public String getMimeType(String arg0) { - return delegate.getMimeType(arg0); - } - - public int getMinorVersion() { - return delegate.getMinorVersion(); - } - - public RequestDispatcher getNamedDispatcher(String arg0) { - return delegate.getNamedDispatcher(arg0); - } - - public String getRealPath(String arg0) { - return delegate.getRealPath(arg0); - } - - public String getServerInfo() { - return delegate.getServerInfo(); - } - - /** @deprecated **/ - public Servlet getServlet(String arg0) throws ServletException { - return delegate.getServlet(arg0); - } - - public String getServletContextName() { - return delegate.getServletContextName(); - } - - /** @deprecated **/ - public Enumeration getServletNames() { - return delegate.getServletNames(); - } - - /** @deprecated **/ - public Enumeration getServlets() { - return delegate.getServlets(); - } - - /** @deprecated **/ - public void log(Exception arg0, String arg1) { - delegate.log(arg0, arg1); - } - - public void log(String arg0, Throwable arg1) { - delegate.log(arg0, arg1); - } - - public void log(String arg0) { - delegate.log(arg0); - } - - public void removeAttribute(String arg0) { - delegate.removeAttribute(arg0); - } - - public void setAttribute(String arg0, Object arg1) { - delegate.setAttribute(arg0, arg1); - } - - // Added in Servlet 2.5 - public String getContextPath() { - try { - Method getContextPathMethod = delegate.getClass().getMethod("getContextPath", null); //$NON-NLS-1$ - return (String) getContextPathMethod.invoke(delegate, null); - } catch (Exception e) { - // ignore + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + Method m = (Method) contextToHandlerMethods.get(method); + if (m != null) { + return m.invoke(this, args); } - return null; + return method.invoke(delegate, args); } } } |
