PageRenderTime 4373ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 0ms

/src/main/java/org/snowfk/web/ApplicationLoader.java

https://github.com/jeremychone/deprecated-snow-1.x
Java | 199 lines | 130 code | 39 blank | 30 comment | 25 complexity | 4810ab51ef2799a5280294cb89cb3924 MD5 | raw file
  1. /* Copyright 2009 Jeremy Chone - Licensed under the Apache License, Version 2.0
  2. * http://www.apache.org/licenses/LICENSE-2.0
  3. */
  4. package org.snowfk.web;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Properties;
  10. import javax.servlet.ServletContext;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.snowfk.util.MapUtil;
  14. import org.snowfk.web.db.hibernate.DefaultHibernateModule;
  15. import com.google.inject.Guice;
  16. import com.google.inject.Injector;
  17. import com.google.inject.Module;
  18. import com.google.inject.util.Modules;
  19. public class ApplicationLoader {
  20. static private Logger logger = LoggerFactory.getLogger(ApplicationLoader.class);
  21. protected Injector appInjector;
  22. //File sfkFolder;
  23. private ServletContext servletContext;
  24. private File webAppFolder;
  25. PropertyPostProcessor propertyPostProcessor;
  26. public ApplicationLoader(File webAppFolder, ServletContext servletContext) {
  27. this.webAppFolder = fixWebAppFolder(webAppFolder);
  28. this.servletContext = servletContext;
  29. }
  30. public File getWebAppFolder(){
  31. return webAppFolder;
  32. }
  33. @SuppressWarnings("unchecked")
  34. public ApplicationLoader load() throws Exception {
  35. /*--------- Load the Properties ---------*/
  36. // First load the application.properties
  37. Properties appProperties = new Properties();
  38. File propertiesFile = getWebInfPropertiesFile();
  39. if (propertiesFile.exists()) {
  40. appProperties.load(new FileReader(propertiesFile));
  41. } else {
  42. logger.info("No application.properties found at " + propertiesFile.getAbsolutePath()
  43. + " - Starting blank application.");
  44. }
  45. // if a ServletContext, then look if there is a WebApp instance in the appDir parent folder
  46. // with the name [appDir].application.properties
  47. // The appDir is either the webApp root or the appDir that has been set in the servletContext initParameter
  48. // (usually by snowServlet)
  49. File appDir = null;
  50. if (servletContext != null) {
  51. String appDirPath = servletContext.getInitParameter("appDir");
  52. if (appDirPath != null) {
  53. appDir = new File(appDirPath);
  54. } else {
  55. appDir = getWebAppFolder();
  56. }
  57. String appDirFolderName = appDir.getName();
  58. File appDirPropertiesFile = new File(appDir.getParentFile(), appDirFolderName + ".application.properties");
  59. if (appDirPropertiesFile.exists()) {
  60. Properties appDirProperties = new Properties();
  61. appDirProperties.load(new FileReader(appDirPropertiesFile));
  62. // override the appProperties with the WebAppRoperties
  63. appProperties.putAll(appDirProperties);
  64. }
  65. }else{
  66. appDir = getWebAppFolder();
  67. }
  68. PropertyPostProcessor propertyPostProcessor = getPropertyPostProcessor();
  69. // if we do not have it programmatically, then, look in the
  70. // snow.snow.propertyPostProcessorClass properties
  71. if (propertyPostProcessor == null) {
  72. String propertyPostProcessorClassName = appProperties.getProperty("snow.propertyPostProcessorClass");
  73. if (propertyPostProcessorClassName != null) {
  74. try {
  75. Class<PropertyPostProcessor> propertyPostProcessorClass = (Class<PropertyPostProcessor>) Class.forName(propertyPostProcessorClassName);
  76. if (propertyPostProcessorClass != null) {
  77. propertyPostProcessor = propertyPostProcessorClass.newInstance();
  78. }
  79. } catch (Exception e) {
  80. logger.error("Cannot load or process the PropertyPostProcess class: " + propertyPostProcessorClassName
  81. + "\nException: "
  82. + e.getMessage());
  83. }
  84. }
  85. }
  86. try {
  87. if (propertyPostProcessor != null) {
  88. appProperties = propertyPostProcessor.processProperties(appProperties);
  89. }
  90. } catch (Exception e) {
  91. logger.error("Cannot process PropertyPostProcess class: " + propertyPostProcessor
  92. + "\nException: "
  93. + e.getMessage());
  94. }
  95. /*--------- /Load the Properties ---------*/
  96. /*--------- Load WebApplication ---------*/
  97. // Building the root modules
  98. // rootModules cannot be overrided
  99. List<Module> rootModules = new ArrayList<Module>();
  100. rootModules.add(new RootWebModule(servletContext));
  101. rootModules.add(new RootApplicationModule(appProperties, getWebAppFolder(), appDir));
  102. String applicationConfigClassStr = appProperties.getProperty("snow.applicationWebModuleConfigClass");
  103. Class applicationModuleClass = null;
  104. String applicationPackageBase = null;
  105. if (applicationConfigClassStr != null){
  106. applicationModuleClass = Class.forName(applicationConfigClassStr);
  107. applicationPackageBase = applicationModuleClass.getPackage().getName();
  108. }
  109. // build the default modules
  110. // default modules can be overrided
  111. List<Module> defaultModules = new ArrayList<Module>();
  112. defaultModules.add(new DefaultApplicationModule(applicationPackageBase));
  113. boolean hasHibernate = (appProperties != null && MapUtil.hasKeyStartsWith(appProperties, "hibernate."));
  114. if (hasHibernate) {
  115. defaultModules.add(new DefaultHibernateModule());
  116. }
  117. Module combineAppModule;
  118. if (applicationModuleClass != null) {
  119. Module applicationModule = (Module) applicationModuleClass.newInstance();
  120. combineAppModule = Modules.override(defaultModules).with(applicationModule);
  121. } else {
  122. combineAppModule = Modules.combine(defaultModules);
  123. }
  124. rootModules.add(combineAppModule);
  125. appInjector = Guice.createInjector(rootModules);
  126. /*--------- /Load WebApplication ---------*/
  127. return this;
  128. }
  129. public WebController getWebController(){
  130. return appInjector.getInstance(WebController.class);
  131. }
  132. // --------- PropertyPostProcessor Methods --------- //
  133. public PropertyPostProcessor getPropertyPostProcessor() {
  134. return propertyPostProcessor;
  135. }
  136. /**
  137. * Allow to programatically set a propertyPostProcessor to the appLoader. Usually used for Unit Testing.
  138. *
  139. * @param propertyPostProcessor
  140. */
  141. public void setPropertyPostProcessor(PropertyPostProcessor propertyPostProcessor) {
  142. this.propertyPostProcessor = propertyPostProcessor;
  143. }
  144. // --------- /PropertyPostProcessor Methods --------- //
  145. private File getWebInfPropertiesFile(){
  146. File webAppFolder = getWebAppFolder();
  147. return new File(webAppFolder,"WEB-INF/snow/application.properties");
  148. }
  149. private File fixWebAppFolder(File webAppFolder) {
  150. String webAppFolderName = webAppFolder.getName();
  151. // Linux hack (somehow on Linux when contextPath empty, the
  152. // webAppFolder.getName() return ".", so, if this
  153. // is the case, need to go one parent up
  154. if (".".equals(webAppFolderName)) {
  155. webAppFolder = webAppFolder.getParentFile();
  156. webAppFolderName = webAppFolder.getName();
  157. }
  158. return webAppFolder;
  159. }
  160. }