From a429e230b632c40cf3167cbac54ea87b6ad87297 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 20 Oct 2009 16:53:24 +0000 Subject: [PATCH] revised version checks and exception signatures --- .../web/portlet/DispatcherPortlet.java | 4 +- .../web/portlet/FrameworkPortlet.java | 23 +++----- .../web/servlet/DispatcherServlet.java | 18 ++---- .../web/servlet/FrameworkServlet.java | 59 +++++++++---------- .../web/context/ContextLoader.java | 44 +++++--------- 5 files changed, 57 insertions(+), 91 deletions(-) diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/DispatcherPortlet.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/DispatcherPortlet.java index d784450b67..878053a0eb 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/DispatcherPortlet.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/DispatcherPortlet.java @@ -42,7 +42,6 @@ import javax.portlet.UnavailableException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; @@ -583,10 +582,9 @@ public class DispatcherPortlet extends FrameworkPortlet { * @param context the current Portlet ApplicationContext * @param clazz the strategy implementation class to instantiate * @return the fully configured strategy instance - * @throws BeansException if initialization failed * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory() */ - protected Object createDefaultStrategy(ApplicationContext context, Class clazz) throws BeansException { + protected Object createDefaultStrategy(ApplicationContext context, Class clazz) { return context.getAutowireCapableBeanFactory().createBean(clazz); } diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/FrameworkPortlet.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/FrameworkPortlet.java index 75cad59c52..3ddac18c10 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/FrameworkPortlet.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/FrameworkPortlet.java @@ -32,7 +32,6 @@ import javax.portlet.ResourceRequest; import javax.portlet.ResourceResponse; import org.springframework.beans.BeanUtils; -import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationListener; @@ -258,7 +257,7 @@ public abstract class FrameworkPortlet extends GenericPortletBean * have been set. Creates this portlet's ApplicationContext. */ @Override - protected final void initPortletBean() throws PortletException, BeansException { + protected final void initPortletBean() throws PortletException { getPortletContext().log("Initializing Spring FrameworkPortlet '" + getPortletName() + "'"); if (logger.isInfoEnabled()) { logger.info("FrameworkPortlet '" + getPortletName() + "': initialization started"); @@ -273,7 +272,7 @@ public abstract class FrameworkPortlet extends GenericPortletBean logger.error("Context initialization failed", ex); throw ex; } - catch (BeansException ex) { + catch (RuntimeException ex) { logger.error("Context initialization failed", ex); throw ex; } @@ -289,9 +288,8 @@ public abstract class FrameworkPortlet extends GenericPortletBean *

Delegates to {@link #createPortletApplicationContext} for actual creation. * Can be overridden in subclasses. * @return the ApplicationContext for this portlet - * @throws BeansException if the context couldn't be initialized */ - protected ApplicationContext initPortletApplicationContext() throws BeansException { + protected ApplicationContext initPortletApplicationContext() { ApplicationContext parent = PortletApplicationContextUtils.getWebApplicationContext(getPortletContext()); ApplicationContext pac = createPortletApplicationContext(parent); @@ -320,13 +318,10 @@ public abstract class FrameworkPortlet extends GenericPortletBean * ConfigurablePortletApplicationContext. Can be overridden in subclasses. * @param parent the parent ApplicationContext to use, or null if none * @return the Portlet ApplicationContext for this portlet - * @throws BeansException if the context couldn't be initialized * @see #setContextClass * @see org.springframework.web.portlet.context.XmlPortletApplicationContext */ - protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) - throws BeansException { - + protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) { Class contextClass = getContextClass(); if (logger.isDebugEnabled()) { logger.debug("Portlet with name '" + getPortletName() + @@ -338,7 +333,6 @@ public abstract class FrameworkPortlet extends GenericPortletBean "': custom ApplicationContext class [" + contextClass.getName() + "] is not of type ConfigurablePortletApplicationContext"); } - ConfigurablePortletApplicationContext pac = (ConfigurablePortletApplicationContext) BeanUtils.instantiateClass(contextClass); @@ -400,19 +394,17 @@ public abstract class FrameworkPortlet extends GenericPortletBean *

The default implementation is empty; subclasses may override this method * to perform any initialization they require. * @throws PortletException in case of an initialization exception - * @throws BeansException if thrown by ApplicationContext methods */ - protected void initFrameworkPortlet() throws PortletException, BeansException { + protected void initFrameworkPortlet() throws PortletException { } /** * Refresh this portlet's application context, as well as the * dependent state of the portlet. - * @throws BeansException in case of errors * @see #getPortletApplicationContext() * @see org.springframework.context.ConfigurableApplicationContext#refresh() */ - public void refresh() throws BeansException { + public void refresh() { ApplicationContext pac = getPortletApplicationContext(); if (!(pac instanceof ConfigurableApplicationContext)) { throw new IllegalStateException("Portlet ApplicationContext does not support refresh: " + pac); @@ -438,10 +430,9 @@ public abstract class FrameworkPortlet extends GenericPortletBean * Called after successful context refresh. *

This implementation is empty. * @param context the current Portlet ApplicationContext - * @throws BeansException in case of errors * @see #refresh() */ - protected void onRefresh(ApplicationContext context) throws BeansException { + protected void onRefresh(ApplicationContext context) { // For subclasses: do nothing by default. } diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/DispatcherServlet.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/DispatcherServlet.java index d94b9907f8..e870cd5525 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/DispatcherServlet.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/DispatcherServlet.java @@ -35,7 +35,6 @@ import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; @@ -322,7 +321,7 @@ public class DispatcherServlet extends FrameworkServlet { * This implementation calls {@link #initStrategies}. */ @Override - protected void onRefresh(ApplicationContext context) throws BeansException { + protected void onRefresh(ApplicationContext context) { initStrategies(context); } @@ -673,11 +672,10 @@ public class DispatcherServlet extends FrameworkServlet { * @param context the current WebApplicationContext * @param clazz the strategy implementation class to instantiate * @return the fully configured strategy instance - * @throws BeansException if initialization failed * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory() * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean */ - protected Object createDefaultStrategy(ApplicationContext context, Class clazz) throws BeansException { + protected Object createDefaultStrategy(ApplicationContext context, Class clazz) { return context.getAutowireCapableBeanFactory().createBean(clazz); } @@ -742,7 +740,7 @@ public class DispatcherServlet extends FrameworkServlet { int interceptorIndex = -1; try { - ModelAndView mv = null; + ModelAndView mv; boolean errorView = false; try { @@ -1032,13 +1030,11 @@ public class DispatcherServlet extends FrameworkServlet { * @throws Exception if there's a problem rendering the view */ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception { - // Determine locale for request and apply it to the response. Locale locale = this.localeResolver.resolveLocale(request); response.setLocale(locale); - View view = null; - + View view; if (mv.isReference()) { // We need to resolve the view name. view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request); @@ -1076,7 +1072,7 @@ public class DispatcherServlet extends FrameworkServlet { /** * Resolve the given view name into a View object (to be rendered). - *

Default implementations asks all ViewResolvers of this dispatcher. + *

The default implementations asks all ViewResolvers of this dispatcher. * Can be overridden for custom resolution strategies, potentially based on * specific model attributes or request parameters. * @param viewName the name of the view to resolve @@ -1088,9 +1084,7 @@ public class DispatcherServlet extends FrameworkServlet { * (typically in case of problems creating an actual View object) * @see ViewResolver#resolveViewName */ - protected View resolveViewName(String viewName, - Map model, - Locale locale, + protected View resolveViewName(String viewName, Map model, Locale locale, HttpServletRequest request) throws Exception { for (ViewResolver viewResolver : this.viewResolvers) { diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java index 820cd0d702..f4fefa8555 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java @@ -24,7 +24,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.BeanUtils; -import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationListener; @@ -297,7 +296,7 @@ public abstract class FrameworkServlet extends HttpServletBean * have been set. Creates this servlet's WebApplicationContext. */ @Override - protected final void initServletBean() throws ServletException, BeansException { + protected final void initServletBean() throws ServletException { getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'"); if (this.logger.isInfoEnabled()) { this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started"); @@ -312,7 +311,7 @@ public abstract class FrameworkServlet extends HttpServletBean this.logger.error("Context initialization failed", ex); throw ex; } - catch (BeansException ex) { + catch (RuntimeException ex) { this.logger.error("Context initialization failed", ex); throw ex; } @@ -329,11 +328,10 @@ public abstract class FrameworkServlet extends HttpServletBean *

Delegates to {@link #createWebApplicationContext} for actual creation * of the context. Can be overridden in subclasses. * @return the WebApplicationContext instance - * @throws BeansException if the context couldn't be initialized * @see #setContextClass * @see #setContextConfigLocation */ - protected WebApplicationContext initWebApplicationContext() throws BeansException { + protected WebApplicationContext initWebApplicationContext() { WebApplicationContext wac = findWebApplicationContext(); if (wac == null) { // No fixed context defined for this servlet - create a local one. @@ -397,12 +395,9 @@ public abstract class FrameworkServlet extends HttpServletBean * before returning the context instance. * @param parent the parent ApplicationContext to use, or null if none * @return the WebApplicationContext for this servlet - * @throws BeansException if the context couldn't be initialized * @see org.springframework.web.context.support.XmlWebApplicationContext */ - protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) - throws BeansException { - + protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { Class contextClass = getContextClass(); if (this.logger.isDebugEnabled()) { this.logger.debug("Servlet with name '" + getServletName() + @@ -415,26 +410,27 @@ public abstract class FrameworkServlet extends HttpServletBean "': custom WebApplicationContext class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext"); } - ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); // Assign the best possible id value. - ServletContext servletContext = getServletContext(); - if (servletContext.getMajorVersion() > 2 || servletContext.getMinorVersion() >= 5) { - // Servlet 2.5's getContextPath available! - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContext.getContextPath() + "/" + getServletName()); - } - else { + ServletContext sc = getServletContext(); + if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) { // Servlet <= 2.4: resort to name specified in web.xml, if any. - String servletContextName = servletContext.getServletContextName(); + String servletContextName = sc.getServletContextName(); if (servletContextName != null) { - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName + "." + getServletName()); + 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 + sc.getContextPath() + + "/" + getServletName()); + } wac.setParent(parent); wac.setServletContext(getServletContext()); @@ -485,19 +481,17 @@ public abstract class FrameworkServlet extends HttpServletBean * the WebApplicationContext has been loaded. The default implementation is empty; * subclasses may override this method to perform any initialization they require. * @throws ServletException in case of an initialization exception - * @throws BeansException if thrown by ApplicationContext methods */ - protected void initFrameworkServlet() throws ServletException, BeansException { + protected void initFrameworkServlet() throws ServletException { } /** * Refresh this servlet's application context, as well as the * dependent state of the servlet. - * @throws BeansException in case of errors * @see #getWebApplicationContext() * @see org.springframework.context.ConfigurableApplicationContext#refresh() */ - public void refresh() throws BeansException { + public void refresh() { WebApplicationContext wac = getWebApplicationContext(); if (!(wac instanceof ConfigurableApplicationContext)) { throw new IllegalStateException("WebApplicationContext does not support refresh: " + wac); @@ -523,10 +517,9 @@ public abstract class FrameworkServlet extends HttpServletBean * Called after successful context refresh. *

This implementation is empty. * @param context the current WebApplicationContext - * @throws BeansException in case of errors * @see #refresh() */ - protected void onRefresh(ApplicationContext context) throws BeansException { + protected void onRefresh(ApplicationContext context) { // For subclasses: do nothing by default. } @@ -540,7 +533,7 @@ public abstract class FrameworkServlet extends HttpServletBean */ @Override protected final void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws ServletException, IOException { processRequest(request, response); } @@ -551,7 +544,7 @@ public abstract class FrameworkServlet extends HttpServletBean */ @Override protected final void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws ServletException, IOException { processRequest(request, response); } @@ -562,7 +555,7 @@ public abstract class FrameworkServlet extends HttpServletBean */ @Override protected final void doPut(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws ServletException, IOException { processRequest(request, response); } @@ -573,7 +566,7 @@ public abstract class FrameworkServlet extends HttpServletBean */ @Override protected final void doDelete(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws ServletException, IOException { processRequest(request, response); } @@ -584,7 +577,9 @@ public abstract class FrameworkServlet extends HttpServletBean * @see #doService */ @Override - protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doOptions(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + super.doOptions(request, response); if (this.dispatchOptionsRequest) { processRequest(request, response); @@ -597,7 +592,9 @@ public abstract class FrameworkServlet extends HttpServletBean * @see #doService */ @Override - protected void doTrace(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doTrace(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + super.doTrace(request, response); if (this.dispatchTraceRequest) { processRequest(request, response); @@ -715,7 +712,7 @@ public abstract class FrameworkServlet extends HttpServletBean * @see javax.servlet.http.HttpServlet#doPost */ protected abstract void doService(HttpServletRequest request, HttpServletResponse response) - throws Exception; + throws Exception; /** diff --git a/org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java b/org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java index d1336917cc..22345a31f0 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java +++ b/org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java @@ -26,7 +26,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeanUtils; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.access.BeanFactoryLocator; import org.springframework.beans.factory.access.BeanFactoryReference; import org.springframework.context.ApplicationContext; @@ -167,14 +166,10 @@ public class ContextLoader { * "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params. * @param servletContext current servlet context * @return the new WebApplicationContext - * @throws IllegalStateException if there is already a root application context present - * @throws BeansException if the context failed to initialize * @see #CONTEXT_CLASS_PARAM * @see #CONFIG_LOCATION_PARAM */ - public WebApplicationContext initWebApplicationContext(ServletContext servletContext) - throws IllegalStateException, BeansException { - + public WebApplicationContext initWebApplicationContext(ServletContext servletContext) { if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) { throw new IllegalStateException( "Cannot initialize context because there is already a root application context present - " + @@ -229,32 +224,24 @@ public class ContextLoader { * Can be overridden in subclasses. *

In addition, {@link #customizeContext} gets called prior to refreshing the * context, allowing subclasses to perform custom modifications to the context. - * @param servletContext current servlet context + * @param sc current servlet context * @param parent the parent ApplicationContext to use, or null if none * @return the root WebApplicationContext - * @throws BeansException if the context couldn't be initialized * @see ConfigurableWebApplicationContext */ - protected WebApplicationContext createWebApplicationContext( - ServletContext servletContext, ApplicationContext parent) throws BeansException { - - Class contextClass = determineContextClass(servletContext); + protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) { + Class contextClass = determineContextClass(sc); if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]"); } - ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); // Assign the best possible id value. - if (servletContext.getMajorVersion() > 2 || servletContext.getMinorVersion() >= 5) { - // Servlet 2.5's getContextPath available! - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContext.getContextPath()); - } - else { + if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) { // Servlet <= 2.4: resort to name specified in web.xml, if any. - String servletContextName = servletContext.getServletContextName(); + String servletContextName = sc.getServletContextName(); if (servletContextName != null) { wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName); } @@ -262,13 +249,16 @@ public class ContextLoader { wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX); } } + else { + // Servlet 2.5's getContextPath available! + wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + sc.getContextPath()); + } wac.setParent(parent); - wac.setServletContext(servletContext); - wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM)); - customizeContext(servletContext, wac); + wac.setServletContext(sc); + wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM)); + customizeContext(sc, wac); wac.refresh(); - return wac; } @@ -277,11 +267,10 @@ public class ContextLoader { * default XmlWebApplicationContext or a custom context class if specified. * @param servletContext current servlet context * @return the WebApplicationContext implementation class to use - * @throws ApplicationContextException if the context class couldn't be loaded * @see #CONTEXT_CLASS_PARAM * @see org.springframework.web.context.support.XmlWebApplicationContext */ - protected Class determineContextClass(ServletContext servletContext) throws ApplicationContextException { + protected Class determineContextClass(ServletContext servletContext) { String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM); if (contextClassName != null) { try { @@ -336,12 +325,9 @@ public class ContextLoader { * which also use the same configuration parameters. * @param servletContext current servlet context * @return the parent application context, or null if none - * @throws BeansException if the context couldn't be initialized * @see org.springframework.context.access.ContextSingletonBeanFactoryLocator */ - protected ApplicationContext loadParentContext(ServletContext servletContext) - throws BeansException { - + protected ApplicationContext loadParentContext(ServletContext servletContext) { ApplicationContext parentContext = null; String locatorFactorySelector = servletContext.getInitParameter(LOCATOR_FACTORY_SELECTOR_PARAM); String parentContextKey = servletContext.getInitParameter(LOCATOR_FACTORY_KEY_PARAM);