- Add a new init param for controlling display of source fragments.
authorremm <remm@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 28 Sep 2006 15:28:05 +0000 (15:28 +0000)
committerremm <remm@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 28 Sep 2006 15:28:05 +0000 (15:28 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@450897 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/jasper/EmbeddedServletOptions.java
java/org/apache/jasper/JspC.java
java/org/apache/jasper/Options.java
java/org/apache/jasper/resources/LocalStrings.properties
java/org/apache/jasper/servlet/JspServletWrapper.java

index 6c3a320..9904d48 100644 (file)
@@ -179,6 +179,13 @@ public final class EmbeddedServletOptions implements Options {
      */
     private boolean xpoweredBy;
     
+    /**
+     * Should we include a source fragment in exception messages, which could be displayed
+     * to the developer ?
+     */
+    private boolean displaySourceFragment = true;
+
+    
     public String getProperty(String name ) {
         return settings.getProperty( name );
     }
@@ -368,6 +375,14 @@ public final class EmbeddedServletOptions implements Options {
     }
 
     /**
+     * Should we include a source fragment in exception messages, which could be displayed
+     * to the developer ?
+     */
+    public boolean getDisplaySourceFragment() {
+        return displaySourceFragment;
+    }
+
+    /**
      * Create an EmbeddedServletOptions object using data available from
      * ServletConfig and ServletContext. 
      */
@@ -653,6 +668,19 @@ public final class EmbeddedServletOptions implements Options {
             }
         }
         
+        String displaySourceFragment = config.getInitParameter("displaySourceFragment"); 
+        if (displaySourceFragment != null) {
+            if (displaySourceFragment.equalsIgnoreCase("true")) {
+                this.displaySourceFragment = true;
+            } else if (displaySourceFragment.equalsIgnoreCase("false")) {
+                this.displaySourceFragment = false;
+            } else {
+                if (log.isWarnEnabled()) {
+                    log.warn(Localizer.getMessage("jsp.warning.displaySourceFragment"));
+                }
+            }
+        }
+        
         // Setup the global Tag Libraries location cache for this
         // web-application.
         tldLocationsCache = new TldLocationsCache(context);
index 158a5d6..5f667bf 100644 (file)
@@ -385,6 +385,10 @@ public class JspC implements Options {
         this.xpoweredBy = xpoweredBy;
     }
 
+    public boolean getDisplaySourceFragment() {
+        return true;
+    }
+    
     public boolean getErrorOnUseBeanInvalidClassAttribute() {
         return errorOnUseBeanInvalidClassAttribute;
     }
index 62130ff..3043768 100644 (file)
@@ -75,6 +75,12 @@ public interface Options {
     public boolean getDevelopment();
 
     /**
+     * Should we include a source fragment in exception messages, which could be displayed
+     * to the developer ?
+     */
+    public boolean getDisplaySourceFragment();
+
+    /**
      * Is the generation of SMAP info for JSR45 debugging suppressed?
      */
     public boolean isSmapSuppressed();
index 3c5ef38..c68e02f 100644 (file)
@@ -155,6 +155,7 @@ jsp.warning.reloading=Warning: Invalid value for the initParam reloading. Will u
 jsp.warning.dumpSmap=Warning: Invalid value for the initParam dumpSmap. Will use the default value of \"false\"
 jsp.warning.genchararray=Warning: Invalid value for the initParam genStrAsCharArray. Will use the default value of \"false\"
 jsp.warning.suppressSmap=Warning: Invalid value for the initParam suppressSmap. Will use the default value of \"false\"
+jsp.warning.displaySourceFragment=Warning: Invalid value for the initParam displaySourceFragment. Will use the default value of \"true\"
 jsp.error.badtaglib=Unable to open taglibrary {0} : {1}
 jsp.error.badGetReader=Cannot create a reader when the stream is not buffered
 jsp.warning.unknown.element.in.taglib=Unknown element ({0}) in taglib
@@ -413,6 +414,8 @@ jsp.error.unbalanced.endtag=The end tag \"&lt;/{0}\" is unbalanced
 jsp.error.invalid.bean=The value for the useBean class attribute {0} is invalid.
 jsp.error.prefix.use_before_dcl=The prefix {0} specified in this tag directive has been previously used by an action in file {1} line {2}.
 
+jsp.exception=An exception occurred processing JSP page {0} at line {1}
+
 # JSP 2.1
 jsp.error.el.template.deferred=#{..} is not allowed in template text
 jsp.error.el.parse={0} : {1}
index 2d67bb1..8e9ac22 100644 (file)
@@ -515,43 +515,49 @@ public class JspServletWrapper {
                     throw new JasperException(ex);
                 }
 
-                // Read both files in, so we can inspect them
-                String[] jspLines = readFile
+                if (options.getDisplaySourceFragment()) {
+
+                    // Read both files in, so we can inspect them
+                    String[] jspLines = readFile
                     (this.ctxt.getResourceAsStream(this.ctxt.getJspFile()));
 
-                String[] javaLines = readFile
+                    String[] javaLines = readFile
                     (new FileInputStream(this.ctxt.getServletJavaFileName()));
 
-                // If the line contains the opening of a multi-line scriptlet
-                // block, then the JSP line number we got back is probably
-                // faulty.  Scan forward to match the java line...
-                if (jspLines[jspLineNumber-1].lastIndexOf("<%") >
+                    // If the line contains the opening of a multi-line scriptlet
+                    // block, then the JSP line number we got back is probably
+                    // faulty.  Scan forward to match the java line...
+                    if (jspLines[jspLineNumber-1].lastIndexOf("<%") >
                     jspLines[jspLineNumber-1].lastIndexOf("%>")) {
-                    String javaLine = javaLines[javaLineNumber-1].trim();
+                        String javaLine = javaLines[javaLineNumber-1].trim();
 
-                    for (int i=jspLineNumber-1; i<jspLines.length; i++) {
-                        if (jspLines[i].indexOf(javaLine) != -1) {
-                            jspLineNumber = i+1;
-                            break;
+                        for (int i=jspLineNumber-1; i<jspLines.length; i++) {
+                            if (jspLines[i].indexOf(javaLine) != -1) {
+                                jspLineNumber = i+1;
+                                break;
+                            }
                         }
                     }
-                }
 
-                // copy out a fragment of JSP to display to the user
-                StringBuffer buffer = new StringBuffer(1024);
-                int startIndex = Math.max(0, jspLineNumber-1-3);
-                int endIndex = Math.min(jspLines.length-1, jspLineNumber-1+3);
+                    // copy out a fragment of JSP to display to the user
+                    StringBuffer buffer = new StringBuffer(1024);
+                    int startIndex = Math.max(0, jspLineNumber-1-3);
+                    int endIndex = Math.min(jspLines.length-1, jspLineNumber-1+3);
 
-                for (int i=startIndex;i<=endIndex; ++i) {
-                    buffer.append(i+1);
-                    buffer.append(": ");
-                    buffer.append(jspLines[i]);
-                    buffer.append("\n");
-                }
+                    for (int i=startIndex;i<=endIndex; ++i) {
+                        buffer.append(i+1);
+                        buffer.append(": ");
+                        buffer.append(jspLines[i]);
+                        buffer.append("\n");
+                    }
 
-                return new JasperException(
-                                           "Exception in JSP: " + detail.getJspFileName() + ":" +
-                                           jspLineNumber + "\n\n" + buffer + "\n\nStacktrace:", ex);
+                    return new JasperException(Localizer.getMessage
+                            ("jsp.exception", detail.getJspFileName(), "" + jspLineNumber) + "\n" + buffer, ex);
+                    
+                } else {
+                    return new JasperException(Localizer.getMessage
+                            ("jsp.exception", detail.getJspFileName(), "" + jspLineNumber), ex);
+                }
             }
         } catch (Exception je) {
             // If anything goes wrong, just revert to the original behaviour