This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 1825ed0c8ff1a368010d6d293568ea71e46dba35 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 2672861433..7dbc88bd73 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); @@ -911,12 +906,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); @@ -1041,11 +1032,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) { @@ -1075,12 +1062,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; } @@ -1131,11 +1113,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 || @@ -1201,12 +1179,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( @@ -1311,12 +1285,7 @@ public class ApplicationContext implements ServletContext { @Override 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); } @@ -1329,12 +1298,7 @@ public class ApplicationContext implements ServletContext { @Override 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); } @@ -1347,13 +1311,15 @@ public class ApplicationContext implements ServletContext { @Override 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