/** * 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; }
/** * Set the bean factory from a portlet context. * @param portletContext */ public static void initBeanFactory(final PortletContext portletContext) { CONTEXT_HOLDER.set( PortletApplicationContextUtils.getRequiredWebApplicationContext(portletContext)); }
/** * {@inheritDoc} * <p>Replace {@code Portlet}- and {@code Servlet}-related property sources. */ @Override protected void initPropertySources() { PortletApplicationContextUtils.initPortletPropertySources(getEnvironment().getPropertySources(), this.servletContext, this.portletContext, this.portletConfig); }
/** * Register request/session scopes, a {@link PortletContextAwareProcessor}, etc. */ @Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext)); beanFactory.addBeanPostProcessor(new PortletContextAwareProcessor(this.portletContext, this.portletConfig)); beanFactory.ignoreDependencyInterface(PortletContextAware.class); beanFactory.ignoreDependencyInterface(PortletConfigAware.class); PortletApplicationContextUtils.registerPortletApplicationScopes(beanFactory, this.portletContext); PortletApplicationContextUtils.registerEnvironmentBeans( beanFactory, this.servletContext, this.portletContext, this.portletConfig); }
/** * Register request/session scopes, a {@link PortletContextAwareProcessor}, etc. */ @Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext)); beanFactory.addBeanPostProcessor(new PortletContextAwareProcessor(this.portletContext, this.portletConfig)); beanFactory.ignoreDependencyInterface(PortletContextAware.class); beanFactory.ignoreDependencyInterface(PortletConfigAware.class); PortletApplicationContextUtils.registerPortletApplicationScopes(beanFactory, this.portletContext); PortletApplicationContextUtils.registerEnvironmentBeans( beanFactory, this.servletContext, this.portletContext, this.portletConfig); }
/** * Register request/session scopes, a {@link PortletContextAwareProcessor}, etc. */ @Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext)); beanFactory.addBeanPostProcessor(new PortletContextAwareProcessor(this.portletContext, this.portletConfig)); beanFactory.ignoreDependencyInterface(ServletContextAware.class); beanFactory.ignoreDependencyInterface(PortletContextAware.class); beanFactory.ignoreDependencyInterface(PortletConfigAware.class); PortletApplicationContextUtils.registerPortletApplicationScopes(beanFactory, this.portletContext); PortletApplicationContextUtils.registerEnvironmentBeans( beanFactory, this.servletContext, this.portletContext, this.portletConfig); }
/** * 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; }
/** * Replace any {@linkplain * org.springframework.core.env.PropertySource.StubPropertySource stub property source} * instances acting as placeholders with real portlet context/config property sources * using the given parameters. * @param servletContext the {@link ServletContext} (may be {@code null}) * @param portletContext the {@link PortletContext} (may not be {@code null}) * @param portletConfig the {@link PortletConfig} ({@code null} if not available) * @see org.springframework.web.portlet.context.PortletApplicationContextUtils#initPortletPropertySources( * org.springframework.core.env.MutablePropertySources, ServletContext, PortletContext, PortletConfig) */ public void initPropertySources(ServletContext servletContext, PortletContext portletContext, PortletConfig portletConfig) { PortletApplicationContextUtils.initPortletPropertySources(getPropertySources(), servletContext, portletContext, portletConfig); }
@Override public void sessionInit(SessionInitEvent event) throws ServiceException { try { ApplicationContext context = PortletApplicationContextUtils .getRequiredWebApplicationContext(getPortletContext()); SpringAwareUIProvider uiProvider = new SpringAwareUIProvider(context); event.getSession().addUIProvider(uiProvider); } catch (IllegalStateException e) { throw new ServiceException(e); } } });
/** * Register request/session scopes, a {@link PortletContextAwareProcessor}, etc. */ @Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext)); beanFactory.addBeanPostProcessor(new PortletContextAwareProcessor(this.portletContext, this.portletConfig)); beanFactory.ignoreDependencyInterface(ServletContextAware.class); beanFactory.ignoreDependencyInterface(PortletContextAware.class); beanFactory.ignoreDependencyInterface(PortletConfigAware.class); PortletApplicationContextUtils.registerPortletApplicationScopes(beanFactory, this.portletContext); PortletApplicationContextUtils.registerEnvironmentBeans( beanFactory, this.servletContext, this.portletContext, this.portletConfig); }
/** * 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; }
/** * {@inheritDoc} * <p>Replace {@code Portlet}- and {@code Servlet}-related property sources. */ @Override protected void initPropertySources() { super.initPropertySources(); PortletApplicationContextUtils.initPortletPropertySources( this.getEnvironment().getPropertySources(), this.servletContext, this.portletContext, this.portletConfig); }
/** * 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); }