This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit ed6f1a616c0d80c292a883cb3b361a49863d728a
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Jan 18 20:02:14 2023 +0000

    Refactor to reduce code duplication
---
 .../apache/catalina/core/ApplicationContext.java   | 72 ++++++----------------
 1 file changed, 19 insertions(+), 53 deletions(-)

diff --git a/java/org/apache/catalina/core/ApplicationContext.java 
b/java/org/apache/catalina/core/ApplicationContext.java
index ab125aa829..6d5615ddd1 100644
--- a/java/org/apache/catalina/core/ApplicationContext.java
+++ b/java/org/apache/catalina/core/ApplicationContext.java
@@ -93,7 +93,6 @@ public class ApplicationContext implements ServletContext {
 
     protected static final boolean GET_RESOURCE_REQUIRE_SLASH;
 
-
     static {
         STRICT_SERVLET_COMPLIANCE = Globals.STRICT_SERVLET_COMPLIANCE;
 
@@ -106,8 +105,8 @@ public class ApplicationContext implements ServletContext {
         }
     }
 
-    // ----------------------------------------------------------- Constructors
 
+    // ----------------------------------------------------------- Constructors
 
     /**
      * Construct a new instance of this class, associated with the specified
@@ -790,12 +789,8 @@ public class ApplicationContext implements ServletContext {
                     "applicationContext.invalidFilterName", filterName));
         }
 
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            //TODO Spec breaking enhancement to ignore this restriction
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.addFilter.ise",
-                            getContextPath()));
-        }
+        // TODO Spec breaking enhancement to ignore this restriction
+        checkState("applicationContext.addFilter.ise");
 
         FilterDef filterDef = context.findFilterDef(filterName);
 
@@ -910,12 +905,8 @@ public class ApplicationContext implements ServletContext {
                     "applicationContext.invalidServletName", servletName));
         }
 
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            //TODO Spec breaking enhancement to ignore this restriction
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.addServlet.ise",
-                            getContextPath()));
-        }
+        // TODO Spec breaking enhancement to ignore this restriction
+        checkState("applicationContext.addServlet.ise");
 
         Wrapper wrapper = (Wrapper) context.findChild(servletName);
 
@@ -1040,11 +1031,7 @@ public class ApplicationContext implements 
ServletContext {
     @Override
     public void setSessionTrackingModes(Set<SessionTrackingMode> 
sessionTrackingModes) {
 
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.setSessionTracking.ise",
-                            getContextPath()));
-        }
+        checkState("applicationContext.setSessionTracking.ise");
 
         // Check that only supported tracking modes have been requested
         for (SessionTrackingMode sessionTrackingMode : sessionTrackingModes) {
@@ -1074,12 +1061,7 @@ public class ApplicationContext implements 
ServletContext {
         if (name == null) {
             throw new 
NullPointerException(sm.getString("applicationContext.setAttribute.namenull"));
         }
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.setInitParam.ise",
-                            getContextPath()));
-        }
-
+        checkState("applicationContext.setInitParam.ise");
         return parameters.putIfAbsent(name, value) == null;
     }
 
@@ -1130,11 +1112,7 @@ public class ApplicationContext implements 
ServletContext {
 
     @Override
     public <T extends EventListener> void addListener(T t) {
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.addListener.ise",
-                            getContextPath()));
-        }
+        checkState("applicationContext.addListener.ise");
 
         boolean match = false;
         if (t instanceof ServletContextAttributeListener ||
@@ -1200,12 +1178,8 @@ public class ApplicationContext implements 
ServletContext {
     @Override
     public void declareRoles(String... roleNames) {
 
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            //TODO Spec breaking enhancement to ignore this restriction
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.addRole.ise",
-                            getContextPath()));
-        }
+        // TODO Spec breaking enhancement to ignore this restriction
+        checkState("applicationContext.addRole.ise");
 
         if (roleNames == null) {
             throw new IllegalArgumentException(
@@ -1308,12 +1282,7 @@ public class ApplicationContext implements 
ServletContext {
 
 
     public void setSessionTimeout(int sessionTimeout) {
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.setSessionTimeout.ise",
-                            getContextPath()));
-        }
-
+        checkState("applicationContext.setSessionTimeout.ise");
         context.setSessionTimeout(sessionTimeout);
     }
 
@@ -1324,12 +1293,7 @@ public class ApplicationContext implements 
ServletContext {
 
 
     public void setRequestCharacterEncoding(String encoding) {
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.setRequestEncoding.ise",
-                            getContextPath()));
-        }
-
+        checkState("applicationContext.setRequestEncoding.ise");
         context.setRequestCharacterEncoding(encoding);
     }
 
@@ -1340,13 +1304,15 @@ public class ApplicationContext implements 
ServletContext {
 
 
     public void setResponseCharacterEncoding(String encoding) {
+        checkState("applicationContext.setResponseEncoding.ise");
+        context.setResponseCharacterEncoding(encoding);
+    }
+
+
+    private void checkState(String messageKey) {
         if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.setResponseEncoding.ise",
-                            getContextPath()));
+            throw new 
IllegalStateException(sm.getString(messageKey,getContextPath()));
         }
-
-        context.setResponseCharacterEncoding(encoding);
     }
 
 


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

Reply via email to