You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2012/08/26 22:25:31 UTC

svn commit: r1377509 - in /tomcat/trunk/java/org/apache/jasper/compiler: JspConfig.java TldLocationsCache.java WebXml.java

Author: markt
Date: Sun Aug 26 20:25:30 2012
New Revision: 1377509

URL: http://svn.apache.org/viewvc?rev=1377509&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=53737
Access JSP configuration information via the ServletContext rather than re-parsing web.xml

Modified:
    tomcat/trunk/java/org/apache/jasper/compiler/JspConfig.java
    tomcat/trunk/java/org/apache/jasper/compiler/TldLocationsCache.java
    tomcat/trunk/java/org/apache/jasper/compiler/WebXml.java

Modified: tomcat/trunk/java/org/apache/jasper/compiler/JspConfig.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/JspConfig.java?rev=1377509&r1=1377508&r2=1377509&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/jasper/compiler/JspConfig.java (original)
+++ tomcat/trunk/java/org/apache/jasper/compiler/JspConfig.java Sun Aug 26 20:25:30 2012
@@ -17,14 +17,14 @@
 
 package org.apache.jasper.compiler;
 
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.Vector;
 
 import javax.servlet.ServletContext;
+import javax.servlet.descriptor.JspConfigDescriptor;
+import javax.servlet.descriptor.JspPropertyGroupDescriptor;
 
-import org.apache.jasper.JasperException;
-import org.apache.jasper.xmlparser.ParserUtils;
-import org.apache.jasper.xmlparser.TreeNode;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 
@@ -59,171 +59,105 @@ public class JspConfig {
         this.ctxt = ctxt;
     }
 
