From 9391163a29bcefe1e709552050fa9638b6b19536 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Fri, 22 May 2009 08:31:06 +0000 Subject: [PATCH] Remove draft servlet spec 3 pojo annotation types; refactor annotation processing to make processing of non-classpath files easier (eg for jetty-maven-plugin); take out some debug printlns accidentally left in. git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@257 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../jetty/annotations/AnnotationFinder.java | 61 ++++++ .../annotations/AnnotationProcessor.java | 30 +-- .../jetty/annotations/Configuration.java | 98 ++++++---- .../TestAnnotationInheritance.java | 6 +- .../plus/annotation/InjectionCollection.java | 14 +- .../LifeCycleCallbackCollection.java | 28 +-- .../plus/annotation/PojoContextListener.java | 76 -------- .../jetty/plus/annotation/PojoFilter.java | 82 --------- .../jetty/plus/annotation/PojoServlet.java | 174 ------------------ .../jetty/plus/annotation/PojoWrapper.java | 20 -- .../eclipse/jetty/plus/annotation/RunAs.java | 2 - .../plus/annotation/RunAsCollection.java | 1 + .../plus/webapp/AbstractConfiguration.java | 88 +++++---- .../jetty/plus/webapp/Configuration.java | 8 +- .../jetty/webapp/WebInfConfiguration.java | 2 - 15 files changed, 204 insertions(+), 486 deletions(-) delete mode 100644 jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoContextListener.java delete mode 100644 jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoFilter.java delete mode 100644 jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoServlet.java delete mode 100644 jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoWrapper.java diff --git a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationFinder.java b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationFinder.java index 34b6d456aa6..f3faa9a1463 100644 --- a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationFinder.java +++ b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationFinder.java @@ -576,6 +576,16 @@ public class AnnotationFinder } + /** + * Find annotations on classes in the supplied classloader. + * Only class files in jar files will be scanned. + * @param loader + * @param visitParents + * @param jarNamePattern + * @param nullInclusive + * @param resolver + * @throws Exception + */ public void find (ClassLoader loader, boolean visitParents, String jarNamePattern, boolean nullInclusive, final ClassNameResolver resolver) throws Exception { @@ -621,6 +631,57 @@ public class AnnotationFinder } + /** + * Find annotations in classes in classes in the supplied url of jar files. + * @param urls + * @param visitParents + * @param jarNamePattern + * @param nullInclusive + * @param resolver + * @throws Exception + */ + public void find (URL[] urls, boolean visitParents, String jarNamePattern, boolean nullInclusive, final ClassNameResolver resolver) + throws Exception + { + if (urls==null) + return; + + JarScanner scanner = new JarScanner() + { + public void processEntry(URL jarUrl, JarEntry entry) + { + try + { + String name = entry.getName(); + if (name.toLowerCase().endsWith(".class")) + { + String shortName = name.replace('/', '.').substring(0,name.length()-6); + if (!resolver.isExcluded(shortName)) + { + if ((parsedClasses.get(shortName) == null) || (resolver.shouldOverride(shortName))) + { + parsedClasses.remove(shortName); + Resource clazz = Resource.newResource("jar:"+jarUrl+"!/"+name); + scanClass(clazz.getInputStream()); + } + } + } + } + catch (Exception e) + { + Log.warn("Problem processing jar entry "+entry, e); + } + } + + }; + Pattern pattern = null; + if (jarNamePattern!=null) + pattern = Pattern.compile(jarNamePattern); + + scanner.scan(pattern, urls, nullInclusive); + } + + /** Exclude class by name * Instances of {@link AnnotationFinder} can implement this method to exclude * classes by name. diff --git a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationProcessor.java b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationProcessor.java index 45ce46a20da..3b727d6beaf 100644 --- a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationProcessor.java +++ b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationProcessor.java @@ -35,7 +35,9 @@ import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection; import org.eclipse.jetty.plus.annotation.PostConstructCallback; import org.eclipse.jetty.plus.annotation.PreDestroyCallback; import org.eclipse.jetty.plus.annotation.RunAsCollection; +import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.util.IntrospectionUtil; +import org.eclipse.jetty.util.LazyList; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.webapp.WebAppContext; @@ -58,25 +60,31 @@ public class AnnotationProcessor List _listeners; List _servletMappings; List _filterMappings; - Map _pojoInstances = new HashMap(); WebAppContext _webApp; private static Class[] __envEntryTypes = new Class[] {String.class, Character.class, Integer.class, Boolean.class, Double.class, Byte.class, Short.class, Long.class, Float.class}; - public AnnotationProcessor(WebAppContext webApp, AnnotationFinder finder, RunAsCollection runAs, InjectionCollection injections, LifeCycleCallbackCollection callbacks, - List servlets, List filters, List listeners, List servletMappings, List filterMappings) + public AnnotationProcessor(WebAppContext webApp, AnnotationFinder finder) { + if (webApp == null) + throw new IllegalStateException("No WebAppContext"); + _webApp=webApp; _finder=finder; - _runAs=runAs; - _injections=injections; - _callbacks=callbacks; - _servlets=servlets; - _filters=filters; - _listeners=listeners; - _servletMappings=servletMappings; - _filterMappings=filterMappings; + ServletHandler servletHandler = _webApp.getServletHandler(); + _filters = LazyList.array2List(servletHandler.getFilters()); + _filterMappings = LazyList.array2List(servletHandler.getFilterMappings()); + _servlets = LazyList.array2List(servletHandler.getServlets()); + _servletMappings = LazyList.array2List(servletHandler.getServletMappings()); + _listeners = LazyList.array2List(_webApp.getEventListeners()); + + _runAs = (RunAsCollection)_webApp.getAttribute(RunAsCollection.RUNAS_COLLECTION); + _injections = (InjectionCollection)_webApp.getAttribute(InjectionCollection.INJECTION_COLLECTION); + _callbacks = (LifeCycleCallbackCollection)_webApp.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION); + + if (_runAs == null || _injections == null || _callbacks == null) + throw new IllegalStateException("RunAs, Injections or LifeCycleCallbacks is null"); } diff --git a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/Configuration.java b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/Configuration.java index a46d6269f6d..a7111d234f2 100644 --- a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/Configuration.java +++ b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/Configuration.java @@ -16,6 +16,9 @@ package org.eclipse.jetty.annotations; import java.util.EventListener; import java.util.List; +import org.eclipse.jetty.plus.annotation.InjectionCollection; +import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection; +import org.eclipse.jetty.plus.annotation.RunAsCollection; import org.eclipse.jetty.plus.servlet.ServletHandler; import org.eclipse.jetty.security.SecurityHandler; import org.eclipse.jetty.servlet.FilterHolder; @@ -24,6 +27,7 @@ import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletMapping; import org.eclipse.jetty.util.LazyList; import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.webapp.WebAppContext; /** @@ -38,11 +42,6 @@ public class Configuration extends org.eclipse.jetty.plus.webapp.Configuration - public Configuration () throws ClassNotFoundException - { - super(); - } - /** * @see org.eclipse.jetty.plus.webapp.AbstractConfiguration#parseAnnotations() */ @@ -65,13 +64,36 @@ public class Configuration extends org.eclipse.jetty.plus.webapp.Configuration * webappcontext parentloaderpriority to work out which one contributes the * annotation. */ - - AnnotationFinder finder = new AnnotationFinder(); + //TODO change for servlet spec 3 + parseContainerPath (context, finder); + parseWebInfLib (context, finder); + parseWebInfClasses (context, finder); + + AnnotationProcessor processor = new AnnotationProcessor(context, finder); + processor.process(); + + List servlets = processor.getServlets(); + List filters = processor.getFilters(); + List servletMappings = processor.getServletMappings(); + List filterMappings = processor.getFilterMappings(); + List listeners = processor.getListeners(); + + ServletHandler servletHandler = (ServletHandler)context.getServletHandler(); + servletHandler.setFilters((FilterHolder[])LazyList.toArray(filters,FilterHolder.class)); + servletHandler.setFilterMappings((FilterMapping[])LazyList.toArray(filterMappings,FilterMapping.class)); + servletHandler.setServlets((ServletHolder[])LazyList.toArray(servlets,ServletHolder.class)); + servletHandler.setServletMappings((ServletMapping[])LazyList.toArray(servletMappings,ServletMapping.class)); + context.setEventListeners((EventListener[])LazyList.toArray(listeners,EventListener.class)); + } + + public void parseContainerPath (final WebAppContext context, final AnnotationFinder finder) + throws Exception + { //if no pattern for the container path is defined, then by default scan NOTHING - Log.debug("Scanning system jars"); - finder.find(context.getClassLoader().getParent(), true, context.getInitParameter(__container_pattern), false, + Log.debug("Scanning container jars"); + parseAnnotationsFromJars(context, finder, context.getClassLoader().getParent(), context.getInitParameter(__container_pattern),true, false, new ClassNameResolver () { public boolean isExcluded (String name) @@ -89,10 +111,15 @@ public class Configuration extends org.eclipse.jetty.plus.webapp.Configuration return false; } }); - + } + + + public void parseWebInfLib (final WebAppContext context, final AnnotationFinder finder) + throws Exception + { Log.debug("Scanning WEB-INF/lib jars"); //if no pattern for web-inf/lib is defined, then by default scan everything in it - finder.find (context.getClassLoader(), false, context.getInitParameter(__web_inf_pattern), true, + parseAnnotationsFromJars (context, finder, context.getClassLoader(), context.getInitParameter(__web_inf_pattern), false, true, new ClassNameResolver() { public boolean isExcluded (String name) @@ -109,10 +136,15 @@ public class Configuration extends org.eclipse.jetty.plus.webapp.Configuration return false; return true; } - }); - + }); + + } + + public void parseWebInfClasses (final WebAppContext context, final AnnotationFinder finder) + throws Exception + { Log.debug("Scanning classes in WEB-INF/classes"); - finder.find(context.getWebInf().addPath("classes/"), + parseAnnotationsFromDir (context, finder, context.getWebInf().addPath("classes/"), new ClassNameResolver() { public boolean isExcluded (String name) @@ -130,29 +162,19 @@ public class Configuration extends org.eclipse.jetty.plus.webapp.Configuration return true; } }); - - ServletHandler servletHandler = (ServletHandler)context.getServletHandler(); - List filters = LazyList.array2List(servletHandler.getFilters()); - List filterMappings = LazyList.array2List(servletHandler.getFilterMappings()); - List servlets = LazyList.array2List(servletHandler.getServlets()); - List servletMappings = LazyList.array2List(servletHandler.getServletMappings()); - List listeners = LazyList.array2List(context.getEventListeners()); - - AnnotationProcessor processor = new AnnotationProcessor(context, finder, _runAsCollection, _injections, _callbacks, - servlets, filters,listeners, - servletMappings, filterMappings); - processor.process(); - - servlets = processor.getServlets(); - filters = processor.getFilters(); - servletMappings = processor.getServletMappings(); - filterMappings = processor.getFilterMappings(); - listeners = processor.getListeners(); - - servletHandler.setFilters((FilterHolder[])LazyList.toArray(filters,FilterHolder.class)); - servletHandler.setFilterMappings((FilterMapping[])LazyList.toArray(filterMappings,FilterMapping.class)); - servletHandler.setServlets((ServletHolder[])LazyList.toArray(servlets,ServletHolder.class)); - servletHandler.setServletMappings((ServletMapping[])LazyList.toArray(servletMappings,ServletMapping.class)); - context.setEventListeners((EventListener[])LazyList.toArray(listeners,EventListener.class)); + } + + + + public void parseAnnotationsFromJars (final WebAppContext context, final AnnotationFinder finder, final ClassLoader classloader, final String pattern, boolean visitParents, boolean isNullInclusive, ClassNameResolver resolver) + throws Exception + { + finder.find (classloader, visitParents, pattern, isNullInclusive,resolver); + } + + public void parseAnnotationsFromDir (final WebAppContext context, final AnnotationFinder finder, final Resource dir, ClassNameResolver resolver) + throws Exception + { + finder.find(dir, resolver); } } diff --git a/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/TestAnnotationInheritance.java b/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/TestAnnotationInheritance.java index 6efe9eac2db..0ad365fdee2 100644 --- a/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/TestAnnotationInheritance.java +++ b/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/TestAnnotationInheritance.java @@ -164,10 +164,12 @@ public class TestAnnotationInheritance extends TestCase assertEquals(6, annotatedFields.size()); InjectionCollection injections = new InjectionCollection(); + wac.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections); LifeCycleCallbackCollection callbacks = new LifeCycleCallbackCollection(); + wac.setAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION, callbacks); RunAsCollection runAses = new RunAsCollection(); - AnnotationProcessor processor = new AnnotationProcessor(wac, finder, runAses, injections, callbacks, - Collections.EMPTY_LIST, Collections.EMPTY_LIST, Collections.EMPTY_LIST, Collections.EMPTY_LIST, Collections.EMPTY_LIST); + wac.setAttribute(RunAsCollection.RUNAS_COLLECTION, runAses); + AnnotationProcessor processor = new AnnotationProcessor(wac, finder); //process with all the specific annotations turned into injections, callbacks etc processor.process(); diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/InjectionCollection.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/InjectionCollection.java index 49a94f46e9c..3c9dc0df763 100644 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/InjectionCollection.java +++ b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/InjectionCollection.java @@ -31,6 +31,7 @@ import org.eclipse.jetty.util.log.Log; */ public class InjectionCollection { + public static final String INJECTION_COLLECTION = "org.eclipse.jetty.injectionCollection"; private HashMap, List> fieldInjectionsMap = new HashMap, List>();//map of classname to field injections private HashMap, List> methodInjectionsMap = new HashMap, List>();//map of classname to method injections @@ -126,25 +127,22 @@ public class InjectionCollection //looking at it's class hierarchy Class clazz = injectable.getClass(); - - if (injectable instanceof PojoWrapper) - { - injectable = ((PojoWrapper)injectable).getPojo(); - clazz = injectable.getClass(); - } - - + while (clazz != null) { //Do field injections List injections = getFieldInjections(clazz); for (Injection i : injections) + { i.inject(injectable); + } //Do method injections injections = getMethodInjections(clazz); for (Injection i : injections) + { i.inject(injectable); + } clazz = clazz.getSuperclass(); } diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/LifeCycleCallbackCollection.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/LifeCycleCallbackCollection.java index a8c521ff68c..35491656b99 100644 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/LifeCycleCallbackCollection.java +++ b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/LifeCycleCallbackCollection.java @@ -28,6 +28,7 @@ import org.eclipse.jetty.util.log.Log; */ public class LifeCycleCallbackCollection { + public static final String LIFECYCLE_CALLBACK_COLLECTION = "org.eclipse.jetty.lifecyleCallbackCollection"; private HashMap postConstructCallbacksMap = new HashMap(); private HashMap preDestroyCallbacksMap = new HashMap(); @@ -75,13 +76,6 @@ public class LifeCycleCallbackCollection return null; Class clazz = o.getClass(); - - if (o instanceof PojoWrapper) - { - o = ((PojoWrapper)o).getPojo(); - clazz = o.getClass(); - } - return (List)preDestroyCallbacksMap.get(clazz); } @@ -91,13 +85,6 @@ public class LifeCycleCallbackCollection return null; Class clazz = o.getClass(); - - if (o instanceof PojoWrapper) - { - o = ((PojoWrapper)o).getPojo(); - clazz = o.getClass(); - } - return (List)postConstructCallbacksMap.get(clazz); } @@ -114,12 +101,6 @@ public class LifeCycleCallbackCollection return; Class clazz = o.getClass(); - - if (o instanceof PojoWrapper) - { - o = ((PojoWrapper)o).getPojo(); - clazz = o.getClass(); - } List callbacks = (List)postConstructCallbacksMap.get(clazz); if (callbacks == null) @@ -142,13 +123,6 @@ public class LifeCycleCallbackCollection return; Class clazz = o.getClass(); - - if (o instanceof PojoWrapper) - { - o = ((PojoWrapper)o).getPojo(); - clazz = o.getClass(); - } - List callbacks = (List)preDestroyCallbacksMap.get(clazz); if (callbacks == null) return; diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoContextListener.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoContextListener.java deleted file mode 100644 index 02602669af8..00000000000 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoContextListener.java +++ /dev/null @@ -1,76 +0,0 @@ -// ======================================================================== -// Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== - -package org.eclipse.jetty.plus.annotation; - -import java.lang.reflect.Method; - -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; - -public class PojoContextListener implements ServletContextListener, PojoWrapper -{ - private Object _pojo; - private Method _contextDestroyedMethod; - private Method _contextInitializedMethod; - private static final Class[] __params = new Class[]{ServletContextEvent.class}; - - public PojoContextListener(Object pojo) - throws IllegalArgumentException - { - if (pojo==null) - throw new IllegalArgumentException("Pojo is null"); - - _pojo = pojo; - try - { - _contextDestroyedMethod = _pojo.getClass().getDeclaredMethod("contextDestroyed", __params); - _contextInitializedMethod = _pojo.getClass().getDeclaredMethod("contextInitialized", __params); - } - catch (NoSuchMethodException e) - { - throw new IllegalStateException (e.getLocalizedMessage()); - } - } - - public Object getPojo() - { - return _pojo; - } - - public void contextDestroyed(ServletContextEvent event) - { - try - { - _contextDestroyedMethod.invoke(_pojo, new Object[]{event}); - } - catch (Exception e) - { - event.getServletContext().log("Error invoking contextInitialized", e); - } - - } - - public void contextInitialized(ServletContextEvent event) - { - try - { - _contextInitializedMethod.invoke(_pojo, new Object[]{event}); - } - catch (Exception e) - { - event.getServletContext().log("Error invoking contextInitialized", e); - } - } - -} diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoFilter.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoFilter.java deleted file mode 100644 index 2c632033360..00000000000 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoFilter.java +++ /dev/null @@ -1,82 +0,0 @@ -// ======================================================================== -// Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== - -package org.eclipse.jetty.plus.annotation; - -import java.io.IOException; -import java.lang.reflect.Method; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -public class PojoFilter implements Filter, PojoWrapper -{ - private Object _pojo; - private Method _doFilterMethod; - private static final Class[] __params = new Class[] {HttpServletRequest.class, HttpServletResponse.class, FilterChain.class}; - - public PojoFilter (Object pojo) - { - if (pojo == null) - throw new IllegalArgumentException ("Pojo is null"); - - _pojo=pojo; - - try - { - _doFilterMethod = _pojo.getClass().getDeclaredMethod("doFilter", __params); - } - catch (Exception e) - { - throw new IllegalStateException (e); - } - - } - - public Object getPojo() - { - return _pojo; - } - - public void destroy() - { - //TODO???? Should try to find a destroy method on the pojo? - } - - - public void doFilter(ServletRequest req, ServletResponse resp, - FilterChain chain) throws IOException, ServletException - { - try - { - _doFilterMethod.invoke(_pojo, new Object[]{req, resp, chain}); - } - catch (Exception e) - { - throw new ServletException(e); - } - } - - public void init(FilterConfig arg0) throws ServletException - { - // TODO ???? Should try to find an init() method on the pojo? - } - - -} diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoServlet.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoServlet.java deleted file mode 100644 index c417b6fbf9c..00000000000 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoServlet.java +++ /dev/null @@ -1,174 +0,0 @@ -// ======================================================================== -// Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== - -package org.eclipse.jetty.plus.annotation; - -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -public class PojoServlet extends HttpServlet implements PojoWrapper -{ - private Object _pojo; - private String _deleteMethodName; - private Method _deleteMethod; - private String _putMethodName; - private Method _putMethod; - private String _headMethodName; - private Method _headMethod; - private String _postMethodName; - private Method _postMethod; - private String _getMethodName; - private Method _getMethod; - private static final Class[] __params = new Class[]{HttpServletRequest.class, HttpServletResponse.class}; - - public PojoServlet (Object pojo) - { - if (pojo==null) - throw new IllegalArgumentException("Pojo is null"); - - _pojo=pojo; - } - - public Object getPojo() - { - return _pojo; - } - public void setDeleteMethodName (String name) - { - _deleteMethodName = name; - } - public String getDeleteMethodName () - { - return _deleteMethodName; - } - public void setPutMethodName (String name) - { - _putMethodName = name; - } - public String getPutMethodName () - { - return _putMethodName; - } - public void setHeadMethodName (String name) - { - _headMethodName = name; - } - - public String getHeadMethodName () - { - return _headMethodName; - } - public void setPostMethodName (String name) - { - _postMethodName = name; - } - public String getPostMethodName () - { - return _postMethodName; - } - - public void setGetMethodName (String name) - { - _getMethodName = name; - } - public String getGetMethodName () - { - return _getMethodName; - } - - - public void init() throws ServletException - { - - try - { - if (_getMethodName != null) - _getMethod = _pojo.getClass().getDeclaredMethod(_getMethodName, __params); - if (_postMethodName != null) - _postMethod = _pojo.getClass().getDeclaredMethod(_postMethodName, __params); - if (_headMethodName != null) - _headMethod = _pojo.getClass().getDeclaredMethod(_headMethodName, __params); - if (_putMethodName != null) - _putMethod = _pojo.getClass().getDeclaredMethod(_putMethodName, __params); - if (_deleteMethodName != null) - _deleteMethod = _pojo.getClass().getDeclaredMethod(_deleteMethodName, __params); - } - catch (NoSuchMethodException e) - { - throw new ServletException (e); - } - super.init(); - } - - - protected void doDelete(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException - { - invoke (_deleteMethod, req, resp); - } - - - protected void doGet(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException - { - invoke(_getMethod,req, resp); - } - - - protected void doHead(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException - { - invoke(_headMethod, req, resp); - } - - - protected void doPost(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException - { - invoke(_postMethod, req, resp); - } - - - protected void doPut(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException - { - invoke(_putMethod, req, resp); - } - - private void invoke (Method method, HttpServletRequest req, HttpServletResponse resp) - throws ServletException - { - if (method == null) - throw new ServletException ("No method"); - - try - { - method.invoke(_pojo, new Object[]{req, resp}); - } - catch (IllegalAccessException e) - { - throw new ServletException (e); - } - catch (InvocationTargetException e) - { - throw new ServletException (e); - } - } - -} diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoWrapper.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoWrapper.java deleted file mode 100644 index 7d9bff929b3..00000000000 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/PojoWrapper.java +++ /dev/null @@ -1,20 +0,0 @@ -// ======================================================================== -// Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== - - -package org.eclipse.jetty.plus.annotation; - -public interface PojoWrapper -{ - public Object getPojo(); -} diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/RunAs.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/RunAs.java index 46f9069c5e6..70055bc8cf9 100644 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/RunAs.java +++ b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/RunAs.java @@ -67,8 +67,6 @@ public class RunAs public static String getServletClassNameForHolder (ServletHolder holder) throws ServletException { - if (PojoServlet.class.getName().equals(holder.getClassName())) - return ((PojoWrapper)holder.getServlet()).getPojo().getClass().getName(); return holder.getClassName(); } diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/RunAsCollection.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/RunAsCollection.java index 57534bb924c..34e6c40dc60 100644 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/RunAsCollection.java +++ b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/RunAsCollection.java @@ -29,6 +29,7 @@ import org.eclipse.jetty.util.log.Log; */ public class RunAsCollection { + public static final String RUNAS_COLLECTION = "org.eclipse.jetty.runAsCollection"; private HashMap _runAsMap = new HashMap();//map of classname to run-as diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/webapp/AbstractConfiguration.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/webapp/AbstractConfiguration.java index 10fc1f6f157..881658deaaf 100644 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/webapp/AbstractConfiguration.java +++ b/jetty-plus/src/main/java/org/eclipse/jetty/plus/webapp/AbstractConfiguration.java @@ -46,11 +46,6 @@ import org.eclipse.jetty.xml.XmlParser; */ public abstract class AbstractConfiguration implements Configuration { - protected LifeCycleCallbackCollection _callbacks = new LifeCycleCallbackCollection(); - protected InjectionCollection _injections = new InjectionCollection(); - protected RunAsCollection _runAsCollection = new RunAsCollection(); - protected SecurityHandler _securityHandler; - public abstract void bindEnvEntry (WebAppContext context, String name, Object value) throws Exception; public abstract void bindResourceRef (WebAppContext context, String name, Class type) throws Exception; @@ -292,12 +287,13 @@ public abstract class AbstractConfiguration implements Configuration return; } + LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION); try { Class clazz = _context.loadClass(className); LifeCycleCallback callback = new PostConstructCallback(); callback.setTarget(clazz, methodName); - _callbacks.add(callback); + callbacks.add(callback); } catch (ClassNotFoundException e) { @@ -324,13 +320,13 @@ public abstract class AbstractConfiguration implements Configuration Log.warn("No lifecycle-callback-method specified for pre-destroy class "+className); return; } - + LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)_context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION); try { Class clazz = _context.loadClass(className); LifeCycleCallback callback = new PreDestroyCallback(); callback.setTarget(clazz, methodName); - _callbacks.add(callback); + callbacks.add(callback); } catch (ClassNotFoundException e) { @@ -367,6 +363,7 @@ public abstract class AbstractConfiguration implements Configuration continue; } + InjectionCollection injections = (InjectionCollection)_context.getAttribute(InjectionCollection.INJECTION_COLLECTION); // comments in the javaee_5.xsd file specify that the targetName is looked // for first as a java bean property, then if that fails, as a field try @@ -376,7 +373,7 @@ public abstract class AbstractConfiguration implements Configuration injection.setTargetClass(clazz); injection.setJndiName(jndiName); injection.setTarget(clazz, targetName, valueClass); - _injections.add(injection); + injections.add(injection); } catch (ClassNotFoundException e) { @@ -386,16 +383,6 @@ public abstract class AbstractConfiguration implements Configuration } } - - - /** - * @throws ClassNotFoundException - */ - public AbstractConfiguration() throws ClassNotFoundException - { - super(); - } - public void preConfigure (WebAppContext context) @@ -403,14 +390,21 @@ public abstract class AbstractConfiguration implements Configuration { //set up our special ServletHandler to remember injections and lifecycle callbacks ServletHandler servletHandler = new ServletHandler(); - _securityHandler = context.getSecurityHandler(); + SecurityHandler securityHandler = context.getSecurityHandler(); org.eclipse.jetty.servlet.ServletHandler existingHandler = context.getServletHandler(); servletHandler.setFilterMappings(existingHandler.getFilterMappings()); servletHandler.setFilters(existingHandler.getFilters()); servletHandler.setServlets(existingHandler.getServlets()); servletHandler.setServletMappings(existingHandler.getServletMappings()); context.setServletHandler(servletHandler); - _securityHandler.setHandler(servletHandler); + securityHandler.setHandler(servletHandler); + + LifeCycleCallbackCollection callbacks = new LifeCycleCallbackCollection(); + context.setAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION, callbacks); + InjectionCollection injections = new InjectionCollection(); + context.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections); + RunAsCollection runAsCollection = new RunAsCollection(); + context.setAttribute(RunAsCollection.RUNAS_COLLECTION, runAsCollection); } public void configure (WebAppContext context) @@ -422,12 +416,6 @@ public abstract class AbstractConfiguration implements Configuration if (webXmlProcessor == null) throw new IllegalStateException ("No processor for web xml"); - //parse classes for annotations, if necessary - if (!webXmlProcessor.isMetaDataComplete()) - { - if (Log.isDebugEnabled()) Log.debug("Processing annotations"); - parseAnnotations(context); - } //TODO: When webdefaults.xml, web.xml, fragments and web-override.xml are merged into an effective web.xml this //will change PlusWebXmlProcessor plusProcessor = new PlusWebXmlProcessor(context); @@ -440,11 +428,18 @@ public abstract class AbstractConfiguration implements Configuration plusProcessor.process(webXmlProcessor.getOverrideWeb()); - + + //parse classes for annotations, if necessary + if (!webXmlProcessor.isMetaDataComplete()) + { + if (Log.isDebugEnabled()) Log.debug("Processing annotations"); + parseAnnotations(context); + } + //configure injections and callbacks to be called by the FilterHolder and ServletHolder //when they lazily instantiate the Filter/Servlet. - ((ServletHandler)context.getServletHandler()).setInjections(_injections); - ((ServletHandler)context.getServletHandler()).setCallbacks(_callbacks); + ((ServletHandler)context.getServletHandler()).setInjections((InjectionCollection)context.getAttribute(InjectionCollection.INJECTION_COLLECTION)); + ((ServletHandler)context.getServletHandler()).setCallbacks((LifeCycleCallbackCollection)context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION)); //do any injects on the listeners that were created and then //also callback any postConstruct lifecycle methods @@ -463,30 +458,45 @@ public abstract class AbstractConfiguration implements Configuration protected void injectAndCallPostConstructCallbacks(WebAppContext context) throws Exception { + InjectionCollection injections = (InjectionCollection)context.getAttribute(InjectionCollection.INJECTION_COLLECTION); + RunAsCollection runAsCollection = (RunAsCollection)context.getAttribute(RunAsCollection.RUNAS_COLLECTION); + LifeCycleCallbackCollection callbacks = (LifeCycleCallbackCollection)context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION); + SecurityHandler securityHandler = context.getSecurityHandler(); + //look thru the servlets to apply any runAs annotations //NOTE: that any run-as in web.xml will already have been applied - ServletHolder[] holders = context.getServletHandler().getServlets(); - for (int i=0;holders!=null && i