From: costin Date: Tue, 14 Jul 2009 05:38:02 +0000 (+0000) Subject: Some initial implementations for the 3.0 dynamic registration methods. Moving back... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=1ee20e64d1d1eae0492438fb3164e0f6db3d60b8;p=tomcat7.0 Some initial implementations for the 3.0 dynamic registration methods. Moving back web.xml config to separate package - with annotations requiring scanning of all files it needs to be done at deploy time - so it'll be better to just load a .ser file at startup. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@793797 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/modules/tomcat-lite/java/org/apache/tomcat/lite/FilterConfigImpl.java b/modules/tomcat-lite/java/org/apache/tomcat/lite/FilterConfigImpl.java index 80f1e3f62..bd0275f6a 100644 --- a/modules/tomcat-lite/java/org/apache/tomcat/lite/FilterConfigImpl.java +++ b/modules/tomcat-lite/java/org/apache/tomcat/lite/FilterConfigImpl.java @@ -19,13 +19,19 @@ package org.apache.tomcat.lite; import java.util.ArrayList; +import java.util.EnumSet; import java.util.Enumeration; +import java.util.HashSet; import java.util.Map; +import java.util.Set; +import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterConfig; +import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; import javax.servlet.ServletException; +import javax.servlet.FilterRegistration.Dynamic; import org.apache.tomcat.servlets.util.Enumerator; @@ -41,29 +47,37 @@ import org.apache.tomcat.servlets.util.Enumerator; * * @see ServletConfigImpl */ -public final class FilterConfigImpl implements FilterConfig { +public final class FilterConfigImpl implements FilterConfig, FilterRegistration { + DynamicFilterRegistration dynamic = new DynamicFilterRegistration(); + public FilterConfigImpl(ServletContextImpl context) { - this.context = context; + this.ctx = context; } - private ServletContextImpl context = null; + boolean asyncSupported; + + private ServletContextImpl ctx = null; /** * The application Filter we are configured for. */ private transient Filter filter = null; - + + String descryption; + private String filterName; - private String filterClass; + private String filterClassName; + + Map initParams; - private Map initParams; + private Class filterClass; public void setData(String filterName, String filterClass, Map params) { this.filterName = filterName; - this.filterClass = filterClass; + this.filterClassName = filterClass; this.initParams = params; } @@ -75,6 +89,11 @@ public final class FilterConfigImpl implements FilterConfig { return filterName; } + public void setFilterClass(Class filterClass2) { + this.filterClass = filterClass2; + } + + public String getInitParameter(String name) { if (initParams == null) return null; return initParams.get(name); @@ -96,20 +115,20 @@ public final class FilterConfigImpl implements FilterConfig { * Return the ServletContext of our associated web application. */ public ServletContext getServletContext() { - return context; + return ctx; } /** * Return the application Filter we are configured for. */ - public Filter getFilter() throws ClassCastException, ClassNotFoundException, + public Filter createFilter() throws ClassCastException, ClassNotFoundException, IllegalAccessException, InstantiationException, ServletException { // Return the existing filter instance, if any if (filter != null) return filter; - ClassLoader classLoader = context.getClassLoader(); + ClassLoader classLoader = ctx.getClassLoader(); ClassLoader oldCtxClassLoader = Thread.currentThread().getContextClassLoader(); @@ -117,14 +136,23 @@ public final class FilterConfigImpl implements FilterConfig { Thread.currentThread().setContextClassLoader(classLoader); } try { - Class clazz = classLoader.loadClass(filterClass); - this.filter = (Filter) clazz.newInstance(); + if (filterClass == null) { + filterClass = (Class) classLoader.loadClass(filterClassName); + } + this.filter = (Filter) filterClass.newInstance(); } finally { if (classLoader != oldCtxClassLoader) { Thread.currentThread().setContextClassLoader(oldCtxClassLoader); } } + // TODO: resource injection + + return filter; + } + + public Filter getFilter() throws ClassCastException, ClassNotFoundException, IllegalAccessException, InstantiationException, ServletException { + Filter filter = createFilter(); filter.init(this); return (this.filter); } @@ -140,4 +168,105 @@ public final class FilterConfigImpl implements FilterConfig { } this.filter = null; } + + @Override + public void addMappingForServletNames(EnumSet dispatcherTypes, + boolean isMatchAfter, + String... servletNames) { + if (ctx.startDone) { + // Use the context method instead of the servlet API to + // add mappings after context init. + throw new IllegalStateException(); + } + ArrayList dispatchers = new ArrayList(); + for (DispatcherType dt: dispatcherTypes) { + dispatchers.add(dt.name()); + } + for (String servletName: servletNames) { + ctx.getFilterMapper().addMapping(getFilterName(), + null, servletName, (String[]) dispatchers.toArray(), isMatchAfter); + } + } + + @Override + public void addMappingForUrlPatterns(EnumSet dispatcherTypes, + boolean isMatchAfter, + String... urlPatterns) { + if (ctx.startDone) { + // Use the context method instead of the servlet API to + // add mappings after context init. + throw new IllegalStateException(); + } + ArrayList dispatchers = new ArrayList(); + for (DispatcherType dt: dispatcherTypes) { + dispatchers.add(dt.name()); + } + for (String url: urlPatterns) { + ctx.getFilterMapper().addMapping(getFilterName(), + url, null, (String[]) dispatchers.toArray(), isMatchAfter); + } + } + + @Override + public boolean setInitParameter(String name, String value) + throws IllegalArgumentException, IllegalStateException { + return ServletContextImpl.setInitParameter(ctx, initParams, + name, value); + } + + @Override + public Set setInitParameters(Map initParameters) + throws IllegalArgumentException, IllegalStateException { + return ServletContextImpl.setInitParameters(ctx, initParams, + initParameters); + } + + public Dynamic getDynamic() { + return dynamic; + } + + public class DynamicFilterRegistration implements Dynamic { + + @Override + public void addMappingForServletNames(EnumSet dispatcherTypes, + boolean isMatchAfter, + String... servletNames) { + FilterConfigImpl.this.addMappingForServletNames(dispatcherTypes, isMatchAfter, servletNames); + } + + @Override + public void addMappingForUrlPatterns(EnumSet dispatcherTypes, + boolean isMatchAfter, + String... urlPatterns) { + FilterConfigImpl.this.addMappingForUrlPatterns(dispatcherTypes, isMatchAfter, urlPatterns); + } + + @Override + public boolean setInitParameter(String name, String value) + throws IllegalArgumentException, IllegalStateException { + return ServletContextImpl.setInitParameter(ctx, initParams, + name, value); + } + + @Override + public Set setInitParameters(Map initParameters) + throws IllegalArgumentException, IllegalStateException { + return ServletContextImpl.setInitParameters(ctx, initParams, + initParameters); + } + + @Override + public void setAsyncSupported(boolean isAsyncSupported) + throws IllegalStateException { + asyncSupported = isAsyncSupported; + } + + @Override + public void setDescription(String description) + throws IllegalStateException { + FilterConfigImpl.this.descryption = description; + } + } + + } diff --git a/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletConfigImpl.java b/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletConfigImpl.java index 85be60b5a..caf8b61d7 100644 --- a/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletConfigImpl.java +++ b/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletConfigImpl.java @@ -23,6 +23,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.Stack; import java.util.logging.Level; import java.util.logging.Logger; @@ -31,6 +32,7 @@ import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; import javax.servlet.SingleThreadModel; import javax.servlet.UnavailableException; @@ -48,7 +50,12 @@ import org.apache.tomcat.util.IntrospectionUtils; * @author Craig R. McClanahan * @author Remy Maucherat */ -public class ServletConfigImpl implements ServletConfig { +@SuppressWarnings("deprecation") +public class ServletConfigImpl implements ServletConfig, ServletRegistration { + + ServletDynamicRegistration dynamic = new ServletDynamicRegistration(); + + protected boolean asyncSupported; private static Logger log= Logger.getLogger(ServletConfigImpl.class.getName()); @@ -60,10 +67,10 @@ public class ServletConfigImpl implements ServletConfig { // public static final String SINGLE_THREADED_PROXY = // "org.apache.tomcat.servlets.jsp.SingleThreadedProxyServlet"; - + protected String description; protected Map initParams = new HashMap(); protected String servletName; - protected String servletClass; + protected String servletClassName; protected String jspFile; protected int loadOnStartup = -1; protected String runAs; @@ -89,7 +96,7 @@ public class ServletConfigImpl implements ServletConfig { */ private transient boolean unloading = false; - private Class classClass = null; + private Class servletClass = null; // Support for SingleThreaded /** @@ -113,7 +120,7 @@ public class ServletConfigImpl implements ServletConfig { public ServletConfigImpl(ServletContextImpl ctx, String name, String classname) { this.servletName = name; - this.servletClass = classname; + this.servletClassName = classname; this.ctx = ctx; ctx.facade.notifyAdd(this); } @@ -183,7 +190,7 @@ public class ServletConfigImpl implements ServletConfig { * Return the fully qualified servlet class name for this servlet. */ public String getServletClass() { - return servletClass; + return servletClassName; } /** @@ -368,9 +375,12 @@ public class ServletConfigImpl implements ServletConfig { } } - private Servlet newInstance() throws ServletException { - String actualClass = servletClass; + public Servlet newInstance() throws ServletException { + String actualClass = servletClassName; + if (instance != null) { + return instance; + } if (actualClass == null) { // No explicit name. Try to use the framework if (jspFile != null) { @@ -378,8 +388,8 @@ public class ServletConfigImpl implements ServletConfig { // Named JSPs can be handled by a servlet or by the mapper. Servlet res = (Servlet) ctx.getObjectManager().get("filetemplate-servlet"); if (res != null) { - classClass = res.getClass(); - actualClass = classClass.getName(); + servletClass = res.getClass(); + actualClass = servletClass.getName(); initParams.put("jsp-file", jspFile); return res; } else { @@ -396,8 +406,8 @@ public class ServletConfigImpl implements ServletConfig { Servlet res = (Servlet) ctx.getObjectManager().get( servletName + "-servlet"); if (res != null) { - classClass = res.getClass(); - actualClass = classClass.getName(); + servletClass = res.getClass(); + actualClass = servletClass.getName(); return res; } } @@ -407,7 +417,7 @@ public class ServletConfigImpl implements ServletConfig { } - if (classClass == null) { + if (servletClass == null) { // set classClass loadClass(actualClass); } @@ -424,14 +434,14 @@ public class ServletConfigImpl implements ServletConfig { // the jsp proxy is replaced by the web.xml processor - if (classClass == null) { + if (servletClass == null) { unavailable(null); throw new UnavailableException("ClassNotFound: " + actualClass); } // Instantiate and initialize an instance of the servlet class itself try { - return (Servlet) classClass.newInstance(); + return (Servlet) servletClass.newInstance(); } catch (ClassCastException e) { unavailable(null); throw new UnavailableException("ClassCast: (Servlet)" + @@ -509,9 +519,9 @@ public class ServletConfigImpl implements ServletConfig { // Load the specified servlet class from the appropriate class loader try { - classClass = classLoader.loadClass(actualClass); + servletClass = classLoader.loadClass(actualClass); } catch (ClassNotFoundException e) { - classClass = null; + servletClass = null; } } @@ -526,7 +536,7 @@ public class ServletConfigImpl implements ServletConfig { } sb.append("Servlet["); sb.append(getServletName()).append(" "); - sb.append(servletClass); + sb.append(servletClassName); if (jspFile != null) { sb.append(" jsp=").append(jspFile); } @@ -652,9 +662,7 @@ public class ServletConfigImpl implements ServletConfig { * @param name Name of the initialization parameter to retrieve */ public String getInitParameter(String name) { - - return (String)initParams.get(name); - + return initParams.get(name); } @@ -663,11 +671,9 @@ public class ServletConfigImpl implements ServletConfig { * servlet. If none are defined, an empty Enumeration is returned. */ public Enumeration getInitParameterNames() { - synchronized (initParams) { return (new Enumerator(initParams.keySet())); } - } @@ -805,5 +811,63 @@ public class ServletConfigImpl implements ServletConfig { this.loadOnStartup = loadOnStartup; } + @Override + public Set addMapping(String... urlPatterns) { + if (ctx.startDone) { + // Use the context method instead of the servlet API to + // add mappings after context init. + throw new IllegalStateException(); + } + Set failed = new HashSet(); + for (String url: urlPatterns) { + if (url == null) { + throw new IllegalArgumentException(); + } + if (ctx.contextConfig.servletMapping.get(url) != null) { + failed.add(url); + } else { + ctx.contextConfig.servletMapping.put(url, getServletName()); + ctx.addMapping(url, this); + } + } + return failed; + } + @Override + public boolean setInitParameter(String name, String value) + throws IllegalArgumentException, IllegalStateException { + return ServletContextImpl.setInitParameter(ctx, initParams, + name, value); + } + + @Override + public Set setInitParameters(Map initParameters) + throws IllegalArgumentException, IllegalStateException { + return ServletContextImpl.setInitParameters(ctx, initParams, + initParameters); + } + + public Dynamic getDynamic() { + return dynamic; + } + + class ServletDynamicRegistration implements Dynamic { + + @Override + public void setAsyncSupported(boolean isAsyncSupported) + throws IllegalStateException { + asyncSupported = isAsyncSupported; + } + + @Override + public void setDescription(String description) + throws IllegalStateException { + ServletConfigImpl.this.description = description; + } + + } + + public void setServletClass(Class servletClass2) { + servletClass = servletClass2; + } } diff --git a/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletContextConfig.java b/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletContextConfig.java deleted file mode 100644 index bbb43b877..000000000 --- a/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletContextConfig.java +++ /dev/null @@ -1,131 +0,0 @@ -/** - * - */ -package org.apache.tomcat.lite; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.HashMap; - -/** - * All the data in web.xml should be represented here. - * - * Public fields to make it easy to access it. - * Naming should match the web.xml element name. - * - * @author Costin Manolache - */ -public class ServletContextConfig implements Serializable { - - private static final long serialVersionUID = 1728492145981883124L; - - public String fileName; - public long timestamp; - - public boolean full; - - public String displayName; - - public HashMap contextParam = new HashMap(); - - public HashMap mimeMapping = new HashMap(); // extension -> mime-type - - public ArrayList listenerClass = new ArrayList(); - - public ArrayList welcomeFileList = new ArrayList(); - - // code -> location - public HashMap errorPageCode= new HashMap(); - - // exception -> location - public HashMap errorPageException= new HashMap(); - - public HashMap localeEncodingMapping= new HashMap(); // locale -> encoding - - // public HashMap tagLibs; // uri->location - // jsp-property-group - - // securityConstraint - public ArrayList securityConstraint = new ArrayList(); - - // loginConfig - public String authMethod; - public String realmName; - public String formLoginPage; - public String formErrorPage; - - public ArrayList securityRole = new ArrayList(); - - // envEntry - public ArrayList envEntry = new ArrayList(); - - // ejbRef - // ejbLocalRef - // serviceRef - // resourceRef - // resourceEnvRef - // message-destination - // message-destinationRef - public HashMap filters = new HashMap(); - public HashMap servlets = new HashMap(); - - public int sessionTimeout; - public boolean distributable; - - public HashMap servletMapping = new HashMap(); // url -> servlet - public ArrayList filterMappings = new ArrayList(); - - - public static class FilterData implements Serializable { - private static final long serialVersionUID = -535820271746973166L; - - public HashMap initParams = new HashMap(); - public String filterClass; - public String filterName; - } - - public static class FilterMappingData implements Serializable { - private static final long serialVersionUID = -4533568066713041994L; - public String filterName; - public String urlPattern; - public String servletName; - public ArrayList dispatcher = new ArrayList(); - } - - public static class EnvEntryData implements Serializable { - private static final long serialVersionUID = 7023847615343715257L; - public String envEntryName; - public String envEntryType; - public String envEntryValue; - } - - public static class ServletData implements Serializable { - private static final long serialVersionUID = -3216904178501185930L; - - public ServletData() { - } - public ServletData(String servletName, String servletClass) { - this.servletClass = servletClass; - this.servletName = servletName; - } - - public HashMap initParams = new HashMap(); - public String servletName; - public String servletClass; - public String jspFile; - public int loadOnStartup = -1; - public String runAs; - public HashMap securityRoleRef = new HashMap(); // roleName -> [roleLink] - - } - - public static class SecurityConstraintData implements Serializable { - private static final long serialVersionUID = -4780214921810871769L; - - public ArrayList roleName = new ArrayList(); // auth-constraint/role - - public ArrayList webResourceCollection = new ArrayList(); - public String transportGuarantee; - - } -} \ No newline at end of file diff --git a/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletContextImpl.java b/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletContextImpl.java index f65866f5e..061e9d452 100644 --- a/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletContextImpl.java +++ b/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletContextImpl.java @@ -37,9 +37,11 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; +import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterRegistration; import javax.servlet.RequestDispatcher; @@ -58,9 +60,10 @@ import javax.servlet.FilterRegistration.Dynamic; import org.apache.tomcat.addons.UserSessionManager; import org.apache.tomcat.integration.ObjectManager; -import org.apache.tomcat.lite.ServletContextConfig.FilterData; -import org.apache.tomcat.lite.ServletContextConfig.FilterMappingData; -import org.apache.tomcat.lite.ServletContextConfig.ServletData; +import org.apache.tomcat.lite.webxml.ServletContextConfig; +import org.apache.tomcat.lite.webxml.ServletContextConfig.FilterData; +import org.apache.tomcat.lite.webxml.ServletContextConfig.FilterMappingData; +import org.apache.tomcat.lite.webxml.ServletContextConfig.ServletData; import org.apache.tomcat.servlets.util.Enumerator; import org.apache.tomcat.servlets.util.RequestUtil; import org.apache.tomcat.servlets.util.UrlUtils; @@ -144,6 +147,11 @@ public class ServletContextImpl implements ServletContext { private String hostname; + + boolean initDone = false; + + boolean startDone = false; + // ------------------------------------------------- ServletContext Methods public ServletContextImpl() { } @@ -836,13 +844,6 @@ public class ServletContextImpl implements ServletContext { } } - public void addFilter(String filterName, String filterClass, - Map params) { - FilterConfigImpl fc = new FilterConfigImpl(this); - fc.setData(filterName, filterClass, params); - filters.put(filterName, fc); - } - public void initFilters() throws ServletException { Iterator fI = getFilters().values().iterator(); while (fI.hasNext()) { @@ -856,6 +857,7 @@ public class ServletContextImpl implements ServletContext { } } + public void initServlets() throws ServletException { Iterator fI = getServletConfigs().values().iterator(); Map/*>*/ onStartup = @@ -915,6 +917,8 @@ public class ServletContextImpl implements ServletContext { getMapper().addWrapper(getMapper().contextMapElement, path, wrapper); } + + public void setWelcomeFiles(String[] name) { getMapper().contextMapElement.welcomeResources = name; } @@ -1010,12 +1014,13 @@ public class ServletContextImpl implements ServletContext { sc.setConfig(params); addServletConfig(sc); } - + + @Override public javax.servlet.Registration.Dynamic addServlet(String servletName, Servlet servlet) { ServletConfigImpl sc = new ServletConfigImpl(this, servletName, null); sc.setServlet(servlet); addServletConfig(sc); - return null; + return sc.getDynamic(); } public void addServletSec(String serlvetName, String runAs, Map roles) { @@ -1027,7 +1032,7 @@ public class ServletContextImpl implements ServletContext { public void addFilterMapping(String path, String filterName, String[] dispatcher) { getFilterMapper().addMapping(filterName, - path, null, dispatcher); + path, null, dispatcher, true); } @@ -1036,11 +1041,9 @@ public class ServletContextImpl implements ServletContext { String[] dispatcher) { getFilterMapper().addMapping(filterName, null, servlet, - dispatcher); + dispatcher, true); } - boolean initDone = false; - /** * Called from TomcatLite.init(), required before start. * @@ -1167,6 +1170,9 @@ public class ServletContextImpl implements ServletContext { public void start() throws ServletException { + if (startDone) { + return; + } String base = getBasePath(); ArrayList urls = getClasspath(new File(base + "/WEB-INF/lib"), @@ -1205,6 +1211,7 @@ public class ServletContextImpl implements ServletContext { event = new ServletContextEvent(this); } try { + // May add servlets/filters listener.contextInitialized(event); } catch (Throwable t) { log.log(Level.WARNING, "Context.init() contextInitialized() error:", t); @@ -1214,6 +1221,8 @@ public class ServletContextImpl implements ServletContext { initFilters(); initServlets(); + + startDone = true; } public UserSessionManager getManager() { @@ -1392,57 +1401,151 @@ public class ServletContextImpl implements ServletContext { public void setSessionTrackingModes(EnumSet sessionTrackingModes) { } + public void addFilter(String filterName, String filterClass, + Map params) { + FilterConfigImpl fc = new FilterConfigImpl(this); + fc.setData(filterName, filterClass, params); + filters.put(filterName, fc); + } + @Override public Dynamic addFilter(String filterName, String className) { - return null; + FilterConfigImpl fc = new FilterConfigImpl(this); + fc.setData(filterName, className, new HashMap()); + filters.put(filterName, fc); + return fc.getDynamic(); } @Override public Dynamic addFilter(String filterName, Filter filter) { - return null; + FilterConfigImpl fc = new FilterConfigImpl(this); + fc.setData(filterName, null, new HashMap()); + fc.setFilter(filter); + filters.put(filterName, fc); + return fc.getDynamic(); } @Override public Dynamic addFilter(String filterName, Class filterClass) { - return null; + FilterConfigImpl fc = new FilterConfigImpl(this); + fc.setData(filterName, null, new HashMap()); + fc.setFilterClass(filterClass); + filters.put(filterName, fc); + return fc.getDynamic(); } @Override public javax.servlet.Registration.Dynamic addServlet(String servletName, String className) { - return null; + ServletConfigImpl sc = new ServletConfigImpl(this, servletName, className); + addServletConfig(sc); + return sc.getDynamic(); } @Override - public javax.servlet.Registration.Dynamic addServlet( - String servletName, + public javax.servlet.Registration.Dynamic addServlet(String servletName, Class servletClass) { - return null; + ServletConfigImpl sc = new ServletConfigImpl(this, servletName, servletClass.getName()); + sc.setServletClass(servletClass); + addServletConfig(sc); + return sc.getDynamic(); } + // That's tricky - this filter will have no name. We need to generate one + // because our code relies on names. + AtomicInteger autoName = new AtomicInteger(); + @Override public T createFilter(Class c) throws ServletException { - return null; + FilterConfigImpl fc = new FilterConfigImpl(this); + String filterName = "_tomcat_auto_filter_" + autoName.incrementAndGet(); + fc.setData(filterName, null, new HashMap()); + fc.setFilterClass(c); + filters.put(filterName, fc); + + try { + return (T) fc.createFilter(); + } catch (ClassCastException e) { + throw new ServletException(e); + } catch (ClassNotFoundException e) { + throw new ServletException(e); + } catch (IllegalAccessException e) { + throw new ServletException(e); + } catch (InstantiationException e) { + throw new ServletException(e); + } } @Override public T createServlet(Class c) throws ServletException { - return null; + String filterName = "_tomcat_auto_servlet_" + autoName.incrementAndGet(); + ServletConfigImpl fc = new ServletConfigImpl(this, filterName, null); + fc.setServletClass(c); + servlets.put(filterName, fc); + + try { + return (T) fc.newInstance(); + } catch (ClassCastException e) { + throw new ServletException(e); + } } @Override public FilterRegistration findFilterRegistration(String filterName) { - return null; + return filters.get(filterName); } @Override public ServletRegistration findServletRegistration(String servletName) { - return null; + return servlets.get(servletName); } - + @Override public boolean setInitParameter(String name, String value) { - return false; + HashMap params = contextConfig.contextParam; + return setInitParameter(this, params, name, value); + } + + static Set setInitParameters(ServletContextImpl ctx, + Map params, + Map initParameters) + throws IllegalArgumentException, IllegalStateException { + if (ctx.startDone) { + throw new IllegalStateException(); + } + Set result = new HashSet(); + for (String name: initParameters.keySet()) { + String value = initParameters.get(name); + if (name == null || value == null) { + throw new IllegalArgumentException(); + } + if (!setInitParameter(ctx, params, name, value)) { + result.add(name); + } + } + return result; + } + + /** + * true if the context initialization parameter with the given name and value was set successfully on this ServletContext, and false if it was not set because this ServletContext already contains a context initialization parameter with a matching name + * Throws: + * java.lang.IllegalStateException - if this ServletContext has already been initialized + */ + static boolean setInitParameter(ServletContextImpl ctx, Map params, + String name, String value) { + if (name == null || value == null) { + throw new IllegalArgumentException(); + } + if (ctx.startDone) { + throw new IllegalStateException(); + } + String oldValue = params.get(name); + if (oldValue != null) { + return false; + } else { + params.put(name, value); + return true; + } } } diff --git a/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletResponseImpl.java b/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletResponseImpl.java index 0ae93b1e6..34dae5d6d 100644 --- a/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletResponseImpl.java +++ b/modules/tomcat-lite/java/org/apache/tomcat/lite/ServletResponseImpl.java @@ -1,5 +1,5 @@ /* - * ontentLicensed to the Apache Software Foundation (ASF) under one or more + * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 diff --git a/modules/tomcat-lite/java/org/apache/tomcat/lite/WebappFilterMapper.java b/modules/tomcat-lite/java/org/apache/tomcat/lite/WebappFilterMapper.java index 78847a346..f6cfaedad 100644 --- a/modules/tomcat-lite/java/org/apache/tomcat/lite/WebappFilterMapper.java +++ b/modules/tomcat-lite/java/org/apache/tomcat/lite/WebappFilterMapper.java @@ -94,12 +94,16 @@ public class WebappFilterMapper implements Filter { public void addMapping(String filterName, String url, String servletName, - String type[]) { + String type[], boolean isMatchAfter) { FilterMap map = new FilterMap(); map.setURLPattern(url); map.setFilterName(filterName); map.setServletName(servletName); - filterMaps.add(map); + if (isMatchAfter) { + filterMaps.add(map); + } else { + filterMaps.add(0, map); + } } /** diff --git a/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/ServletContextConfig.java b/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/ServletContextConfig.java new file mode 100644 index 000000000..868660d02 --- /dev/null +++ b/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/ServletContextConfig.java @@ -0,0 +1,144 @@ +/** + * + */ +package org.apache.tomcat.lite.webxml; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; + +/** + * All the data in web.xml, annotations, etc should be represented + * here. This class is serializable. + * + * Public fields to make it easy to access it. + * Naming should match the web.xml element name. + * + * @author Costin Manolache + */ +public class ServletContextConfig implements Serializable { + + private static final long serialVersionUID = 1728492145981883124L; + + public String fileName; + public long timestamp; + + public boolean full; + + public String displayName; + + public HashMap contextParam = new HashMap(); + + public HashMap mimeMapping = new HashMap(); // extension -> mime-type + + public ArrayList listenerClass = new ArrayList(); + + public ArrayList welcomeFileList = new ArrayList(); + + // code -> location + public HashMap errorPageCode= new HashMap(); + + // exception -> location + public HashMap errorPageException= new HashMap(); + + public HashMap localeEncodingMapping= new HashMap(); // locale -> encoding + + // public HashMap tagLibs; // uri->location + // jsp-property-group + + // securityConstraint + public ArrayList securityConstraint = new ArrayList(); + + // loginConfig + public String authMethod; + public String realmName; + public String formLoginPage; + public String formErrorPage; + + public ArrayList securityRole = new ArrayList(); + + // envEntry + public ArrayList envEntry = new ArrayList(); + + // ejbRef + // ejbLocalRef + // serviceRef + // resourceRef + // resourceEnvRef + // message-destination + // message-destinationRef + public HashMap filters = new HashMap(); + public HashMap servlets = new HashMap(); + + public int sessionTimeout; + public boolean distributable; + + public HashMap servletMapping = new HashMap(); // url -> servlet + public ArrayList filterMappings = new ArrayList(); + + + public static class FilterData implements Serializable { + private static final long serialVersionUID = -535820271746973166L; + + public HashMap initParams = new HashMap(); + public String filterClass; + public String filterName; + } + + // Normalized + public static class FilterMappingData implements Serializable { + private static final long serialVersionUID = -4533568066713041994L; + public String filterName; + + // Only one of the 2 + public String urlPattern; + public String servletName; + + // REQUEST, FORWARD, INCLUDE, ERROR, ASYNC + public ArrayList dispatcher = new ArrayList(); + } + + public static class EnvEntryData implements Serializable { + private static final long serialVersionUID = 7023847615343715257L; + public String envEntryName; + public String envEntryType; + public String envEntryValue; + } + + public static class ServletData implements Serializable { + private static final long serialVersionUID = -3216904178501185930L; + + public ServletData() { + } + public ServletData(String servletName, String servletClass) { + this.servletClass = servletClass; + this.servletName = servletName; + } + + public HashMap initParams = new HashMap(); + public String servletName; + public String servletClass; + public String jspFile; + public int loadOnStartup = -1; + public String runAs; + public HashMap securityRoleRef = new HashMap(); // roleName -> [roleLink] + + } + + public static class WebResourceCollectionData implements Serializable { + public String webResourceName; + public ArrayList urlPattern = new ArrayList(); + public ArrayList httpMethod = new ArrayList(); + } + + public static class SecurityConstraintData implements Serializable { + private static final long serialVersionUID = -4780214921810871769L; + + public ArrayList roleName = new ArrayList(); // auth-constraint/role + + public ArrayList webResourceCollection = + new ArrayList(); + public String transportGuarantee; + + } +} \ No newline at end of file diff --git a/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/WebAnnotation.java b/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/WebAnnotation.java deleted file mode 100644 index cecaf60a8..000000000 --- a/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/WebAnnotation.java +++ /dev/null @@ -1,152 +0,0 @@ -package org.apache.tomcat.lite.webxml; - -import java.util.Iterator; -import java.util.List; - -import javax.annotation.security.DeclareRoles; -import javax.annotation.security.RunAs; - -import org.apache.tomcat.lite.ServletContextConfig; -import org.apache.tomcat.lite.ServletContextConfig.FilterData; -import org.apache.tomcat.lite.ServletContextConfig.ServletData; - - - - -/** - * Based on catalina.WebAnnotationSet - * - * Supports: - * @DeclaresRoles - on Servlet class - web-app/security-role/role-name - * @RunAs - on Servlet class - web-app/servlet/run-as - * - * - * No support for jndi @Resources, @Resource - * No @InjectionComplete callback annotation - * - * No support for @EJB, @WebServiceRef - * - * @author costin - * @author Fabien Carrion - */ -public class WebAnnotation { - - /** - * Process the annotations on a context. - */ - public static void loadApplicationAnnotations(ServletContextConfig context, ClassLoader classLoader) { - loadApplicationListenerAnnotations(context, classLoader); - loadApplicationFilterAnnotations(context, classLoader); - loadApplicationServletAnnotations(context, classLoader); - } - - - // -------------------------------------------------------- protected Methods - - - /** - * Process the annotations for the listeners. - */ - static void loadApplicationListenerAnnotations(ServletContextConfig context, ClassLoader classLoader) { - List applicationListeners = context.listenerClass; - for (int i = 0; i < applicationListeners.size(); i++) { - loadClassAnnotation(context, (String)applicationListeners.get(i), classLoader); - } - } - - - /** - * Process the annotations for the filters. - */ - static void loadApplicationFilterAnnotations(ServletContextConfig context, ClassLoader classLoader) { - Iterator i1 = context.filters.values().iterator(); - while (i1.hasNext()) { - FilterData fc = (FilterData) i1.next(); - loadClassAnnotation(context, fc.filterClass, classLoader); - } - } - - - /** - * Process the annotations for the servlets. - * @param classLoader - */ - static void loadApplicationServletAnnotations(ServletContextConfig context, ClassLoader classLoader) { - Class classClass = null; - - - Iterator i1 = context.servlets.values().iterator(); - while (i1.hasNext()) { - ServletData sd = (ServletData) i1.next(); - if (sd.servletClass == null) { - continue; - } - - try { - classClass = classLoader.loadClass(sd.servletClass); - } catch (ClassNotFoundException e) { - // We do nothing - } catch (NoClassDefFoundError e) { - // We do nothing - } - - if (classClass == null) { - continue; - } - - loadClassAnnotation(context, classClass); - /* Process RunAs annotation which can be only on servlets. - * Ref JSR 250, equivalent to the run-as element in - * the deployment descriptor - */ - if (classClass.isAnnotationPresent(RunAs.class)) { - RunAs annotation = (RunAs) - classClass.getAnnotation(RunAs.class); - sd.runAs = annotation.value(); - } - } - } - - /** - * Process the annotations on a context for a given className. - */ - static void loadClassAnnotation(ServletContextConfig context, - String classClass2, ClassLoader classLoader) { - - Class classClass = null; - - try { - classClass = classLoader.loadClass(classClass2); - } catch (ClassNotFoundException e) { - // We do nothing - } catch (NoClassDefFoundError e) { - // We do nothing - } - - if (classClass == null) { - return; - } - loadClassAnnotation(context, classClass); - } - - static void loadClassAnnotation(ServletContextConfig context, - Class classClass) { - - /* Process DeclareRoles annotation. - * Ref JSR 250, equivalent to the security-role element in - * the deployment descriptor - */ - if (classClass.isAnnotationPresent(DeclareRoles.class)) { - DeclareRoles annotation = (DeclareRoles) - classClass.getAnnotation(DeclareRoles.class); - for (int i = 0; annotation.value() != null && - i < annotation.value().length; i++) { - context.securityRole.add(annotation.value()[i]); - } - } - - - } - - -} diff --git a/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/WebResourceCollectionData.java b/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/WebResourceCollectionData.java deleted file mode 100644 index 94fa2a41c..000000000 --- a/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/WebResourceCollectionData.java +++ /dev/null @@ -1,13 +0,0 @@ -/** - * - */ -package org.apache.tomcat.lite.webxml; - -import java.io.Serializable; -import java.util.ArrayList; - -public class WebResourceCollectionData implements Serializable { - public String webResourceName; - public ArrayList urlPattern = new ArrayList(); - public ArrayList httpMethod = new ArrayList(); -} \ No newline at end of file diff --git a/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/WebXml.java b/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/WebXml.java index 2611050d2..8bbf88bc0 100644 --- a/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/WebXml.java +++ b/modules/tomcat-lite/java/org/apache/tomcat/lite/webxml/WebXml.java @@ -10,12 +10,12 @@ import java.util.HashMap; import javax.servlet.ServletException; -import org.apache.tomcat.lite.ServletContextConfig; -import org.apache.tomcat.lite.ServletContextConfig.EnvEntryData; -import org.apache.tomcat.lite.ServletContextConfig.FilterData; -import org.apache.tomcat.lite.ServletContextConfig.FilterMappingData; -import org.apache.tomcat.lite.ServletContextConfig.SecurityConstraintData; -import org.apache.tomcat.lite.ServletContextConfig.ServletData; +import org.apache.tomcat.lite.webxml.ServletContextConfig.EnvEntryData; +import org.apache.tomcat.lite.webxml.ServletContextConfig.FilterData; +import org.apache.tomcat.lite.webxml.ServletContextConfig.FilterMappingData; +import org.apache.tomcat.lite.webxml.ServletContextConfig.SecurityConstraintData; +import org.apache.tomcat.lite.webxml.ServletContextConfig.ServletData; +import org.apache.tomcat.lite.webxml.ServletContextConfig.WebResourceCollectionData; import org.apache.tomcat.util.DomUtil; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -252,7 +252,9 @@ public class WebXml { scn = DomUtil.getNext(scn); } cn = DomUtil.getNext(cn); + sd.webResourceCollection.add(wrd); } + d.securityConstraint.add(sd); }