Author: markt Date: Fri Feb 9 14:04:42 2018 New Revision: 1823658 URL: http://svn.apache.org/viewvc?rev=1823658&view=rev Log: Refactor the code that searches for a matching error page by working up the class hierarchy of an exception type to ErrorPageSupport to enable re-use.
Modified: tomcat/trunk/java/org/apache/catalina/Context.java tomcat/trunk/java/org/apache/catalina/core/StandardContext.java tomcat/trunk/java/org/apache/catalina/core/StandardHostValve.java tomcat/trunk/java/org/apache/catalina/mbeans/ContextMBean.java tomcat/trunk/java/org/apache/catalina/startup/FailedContext.java tomcat/trunk/java/org/apache/catalina/util/ErrorPageSupport.java tomcat/trunk/test/org/apache/tomcat/unittest/TesterContext.java Modified: tomcat/trunk/java/org/apache/catalina/Context.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/Context.java?rev=1823658&r1=1823657&r2=1823658&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/Context.java (original) +++ tomcat/trunk/java/org/apache/catalina/Context.java Fri Feb 9 14:04:42 2018 @@ -969,14 +969,31 @@ public interface Context extends Contain /** + * @param exceptionType Exception type to look up + * * @return the error page entry for the specified Java exception type, - * if any; otherwise return <code>null</code>. + * if any; otherwise return {@code null}. * - * @param exceptionType Exception type to look up + * @deprecated Unused. Will be removed in Tomcat 10. + * Use {@link #findErrorPage(Throwable)} instead. */ + @Deprecated public ErrorPage findErrorPage(String exceptionType); + /** + * Find and return the ErrorPage instance for the specified exception's + * class, or an ErrorPage instance for the closest superclass for which + * there is such a definition. If no associated ErrorPage instance is + * found, return <code>null</code>. + * + * @param throwable The exception type for which to find an ErrorPage + * + * @return the error page entry for the specified Java exception type, + * if any; otherwise return {@code null}. + */ + public ErrorPage findErrorPage(Throwable throwable); + /** * @return the set of defined error pages for all specified error codes Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardContext.java?rev=1823658&r1=1823657&r2=1823658&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Fri Feb 9 14:04:42 2018 @@ -3278,17 +3278,18 @@ public class StandardContext extends Con } - /** - * Return the error page entry for the specified Java exception type, - * if any; otherwise return <code>null</code>. - * - * @param exceptionType Exception type to look up - */ @Override + @Deprecated public ErrorPage findErrorPage(String exceptionType) { return errorPageSupport.find(exceptionType); } + + @Override + public ErrorPage findErrorPage(Throwable exceptionType) { + return errorPageSupport.find(exceptionType); + } + /** * Return the set of defined error pages for all specified error codes Modified: tomcat/trunk/java/org/apache/catalina/core/StandardHostValve.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardHostValve.java?rev=1823658&r1=1823657&r2=1823658&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/StandardHostValve.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/StandardHostValve.java Fri Feb 9 14:04:42 2018 @@ -293,9 +293,9 @@ final class StandardHostValve extends Va return; } - ErrorPage errorPage = findErrorPage(context, throwable); + ErrorPage errorPage = context.findErrorPage(throwable); if ((errorPage == null) && (realError != throwable)) { - errorPage = findErrorPage(context, realError); + errorPage = context.findErrorPage(realError); } if (errorPage != null) { @@ -397,40 +397,6 @@ final class StandardHostValve extends Va // Report our failure to process this custom page container.getLogger().error("Exception Processing " + errorPage, t); return false; - } } - - - /** - * Find and return the ErrorPage instance for the specified exception's - * class, or an ErrorPage instance for the closest superclass for which - * there is such a definition. If no associated ErrorPage instance is - * found, return <code>null</code>. - * - * @param context The Context in which to search - * @param exception The exception for which to find an ErrorPage - */ - private static ErrorPage findErrorPage - (Context context, Throwable exception) { - - if (exception == null) { - return null; - } - Class<?> clazz = exception.getClass(); - String name = clazz.getName(); - while (!Object.class.equals(clazz)) { - ErrorPage errorPage = context.findErrorPage(name); - if (errorPage != null) { - return errorPage; - } - clazz = clazz.getSuperclass(); - if (clazz == null) { - break; - } - name = clazz.getName(); - } - return null; - - } } Modified: tomcat/trunk/java/org/apache/catalina/mbeans/ContextMBean.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/mbeans/ContextMBean.java?rev=1823658&r1=1823657&r2=1823658&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/mbeans/ContextMBean.java (original) +++ tomcat/trunk/java/org/apache/catalina/mbeans/ContextMBean.java Fri Feb 9 14:04:42 2018 @@ -88,11 +88,28 @@ public class ContextMBean extends BaseCa * @param exceptionType Exception type to look up * @return a string representation of the error page * @throws MBeanException propagated from the managed resource access + * @deprecated Unused. Will be removed in Tomcat 10. + * Use {@link #findErrorPage(Throwable)} instead. */ + @Deprecated public String findErrorPage(String exceptionType) throws MBeanException { Context context = doGetManagedResource(); return context.findErrorPage(exceptionType).toString(); } + + + /** + * Return the error page entry for the specified Java exception type, + * if any; otherwise return <code>null</code>. + * + * @param exceptionType Exception type to look up + * @return a string representation of the error page + * @throws MBeanException propagated from the managed resource access + */ + public String findErrorPage(Throwable exceptionType) throws MBeanException { + Context context = doGetManagedResource(); + return context.findErrorPage(exceptionType).toString(); + } /** Modified: tomcat/trunk/java/org/apache/catalina/startup/FailedContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/FailedContext.java?rev=1823658&r1=1823657&r2=1823658&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/startup/FailedContext.java (original) +++ tomcat/trunk/java/org/apache/catalina/startup/FailedContext.java Fri Feb 9 14:04:42 2018 @@ -510,6 +510,8 @@ public class FailedContext extends Lifec @Override public ErrorPage findErrorPage(String exceptionType) { return null; } @Override + public ErrorPage findErrorPage(Throwable throwable) { return null; } + @Override public ErrorPage[] findErrorPages() { return null; } @Override public void removeErrorPage(ErrorPage errorPage) { /* NO-OP */ } Modified: tomcat/trunk/java/org/apache/catalina/util/ErrorPageSupport.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/util/ErrorPageSupport.java?rev=1823658&r1=1823657&r2=1823658&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/util/ErrorPageSupport.java (original) +++ tomcat/trunk/java/org/apache/catalina/util/ErrorPageSupport.java Fri Feb 9 14:04:42 2018 @@ -61,11 +61,44 @@ public class ErrorPageSupport { } + /** + * Find the ErrorPage, if any, for the named exception type. + * + * @param exceptionType The fully qualified class name of the exception type + * + * @return The ErrorPage for the named exception type, or {@code null} if + * none is configured + * + * @deprecated Unused. Will be removed in Tomcat 10. + * Use {@link #find(Throwable)} instead. + */ + @Deprecated public ErrorPage find(String exceptionType) { return exceptionPages.get(exceptionType); } + public ErrorPage find(Throwable exceptionType) { + if (exceptionType == null) { + return null; + } + Class<?> clazz = exceptionType.getClass(); + String name = clazz.getName(); + while (!Object.class.equals(clazz)) { + ErrorPage errorPage = exceptionPages.get(name); + if (errorPage != null) { + return errorPage; + } + clazz = clazz.getSuperclass(); + if (clazz == null) { + break; + } + name = clazz.getName(); + } + return null; + } + + public ErrorPage[] findAll() { Set<ErrorPage> errorPages = new HashSet<>(); errorPages.addAll(exceptionPages.values()); Modified: tomcat/trunk/test/org/apache/tomcat/unittest/TesterContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/unittest/TesterContext.java?rev=1823658&r1=1823657&r2=1823658&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/unittest/TesterContext.java (original) +++ tomcat/trunk/test/org/apache/tomcat/unittest/TesterContext.java Fri Feb 9 14:04:42 2018 @@ -811,6 +811,11 @@ public class TesterContext implements Co } @Override + public ErrorPage findErrorPage(Throwable exceptionType) { + return null; + } + + @Override public ErrorPage[] findErrorPages() { return null; } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org