From 179dad8c249dc7debb55aa181710af8e437d9807 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 25 Jul 2011 11:33:08 +0000 Subject: [PATCH] defensive checking that a provided class actually implements WebApplicationInitializer (SPR-8557); avoid early initialization of Commons Logging (using ServletContext logging instead) --- .../SpringServletContainerInitializer.java | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/org.springframework.web/src/main/java/org/springframework/web/SpringServletContainerInitializer.java b/org.springframework.web/src/main/java/org/springframework/web/SpringServletContainerInitializer.java index 0b70116458..6a68645ec3 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/SpringServletContainerInitializer.java +++ b/org.springframework.web/src/main/java/org/springframework/web/SpringServletContainerInitializer.java @@ -16,21 +16,17 @@ package org.springframework.web; -import static org.springframework.beans.BeanUtils.instantiateClass; - import java.lang.reflect.Modifier; -import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedList; +import java.util.List; import java.util.ServiceLoader; import java.util.Set; - import javax.servlet.ServletContainerInitializer; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.HandlesTypes; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.springframework.core.annotation.AnnotationAwareOrderComparator; /** @@ -94,6 +90,7 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator; * recommendations.

* * @author Chris Beams + * @author Juergen Hoeller * @since 3.1 * @see #onStartup(Set, ServletContext) * @see WebApplicationInitializer @@ -101,8 +98,6 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator; @HandlesTypes(WebApplicationInitializer.class) public class SpringServletContainerInitializer implements ServletContainerInitializer { - private static final Log logger = LogFactory.getLog(SpringServletContainerInitializer.class); - /** * Delegate the {@code ServletContext} to any {@link WebApplicationInitializer} * implementations present on the application classpath. @@ -129,39 +124,43 @@ public class SpringServletContainerInitializer implements ServletContainerInitia * or any other Servlet API componentry such as filters. * * @param webAppInitializerClasses all implementations of - * {@link WebApplicationInitializer} found on the application classpath. + * {@link WebApplicationInitializer} found on the application classpath * @param servletContext the servlet context to be initialized * @see WebApplicationInitializer#onStartup(ServletContext) * @see AnnotationAwareOrderComparator */ - public void onStartup(Set> webAppInitializerClasses, - ServletContext servletContext) throws ServletException { + public void onStartup(Set> webAppInitializerClasses, ServletContext servletContext) + throws ServletException { - ArrayList initializers = new ArrayList(); + List initializers = new LinkedList(); if (webAppInitializerClasses != null) { for (Class waiClass : webAppInitializerClasses) { - if (!Modifier.isAbstract(waiClass.getModifiers())) { - // the class can be instantiated -> add it - initializers.add(instantiateClass(waiClass, WebApplicationInitializer.class)); + // Be defensive: Some servlet containers provide us with invalid classes, + // no matter what @HandlesTypes says... + if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) && + WebApplicationInitializer.class.isAssignableFrom(waiClass)) { + try { + initializers.add((WebApplicationInitializer) waiClass.newInstance()); + } + catch (Throwable ex) { + throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex); + } } } } if (initializers.isEmpty()) { - logger.info("Detected no WebApplicationInitializer types on the classpath: exiting."); + servletContext.log("No Spring WebApplicationInitializer types detected on classpath"); return; } Collections.sort(initializers, new AnnotationAwareOrderComparator()); - - logger.info("Delegating ServletContext to the following " + - "WebApplicationInitializer instances: " + initializers); + servletContext.log("Spring WebApplicationInitializers detected on classpath: " + initializers); for (WebApplicationInitializer initializer : initializers) { initializer.onStartup(servletContext); } - } }