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 2018/02/09 12:27:53 UTC

svn commit: r1823647 - in /tomcat/trunk/java/org/apache/catalina: Context.java core/StandardContext.java util/ErrorPageSupport.java

Author: markt
Date: Fri Feb  9 12:27:53 2018
New Revision: 1823647

URL: http://svn.apache.org/viewvc?rev=1823647&view=rev
Log:
Refactor error page related code into a separate class so it can be re-used in the ErrorReportValve.
Deprecate a couple of unused methods.

Added:
    tomcat/trunk/java/org/apache/catalina/util/ErrorPageSupport.java   (with props)
Modified:
    tomcat/trunk/java/org/apache/catalina/Context.java
    tomcat/trunk/java/org/apache/catalina/core/StandardContext.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=1823647&r1=1823646&r2=1823647&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/Context.java (original)
+++ tomcat/trunk/java/org/apache/catalina/Context.java Fri Feb  9 12:27:53 2018
@@ -1087,7 +1087,11 @@ public interface Context extends Contain
      * HTTP status code, if any; otherwise return <code>null</code>.
      *
      * @param status HTTP status code to look up
+     *
+     * @deprecated Unused. Will be removed in Tomcat 10.
+     *             Use {@link #findErrorPage(int)} instead.
      */
+    @Deprecated
     public String findStatusPage(int status);
 
 
@@ -1095,7 +1099,11 @@ public interface Context extends Contain
      * @return the set of HTTP status codes for which error pages have
      * been specified.  If none are specified, a zero-length array
      * is returned.
+     *
+     * @deprecated Unused. Will be removed in Tomcat 10.
+     *             Use {@link #findErrorPages()} instead.
      */
+    @Deprecated
     public int[] findStatusPages();
 
 

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=1823647&r1=1823646&r2=1823647&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Fri Feb  9 12:27:53 2018
@@ -31,7 +31,6 @@ import java.util.Enumeration;
 import java.util.EventListener;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
@@ -104,6 +103,7 @@ import org.apache.catalina.loader.Webapp
 import org.apache.catalina.session.StandardManager;
 import org.apache.catalina.util.CharsetMapper;
 import org.apache.catalina.util.ContextName;
+import org.apache.catalina.util.ErrorPageSupport;
 import org.apache.catalina.util.ExtensionValidator;
 import org.apache.catalina.util.URLEncoder;
 import org.apache.catalina.webresources.StandardRoot;
@@ -366,12 +366,7 @@ public class StandardContext extends Con
     private String docBase = null;
 
 
-    /**
-     * The exception pages for this web application, keyed by fully qualified
-     * class name of the Java exception.
-     */
-    private Map<String, ErrorPage> exceptionPages = new HashMap<>();
-
+    private final ErrorPageSupport errorPageSupport = new ErrorPageSupport();
 
     /**
      * The set of filter configurations (and associated filter instances) we
@@ -546,13 +541,6 @@ public class StandardContext extends Con
      */
     private AtomicLong sequenceNumber = new AtomicLong(0);
 
-    /**
-     * The status code error pages for this web application, keyed by
-     * HTTP status code (as an Integer). Note status code zero is used for the
-     * default error page.
-     */
-    private Map<Integer, ErrorPage> statusPages = new HashMap<>();
-
 
     /**
      * Set flag to true to cause the system.out and system.err to be redirected
@@ -2850,20 +2838,8 @@ public class StandardContext extends Con
             }
         }
 
-        // Add the specified error page to our internal collections
-        String exceptionType = errorPage.getExceptionType();
-        if (exceptionType != null) {
-            synchronized (exceptionPages) {
-                exceptionPages.put(exceptionType, errorPage);
-            }
-        } else {
-            synchronized (statusPages) {
-                statusPages.put(Integer.valueOf(errorPage.getErrorCode()),
-                                errorPage);
-            }
-        }
+        errorPageSupport.add(errorPage);
         fireContainerEvent("addErrorPage", errorPage);
-
     }
 
 
@@ -3298,7 +3274,7 @@ public class StandardContext extends Con
      */
     @Override
     public ErrorPage findErrorPage(int errorCode) {
-        return statusPages.get(Integer.valueOf(errorCode));
+        return errorPageSupport.find(errorCode);
     }
 
 
@@ -3310,9 +3286,7 @@ public class StandardContext extends Con
      */
     @Override
     public ErrorPage findErrorPage(String exceptionType) {
-        synchronized (exceptionPages) {
-            return exceptionPages.get(exceptionType);
-        }
+        return errorPageSupport.find(exceptionType);
     }
 
 
@@ -3322,23 +3296,7 @@ public class StandardContext extends Con
      */
     @Override
     public ErrorPage[] findErrorPages() {
-
-        synchronized(exceptionPages) {
-            synchronized(statusPages) {
-                ErrorPage results1[] = new ErrorPage[exceptionPages.size()];
-                results1 = exceptionPages.values().toArray(results1);
-                ErrorPage results2[] = new ErrorPage[statusPages.size()];
-                results2 = statusPages.values().toArray(results2);
-                ErrorPage results[] =
-                    new ErrorPage[results1.length + results2.length];
-                for (int i = 0; i < results1.length; i++)
-                    results[i] = results1[i];
-                for (int i = results1.length; i < results.length; i++)
-                    results[i] = results2[i - results1.length];
-                return results;
-            }
-        }
-
+        return errorPageSupport.findAll();
     }
 
 
@@ -3555,39 +3513,33 @@ public class StandardContext extends Con
     }
 
 
-    /**
-     * @return the context-relative URI of the error page for the specified
-     * HTTP status code, if any; otherwise return <code>null</code>.
-     *
-     * @param status HTTP status code to look up
-     */
     @Override
+    @Deprecated
     public String findStatusPage(int status) {
 
-        ErrorPage errorPage = statusPages.get(Integer.valueOf(status));
-        if (errorPage!=null) {
+        ErrorPage errorPage = findErrorPage(status);
+        if (errorPage != null) {
             return errorPage.getLocation();
         }
         return null;
-
     }
 
 
-    /**
-     * @return the set of HTTP status codes for which error pages have
-     * been specified.  If none are specified, a zero-length array
-     * is returned.
-     */
     @Override
+    @Deprecated
     public int[] findStatusPages() {
-        synchronized (statusPages) {
-            int results[] = new int[statusPages.size()];
-            Iterator<Integer> elements = statusPages.keySet().iterator();
-            int i = 0;
-            while (elements.hasNext())
-                results[i++] = elements.next().intValue();
-            return results;
+        ErrorPage[] errorPages = findErrorPages();
+        int size = errorPages.length;
+        int temp[] = new int[size];
+        int count = 0;
+        for (int i = 0; i < size; i++) {
+            if (errorPages[i].getExceptionType() == null) {
+                temp[count++] = errorPages[i].getErrorCode();
+            }
         }
+        int result[] = new int[count];
+        System.arraycopy(temp, 0, result, 0, count);
+        return result;
     }
 
 
@@ -3861,19 +3813,8 @@ public class StandardContext extends Con
      */
     @Override
     public void removeErrorPage(ErrorPage errorPage) {
-
-        String exceptionType = errorPage.getExceptionType();
-        if (exceptionType != null) {
-            synchronized (exceptionPages) {
-                exceptionPages.remove(exceptionType);
-            }
-        } else {
-            synchronized (statusPages) {
-                statusPages.remove(Integer.valueOf(errorPage.getErrorCode()));
-            }
-        }
+        errorPageSupport.remove(errorPage);
         fireContainerEvent("removeErrorPage", errorPage);
-
     }
 
 

Added: 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=1823647&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/util/ErrorPageSupport.java (added)
+++ tomcat/trunk/java/org/apache/catalina/util/ErrorPageSupport.java Fri Feb  9 12:27:53 2018
@@ -0,0 +1,75 @@
+/*
+ * 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.
+ */
+package org.apache.catalina.util;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.tomcat.util.descriptor.web.ErrorPage;
+
+/**
+ * Provides support for tracking per exception type and per HTTP status code
+ * error pages.
+ */
+public class ErrorPageSupport {
+
+    // Fully qualified class name to error page
+    private Map<String, ErrorPage> exceptionPages = new ConcurrentHashMap<>();
+
+    // HTTP status code to error page
+    private Map<Integer, ErrorPage> statusPages = new ConcurrentHashMap<>();
+
+
+    public void add(ErrorPage errorPage) {
+        String exceptionType = errorPage.getExceptionType();
+        if (exceptionType == null) {
+            statusPages.put(Integer.valueOf(errorPage.getErrorCode()), errorPage);
+        } else {
+            exceptionPages.put(exceptionType, errorPage);
+        }
+    }
+
+
+    public void remove(ErrorPage errorPage) {
+        String exceptionType = errorPage.getExceptionType();
+        if (exceptionType == null) {
+            statusPages.remove(Integer.valueOf(errorPage.getErrorCode()), errorPage);
+        } else {
+            exceptionPages.remove(exceptionType, errorPage);
+        }
+    }
+
+
+    public ErrorPage find(int statusCode) {
+        return statusPages.get(Integer.valueOf(statusCode));
+    }
+
+
+    public ErrorPage find(String exceptionType) {
+        return exceptionPages.get(exceptionType);
+    }
+
+
+    public ErrorPage[] findAll() {
+        Set<ErrorPage> errorPages = new HashSet<>();
+        errorPages.addAll(exceptionPages.values());
+        errorPages.addAll(statusPages.values());
+        return errorPages.toArray(new ErrorPage[errorPages.size()]);
+    }
+}

Propchange: tomcat/trunk/java/org/apache/catalina/util/ErrorPageSupport.java
------------------------------------------------------------------------------
    svn:eol-style = native



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