/** * Find the root {@link WebApplicationContext} for this web app, typically * loaded via {@link org.springframework.web.context.ContextLoaderListener}. * <p>Will rethrow an exception that happened on root context startup, * to differentiate between a failed context startup and no context at all. * @param pc PortletContext to find the web application context for * @return the root WebApplicationContext for this web app * (typed to ApplicationContext to avoid a Servlet API dependency; can usually * be casted to WebApplicationContext, but there shouldn't be a need to) * @throws IllegalStateException if the root WebApplicationContext could not be found * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE */ public static ApplicationContext getRequiredWebApplicationContext(PortletContext pc) throws IllegalStateException { ApplicationContext wac = getWebApplicationContext(pc); if (wac == null) { throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?"); } return wac; }
/** * Find the root WebApplicationContext for this portlet application, which is * typically loaded via ContextLoaderListener or ContextLoaderServlet. * <p>Will rethrow an exception that happened on root context startup, * to differentiate between a failed context startup and no context at all. * @param pc PortletContext to find the web application context for * @return the root WebApplicationContext for this web app * (typed to ApplicationContext to avoid a Servlet API dependency; can usually * be casted to WebApplicationContext, but there shouldn't be a need to) * @throws IllegalStateException if the root WebApplicationContext could not be found * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE */ public static ApplicationContext getRequiredWebApplicationContext(PortletContext pc) throws IllegalStateException { ApplicationContext wac = getWebApplicationContext(pc); if (wac == null) { throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?"); } return wac; }
/** * Initialize and publish the Portlet ApplicationContext for this portlet. * <p>Delegates to {@link #createPortletApplicationContext} for actual creation. * Can be overridden in subclasses. * @return the ApplicationContext for this portlet */ protected ApplicationContext initPortletApplicationContext() { ApplicationContext parent = PortletApplicationContextUtils.getWebApplicationContext(getPortletContext()); ApplicationContext pac = createPortletApplicationContext(parent); if (!this.refreshEventReceived) { // Apparently not a ConfigurableApplicationContext with refresh support: // triggering initial onRefresh manually here. onRefresh(pac); } if (this.publishContext) { // publish the context as a portlet context attribute String attName = getPortletContextAttributeName(); getPortletContext().setAttribute(attName, pac); if (logger.isDebugEnabled()) { logger.debug("Published ApplicationContext of portlet '" + getPortletName() + "' as PortletContext attribute with name [" + attName + "]"); } } return pac; }
/** * Initialize and publish the Portlet ApplicationContext for this portlet. * <p>Delegates to {@link #createPortletApplicationContext} for actual creation. * Can be overridden in subclasses. * @return the ApplicationContext for this portlet */ protected ApplicationContext initPortletApplicationContext() { ApplicationContext parent = PortletApplicationContextUtils.getWebApplicationContext(getPortletContext()); ApplicationContext pac = createPortletApplicationContext(parent); if (!this.refreshEventReceived) { // Apparently not a ConfigurableApplicationContext with refresh support: // triggering initial onRefresh manually here. onRefresh(pac); } if (this.publishContext) { // publish the context as a portlet context attribute String attName = getPortletContextAttributeName(); getPortletContext().setAttribute(attName, pac); if (logger.isDebugEnabled()) { logger.debug("Published ApplicationContext of portlet '" + getPortletName() + "' as PortletContext attribute with name [" + attName + "]"); } } return pac; }
@Override public void init(PortletConfig config) throws PortletException { applicationContext = PortletApplicationContextUtils.getWebApplicationContext(config.getPortletContext()); if (config.getInitParameter(CONTEXT_CONFIG_LOCATION_PARAMETER) != null) { XmlPortletApplicationContext context = new XmlPortletApplicationContext(); context.setParent(applicationContext); context.setConfigLocation(config.getInitParameter(CONTEXT_CONFIG_LOCATION_PARAMETER)); context.setPortletConfig(config); context.setPortletContext(config.getPortletContext()); context.refresh(); applicationContext = context; } if (config.getInitParameter(SYSTEM_MESSAGES_BEAN_NAME_PARAMETER) != null) { systemMessagesBeanName = config.getInitParameter(SYSTEM_MESSAGES_BEAN_NAME_PARAMETER); logger.debug("found SYSTEM_MESSAGES_BEAN_NAME_PARAMETER: {}", systemMessagesBeanName); } if (SpringApplicationContext.getApplicationContext() == null) { SpringApplicationContext.setApplicationContext(applicationContext); } super.init(config); }