http://git-wip-us.apache.org/repos/asf/struts/blob/775c82a7/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/ErrorDispatcher.java ---------------------------------------------------------------------- diff --git a/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/ErrorDispatcher.java b/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/ErrorDispatcher.java index 4e00266..6457904 100644 --- a/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/ErrorDispatcher.java +++ b/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/ErrorDispatcher.java @@ -16,26 +16,32 @@ */ package org.apache.struts2.jasper.compiler; +import org.apache.struts2.jasper.JasperException; +import org.apache.struts2.jasper.JspCompilationContext; +import org.xml.sax.SAXException; + import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; import java.net.MalformedURLException; import java.util.ArrayList; -import org.apache.struts2.jasper.JasperException; -import org.apache.struts2.jasper.JspCompilationContext; -import org.xml.sax.SAXException; - /** + * <p> * Class responsible for dispatching JSP parse and javac compilation errors * to the configured error handler. + * </p> * + * <p> * This class is also responsible for localizing any error codes before they * are passed on to the configured error handler. - * + * </p> + * + * <p> * In the case of a Java compilation error, the compiler error message is - * parsed into an array of JavacErrorDetail instances, which is passed on to + * parsed into an array of JavacErrorDetail instances, which is passed on to * the configured error handler. + * </p> * * @author Jan Luehe * @author Kin-man Chung @@ -49,280 +55,354 @@ public class ErrorDispatcher { private boolean jspcMode = false; - /* + /** * Constructor. * - * @param jspcMode true if compilation has been initiated by JspC, false - * otherwise + * @param jspcMode true if compilation has been initiated by JspC, false otherwise */ public ErrorDispatcher(boolean jspcMode) { - // XXX check web.xml for custom error handler - errHandler = new DefaultErrorHandler(); + // XXX check web.xml for custom error handler + errHandler = new DefaultErrorHandler(); this.jspcMode = jspcMode; } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * * @param errCode Error code + * @throws JasperException in case of Jasper errors */ public void jspError(String errCode) throws JasperException { - dispatch(null, errCode, null, null); + dispatch(null, errCode, null, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. - * - * @param where Error location + * </p> + * @param where Error location * @param errCode Error code + * @throws JasperException in case of Jasper errors */ public void jspError(Mark where, String errCode) throws JasperException { - dispatch(where, errCode, null, null); + dispatch(where, errCode, null, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * - * @param n Node that caused the error + * @param n Node that caused the error * @param errCode Error code + * @throws JasperException in case of Jasper errors */ public void jspError(Node n, String errCode) throws JasperException { - dispatch(n.getStart(), errCode, null, null); + dispatch(n.getStart(), errCode, null, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * * @param errCode Error code - * @param arg Argument for parametric replacement + * @param arg Argument for parametric replacement + * @throws JasperException in case of Jasper errors */ public void jspError(String errCode, String arg) throws JasperException { - dispatch(null, errCode, new Object[] {arg}, null); + dispatch(null, errCode, new Object[]{arg}, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * - * @param where Error location + * @param where Error location * @param errCode Error code - * @param arg Argument for parametric replacement + * @param arg Argument for parametric replacement + * @throws JasperException in case of Jasper errors */ public void jspError(Mark where, String errCode, String arg) - throws JasperException { - dispatch(where, errCode, new Object[] {arg}, null); + throws JasperException { + dispatch(where, errCode, new Object[]{arg}, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * - * @param n Node that caused the error + * @param n Node that caused the error * @param errCode Error code - * @param arg Argument for parametric replacement + * @param arg Argument for parametric replacement + * @throws JasperException in case of Jasper errors */ public void jspError(Node n, String errCode, String arg) - throws JasperException { - dispatch(n.getStart(), errCode, new Object[] {arg}, null); + throws JasperException { + dispatch(n.getStart(), errCode, new Object[]{arg}, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * * @param errCode Error code - * @param arg1 First argument for parametric replacement - * @param arg2 Second argument for parametric replacement + * @param arg1 First argument for parametric replacement + * @param arg2 Second argument for parametric replacement + * @throws JasperException in case of Jasper errors */ public void jspError(String errCode, String arg1, String arg2) - throws JasperException { - dispatch(null, errCode, new Object[] {arg1, arg2}, null); + throws JasperException { + dispatch(null, errCode, new Object[]{arg1, arg2}, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * * @param errCode Error code - * @param arg1 First argument for parametric replacement - * @param arg2 Second argument for parametric replacement - * @param arg3 Third argument for parametric replacement + * @param arg1 First argument for parametric replacement + * @param arg2 Second argument for parametric replacement + * @param arg3 Third argument for parametric replacement + * @throws JasperException in case of Jasper errors */ public void jspError(String errCode, String arg1, String arg2, String arg3) - throws JasperException { - dispatch(null, errCode, new Object[] {arg1, arg2, arg3}, null); + throws JasperException { + dispatch(null, errCode, new Object[]{arg1, arg2, arg3}, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * - * @param where Error location + * @param where Error location * @param errCode Error code - * @param arg1 First argument for parametric replacement - * @param arg2 Second argument for parametric replacement + * @param arg1 First argument for parametric replacement + * @param arg2 Second argument for parametric replacement + * @throws JasperException in case of Jasper errors */ public void jspError(Mark where, String errCode, String arg1, String arg2) - throws JasperException { - dispatch(where, errCode, new Object[] {arg1, arg2}, null); + throws JasperException { + dispatch(where, errCode, new Object[]{arg1, arg2}, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * - * @param where Error location + * @param where Error location * @param errCode Error code - * @param arg1 First argument for parametric replacement - * @param arg2 Second argument for parametric replacement - * @param arg3 Third argument for parametric replacement + * @param arg1 First argument for parametric replacement + * @param arg2 Second argument for parametric replacement + * @param arg3 Third argument for parametric replacement + * @throws JasperException in case of Jasper errors */ - public void jspError(Mark where, String errCode, String arg1, String arg2, String arg3) - throws JasperException { - dispatch(where, errCode, new Object[] {arg1, arg2, arg3}, null); + throws JasperException { + dispatch(where, errCode, new Object[]{arg1, arg2, arg3}, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * - * @param n Node that caused the error + * @param n Node that caused the error * @param errCode Error code - * @param arg1 First argument for parametric replacement - * @param arg2 Second argument for parametric replacement + * @param arg1 First argument for parametric replacement + * @param arg2 Second argument for parametric replacement + * @throws JasperException in case of Jasper errors */ - public void jspError(Node n, String errCode, String arg1, String arg2) - throws JasperException { - dispatch(n.getStart(), errCode, new Object[] {arg1, arg2}, null); + throws JasperException { + dispatch(n.getStart(), errCode, new Object[]{arg1, arg2}, null); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * - * @param n Node that caused the error + * @param n Node that caused the error * @param errCode Error code - * @param arg1 First argument for parametric replacement - * @param arg2 Second argument for parametric replacement - * @param arg3 Third argument for parametric replacement + * @param arg1 First argument for parametric replacement + * @param arg2 Second argument for parametric replacement + * @param arg3 Third argument for parametric replacement + * @throws JasperException in case of Jasper errors */ - public void jspError(Node n, String errCode, String arg1, String arg2, String arg3) - throws JasperException { - dispatch(n.getStart(), errCode, new Object[] {arg1, arg2, arg3}, null); + throws JasperException { + dispatch(n.getStart(), errCode, new Object[]{arg1, arg2, arg3}, null); } - /* + /** + * <p> * Dispatches the given parsing exception to the configured error handler. + * </p> * * @param e Parsing exception + * @throws JasperException in case of Jasper errors */ public void jspError(Exception e) throws JasperException { - dispatch(null, null, null, e); + dispatch(null, null, null, e); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * * @param errCode Error code - * @param arg Argument for parametric replacement - * @param e Parsing exception + * @param arg Argument for parametric replacement + * @param e Parsing exception + * @throws JasperException in case of Jasper errors */ public void jspError(String errCode, String arg, Exception e) - throws JasperException { - dispatch(null, errCode, new Object[] {arg}, e); + throws JasperException { + dispatch(null, errCode, new Object[]{arg}, e); } - /* + /** + * <p> * Dispatches the given JSP parse error to the configured error handler. + * </p> * + * <p> * The given error code is localized. If it is not found in the * resource bundle for localized error messages, it is used as the error * message. + * </p> * - * @param n Node that caused the error + * @param n Node that caused the error * @param errCode Error code - * @param arg Argument for parametric replacement - * @param e Parsing exception + * @param arg Argument for parametric replacement + * @param e Parsing exception + * @throws JasperException in case of Jasper errors */ public void jspError(Node n, String errCode, String arg, Exception e) - throws JasperException { - dispatch(n.getStart(), errCode, new Object[] {arg}, e); + throws JasperException { + dispatch(n.getStart(), errCode, new Object[]{arg}, e); } /** + * <p> * Parses the given error message into an array of javac compilation error * messages (one per javac compilation error line number). + * </p> * * @param errMsg Error message - * @param fname Name of Java source file whose compilation failed - * @param page Node representation of JSP page from which the Java source - * file was generated - * + * @param fname Name of Java source file whose compilation failed + * @param page Node representation of JSP page from which the Java source + * file was generated * @return Array of javac compilation errors, or null if the given error * message does not contain any compilation error line numbers + * @throws JasperException in case of Jasper errors + * @throws IOException in case of IO errors */ public static JavacErrorDetail[] parseJavacErrors(String errMsg, String fname, Node.Nodes page) throws JasperException, IOException { - return parseJavacMessage(errMsg, fname, page); + return parseJavacMessage(errMsg, fname, page); } - /* + /** + * <p> * Dispatches the given javac compilation errors to the configured error * handler. + * </p> * * @param javacErrors Array of javac compilation errors + * @throws JasperException in case of Jasper errors */ public void javacError(JavacErrorDetail[] javacErrors) throws JasperException { @@ -331,15 +411,18 @@ public class ErrorDispatcher { } - /* + /** + * <p> * Dispatches the given compilation error report and exception to the * configured error handler. + * </p> * * @param errorReport Compilation error report - * @param e Compilation exception + * @param e Compilation exception + * @throws JasperException in case of Jasper errors */ public void javacError(String errorReport, Exception e) - throws JasperException { + throws JasperException { errHandler.javacError(errorReport, e); } @@ -361,23 +444,23 @@ public class ErrorDispatcher { * @param e Parsing exception */ private void dispatch(Mark where, String errCode, Object[] args, - Exception e) throws JasperException { - String file = null; - String errMsg = null; - int line = -1; - int column = -1; - boolean hasLocation = false; - - // Localize - if (errCode != null) { - errMsg = Localizer.getMessage(errCode, args); - } else if (e != null) { - // give a hint about what's wrong - errMsg = e.getMessage(); - } - - // Get error location - if (where != null) { + Exception e) throws JasperException { + String file = null; + String errMsg = null; + int line = -1; + int column = -1; + boolean hasLocation = false; + + // Localize + if (errCode != null) { + errMsg = Localizer.getMessage(errCode, args); + } else if (e != null) { + // give a hint about what's wrong + errMsg = e.getMessage(); + } + + // Get error location + if (where != null) { if (jspcMode) { // Get the full URL of the resource that caused the error try { @@ -391,22 +474,22 @@ public class ErrorDispatcher { // disclose any local filesystem details file = where.getFile(); } - line = where.getLineNumber(); - column = where.getColumnNumber(); - hasLocation = true; - } - // Get nested exception - Exception nestedEx = e; - if ((e instanceof SAXException) - && (((SAXException) e).getException() != null)) { - nestedEx = ((SAXException) e).getException(); - } - - if (hasLocation) { - errHandler.jspError(file, line, column, errMsg, nestedEx); - } else { - errHandler.jspError(errMsg, nestedEx); - } + line = where.getLineNumber(); + column = where.getColumnNumber(); + hasLocation = true; + } + // Get nested exception + Exception nestedEx = e; + if ((e instanceof SAXException) + && (((SAXException) e).getException() != null)) { + nestedEx = ((SAXException) e).getException(); + } + + if (hasLocation) { + errHandler.jspError(file, line, column, errMsg, nestedEx); + } else { + errHandler.jspError(errMsg, nestedEx); + } } /* @@ -426,14 +509,14 @@ public class ErrorDispatcher { * compilation errors */ private static JavacErrorDetail[] parseJavacMessage( - String errMsg, String fname, Node.Nodes page) - throws IOException, JasperException { + String errMsg, String fname, Node.Nodes page) + throws IOException, JasperException { ArrayList<JavacErrorDetail> errors = new ArrayList<JavacErrorDetail>(); StringBuffer errMsgBuf = null; int lineNum = -1; JavacErrorDetail javacError = null; - + BufferedReader reader = new BufferedReader(new StringReader(errMsg)); /* @@ -449,77 +532,77 @@ public class ErrorDispatcher { * Ignore colon following drive letter on Windows (fromIndex = 2). * XXX Handle deprecation warnings that don't have line info */ - int beginColon = line.indexOf(':', 2); + int beginColon = line.indexOf(':', 2); int endColon = line.indexOf(':', beginColon + 1); if ((beginColon >= 0) && (endColon >= 0)) { if (javacError != null) { // add previous error to error vector errors.add(javacError); } - + String lineNumStr = line.substring(beginColon + 1, endColon); try { lineNum = Integer.parseInt(lineNumStr); } catch (NumberFormatException e) { lineNum = -1; } - + errMsgBuf = new StringBuffer(); - + javacError = createJavacError(fname, page, errMsgBuf, lineNum); } - + // Ignore messages preceding first error if (errMsgBuf != null) { errMsgBuf.append(line); errMsgBuf.append("\n"); } } - + // Add last error to error vector if (javacError != null) { errors.add(javacError); - } - + } + reader.close(); - + JavacErrorDetail[] errDetails = null; if (errors.size() > 0) { errDetails = new JavacErrorDetail[errors.size()]; errors.toArray(errDetails); } - + return errDetails; } /** - * @param fname - * @param page - * @param errMsgBuf - * @param lineNum + * @param fname Name of the JSP file in which the parse error occurred + * @param page page + * @param errMsgBuf error message buffer + * @param lineNum line number * @return JavacErrorDetail The error details - * @throws JasperException + * @throws JasperException in case of Jasper errors */ public static JavacErrorDetail createJavacError(String fname, - Node.Nodes page, StringBuffer errMsgBuf, int lineNum) - throws JasperException { + Node.Nodes page, StringBuffer errMsgBuf, int lineNum) + throws JasperException { return createJavacError(fname, page, errMsgBuf, lineNum, null); } - - + + /** - * @param fname - * @param page - * @param errMsgBuf - * @param lineNum - * @param ctxt + * @param fname Name of the JSP file in which the parse error occurred + * @param page page + * @param errMsgBuf error message buffer + * @param lineNum line number + * @param ctxt JSP compilation context * @return JavacErrorDetail The error details - * @throws JasperException + * @throws JasperException in case of Jasper errors */ public static JavacErrorDetail createJavacError(String fname, - Node.Nodes page, StringBuffer errMsgBuf, int lineNum, - JspCompilationContext ctxt) throws JasperException { + Node.Nodes page, StringBuffer errMsgBuf, int lineNum, + JspCompilationContext ctxt) throws JasperException { JavacErrorDetail javacError; // Attempt to map javac error line number to line in JSP page ErrorVisitor errVisitor = new ErrorVisitor(lineNum); @@ -534,7 +617,7 @@ public class ErrorDispatcher { lineNum, errNode.getStart().getFile(), errNode.getStart().getLineNumber() + lineNum - - errVisitor.getJspSourceNode().getBeginJavaLine(), + errVisitor.getJspSourceNode().getBeginJavaLine(), errMsgBuf, ctxt); } else { @@ -575,40 +658,40 @@ public class ErrorDispatcher { */ static class ErrorVisitor extends Node.Visitor { - // Java source line number to be mapped - private int lineNum; - - /* - * JSP node whose Java source code range in the generated servlet - * contains the Java source line number to be mapped - */ - Node found; - - /* - * Constructor. - * - * @param lineNum Source line number in the generated servlet code - */ - public ErrorVisitor(int lineNum) { - this.lineNum = lineNum; - } - - public void doVisit(Node n) throws JasperException { - if ((lineNum >= n.getBeginJavaLine()) - && (lineNum < n.getEndJavaLine())) { - found = n; - } + // Java source line number to be mapped + private int lineNum; + + /* + * JSP node whose Java source code range in the generated servlet + * contains the Java source line number to be mapped + */ + Node found; + + /** + * Constructor. + * + * @param lineNum Source line number in the generated servlet code + */ + public ErrorVisitor(int lineNum) { + this.lineNum = lineNum; + } + + public void doVisit(Node n) throws JasperException { + if ((lineNum >= n.getBeginJavaLine()) + && (lineNum < n.getEndJavaLine())) { + found = n; + } } - /* - * Gets the JSP node to which the source line number in the generated - * servlet code was mapped. - * - * @return JSP node to which the source line number in the generated - * servlet code was mapped - */ - public Node getJspSourceNode() { - return found; - } + /** + * Gets the JSP node to which the source line number in the generated + * servlet code was mapped. + * + * @return JSP node to which the source line number in the generated + * servlet code was mapped + */ + public Node getJspSourceNode() { + return found; + } } }
http://git-wip-us.apache.org/repos/asf/struts/blob/775c82a7/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/ErrorHandler.java ---------------------------------------------------------------------- diff --git a/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/ErrorHandler.java b/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/ErrorHandler.java index 4fa6980..a7a73f0 100644 --- a/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/ErrorHandler.java +++ b/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/ErrorHandler.java @@ -20,12 +20,16 @@ package org.apache.struts2.jasper.compiler; import org.apache.struts2.jasper.JasperException; /** + * <p> * Interface for handling JSP parse and javac compilation errors. - * + * </p> + * + * <p> * An implementation of this interface may be registered with the * ErrorDispatcher by setting the XXX initialization parameter in the JSP * page compiler and execution servlet in Catalina's web.xml file to the * implementation's fully qualified class name. + * </p> * * @author Jan Luehe * @author Kin-man Chung @@ -35,39 +39,40 @@ public interface ErrorHandler { /** * Processes the given JSP parse error. * - * @param fname Name of the JSP file in which the parse error occurred - * @param line Parse error line number - * @param column Parse error column number - * @param msg Parse error message + * @param fname Name of the JSP file in which the parse error occurred + * @param line Parse error line number + * @param column Parse error column number + * @param msg Parse error message * @param exception Parse exception + * @throws JasperException in case of Jasper errors */ - public void jspError(String fname, int line, int column, String msg, - Exception exception) throws JasperException; + public void jspError(String fname, int line, int column, String msg, Exception exception) throws JasperException; /** * Processes the given JSP parse error. * - * @param msg Parse error message + * @param msg Parse error message * @param exception Parse exception + * @throws JasperException in case of Jasper errors */ - public void jspError(String msg, Exception exception) - throws JasperException; + public void jspError(String msg, Exception exception) throws JasperException; /** * Processes the given javac compilation errors. * * @param details Array of JavacErrorDetail instances corresponding to the - * compilation errors + * compilation errors + * @throws JasperException in case of Jasper errors */ public void javacError(JavacErrorDetail[] details) - throws JasperException; + throws JasperException; /** * Processes the given javac error report and exception. * * @param errorReport Compilation error report - * @param exception Compilation exception + * @param exception Compilation exception + * @throws JasperException in case of Jasper errors */ - public void javacError(String errorReport, Exception exception) - throws JasperException; + public void javacError(String errorReport, Exception exception) throws JasperException; } http://git-wip-us.apache.org/repos/asf/struts/blob/775c82a7/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/JspConfig.java ---------------------------------------------------------------------- diff --git a/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/JspConfig.java b/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/JspConfig.java index 67aef45..5ec0118 100644 --- a/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/JspConfig.java +++ b/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/JspConfig.java @@ -267,6 +267,7 @@ public class JspConfig { * Find a property that best matches the supplied resource. * @param uri the resource supplied. * @return a JspProperty indicating the best match, or some default. + * @throws JasperException in case of Jasper errors */ public JspProperty findJspProperty(String uri) throws JasperException { @@ -400,6 +401,10 @@ 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. + * + * @param uri the uri + * @return true if uri is a JSP page + * @throws JasperException in case of Jasper errors */ public boolean isJspPage(String uri) throws JasperException { @@ -419,10 +424,9 @@ public class JspConfig { uriExtension = uri.substring(index+1); } - Iterator iter = jspProperties.iterator(); - while (iter.hasNext()) { + for (Object jspProperty : jspProperties) { - JspPropertyGroup jpg = (JspPropertyGroup) iter.next(); + JspPropertyGroup jpg = (JspPropertyGroup) jspProperty; JspProperty jp = jpg.getJspProperty(); String extension = jpg.getExtension(); http://git-wip-us.apache.org/repos/asf/struts/blob/775c82a7/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/JspRuntimeContext.java ---------------------------------------------------------------------- diff --git a/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/JspRuntimeContext.java b/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/JspRuntimeContext.java index 20c9da3..851f972 100644 --- a/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/JspRuntimeContext.java +++ b/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/JspRuntimeContext.java @@ -17,6 +17,18 @@ package org.apache.struts2.jasper.compiler; +import org.apache.juli.logging.Log; +import org.apache.juli.logging.LogFactory; +import org.apache.struts2.jasper.Constants; +import org.apache.struts2.jasper.JspCompilationContext; +import org.apache.struts2.jasper.Options; +import org.apache.struts2.jasper.runtime.JspFactoryImpl; +import org.apache.struts2.jasper.security.SecurityClassLoad; +import org.apache.struts2.jasper.servlet.JspCServletContext; +import org.apache.struts2.jasper.servlet.JspServletWrapper; + +import javax.servlet.ServletContext; +import javax.servlet.jsp.JspFactory; import java.io.File; import java.io.FileNotFoundException; import java.io.FilePermission; @@ -26,32 +38,24 @@ import java.security.CodeSource; import java.security.PermissionCollection; import java.security.Policy; import java.security.cert.Certificate; -import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import javax.servlet.ServletContext; -import javax.servlet.jsp.JspFactory; - -import org.apache.struts2.jasper.Constants; -import org.apache.struts2.jasper.JspCompilationContext; -import org.apache.struts2.jasper.Options; -import org.apache.struts2.jasper.runtime.JspFactoryImpl; -import org.apache.struts2.jasper.security.SecurityClassLoad; -import org.apache.struts2.jasper.servlet.JspServletWrapper; -import org.apache.struts2.jasper.servlet.JspCServletContext; -import org.apache.juli.logging.Log; -import org.apache.juli.logging.LogFactory; - /** + * <p> * Class for tracking JSP compile time file dependencies when the - * &060;%@include file="..."%&062; directive is used. + * <%{@literal @}include file="..."%> directive is used. + * </p> * + * <p> * A background thread periodically checks the files a JSP page - * is dependent upon. If a dpendent file changes the JSP page + * is dependent upon. If a dependent file changes the JSP page * which included it is recompiled. + * </p> * + * <p> * Only used if a web application context is a directory. + * </p> * * @author Glenn L. Nielsen * @version $Revision: 505593 $ @@ -73,21 +77,21 @@ public final class JspRuntimeContext { static { JspFactoryImpl factory = new JspFactoryImpl(); SecurityClassLoad.securityClassLoad(factory.getClass().getClassLoader()); - if( System.getSecurityManager() != null ) { + if (System.getSecurityManager() != null) { String basePackage = "org.apache.struts2.jasper."; try { - factory.getClass().getClassLoader().loadClass( basePackage + - "runtime.JspFactoryImpl$PrivilegedGetPageContext"); - factory.getClass().getClassLoader().loadClass( basePackage + - "runtime.JspFactoryImpl$PrivilegedReleasePageContext"); - factory.getClass().getClassLoader().loadClass( basePackage + - "runtime.JspRuntimeLibrary"); - factory.getClass().getClassLoader().loadClass( basePackage + - "runtime.JspRuntimeLibrary$PrivilegedIntrospectHelper"); - factory.getClass().getClassLoader().loadClass( basePackage + - "runtime.ServletResponseWrapperInclude"); - factory.getClass().getClassLoader().loadClass( basePackage + - "servlet.JspServletWrapper"); + factory.getClass().getClassLoader().loadClass(basePackage + + "runtime.JspFactoryImpl$PrivilegedGetPageContext"); + factory.getClass().getClassLoader().loadClass(basePackage + + "runtime.JspFactoryImpl$PrivilegedReleasePageContext"); + factory.getClass().getClassLoader().loadClass(basePackage + + "runtime.JspRuntimeLibrary"); + factory.getClass().getClassLoader().loadClass(basePackage + + "runtime.JspRuntimeLibrary$PrivilegedIntrospectHelper"); + factory.getClass().getClassLoader().loadClass(basePackage + + "runtime.ServletResponseWrapperInclude"); + factory.getClass().getClassLoader().loadClass(basePackage + + "servlet.JspServletWrapper"); } catch (ClassNotFoundException ex) { throw new IllegalStateException(ex); } @@ -99,11 +103,16 @@ public final class JspRuntimeContext { // ----------------------------------------------------------- Constructors /** + * <p> * Create a JspRuntimeContext for a web application context. + * </p> * + * <p> * Loads in any previously generated dependencies from file. + * </p> * * @param context ServletContext for web application + * @param options options */ public JspRuntimeContext(ServletContext context, Options options) { @@ -112,27 +121,27 @@ public final class JspRuntimeContext { // Get the parent class loader parentClassLoader = - (URLClassLoader) Thread.currentThread().getContextClassLoader(); + (URLClassLoader) Thread.currentThread().getContextClassLoader(); if (parentClassLoader == null) { parentClassLoader = - (URLClassLoader)this.getClass().getClassLoader(); + (URLClassLoader) this.getClass().getClassLoader(); } - if (log.isDebugEnabled()) { - if (parentClassLoader != null) { - log.debug(Localizer.getMessage("jsp.message.parent_class_loader_is", - parentClassLoader.toString())); - } else { - log.debug(Localizer.getMessage("jsp.message.parent_class_loader_is", - "<none>")); - } + if (log.isDebugEnabled()) { + if (parentClassLoader != null) { + log.debug(Localizer.getMessage("jsp.message.parent_class_loader_is", + parentClassLoader.toString())); + } else { + log.debug(Localizer.getMessage("jsp.message.parent_class_loader_is", + "<none>")); + } } initClassPath(); - if (context instanceof JspCServletContext) { - return; - } + if (context instanceof JspCServletContext) { + return; + } if (Constants.IS_SECURITY_ENABLED) { initSecurity(); @@ -140,12 +149,12 @@ public final class JspRuntimeContext { // If this web application context is running from a // directory, start the background compilation thread - String appBase = context.getRealPath("/"); + String appBase = context.getRealPath("/"); if (!options.getDevelopment() && appBase != null && options.getCheckInterval() > 0) { lastCheck = System.currentTimeMillis(); - } + } } // ----------------------------------------------------- Instance Variables @@ -157,15 +166,15 @@ public final class JspRuntimeContext { private Options options; private URLClassLoader parentClassLoader; private PermissionCollection permissionCollection; - private CodeSource codeSource; + private CodeSource codeSource; private String classpath; private long lastCheck = -1L; /** * Maps JSP pages to their JspServletWrapper's */ - private Map<String, JspServletWrapper> jsps = new ConcurrentHashMap<String, JspServletWrapper>(); - + private Map<String, JspServletWrapper> jsps = new ConcurrentHashMap<>(); + // ------------------------------------------------------ Public Methods @@ -173,7 +182,7 @@ public final class JspRuntimeContext { * Add a new JspServletWrapper. * * @param jspUri JSP URI - * @param jsw Servlet wrapper for JSP + * @param jsw Servlet wrapper for JSP */ public void addWrapper(String jspUri, JspServletWrapper jsw) { jsps.put(jspUri, jsw); @@ -238,12 +247,11 @@ public final class JspRuntimeContext { } /** - * Process a "destory" event for this web application context. - */ + * Process a "destroy" event for this web application context. + */ public void destroy() { - Iterator servlets = jsps.values().iterator(); - while (servlets.hasNext()) { - ((JspServletWrapper) servlets.next()).destroy(); + for (Object o : jsps.values()) { + ((JspServletWrapper) o).destroy(); } } @@ -289,21 +297,21 @@ public final class JspRuntimeContext { } else { return; } - - Object [] wrappers = jsps.values().toArray(); - for (int i = 0; i < wrappers.length; i++ ) { - JspServletWrapper jsw = (JspServletWrapper)wrappers[i]; + + Object[] wrappers = jsps.values().toArray(); + for (Object wrapper : wrappers) { + JspServletWrapper jsw = (JspServletWrapper) wrapper; JspCompilationContext ctxt = jsw.getJspEngineContext(); // JspServletWrapper also synchronizes on this when // it detects it has to do a reload - synchronized(jsw) { + synchronized (jsw) { try { ctxt.compile(); } catch (FileNotFoundException ex) { ctxt.incrementRemoved(); } catch (Throwable t) { jsw.getServletContext().log("Background compile failed", - t); + t); } } } @@ -311,7 +319,7 @@ public final class JspRuntimeContext { } /** - * The classpath that is passed off to the Java compiler. + * @return The classpath that is passed off to the Java compiler. */ public String getClassPath() { return classpath; @@ -326,22 +334,22 @@ public final class JspRuntimeContext { */ private void initClassPath() { - URL [] urls = parentClassLoader.getURLs(); + URL[] urls = parentClassLoader.getURLs(); StringBuffer cpath = new StringBuffer(); String sep = System.getProperty("path.separator"); - for(int i = 0; i < urls.length; i++) { + for (int i = 0; i < urls.length; i++) { // Tomcat 4 can use URL's other than file URL's, // a protocol other than file: will generate a // bad file system path, so only add file: // protocol URL's to the classpath. - - if( urls[i].getProtocol().equals("file") ) { - cpath.append((String)urls[i].getFile()+sep); + + if (urls[i].getProtocol().equals("file")) { + cpath.append((String) urls[i].getFile() + sep); } - } + } - cpath.append(options.getScratchDir() + sep); + cpath.append(options.getScratchDir() + sep); String cp = (String) context.getAttribute(Constants.SERVLET_CLASSPATH); if (cp == null || cp.equals("")) { @@ -350,7 +358,7 @@ public final class JspRuntimeContext { classpath = cpath.toString() + cp; - if(log.isDebugEnabled()) { + if (log.isDebugEnabled()) { log.debug("Compilation classpath initialized: " + getClassPath()); } } @@ -365,80 +373,80 @@ public final class JspRuntimeContext { // web app context directory, then add a file read permission // for that directory. Policy policy = Policy.getPolicy(); - if( policy != null ) { - try { + if (policy != null) { + try { // Get the permissions for the web app context String docBase = context.getRealPath("/"); - if( docBase == null ) { + if (docBase == null) { docBase = options.getScratchDir().toString(); } String codeBase = docBase; - if (!codeBase.endsWith(File.separator)){ + if (!codeBase.endsWith(File.separator)) { codeBase = codeBase + File.separator; } File contextDir = new File(codeBase); URL url = contextDir.getCanonicalFile().toURL(); - codeSource = new CodeSource(url,(Certificate[])null); + codeSource = new CodeSource(url, (Certificate[]) null); permissionCollection = policy.getPermissions(codeSource); // Create a file read permission for web app context directory - if (!docBase.endsWith(File.separator)){ + if (!docBase.endsWith(File.separator)) { permissionCollection.add - (new FilePermission(docBase,"read")); + (new FilePermission(docBase, "read")); docBase = docBase + File.separator; } else { permissionCollection.add - (new FilePermission - (docBase.substring(0,docBase.length() - 1),"read")); + (new FilePermission + (docBase.substring(0, docBase.length() - 1), "read")); } docBase = docBase + "-"; - permissionCollection.add(new FilePermission(docBase,"read")); + permissionCollection.add(new FilePermission(docBase, "read")); // Create a file read permission for web app tempdir (work) // directory String workDir = options.getScratchDir().toString(); - if (!workDir.endsWith(File.separator)){ + if (!workDir.endsWith(File.separator)) { permissionCollection.add - (new FilePermission(workDir,"read")); + (new FilePermission(workDir, "read")); workDir = workDir + File.separator; } workDir = workDir + "-"; - permissionCollection.add(new FilePermission(workDir,"read")); + permissionCollection.add(new FilePermission(workDir, "read")); // Allow the JSP to access org.apache.struts2.jasper.runtime.HttpJspBase - permissionCollection.add( new RuntimePermission( - "accessClassInPackage.org.apache.struts2.jasper.runtime") ); + permissionCollection.add(new RuntimePermission( + "accessClassInPackage.org.apache.struts2.jasper.runtime")); if (parentClassLoader instanceof URLClassLoader) { - URL [] urls = parentClassLoader.getURLs(); + URL[] urls = parentClassLoader.getURLs(); String jarUrl = null; String jndiUrl = null; - for (int i=0; i<urls.length; i++) { + for (int i = 0; i < urls.length; i++) { if (jndiUrl == null - && urls[i].toString().startsWith("jndi:") ) { + && urls[i].toString().startsWith("jndi:")) { jndiUrl = urls[i].toString() + "-"; } if (jarUrl == null && urls[i].toString().startsWith("jar:jndi:") ) { jarUrl = urls[i].toString(); - jarUrl = jarUrl.substring(0,jarUrl.length() - 2); + jarUrl = jarUrl.substring(0, jarUrl.length() - 2); jarUrl = jarUrl.substring(0, - jarUrl.lastIndexOf('/')) + "/-"; + jarUrl.lastIndexOf('/')) + "/-"; } } if (jarUrl != null) { permissionCollection.add( - new FilePermission(jarUrl,"read")); + new FilePermission(jarUrl, "read")); permissionCollection.add( - new FilePermission(jarUrl.substring(4),"read")); + new FilePermission(jarUrl.substring(4), "read")); } if (jndiUrl != null) permissionCollection.add( - new FilePermission(jndiUrl,"read") ); + new FilePermission(jndiUrl, "read")); } - } catch(Exception e) { - context.log("Security Init for context failed",e); + } catch (Exception e) { + context.log("Security Init for context failed", e); } } }