From 0de112198efb487d3517f3358323e586a2444af9 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 20 Nov 2013 14:50:32 +0100 Subject: [PATCH] Rely on presence of Servlet 2.5's ServletContext.getContextPath() --- .../web/context/ContextLoader.java | 18 ++++----------- ...tractRefreshableWebApplicationContext.java | 12 +--------- .../support/GenericWebApplicationContext.java | 13 +---------- .../web/servlet/FrameworkServlet.java | 23 +++---------------- 4 files changed, 10 insertions(+), 56 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/context/ContextLoader.java b/spring-web/src/main/java/org/springframework/web/context/ContextLoader.java index b7dbb399b63..9f45686fefd 100644 --- a/spring-web/src/main/java/org/springframework/web/context/ContextLoader.java +++ b/spring-web/src/main/java/org/springframework/web/context/ContextLoader.java @@ -18,7 +18,6 @@ package org.springframework.web.context; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Properties; @@ -368,15 +367,8 @@ public class ContextLoader { } else { // Generate default id... - if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) { - // Servlet <= 2.4: resort to name specified in web.xml, if any. - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + - ObjectUtils.getDisplayString(sc.getServletContextName())); - } - else { - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + - ObjectUtils.getDisplayString(sc.getContextPath())); - } + wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + + ObjectUtils.getDisplayString(sc.getContextPath())); } } @@ -470,7 +462,7 @@ public class ContextLoader { protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) { List>> initializerClasses = determineContextInitializerClasses(servletContext); - if (initializerClasses.size() == 0) { + if (initializerClasses.isEmpty()) { // no ApplicationContextInitializers have been declared -> nothing to do return; } @@ -494,10 +486,10 @@ public class ContextLoader { ConfigurableEnvironment env = applicationContext.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { - ((ConfigurableWebEnvironment)env).initPropertySources(servletContext, null); + ((ConfigurableWebEnvironment) env).initPropertySources(servletContext, null); } - Collections.sort(initializerInstances, new AnnotationAwareOrderComparator()); + AnnotationAwareOrderComparator.sort(initializerInstances); for (ApplicationContextInitializer initializer : initializerInstances) { initializer.initialize(applicationContext); } diff --git a/spring-web/src/main/java/org/springframework/web/context/support/AbstractRefreshableWebApplicationContext.java b/spring-web/src/main/java/org/springframework/web/context/support/AbstractRefreshableWebApplicationContext.java index c25e2ef8346..54552f63184 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/AbstractRefreshableWebApplicationContext.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/AbstractRefreshableWebApplicationContext.java @@ -140,17 +140,7 @@ public abstract class AbstractRefreshableWebApplicationContext extends AbstractR @Override public String getApplicationName() { - if (this.servletContext == null) { - return ""; - } - if (this.servletContext.getMajorVersion() == 2 && this.servletContext.getMinorVersion() < 5) { - String name = this.servletContext.getServletContextName(); - return (name != null ? name : ""); - } - else { - // Servlet 2.5 available - return this.servletContext.getContextPath(); - } + return (this.servletContext != null ? this.servletContext.getContextPath() : ""); } /** diff --git a/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java b/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java index 40dbd0d3f2b..5a39cdeba33 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java @@ -28,7 +28,6 @@ import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.ui.context.Theme; import org.springframework.ui.context.ThemeSource; import org.springframework.ui.context.support.UiApplicationContextUtils; -import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.web.context.ConfigurableWebApplicationContext; @@ -128,17 +127,7 @@ public class GenericWebApplicationContext extends GenericApplicationContext @Override public String getApplicationName() { - if (this.servletContext == null) { - return ""; - } - if (this.servletContext.getMajorVersion() == 2 && this.servletContext.getMinorVersion() < 5) { - String name = this.servletContext.getServletContextName(); - return (name != null ? name : ""); - } - else { - // Servlet 2.5 available - return this.servletContext.getContextPath(); - } + return (this.servletContext != null ? this.servletContext.getContextPath() : ""); } /** diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java index d7036bd8a79..d08d94791d8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java @@ -19,9 +19,7 @@ package org.springframework.web.servlet; import java.io.IOException; import java.security.Principal; import java.util.ArrayList; -import java.util.Collections; import java.util.concurrent.Callable; - import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -617,23 +615,8 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic } else { // Generate default id... - ServletContext sc = getServletContext(); - if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) { - // Servlet <= 2.4: resort to name specified in web.xml, if any. - String servletContextName = sc.getServletContextName(); - if (servletContextName != null) { - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName + - "." + getServletName()); - } - else { - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + getServletName()); - } - } - else { - // Servlet 2.5's getContextPath available! - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + - ObjectUtils.getDisplayString(sc.getContextPath()) + "/" + getServletName()); - } + wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + + ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName()); } } @@ -703,7 +686,7 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic this.contextInitializers.add(initializer); } } - Collections.sort(this.contextInitializers, new AnnotationAwareOrderComparator()); + AnnotationAwareOrderComparator.sort(this.contextInitializers); for (ApplicationContextInitializer initializer : this.contextInitializers) { initializer.initialize(wac); }