Author: markt
Date: Thu Nov 27 21:16:40 2014
New Revision: 1642233

URL: http://svn.apache.org/r1642233
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=57142
Page imports need to be visible to EL

Added:
    tomcat/trunk/test/webapp/bug5nnnn/bug57142.jsp   (with props)
Modified:
    tomcat/trunk/java/org/apache/jasper/compiler/Generator.java
    tomcat/trunk/java/org/apache/jasper/compiler/PageInfo.java
    tomcat/trunk/java/org/apache/jasper/compiler/Validator.java
    tomcat/trunk/test/org/apache/el/TestELInJsp.java

Modified: tomcat/trunk/java/org/apache/jasper/compiler/Generator.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/Generator.java?rev=1642233&r1=1642232&r2=1642233&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/jasper/compiler/Generator.java (original)
+++ tomcat/trunk/java/org/apache/jasper/compiler/Generator.java Thu Nov 27 
21:16:40 2014
@@ -712,6 +712,34 @@ class Generator {
         out.printil("out = pageContext.getOut();");
         out.printil("_jspx_out = out;");
         out.println();
+
+        if (pageInfo.isELUsed()) {
+            // If EL is going to be used on this page then make sure that the
+            // EL Context is properly configured with the imports.
+            // The clarification provided in 
https://java.net/jira/browse/JSP-44
+            // is the the page import directive applies both to the scripting
+            // environment and to the EL environment.
+            out.printin("javax.el.ImportHandler _jspx_handler = 
pageContext.getELContext().getImportHandler();");
+            out.println();
+            for (String importName : pageInfo.getImports()) {
+                if (importName == null) {
+                    continue;
+                }
+                String trimmed = importName.trim();
+                if (trimmed.length() == 0) {
+                    continue;
+                }
+                if (trimmed.endsWith(".*")) {
+                    out.printin("_jspx_handler.importPackage(\"");
+                    out.print(trimmed.substring(0, trimmed.length() - 2));
+                    out.println("\");");
+                } else {
+                    out.printin("_jspx_handler.importClass(\"");
+                    out.print(trimmed);
+                    out.println("\");");
+                }
+            }
+        }
     }
 
     /**

Modified: tomcat/trunk/java/org/apache/jasper/compiler/PageInfo.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/PageInfo.java?rev=1642233&r1=1642232&r2=1642233&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/jasper/compiler/PageInfo.java (original)
+++ tomcat/trunk/java/org/apache/jasper/compiler/PageInfo.java Thu Nov 27 
21:16:40 2014
@@ -72,6 +72,7 @@ class PageInfo {
 
     private String isELIgnoredValue;
     private boolean isELIgnored = false;
+    private boolean isELUsed = false;
 
     // JSP 2.1
     private String deferredSyntaxAllowedAsLiteralValue;
@@ -677,6 +678,27 @@ class PageInfo {
         return isELIgnored;
     }
 
+    /**
+     * Marks the current page as using EL. This allows an optimisation when
+     * generating the page. The imports need to be added to the EL Context but
+     * this is wasteful if the EL Context is never going to be used. The
+     * associated field allows the Generator to determine whether or not to
+     * configure the imports.
+     */
+    public void setELUsed() {
+        isELUsed = true;
+    }
+
+    /**
+     * Is expression language used on this page.
+     *
+     * @return <code>true</code> if expression language is used, otherwise
+     *         <code>false</code>
+     */
+    public boolean isELUsed() {
+        return isELUsed;
+    }
+
     public void putNonCustomTagPrefix(String prefix, Mark where) {
         nonCustomTagPrefixMap.put(prefix, where);
     }

Modified: tomcat/trunk/java/org/apache/jasper/compiler/Validator.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/Validator.java?rev=1642233&r1=1642232&r2=1642233&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/jasper/compiler/Validator.java (original)
+++ tomcat/trunk/java/org/apache/jasper/compiler/Validator.java Thu Nov 27 
21:16:40 2014
@@ -730,6 +730,10 @@ class Validator {
             if (pageInfo.isELIgnored())
                 return;
 
+            // EL is known to be used on this page. Mark the PageInfo
+            // accordingly.
+            pageInfo.setELUsed();
+
             // JSP.2.2 - '#{' not allowed in template text
             if (n.getType() == '#') {
                 if (!pageInfo.isDeferredSyntaxAllowedAsLiteral()) {
@@ -1120,6 +1124,12 @@ class Validator {
                     }
                 }
 
+                if (elExpression) {
+                    // EL is known to be used on this page. Mark the PageInfo
+                    // accordingly.
+                    pageInfo.setELUsed();
+                }
+
                 boolean expression = runtimeExpression || elExpression;
 
                 // When attribute is not an expression,
@@ -1376,6 +1386,9 @@ class Validator {
 
                         if (el.containsEL()) {
                             validateFunctions(el, n);
+                            // EL is known to be used on this page. Mark the
+                            // PageInfo accordingly.
+                            pageInfo.setELUsed();
                         } else {
                             // Get text with \$ and \# escaping removed.
                             // Should be a single Text node

Modified: tomcat/trunk/test/org/apache/el/TestELInJsp.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/el/TestELInJsp.java?rev=1642233&r1=1642232&r2=1642233&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/el/TestELInJsp.java (original)
+++ tomcat/trunk/test/org/apache/el/TestELInJsp.java Thu Nov 27 21:16:40 2014
@@ -16,7 +16,10 @@
  */
 package org.apache.el;
 
-import static org.junit.Assert.assertTrue;
+import java.math.BigDecimal;
+import java.util.Collections;
+
+import javax.servlet.DispatcherType;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -402,8 +405,33 @@ public class TestELInJsp extends TomcatB
     }
 
 
+    /*
+     * BZ https://issues.apache.org/bugzilla/show_bug.cgi?id=57142
+     * javax.servlet, javax.servlet.http and javax.servlet.jsp should be
+     * imported by default.
+     */
+    @Test
+    public void testBug57142() throws Exception {
+        getTomcatInstanceTestWebapp(false, true);
+
+        ByteChunk res = getUrl("http://localhost:"; + getPort() +
+                "/test/bug5nnnn/bug57142.jsp");
+
+        String result = res.toString();
+        // javax.servlet
+        assertEcho(result, "00-" + DispatcherType.ASYNC);
+        // No obvious status fields for javax.servlet.http
+        // Could hack something with HttpUtils...
+        // No obvious status fields for javax.servlet.jsp
+        // Wild card (package) import
+        assertEcho(result, "01-" + BigDecimal.ROUND_UP);
+        // Class import
+        assertEcho(result, "02-" + Collections.EMPTY_LIST.size());
+    }
+
+
     // Assertion for text contained with <p></p>, e.g. printed by tags:echo
     private static void assertEcho(String result, String expected) {
-        assertTrue(result, result.indexOf("<p>" + expected + "</p>") > 0);
+        Assert.assertTrue(result, result.indexOf("<p>" + expected + "</p>") > 
0);
     }
 }

Added: tomcat/trunk/test/webapp/bug5nnnn/bug57142.jsp
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/webapp/bug5nnnn/bug57142.jsp?rev=1642233&view=auto
==============================================================================
--- tomcat/trunk/test/webapp/bug5nnnn/bug57142.jsp (added)
+++ tomcat/trunk/test/webapp/bug5nnnn/bug57142.jsp Thu Nov 27 21:16:40 2014
@@ -0,0 +1,24 @@
+<%--
+ 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.
+--%>
+<%@page import="java.math.*, java.util.Collections" %>
+<html>
+  <body>
+    <p>00-${DispatcherType.ASYNC}</p>
+    <p>01-${BigDecimal.ROUND_UP}</p>
+    <p>02-${Collections.EMPTY_LIST.stream().count()}</p>
+  </body>
+</html>
\ No newline at end of file

Propchange: tomcat/trunk/test/webapp/bug5nnnn/bug57142.jsp
------------------------------------------------------------------------------
    svn:eol-style = native



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

Reply via email to