Rely on presence of Servlet 2.5's ServletContext.getContextPath()

This commit is contained in:
Juergen Hoeller 2013-11-20 14:50:32 +01:00
parent 2ea5360b82
commit 0de112198e
4 changed files with 10 additions and 56 deletions

View File

@ -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<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> 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<ConfigurableApplicationContext> initializer : initializerInstances) {
initializer.initialize(applicationContext);
}

View File

@ -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() : "");
}
/**

View File

@ -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() : "");
}
/**

View File

@ -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<ConfigurableApplicationContext> initializer : this.contextInitializers) {
initializer.initialize(wac);
}