/* =================================================================== * AppContextInitializer.java * * Created Feb 28, 2005 8:54:00 PM * * Copyright (c) 2005 Matt Magoffin (spamsqr@msqr.us) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA * =================================================================== * $Id: AppContextInitializer.java 74 2009-05-15 04:44:37Z msqr $ * =================================================================== */ package magoffin.matt.ieat.test; import java.io.IOException; import java.util.Iterator; import java.util.Map; import java.util.Properties; import magoffin.matt.ieat.biz.DomainObjectFactory; import magoffin.matt.ieat.web.WebConstants; import magoffin.matt.util.StringUtil; import magoffin.matt.xweb.XAppContext; import magoffin.matt.xweb.XwebParameter; import magoffin.matt.xweb.util.AppContextSupport; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.core.io.Resource; /** * Class to initialize the iEat AppContext data. * *

The configurable properties of this class are:

* *
*
domainObjectFactory
*
The DomainObjectFactory to use for creating domain objects.
* *
environment
*
The property file resource containing all application properties.
*
* * @author Matt Magoffin (spamsqr@msqr.us) * @version $Revision: 74 $ $Date: 2009-05-15 16:44:37 +1200 (Fri, 15 May 2009) $ */ public class AppContextInitializer implements ApplicationContextAware { private final Log logger = LogFactory.getLog(getClass()); /** DomainObjectFactory for creating our domain objects. */ private DomainObjectFactory domainObjectFactory = null; /** The environment property file to make available via the AppContext object. */ private Resource environment = null; /* Internal fields. */ private AppContextSupport appContextSupport = null; /* (non-Javadoc) * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) */ @SuppressWarnings("unchecked") public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { XAppContext appCtx = domainObjectFactory.newXAppContextInstance(); Properties props = new Properties(); try{ props.load(environment.getInputStream()); } catch ( IOException e ) { throw new RuntimeException("Unable to load environment properties",e); } for ( Iterator itr = props.entrySet().iterator(); itr.hasNext(); ) { Map.Entry me = (Map.Entry)itr.next(); String value = StringUtil.trimToNull((String)me.getValue()); if ( value != null ) { XwebParameter param = domainObjectFactory.newXwebParameterInstance(); param.setKey(me.getKey().toString()); param.setValue(value); appCtx.getParam().add(param); } } // cache the configuration in the servlet context AppContextSupport appCtxSupport = new AppContextSupport(appCtx); if ( logger.isDebugEnabled() ) { logger.debug("Storing AppContext [" +appCtx +"] with " +props.size() +" properties in the servlet context at [" +WebConstants.APP_KEY_APP_CONTEXT +"]"); } this.appContextSupport = appCtxSupport; } /** * @return Returns the appContextSupport. */ public AppContextSupport getAppContextSupport() { return appContextSupport; } /** * @return the domainObjectFactory */ public DomainObjectFactory getDomainObjectFactory() { return domainObjectFactory; } /** * @param domainObjectFactory the domainObjectFactory to set */ public void setDomainObjectFactory(DomainObjectFactory domainObjectFactory) { this.domainObjectFactory = domainObjectFactory; } /** * @return the environment */ public Resource getEnvironment() { return environment; } /** * @param environment the environment to set */ public void setEnvironment(Resource environment) { this.environment = environment; } }