-    private double getVersion(TreeNode webApp) {
-        String v = webApp.findAttribute("version");
-        if (v != null) {
-            try {
-                return Double.parseDouble(v);
-            } catch (NumberFormatException e) {
-            }
-        }
-        return 2.3;
-    }
-
-    private void processWebDotXml() throws JasperException {
-
-        WebXml webXml = null;
-
-        try {
-            webXml = new WebXml(ctxt);
-
-            TreeNode webApp = null;
-            if (webXml.getInputSource() != null) {
-                ParserUtils pu = new ParserUtils();
-                webApp = pu.parseXMLDocument(webXml.getSystemId(),
-                        webXml.getInputSource());
-            }
+    private void processWebDotXml() {
 
-            if (webApp == null
-                    || getVersion(webApp) < 2.4) {
-                defaultIsELIgnored = "true";
-                defaultDeferedSyntaxAllowedAsLiteral = "true";
-                return;
-            }
-            if (getVersion(webApp) < 2.5) {
+        // Very, very unlikely but just in case...
+        if (ctxt.getMajorVersion() < 2) {
+            defaultIsELIgnored = "true";
+            defaultDeferedSyntaxAllowedAsLiteral = "true";
+            return;
+        }
+        if (ctxt.getMajorVersion() == 2) {
+            if (ctxt.getMinorVersion() < 5) {
                 defaultDeferedSyntaxAllowedAsLiteral = "true";
             }
-            TreeNode jspConfig = webApp.findChild("jsp-config");
-            if (jspConfig == null) {
+            if (ctxt.getMinorVersion() < 4) {
+                defaultIsELIgnored = "true";
                 return;
             }
+        }
 
-            jspProperties = new Vector<>();
-            Iterator<TreeNode> jspPropertyList =
-                jspConfig.findChildren("jsp-property-group");
-            while (jspPropertyList.hasNext()) {
-
-                TreeNode element = jspPropertyList.next();
-                Iterator<TreeNode> list = element.findChildren();
-
-                Vector<String> urlPatterns = new Vector<>();
-                String pageEncoding = null;
-                String scriptingInvalid = null;
-                String elIgnored = null;
-                String isXml = null;
-                Vector<String> includePrelude = new Vector<>();
-                Vector<String> includeCoda = new Vector<>();
-                String deferredSyntaxAllowedAsLiteral = null;
-                String trimDirectiveWhitespaces = null;
-                String defaultContentType = null;
-                String buffer = null;
-                String errorOnUndeclaredNamespace = null;
-
-                while (list.hasNext()) {
-
-                    element = list.next();
-                    String tname = element.getName();
-
-                    if ("url-pattern".equals(tname))
-                        urlPatterns.addElement( element.getBody() );
-                    else if ("page-encoding".equals(tname))
-                        pageEncoding = element.getBody();
-                    else if ("is-xml".equals(tname))
-                        isXml = element.getBody();
-                    else if ("el-ignored".equals(tname))
-                        elIgnored = element.getBody();
-                    else if ("scripting-invalid".equals(tname))
-                        scriptingInvalid = element.getBody();
-                    else if ("include-prelude".equals(tname))
-                        includePrelude.addElement(element.getBody());
-                    else if ("include-coda".equals(tname))
-                        includeCoda.addElement(element.getBody());
-                    else if ("deferred-syntax-allowed-as-literal".equals(tname))
-                        deferredSyntaxAllowedAsLiteral = element.getBody();
-                    else if ("trim-directive-whitespaces".equals(tname))
-                        trimDirectiveWhitespaces = element.getBody();
-                    else if ("default-content-type".equals(tname))
-                        defaultContentType = element.getBody();
-                    else if ("buffer".equals(tname))
-                        buffer = element.getBody();
-                    else if ("error-on-undeclared-namespace".equals(tname))
-                        errorOnUndeclaredNamespace = element.getBody();
-                }
+        JspConfigDescriptor jspConfig = ctxt.getJspConfigDescriptor();
 
-                if (urlPatterns.size() == 0) {
-                    continue;
-                }
+        Collection<JspPropertyGroupDescriptor> jspPropertyGroups =
+                jspConfig.getJspPropertyGroups();
+
+        for (JspPropertyGroupDescriptor jspPropertyGroup : jspPropertyGroups) {
 
-                // Add one JspPropertyGroup for each URL Pattern.  This makes
-                // the matching logic easier.
-                for( int p = 0; p < urlPatterns.size(); p++ ) {
-                    String urlPattern = urlPatterns.elementAt( p );
-                    String path = null;
-                    String extension = null;
-
-                    if (urlPattern.indexOf('*') < 0) {
-                        // Exact match
-                        path = urlPattern;
+            Collection<String> urlPatterns = jspPropertyGroup.getUrlPatterns();
+
+            if (urlPatterns.size() == 0) {
+                continue;
+            }
+
+            // Add one JspPropertyGroup for each URL Pattern.  This makes
+            // the matching logic easier.
+            for (String urlPattern : urlPatterns) {
+                String path = null;
+                String extension = null;
+
+                if (urlPattern.indexOf('*') < 0) {
+                    // Exact match
+                    path = urlPattern;
+                } else {
+                    int i = urlPattern.lastIndexOf('/');
+                    String file;
+                    if (i >= 0) {
+                        path = urlPattern.substring(0,i+1);
+                        file = urlPattern.substring(i+1);
                     } else {
-                        int i = urlPattern.lastIndexOf('/');
-                        String file;
-                        if (i >= 0) {
-                            path = urlPattern.substring(0,i+1);
-                            file = urlPattern.substring(i+1);
-                        } else {
-                            file = urlPattern;
-                        }
+                        file = urlPattern;
+                    }
 
-                        // pattern must be "*", or of the form "*.jsp"
-                        if (file.equals("*")) {
-                            extension = "*";
-                        } else if (file.startsWith("*.")) {
-                            extension = file.substring(file.indexOf('.')+1);
-                        }
+                    // pattern must be "*", or of the form "*.jsp"
+                    if (file.equals("*")) {
+                        extension = "*";
+                    } else if (file.startsWith("*.")) {
+                        extension = file.substring(file.indexOf('.')+1);
+                    }
 
-                        // The url patterns are reconstructed as the following:
-                        // path != null, extension == null:  / or /foo/bar.ext
-                        // path == null, extension != null:  *.ext
-                        // path != null, extension == "*":   /foo/*
-                        boolean isStar = "*".equals(extension);
-                        if ((path == null && (extension == null || isStar))
-                                || (path != null && !isStar)) {
-                            if (log.isWarnEnabled()) {
-                                log.warn(Localizer.getMessage(
-                                        "jsp.warning.bad.urlpattern.propertygroup",
-                                        urlPattern));
-                            }
-                            continue;
+                    // The url patterns are reconstructed as the following:
+                    // path != null, extension == null:  / or /foo/bar.ext
+                    // path == null, extension != null:  *.ext
+                    // path != null, extension == "*":   /foo/*
+                    boolean isStar = "*".equals(extension);
+                    if ((path == null && (extension == null || isStar))
+                            || (path != null && !isStar)) {
+                        if (log.isWarnEnabled()) {
+                            log.warn(Localizer.getMessage(
+                                    "jsp.warning.bad.urlpattern.propertygroup",
+                                    urlPattern));
                         }
+                        continue;
                     }
+                }
 
-                    JspProperty property = new JspProperty(isXml,
-                            elIgnored,
-                            scriptingInvalid,
-                            pageEncoding,
-                            includePrelude,
-                            includeCoda,
-                            deferredSyntaxAllowedAsLiteral,
-                            trimDirectiveWhitespaces,
-                            defaultContentType,
-                            buffer,
-                            errorOnUndeclaredNamespace);
-                    JspPropertyGroup propertyGroup =
-                        new JspPropertyGroup(path, extension, property);
+                Vector<String> includePreludes = new Vector<>();
+                includePreludes.addAll(jspPropertyGroup.getIncludePreludes());
 
-                    jspProperties.addElement(propertyGroup);
-                }
-            }
-        } catch (Exception ex) {
-            throw new JasperException(ex);
-        } finally {
-            if (webXml != null) {
-                webXml.close();
+                Vector<String> includeCodas = new Vector<>();
+                includeCodas.addAll(jspPropertyGroup.getIncludeCodas());
+
+                JspProperty property = new JspProperty(jspPropertyGroup.getIsXml(),
+                        jspPropertyGroup.getElIgnored(),
+                        jspPropertyGroup.getScriptingInvalid(),
+                        jspPropertyGroup.getPageEncoding(),
+                        includePreludes,
+                        includeCodas,
+                        jspPropertyGroup.getDeferredSyntaxAllowedAsLiteral(),
+                        jspPropertyGroup.getTrimDirectiveWhitespaces(),
+                        jspPropertyGroup.getDefaultContentType(),
+                        jspPropertyGroup.getBuffer(),
+                        jspPropertyGroup.getErrorOnUndeclaredNamespace());
+                JspPropertyGroup propertyGroup =
+                    new JspPropertyGroup(path, extension, property);
+
+                jspProperties.addElement(propertyGroup);
             }
         }
     }
 
-    private void init() throws JasperException {
+    private void init() {
 
         if (!initialized) {
             synchronized (this) {
@@ -286,7 +220,7 @@ public class JspConfig {
      * @param uri the resource supplied.
      * @return a JspProperty indicating the best match, or some default.
      */
-    public JspProperty findJspProperty(String uri) throws JasperException {
+    public JspProperty findJspProperty(String uri) {
 
         init();
 
@@ -449,7 +383,7 @@ public class JspConfig {
      * To find out if an uri matches an url pattern in jsp config.  If so,
      * then the uri is a JSP page.  This is used primarily for jspc.
      */
-    public boolean isJspPage(String uri) throws JasperException {
+    public boolean isJspPage(String uri) {
 
         init();
         if (jspProperties == null) {

Modified: tomcat/trunk/java/org/apache/jasper/compiler/TldLocationsCache.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/TldLocationsCache.java?rev=1377509&r1=1377508&r2=1377509&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/jasper/compiler/TldLocationsCache.java (original)
+++ tomcat/trunk/java/org/apache/jasper/compiler/TldLocationsCache.java Sun Aug 26 20:25:30 2012
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.JarURLConnection;
 import java.net.URL;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -29,6 +30,8 @@ import java.util.Set;
 import java.util.StringTokenizer;
 
 import javax.servlet.ServletContext;
+import javax.servlet.descriptor.JspConfigDescriptor;
+import javax.servlet.descriptor.TaglibDescriptor;
 
 import org.apache.jasper.Constants;
 import org.apache.jasper.JasperException;
@@ -150,10 +153,10 @@ public class TldLocationsCache {
      *    [0] The location
      *    [1] If the location is a jar file, this is the location of the tld.
      */
-    private Hashtable<String, TldLocation> mappings;
+    private final Hashtable<String, TldLocation> mappings;
 
     private volatile boolean initialized;
-    private ServletContext ctxt;
+    private final ServletContext ctxt;
 
     /** Constructor.
      *
@@ -162,7 +165,7 @@ public class TldLocationsCache {
      */
     public TldLocationsCache(ServletContext ctxt) {
         this.ctxt = ctxt;
-        mappings = new Hashtable<String, TldLocation>();
+        mappings = new Hashtable<>();
         initialized = false;
     }
 
@@ -272,60 +275,34 @@ public class TldLocationsCache {
     /*
      * Populates taglib map described in web.xml.
      *
-     * This is not kept in sync with o.a.c.startup.TldConfig as the Jasper only
-     * needs the URI to TLD mappings from scan web.xml whereas TldConfig needs
-     * to scan the actual TLD files.
+     * This is not kept in sync with o.a.c.startup.TldConfig as a) Jasper only
+     * needs the URI to TLD mappings and b) Jasper can obtain the information
+     * from the ServletContext.
      */
     private void tldScanWebXml() throws Exception {
 
-        WebXml webXml = null;
-        try {
-            webXml = new WebXml(ctxt);
-            if (webXml.getInputSource() == null) {
-                return;
-            }
-
-            // Parse the web application deployment descriptor
-            TreeNode webtld = null;
-            webtld = new ParserUtils().parseXMLDocument(webXml.getSystemId(),
-                    webXml.getInputSource());
-
-            // Allow taglib to be an element of the root or jsp-config (JSP2.0)
-            TreeNode jspConfig = webtld.findChild("jsp-config");
-            if (jspConfig != null) {
-                webtld = jspConfig;
-            }
-            Iterator<TreeNode> taglibs = webtld.findChildren("taglib");
-            while (taglibs.hasNext()) {
-
-                // Parse the next <taglib> element
-                TreeNode taglib = taglibs.next();
-                String tagUri = null;
-                String tagLoc = null;
-                TreeNode child = taglib.findChild("taglib-uri");
-                if (child != null)
-                    tagUri = child.getBody();
-                child = taglib.findChild("taglib-location");
-                if (child != null)
-                    tagLoc = child.getBody();
+        JspConfigDescriptor jspConfig = ctxt.getJspConfigDescriptor();
 
-                // Save this location if appropriate
-                if (tagLoc == null)
-                    continue;
-                if (uriType(tagLoc) == NOROOT_REL_URI)
-                    tagLoc = "/WEB-INF/" + tagLoc;
-                TldLocation location;
-                if (tagLoc.endsWith(JAR_EXT)) {
-                    location = new TldLocation("META-INF/taglib.tld", ctxt.getResource(tagLoc).toString());
-                } else {
-                    location = new TldLocation(tagLoc);
-                }
-                mappings.put(tagUri, location);
-            }
-        } finally {
-            if (webXml != null) {
-                webXml.close();
+        Collection<TaglibDescriptor> taglibs = jspConfig.getTaglibs();
+
+        for (TaglibDescriptor taglib : taglibs) {
+
+            String tagUri = taglib.getTaglibURI();
+            String tagLoc = taglib.getTaglibLocation();
+
+            // Save this location if appropriate
+            if (tagLoc == null)
+                continue;
+            if (uriType(tagLoc) == NOROOT_REL_URI)
+                tagLoc = "/WEB-INF/" + tagLoc;
+            TldLocation location;
+            if (tagLoc.endsWith(JAR_EXT)) {
+                location = new TldLocation("META-INF/taglib.tld",
+                        ctxt.getResource(tagLoc).toString());
+            } else {
+                location = new TldLocation(tagLoc);
             }
+            mappings.put(tagUri, location);
         }
     }
 

Modified: tomcat/trunk/java/org/apache/jasper/compiler/WebXml.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/WebXml.java?rev=1377509&r1=1377508&r2=1377509&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/jasper/compiler/WebXml.java (original)
+++ tomcat/trunk/java/org/apache/jasper/compiler/WebXml.java Sun Aug 26 20:25:30 2012
@@ -39,7 +39,10 @@ import org.xml.sax.InputSource;
  * annotations with the main web.xml
  *
  * Clients *must* ensure that they call {@link #close()} to clean up resources.
+ *
+ * @deprecated  Unused - will be removed in Tomcat 8.0.x
  */
+@Deprecated
 public class WebXml {
     private static final String FILE_PROTOCOL = "file:";
     private static final String WEB_XML = "/WEB-INF/web.xml";



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org