📄 contextloader.java
字号:
/* * Copyright 2002-2004 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package de.mindmatters.faces.spring.context;import java.io.IOException;import java.util.Properties;import javax.faces.FactoryFinder;import javax.faces.context.FacesContext;import javax.faces.webapp.FacesServlet;import javax.servlet.ServletContext;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.BeanUtils;import org.springframework.beans.factory.access.BeanFactoryLocator;import org.springframework.beans.factory.access.BeanFactoryReference;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextException;import org.springframework.context.access.ContextSingletonBeanFactoryLocator;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.support.PropertiesLoaderUtils;import org.springframework.util.Assert;import org.springframework.util.ClassUtils;import org.springframework.util.StringUtils;import org.springframework.web.context.ConfigurableWebApplicationContext;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import de.mindmatters.faces.context.FacesContextImpl;import de.mindmatters.faces.context.ServletExternalContextFake;/** * Performs the actual initialization work for the root application context. * Called by ContextLoaderListener and ContextLoaderServlet. * * <p> * For further informtion please have a look at the documentation of * {@link org.springframework.web.context.ContextLoader}. * </p> * * @author Andreas Kuhrwahl */public class ContextLoader { /** For logging. */ protected final Log logger = LogFactory.getLog(getClass()); /** * Config param for root faces application context implementation class * {@link FacesWebApplicationContext} to use: * <code>faces.spring.contextClass</code>. */ public static final String CONTEXT_CLASS_PARAM = "faces.spring.contextClass"; /** * Context initialization parameter name for a comma delimited list of * context-relative resource paths (which is loaded automatically if it * exists) containing <i>faces-spring-context</i> configuration * information: <code>faces.spring.CONTEXT_FILES</code>. */ public static final String CONTEXT_LOCATION_PARAM = "faces.spring.CONTEXT_FILES"; /** * Optional servlet context parameter used only when obtaining a parent * context using the default implementation of * {@link #loadParentContext(ServletContext servletContext)}. Specifies the * 'selector' used in the * {@link org.springframework.context.access.ContextSingletonBeanFactoryLocator#getInstance(String selector)} * method call used to obtain the BeanFactoryLocator instance from which the * parent context is obtained. * <p> * This will normally be set to <code>classpath*:beanRefContext.xml</code> * to match the default applied for the * {@link org.springframework.context.access.ContextSingletonBeanFactoryLocator#getInstance()} * method. */ public static final String LOCATOR_FACTORY_SELECTOR_PARAM = "faces.spring.context.locatorFactorySelector"; /** * Optional servlet context parameter used only when obtaining a parent * context using the default implementation of * {@link #loadParentContext(ServletContext servletContext)}. Specifies the * 'factoryKey' used in the * {@link org.springframework.beans.factory.access.BeanFactoryLocator * #useBeanFactory(String factoryKey)} method call used to obtain the parent * application context from the BeanFactoryLocator instance. */ public static final String LOCATOR_FACTORY_KEY_PARAM = "faces.spring.context.parentContextKey"; /** * Name of the class path resource (relative to the ContextLoader class) * that defines ContextLoader's default strategy names. */ private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties"; /** ContextLoader's default strategies. */ private static final Properties DEFAULT_STRATEGIES; static { try { ClassPathResource resource = new ClassPathResource( DEFAULT_STRATEGIES_PATH, ContextLoader.class); DEFAULT_STRATEGIES = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException ex) { throw new IllegalStateException( "Could not load 'ContextLoader.properties': " + ex.getMessage()); } } /** * Holds BeanFactoryReference when loading parent factory via * ContextSingletonBeanFactoryLocator. */ private BeanFactoryReference parentContextRef; /** * The WebApplicationContext instance that this loaded manages. */ private WebApplicationContext context; /** * Initialize web application context for the given servlet context, * regarding the <code>faces.spring.configClass</code> and * <code>faces.spring.CONFIG_FILES</code> context-params. * * @param servletContext * current servlet context * @return the new ConfigWebApplicationContext * @throws org.springframework.beans.BeansException * if the context couldn't be initialized * @see #CONTEXT_CLASS_PARAM * @see #CONTEXT_LOCATION_PARAM */ public final WebApplicationContext initWebApplicationContext( final ServletContext servletContext) { validateFacesConfiguration(servletContext); if (logger.isInfoEnabled()) { logger.info("WebApplicationContext: initialization started"); } servletContext.log("Loading WebApplicationContext"); FacesContext contextFake = new FacesContextImpl( new ServletExternalContextFake(servletContext)); try { ApplicationContext parent = loadParentContext(servletContext); this.context = createWebApplicationContext(servletContext, parent); servletContext .setAttribute( FacesWebApplicationContext.ROOT_FACES_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); if (logger.isDebugEnabled()) { logger .debug("Published WebApplicationContext [" + this.context + "] as ServletContext attribute with name [" + FacesWebApplicationContext.ROOT_FACES_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]"); } return this.context; } catch (RuntimeException ex) { logger.error("Context initialization failed", ex); servletContext .setAttribute( FacesWebApplicationContext.ROOT_FACES_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex); throw ex; } catch (Error err) { logger.error("Context initialization failed", err); servletContext .setAttribute( FacesWebApplicationContext.ROOT_FACES_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err); throw err; } finally { contextFake.release(); } } /** * Checks whether the underlying JSF implementation is configured properly
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -