AppContextInitializer.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* ===================================================================
  2. * AppContextInitializer.java
  3. *
  4. * Created Feb 28, 2005 8:54:00 PM
  5. *
  6. * Copyright (c) 2005 Matt Magoffin (spamsqr@msqr.us)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. * ===================================================================
  23. * $Id: AppContextInitializer.java 74 2009-05-15 04:44:37Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat.test;
  27. import java.io.IOException;
  28. import java.util.Iterator;
  29. import java.util.Map;
  30. import java.util.Properties;
  31. import magoffin.matt.ieat.biz.DomainObjectFactory;
  32. import magoffin.matt.ieat.web.WebConstants;
  33. import magoffin.matt.util.StringUtil;
  34. import magoffin.matt.xweb.XAppContext;
  35. import magoffin.matt.xweb.XwebParameter;
  36. import magoffin.matt.xweb.util.AppContextSupport;
  37. import org.apache.commons.logging.Log;
  38. import org.apache.commons.logging.LogFactory;
  39. import org.springframework.beans.BeansException;
  40. import org.springframework.context.ApplicationContext;
  41. import org.springframework.context.ApplicationContextAware;
  42. import org.springframework.core.io.Resource;
  43. /**
  44. * Class to initialize the iEat AppContext data.
  45. *
  46. * <p>The configurable properties of this class are:</p>
  47. *
  48. * <dl>
  49. * <dt>domainObjectFactory</dt>
  50. * <dd>The DomainObjectFactory to use for creating domain objects.</dd>
  51. *
  52. * <dt>environment</dt>
  53. * <dd>The property file resource containing all application properties.</dd>
  54. * </dl>
  55. *
  56. * @author Matt Magoffin (spamsqr@msqr.us)
  57. * @version $Revision: 74 $ $Date: 2009-05-15 16:44:37 +1200 (Fri, 15 May 2009) $
  58. */
  59. public class AppContextInitializer implements ApplicationContextAware {
  60. private final Log logger = LogFactory.getLog(getClass());
  61. /** DomainObjectFactory for creating our domain objects. */
  62. private DomainObjectFactory domainObjectFactory = null;
  63. /** The environment property file to make available via the AppContext object. */
  64. private Resource environment = null;
  65. /* Internal fields. */
  66. private AppContextSupport appContextSupport = null;
  67. /* (non-Javadoc)
  68. * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
  69. */
  70. @SuppressWarnings("unchecked")
  71. public void setApplicationContext(ApplicationContext applicationContext)
  72. throws BeansException {
  73. XAppContext appCtx = domainObjectFactory.newXAppContextInstance();
  74. Properties props = new Properties();
  75. try{
  76. props.load(environment.getInputStream());
  77. } catch ( IOException e ) {
  78. throw new RuntimeException("Unable to load environment properties",e);
  79. }
  80. for ( Iterator itr = props.entrySet().iterator(); itr.hasNext(); ) {
  81. Map.Entry me = (Map.Entry)itr.next();
  82. String value = StringUtil.trimToNull((String)me.getValue());
  83. if ( value != null ) {
  84. XwebParameter param = domainObjectFactory.newXwebParameterInstance();
  85. param.setKey(me.getKey().toString());
  86. param.setValue(value);
  87. appCtx.getParam().add(param);
  88. }
  89. }
  90. // cache the configuration in the servlet context
  91. AppContextSupport appCtxSupport = new AppContextSupport(appCtx);
  92. if ( logger.isDebugEnabled() ) {
  93. logger.debug("Storing AppContext [" +appCtx +"] with " +props.size()
  94. +" properties in the servlet context at ["
  95. +WebConstants.APP_KEY_APP_CONTEXT +"]");
  96. }
  97. this.appContextSupport = appCtxSupport;
  98. }
  99. /**
  100. * @return Returns the appContextSupport.
  101. */
  102. public AppContextSupport getAppContextSupport() {
  103. return appContextSupport;
  104. }
  105. /**
  106. * @return the domainObjectFactory
  107. */
  108. public DomainObjectFactory getDomainObjectFactory() {
  109. return domainObjectFactory;
  110. }
  111. /**
  112. * @param domainObjectFactory the domainObjectFactory to set
  113. */
  114. public void setDomainObjectFactory(DomainObjectFactory domainObjectFactory) {
  115. this.domainObjectFactory = domainObjectFactory;
  116. }
  117. /**
  118. * @return the environment
  119. */
  120. public Resource getEnvironment() {
  121. return environment;
  122. }
  123. /**
  124. * @param environment the environment to set
  125. */
  126. public void setEnvironment(Resource environment) {
  127. this.environment = environment;
  128. }
  129. }