From 2d0539a2999cdfecfa541ed03ee5753ee4835391 Mon Sep 17 00:00:00 2001 From: markt Date: Sun, 7 Feb 2010 20:08:54 +0000 Subject: [PATCH] Servlet 3.0 Remainder of ServletContext plumbing Plenty of scope to clean this up Only lightly tested so far git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@907468 13f79535-47bb-0310-9956-ffa450edef68 --- .../javax/servlet/descriptor/TaglibDescriptor.java | 2 +- java/org/apache/catalina/Context.java | 54 +------ .../apache/catalina/core/ApplicationContext.java | 3 +- .../core/ApplicationJspConfigDescriptor.java | 45 ++++++ .../ApplicationJspPropertyGroupDescriptor.java | 171 +++++++++++++++++++++ .../catalina/core/ApplicationTaglibDescriptor.java | 81 ++++++++++ java/org/apache/catalina/core/StandardContext.java | 103 ++----------- java/org/apache/catalina/deploy/WebXml.java | 19 ++- .../org/apache/catalina/startup/ContextConfig.java | 6 - java/org/apache/catalina/startup/TldConfig.java | 35 +++-- java/org/apache/catalina/startup/TldRuleSet.java | 8 +- 11 files changed, 361 insertions(+), 166 deletions(-) create mode 100644 java/org/apache/catalina/core/ApplicationJspConfigDescriptor.java create mode 100644 java/org/apache/catalina/core/ApplicationJspPropertyGroupDescriptor.java create mode 100644 java/org/apache/catalina/core/ApplicationTaglibDescriptor.java diff --git a/java/javax/servlet/descriptor/TaglibDescriptor.java b/java/javax/servlet/descriptor/TaglibDescriptor.java index 208640b88..f930c5d58 100644 --- a/java/javax/servlet/descriptor/TaglibDescriptor.java +++ b/java/javax/servlet/descriptor/TaglibDescriptor.java @@ -21,6 +21,6 @@ package javax.servlet.descriptor; * TODO SERVLET3 - Add comments */ public interface TaglibDescriptor { - public String getTaglidURI(); + public String getTaglibURI(); public String getTaglibLocation(); } diff --git a/java/org/apache/catalina/Context.java b/java/org/apache/catalina/Context.java index 83d391352..f7dba3ee0 100644 --- a/java/org/apache/catalina/Context.java +++ b/java/org/apache/catalina/Context.java @@ -20,6 +20,7 @@ package org.apache.catalina; import javax.servlet.ServletContext; +import javax.servlet.descriptor.JspConfigDescriptor; import org.apache.tomcat.JarScanner; import org.apache.tomcat.util.http.mapper.Mapper; @@ -624,18 +625,6 @@ public interface Context extends Container { /** - * Add the given URL pattern as a jsp-property-group. This maps - * resources that match the given pattern so they will be passed - * to the JSP container. Though there are other elements in the - * property group, we only care about the URL pattern here. The - * JSP container will parse the rest. - * - * @param pattern URL pattern to be mapped - */ - public void addJspMapping(String pattern); - - - /** * Add a Locale Encoding Mapping (see Sec 5.4 of Servlet spec 2.4) * * @param locale locale to map an encoding for @@ -692,15 +681,6 @@ public interface Context extends Container { /** - * Add a JSP tag library for the specified URI. - * - * @param uri URI, relative to the web.xml file, of this tag library - * @param location Location of the tag library descriptor - */ - public void addTaglib(String uri, String location); - - - /** * Add a resource which will be watched for reloading by the host auto * deployer. Note: this will not be used in embedded mode. * @@ -911,23 +891,6 @@ public interface Context extends Container { /** - * Return the tag library descriptor location for the specified taglib - * URI, if any; otherwise, return null. - * - * @param uri URI, relative to the web.xml file - */ - public String findTaglib(String uri); - - - /** - * Return the URIs of all tag libraries for which a tag library - * descriptor location has been specified. If none are specified, - * a zero-length array is returned. - */ - public String[] findTaglibs(); - - - /** * Return the set of watched resources for this Context. If none are * defined, a zero length array will be returned. */ @@ -1078,14 +1041,6 @@ public interface Context extends Container { /** - * Remove the tag library location for the specified tag library URI. - * - * @param uri URI, relative to the web.xml file - */ - public void removeTaglib(String uri); - - - /** * Remove the specified watched resource name from the list associated * with this Context. * @@ -1156,5 +1111,12 @@ public interface Context extends Container { * context. */ public void setEffectiveMinorVersion(int minor); + + + /** + * Obtain the JSP configuration for this context. + */ + public JspConfigDescriptor getJspConfigDescriptor(); + } diff --git a/java/org/apache/catalina/core/ApplicationContext.java b/java/org/apache/catalina/core/ApplicationContext.java index c55330be9..2952e5201 100644 --- a/java/org/apache/catalina/core/ApplicationContext.java +++ b/java/org/apache/catalina/core/ApplicationContext.java @@ -1375,8 +1375,7 @@ public class ApplicationContext @Override public JspConfigDescriptor getJspConfigDescriptor() { - // TODO SERVLET3 - return null; + return context.getJspConfigDescriptor(); } diff --git a/java/org/apache/catalina/core/ApplicationJspConfigDescriptor.java b/java/org/apache/catalina/core/ApplicationJspConfigDescriptor.java new file mode 100644 index 000000000..067ad3b34 --- /dev/null +++ b/java/org/apache/catalina/core/ApplicationJspConfigDescriptor.java @@ -0,0 +1,45 @@ +/* + * 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 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.catalina.core; + +import java.util.Collection; +import java.util.HashSet; + +import javax.servlet.descriptor.JspConfigDescriptor; +import javax.servlet.descriptor.JspPropertyGroupDescriptor; +import javax.servlet.descriptor.TaglibDescriptor; + +public class ApplicationJspConfigDescriptor implements JspConfigDescriptor { + + private Collection jspPropertyGroups = + new HashSet(); + + private Collection taglibs = + new HashSet(); + + @Override + public Collection getJspPropertyGroups() { + return jspPropertyGroups; + } + + @Override + public Collection getTaglibs() { + return taglibs; + } + +} diff --git a/java/org/apache/catalina/core/ApplicationJspPropertyGroupDescriptor.java b/java/org/apache/catalina/core/ApplicationJspPropertyGroupDescriptor.java new file mode 100644 index 000000000..498a0e90d --- /dev/null +++ b/java/org/apache/catalina/core/ApplicationJspPropertyGroupDescriptor.java @@ -0,0 +1,171 @@ +/* + * 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 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.catalina.core; + +import java.util.Collection; +import java.util.HashSet; + +import javax.servlet.descriptor.JspPropertyGroupDescriptor; +import org.apache.catalina.deploy.JspPropertyGroup; + + +public class ApplicationJspPropertyGroupDescriptor + implements JspPropertyGroupDescriptor{ + + JspPropertyGroup jspPropertyGroup; + + + public ApplicationJspPropertyGroupDescriptor( + JspPropertyGroup jspPropertyGroup) { + this.jspPropertyGroup = jspPropertyGroup; + } + + + @Override + public String getBuffer() { + String result = null; + + if (jspPropertyGroup.getBuffer() != null) { + result = jspPropertyGroup.getBuffer().toString(); + } + + return result; + } + + + @Override + public String getDefaultContentType() { + String result = null; + + if (jspPropertyGroup.getDefaultContentType() != null) { + result = jspPropertyGroup.getDefaultContentType().toString(); + } + + return result; + } + + + @Override + public String getDeferredSyntaxAllowedAsLiteral() { + String result = null; + + if (jspPropertyGroup.getDeferredSyntax() != null) { + result = jspPropertyGroup.getDeferredSyntax().toString(); + } + + return result; + } + + + @Override + public String getElIgnored() { + String result = null; + + if (jspPropertyGroup.getElIgnored() != null) { + result = jspPropertyGroup.getElIgnored().toString(); + } + + return result; + } + + + @Override + public String getErrorOnUndeclaredNamespace() { + String result = null; + + if (jspPropertyGroup.getErrorOnUndeclaredNamespace() != null) { + result = + jspPropertyGroup.getErrorOnUndeclaredNamespace().toString(); + } + + return result; + } + + + @Override + public Collection getIncludeCodas() { + return jspPropertyGroup.getIncludeCodas(); + } + + + @Override + public Collection getIncludePreludes() { + return jspPropertyGroup.getIncludePreludes(); + } + + + @Override + public String getIsXml() { + String result = null; + + if (jspPropertyGroup.getIsXml() != null) { + result = jspPropertyGroup.getIsXml().toString(); + } + + return result; + } + + + @Override + public String getPageEncoding() { + String result = null; + + if (jspPropertyGroup.getPageEncoding() != null) { + result = jspPropertyGroup.getPageEncoding().toString(); + } + + return result; + } + + + @Override + public String getScriptingInvalid() { + String result = null; + + if (jspPropertyGroup.getScriptingInvalid() != null) { + result = jspPropertyGroup.getScriptingInvalid().toString(); + } + + return result; + } + + + @Override + public String getTrimDirectiveWhitespaces() { + String result = null; + + if (jspPropertyGroup.getTrimWhitespace() != null) { + result = jspPropertyGroup.getTrimWhitespace().toString(); + } + + return result; + } + + + @Override + public Collection getUrlPatterns() { + Collection result = new HashSet(); + + if (jspPropertyGroup.getUrlPattern() != null) { + result.add(jspPropertyGroup.getUrlPattern()); + } + + return result; + } + +} diff --git a/java/org/apache/catalina/core/ApplicationTaglibDescriptor.java b/java/org/apache/catalina/core/ApplicationTaglibDescriptor.java new file mode 100644 index 000000000..a7a6a7925 --- /dev/null +++ b/java/org/apache/catalina/core/ApplicationTaglibDescriptor.java @@ -0,0 +1,81 @@ +/* + * 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 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.catalina.core; + +import javax.servlet.descriptor.TaglibDescriptor; + +public class ApplicationTaglibDescriptor implements TaglibDescriptor { + + private String location; + private String uri; + + public ApplicationTaglibDescriptor(String location, String uri) { + this.location = location; + this.uri = uri; + } + + @Override + public String getTaglibLocation() { + return location; + } + + @Override + public String getTaglibURI() { + return uri; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((location == null) ? 0 : location.hashCode()); + result = prime * result + ((uri == null) ? 0 : uri.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof ApplicationTaglibDescriptor)) { + return false; + } + ApplicationTaglibDescriptor other = (ApplicationTaglibDescriptor) obj; + if (location == null) { + if (other.location != null) { + return false; + } + } else if (!location.equals(other.location)) { + return false; + } + if (uri == null) { + if (other.uri != null) { + return false; + } + } else if (!uri.equals(other.uri)) { + return false; + } + return true; + } + +} diff --git a/java/org/apache/catalina/core/StandardContext.java b/java/org/apache/catalina/core/StandardContext.java index d14675cb8..333c7ee7e 100644 --- a/java/org/apache/catalina/core/StandardContext.java +++ b/java/org/apache/catalina/core/StandardContext.java @@ -54,6 +54,8 @@ import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.servlet.ServletRequestAttributeListener; import javax.servlet.ServletRequestListener; +import javax.servlet.descriptor.JspConfigDescriptor; +import javax.servlet.descriptor.TaglibDescriptor; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionListener; @@ -563,13 +565,6 @@ public class StandardContext /** - * The JSP tag libraries defined in web.xml for this web application, keyed - * by URI. - */ - private HashMap taglibs = new HashMap(); - - - /** * Amount of ms that the container will wait for servlets to unload. */ private long unloadDelay = 2000; @@ -767,6 +762,9 @@ public class StandardContext private int effectiveMinorVersion = 0; + private JspConfigDescriptor jspConfigDescriptor = + new ApplicationJspConfigDescriptor(); + // ----------------------------------------------------- Context Properties public int getEffectiveMajorVersion() { @@ -2054,6 +2052,11 @@ public class StandardContext } + + public JspConfigDescriptor getJspConfigDescriptor() { + return null; + } + // ------------------------------------------------------ Public Properties @@ -2530,30 +2533,6 @@ public class StandardContext } /** - * Add the given URL pattern as a jsp-property-group. This maps - * resources that match the given pattern so they will be passed - * to the JSP container. Though there are other elements in the - * property group, we only care about the URL pattern here. The - * JSP container will parse the rest. - * - * @param pattern URL pattern to be mapped - */ - public void addJspMapping(String pattern) { - String servletName = findServletMapping("*.jsp"); - if (servletName == null) { - servletName = "jsp"; - } - - if( findChild(servletName) != null) { - addServletMapping(pattern, servletName, true); - } else { - if(log.isDebugEnabled()) - log.debug("Skiping " + pattern + " , no servlet " + servletName); - } - } - - - /** * Add a Locale Encoding Mapping (see Sec 5.4 of Servlet spec 2.4) * * @param locale locale to map an encoding for @@ -2734,22 +2713,6 @@ public class StandardContext /** - * Add a JSP tag library for the specified URI. - * - * @param uri URI, relative to the web.xml file, of this tag library - * @param location Location of the tag library descriptor - */ - public void addTaglib(String uri, String location) { - - synchronized (taglibs) { - taglibs.put(uri, location); - } - fireContainerEvent("addTaglib", uri); - - } - - - /** * Add a new watched resource to the set recognized by this Context. * * @param name New watched resource file name @@ -3287,36 +3250,6 @@ public class StandardContext /** - * Return the tag library descriptor location for the specified taglib - * URI, if any; otherwise, return null. - * - * @param uri URI, relative to the web.xml file - */ - public String findTaglib(String uri) { - - synchronized (taglibs) { - return (taglibs.get(uri)); - } - - } - - - /** - * Return the URIs of all tag libraries for which a tag library - * descriptor location has been specified. If none are specified, - * a zero-length array is returned. - */ - public String[] findTaglibs() { - - synchronized (taglibs) { - String results[] = new String[taglibs.size()]; - return (taglibs.keySet().toArray(results)); - } - - } - - - /** * Return true if the specified welcome file is defined * for this Context; otherwise return false. * @@ -3831,20 +3764,6 @@ public class StandardContext /** - * Remove the tag library location forthe specified tag library URI. - * - * @param uri URI, relative to the web.xml file - */ - public void removeTaglib(String uri) { - - synchronized (taglibs) { - taglibs.remove(uri); - } - fireContainerEvent("removeTaglib", uri); - } - - - /** * Remove the specified watched resource name from the list associated * with this Context. * @@ -4968,7 +4887,7 @@ public class StandardContext applicationListeners = new String[0]; applicationEventListenersObjects = new Object[0]; applicationLifecycleListenersObjects = new Object[0]; - taglibs = new HashMap(); + jspConfigDescriptor = new ApplicationJspConfigDescriptor(); if(log.isDebugEnabled()) log.debug("resetContext " + oname); diff --git a/java/org/apache/catalina/deploy/WebXml.java b/java/org/apache/catalina/deploy/WebXml.java index facc43691..7eb896559 100644 --- a/java/org/apache/catalina/deploy/WebXml.java +++ b/java/org/apache/catalina/deploy/WebXml.java @@ -34,9 +34,13 @@ import java.util.Map.Entry; import javax.servlet.MultipartConfigElement; import javax.servlet.SessionCookieConfig; import javax.servlet.SessionTrackingMode; +import javax.servlet.descriptor.JspPropertyGroupDescriptor; +import javax.servlet.descriptor.TaglibDescriptor; import org.apache.catalina.Context; import org.apache.catalina.Wrapper; +import org.apache.catalina.core.ApplicationJspPropertyGroupDescriptor; +import org.apache.catalina.core.ApplicationTaglibDescriptor; import org.apache.tomcat.util.res.StringManager; /** @@ -1161,7 +1165,12 @@ public class WebXml { for (FilterMap filterMap : filterMaps) { context.addFilterMap(filterMap); } - // jsp-property-group needs to be after servlet configuration + for (JspPropertyGroup jspPropertyGroup : jspPropertyGroups) { + JspPropertyGroupDescriptor descriptor = + new ApplicationJspPropertyGroupDescriptor(jspPropertyGroup); + context.getJspConfigDescriptor().getJspPropertyGroups().add( + descriptor); + } for (String listener : listeners) { context.addApplicationListener(listener); } @@ -1279,7 +1288,9 @@ public class WebXml { } } for (Entry entry : taglibs.entrySet()) { - context.addTaglib(entry.getKey(), entry.getValue()); + TaglibDescriptor descriptor = new ApplicationTaglibDescriptor( + entry.getKey(), entry.getValue()); + context.getJspConfigDescriptor().getTaglibs().add(descriptor); } // Context doesn't use version directly @@ -1289,9 +1300,7 @@ public class WebXml { } // Do this last as it depends on servlets - for (JspPropertyGroup jspPropertyGroup : jspPropertyGroups) { - context.addJspMapping(jspPropertyGroup.getUrlPattern()); - } + // TODO } /** diff --git a/java/org/apache/catalina/startup/ContextConfig.java b/java/org/apache/catalina/startup/ContextConfig.java index 12041d821..3d03ea45b 100644 --- a/java/org/apache/catalina/startup/ContextConfig.java +++ b/java/org/apache/catalina/startup/ContextConfig.java @@ -1024,12 +1024,6 @@ public class ContextConfig // FIXME : Removing status pages - // Removing taglibs - String[] taglibs = context.findTaglibs(); - for (i = 0; i < taglibs.length; i++) { - context.removeTaglib(taglibs[i]); - } - // Removing welcome files String[] welcomeFiles = context.findWelcomeFiles(); for (i = 0; i < welcomeFiles.length; i++) { diff --git a/java/org/apache/catalina/startup/TldConfig.java b/java/org/apache/catalina/startup/TldConfig.java index ccae130b5..e8f5169a5 100644 --- a/java/org/apache/catalina/startup/TldConfig.java +++ b/java/org/apache/catalina/startup/TldConfig.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.JarURLConnection; import java.util.ArrayList; +import java.util.Collection; import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; @@ -34,6 +35,7 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; import javax.servlet.ServletContext; +import javax.servlet.descriptor.TaglibDescriptor; import org.apache.catalina.Context; import org.apache.catalina.Lifecycle; @@ -151,7 +153,9 @@ public final class TldConfig implements LifecycleListener { * instance of any URI will be processed. */ private Set taglibUris = new HashSet(); - + + private Set webxmlTaglibUris = new HashSet(); + private ArrayList listeners = new ArrayList(); // --------------------------------------------------------- Public Methods @@ -171,6 +175,13 @@ public final class TldConfig implements LifecycleListener { } /** + * Determines if the provided URI is a known taglib URI. + */ + public boolean isKnownWebxmlTaglibUri(String uri) { + return webxmlTaglibUris.contains(uri); + } + + /** * Sets the list of JARs that are known not to contain any TLDs. * * @param jarNames List of comma-separated names of JAR files that are @@ -348,10 +359,12 @@ public final class TldConfig implements LifecycleListener { if (log.isTraceEnabled()) { log.trace(sm.getString("tldConfig.webxmlStart")); } - - String taglibs[] = context.findTaglibs(); - for (int i = 0; i < taglibs.length; i++) { - String resourcePath = context.findTaglib(taglibs[i]); + + Collection descriptors = + context.getJspConfigDescriptor().getTaglibs(); + + for (TaglibDescriptor descriptor : descriptors) { + String resourcePath = descriptor.getTaglibLocation(); // Note: Whilst the Servlet 2.4 DTD implies that the location must // be a context-relative path starting with '/', JSP.7.3.6.1 states // explicitly how paths that do not start with '/' should be @@ -359,22 +372,23 @@ public final class TldConfig implements LifecycleListener { if (!resourcePath.startsWith("/")) { resourcePath = WEB_INF + resourcePath; } - if (taglibUris.contains(taglibs[i])) { + if (taglibUris.contains(descriptor.getTaglibURI())) { log.warn(sm.getString("tldConfig.webxmlSkip", resourcePath, - taglibs[i])); + descriptor.getTaglibURI())); } else { if (log.isTraceEnabled()) { log.trace(sm.getString("tldConfig.webxmlAdd", resourcePath, - taglibs[i])); + descriptor.getTaglibURI())); } try { InputStream stream = context.getServletContext( ).getResourceAsStream(resourcePath); tldScanStream(stream); - taglibUris.add(taglibs[i]); + taglibUris.add(descriptor.getTaglibURI()); + webxmlTaglibUris.add(descriptor.getTaglibURI()); } catch (IOException ioe) { log.warn(sm.getString("tldConfig.webxmlFail", resourcePath, - taglibs[i]), ioe); + descriptor.getTaglibURI()), ioe); } } } @@ -564,6 +578,7 @@ public final class TldConfig implements LifecycleListener { } } else if (event.getType().equals(Lifecycle.STOP_EVENT)) { taglibUris.clear(); + webxmlTaglibUris.clear(); listeners.clear(); } } diff --git a/java/org/apache/catalina/startup/TldRuleSet.java b/java/org/apache/catalina/startup/TldRuleSet.java index 45003be49..c71a1d760 100644 --- a/java/org/apache/catalina/startup/TldRuleSet.java +++ b/java/org/apache/catalina/startup/TldRuleSet.java @@ -140,14 +140,14 @@ final class TaglibUriRule extends Rule { duplicateUri = true; // This is expected if the URI was defined in web.xml // Log message at debug in this case - if (tldConfig.getContext().findTaglib(text) == null) { - digester.getLogger().info( - "TLD skipped. URI: " + text + " is already defined"); - } else { + if (tldConfig.isKnownWebxmlTaglibUri(text)) { if (digester.getLogger().isDebugEnabled()) { digester.getLogger().debug( "TLD skipped. URI: " + text + " is already defined"); } + } else { + digester.getLogger().info( + "TLD skipped. URI: " + text + " is already defined"); } } else { // New URI. Add it to known list and carry on -- 2.11.0