Skip to content

Commit 6b824d9

Browse files
committed
revised WebApplicationContext id determination; lazy logger initialization in ContextLoaderListener (SPR-5977)
1 parent 33bb81b commit 6b824d9

File tree

5 files changed

+69
-14
lines changed

5 files changed

+69
-14
lines changed

org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/FrameworkPortlet.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,20 +327,30 @@ protected ApplicationContext initPortletApplicationContext() throws BeansExcepti
327327
protected ApplicationContext createPortletApplicationContext(ApplicationContext parent)
328328
throws BeansException {
329329

330+
Class<?> contextClass = getContextClass();
330331
if (logger.isDebugEnabled()) {
331332
logger.debug("Portlet with name '" + getPortletName() +
332333
"' will try to create custom ApplicationContext context of class '" +
333-
getContextClass().getName() + "'" + ", using parent context [" + parent + "]");
334+
contextClass.getName() + "'" + ", using parent context [" + parent + "]");
334335
}
335-
if (!ConfigurablePortletApplicationContext.class.isAssignableFrom(getContextClass())) {
336+
if (!ConfigurablePortletApplicationContext.class.isAssignableFrom(contextClass)) {
336337
throw new ApplicationContextException("Fatal initialization error in portlet with name '" + getPortletName() +
337-
"': custom ApplicationContext class [" + getContextClass().getName() +
338+
"': custom ApplicationContext class [" + contextClass.getName() +
338339
"] is not of type ConfigurablePortletApplicationContext");
339340
}
340341

341342
ConfigurablePortletApplicationContext pac =
342-
(ConfigurablePortletApplicationContext) BeanUtils.instantiateClass(getContextClass());
343-
pac.setId(getPortletContext().getPortletContextName() + "." + getPortletName());
343+
(ConfigurablePortletApplicationContext) BeanUtils.instantiateClass(contextClass);
344+
345+
// Assign the best possible id value.
346+
String portletContextName = getPortletContext().getPortletContextName();
347+
if (portletContextName != null) {
348+
pac.setId(ConfigurablePortletApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + portletContextName + "." + getPortletName());
349+
}
350+
else {
351+
pac.setId(ConfigurablePortletApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + getPortletName());
352+
}
353+
344354
pac.setParent(parent);
345355
pac.setPortletContext(getPortletContext());
346356
pac.setPortletConfig(getPortletConfig());

org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/ConfigurablePortletApplicationContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
public interface ConfigurablePortletApplicationContext
4444
extends WebApplicationContext, ConfigurableApplicationContext {
4545

46+
/**
47+
* Prefix for ApplicationContext ids that refer to portlet name.
48+
*/
49+
String APPLICATION_CONTEXT_ID_PREFIX = WebApplicationContext.class.getName() + ":";
50+
4651
/**
4752
* Name of the PortletContext environment bean in the factory.
4853
* @see javax.portlet.PortletContext

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.security.Principal;
21+
import javax.servlet.ServletContext;
2122
import javax.servlet.ServletException;
2223
import javax.servlet.http.HttpServletRequest;
2324
import javax.servlet.http.HttpServletResponse;
@@ -402,21 +403,39 @@ protected WebApplicationContext findWebApplicationContext() {
402403
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
403404
throws BeansException {
404405

406+
Class<?> contextClass = getContextClass();
405407
if (this.logger.isDebugEnabled()) {
406408
this.logger.debug("Servlet with name '" + getServletName() +
407409
"' will try to create custom WebApplicationContext context of class '" +
408-
getContextClass().getName() + "'" + ", using parent context [" + parent + "]");
410+
contextClass.getName() + "'" + ", using parent context [" + parent + "]");
409411
}
410-
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) {
412+
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
411413
throw new ApplicationContextException(
412414
"Fatal initialization error in servlet with name '" + getServletName() +
413-
"': custom WebApplicationContext class [" + getContextClass().getName() +
415+
"': custom WebApplicationContext class [" + contextClass.getName() +
414416
"] is not of type ConfigurableWebApplicationContext");
415417
}
416418

417419
ConfigurableWebApplicationContext wac =
418-
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass());
419-
wac.setId(getServletContext().getServletContextName() + "." + getServletName());
420+
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
421+
422+
// Assign the best possible id value.
423+
ServletContext servletContext = getServletContext();
424+
if (servletContext.getMajorVersion() > 2 || servletContext.getMinorVersion() >= 5) {
425+
// Servlet 2.5's getContextPath available!
426+
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContext.getContextPath() + "/" + getServletName());
427+
}
428+
else {
429+
// Servlet <= 2.4: resort to name specified in web.xml, if any.
430+
String servletContextName = servletContext.getServletContextName();
431+
if (servletContextName != null) {
432+
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName + "." + getServletName());
433+
}
434+
else {
435+
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + getServletName());
436+
}
437+
}
438+
420439
wac.setParent(parent);
421440
wac.setServletContext(getServletContext());
422441
wac.setServletConfig(getServletConfig());

org.springframework.web/src/main/java/org/springframework/web/context/ConfigurableWebApplicationContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
*/
4040
public interface ConfigurableWebApplicationContext extends WebApplicationContext, ConfigurableApplicationContext {
4141

42+
/**
43+
* Prefix for ApplicationContext ids that refer to context path and/or servlet name.
44+
*/
45+
String APPLICATION_CONTEXT_ID_PREFIX = WebApplicationContext.class.getName() + ":";
46+
4247
/**
4348
* Name of the ServletConfig environment bean in the factory.
4449
* @see javax.servlet.ServletConfig

org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ public class ContextLoader {
141141
}
142142

143143

144-
private static final Log logger = LogFactory.getLog(ContextLoader.class);
145-
146144
/**
147145
* Map from (thread context) ClassLoader to WebApplicationContext.
148146
* Often just holding one reference - if the ContextLoader class is
@@ -183,6 +181,7 @@ public WebApplicationContext initWebApplicationContext(ServletContext servletCon
183181
"check whether you have multiple ContextLoader* definitions in your web.xml!");
184182
}
185183

184+
Log logger = LogFactory.getLog(ContextLoader.class);
186185
servletContext.log("Initializing Spring root WebApplicationContext");
187186
if (logger.isInfoEnabled()) {
188187
logger.info("Root WebApplicationContext: initialization started");
@@ -239,15 +238,31 @@ public WebApplicationContext initWebApplicationContext(ServletContext servletCon
239238
protected WebApplicationContext createWebApplicationContext(
240239
ServletContext servletContext, ApplicationContext parent) throws BeansException {
241240

242-
Class contextClass = determineContextClass(servletContext);
241+
Class<?> contextClass = determineContextClass(servletContext);
243242
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
244243
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
245244
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
246245
}
247246

248247
ConfigurableWebApplicationContext wac =
249248
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
250-
wac.setId(servletContext.getServletContextName());
249+
250+
// Assign the best possible id value.
251+
if (servletContext.getMajorVersion() > 2 || servletContext.getMinorVersion() >= 5) {
252+
// Servlet 2.5's getContextPath available!
253+
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContext.getContextPath());
254+
}
255+
else {
256+
// Servlet <= 2.4: resort to name specified in web.xml, if any.
257+
String servletContextName = servletContext.getServletContextName();
258+
if (servletContextName != null) {
259+
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName);
260+
}
261+
else {
262+
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX);
263+
}
264+
}
265+
251266
wac.setParent(parent);
252267
wac.setServletContext(servletContext);
253268
wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM));
@@ -334,6 +349,7 @@ protected ApplicationContext loadParentContext(ServletContext servletContext)
334349
if (parentContextKey != null) {
335350
// locatorFactorySelector may be null, indicating the default "classpath*:beanRefContext.xml"
336351
BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(locatorFactorySelector);
352+
Log logger = LogFactory.getLog(ContextLoader.class);
337353
if (logger.isDebugEnabled()) {
338354
logger.debug("Getting parent context definition: using parent context key of '" +
339355
parentContextKey + "' with BeanFactoryLocator");

0 commit comments

Comments
 (0)