package org.theonefx.wcframework.mvc;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.theonefx.wcframework.core.ConfigurationReader;
import org.theonefx.wcframework.core.DefaultConfigurationReader;
import org.theonefx.wcframework.core.ObjectRegistry;
import org.theonefx.wcframework.mvc.wcweb.ControllerRegister;
import org.theonefx.wcframework.utils.StringUtils;
/**
* @File : WebApplicationContextImpl.java
* @ClassName : WebApplicationContextImpl
* @Author : 陈曦
* @Date : Feb 3, 2012 10:04:11 AM
* @Version : v1.0
* @Description : web环境上下文实现
*/
public class WebApplicationContextImpl extends ApplicationContextSupport implements WebApplicationContext {
private final Log log = LogFactory.getLog(getClass());
private ServletContext servletContext;
private final ObjectRegistry registry = getBeanDefinitionRegistry();
private ConfigurationReader reader = null;
private final ControllerRegister controllerRegister;
public ServletContext getServletContext() {
return servletContext;
}
public WebApplicationContextImpl(ServletContext servletContext) {
this.servletContext = servletContext;
controllerRegister = new ControllerRegister(servletContext);
servletContext.setAttribute(APPCTX_ATTRNAME, this);
String path = servletContext.getInitParameter(CFG_FILE_LOCATION_PROPERTY);
if (StringUtils.isBlank(path)) {
path = DEFAULT_CFG_FILE;
}
log.info("WCF配置文件路径:" + path);
registry.registObject(CONTROLLER_REGISTRY_NAME, controllerRegister);
reader = new DefaultConfigurationReader(getDefaultBeanFactory(), path);
reader.doLoadConfiguration();
controllerRegister.refresh();
getDefaultBeanFactory().registerSingleton(SERVLET_BEAN_ID, servletContext);
}
public Object getObject(String name) {
return registry.getObject(name);
}
public static boolean hasWcfEnv(ServletContext servletContext) {
Object ctx = servletContext.getAttribute(APPCTX_ATTRNAME);
if (ctx != null && (ctx instanceof WebApplicationContext)) {
return true;
}
return false;
}
}