Guest User

Untitled

a guest
Jul 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. package org.sonatype.idiom.site;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.URLClassLoader;
  9. import java.util.Map;
  10. import java.util.Properties;
  11.  
  12. import javax.servlet.ServletContextEvent;
  13.  
  14. import org.sonatype.guice.bean.binders.ParameterKeys;
  15. import org.sonatype.guice.bean.binders.SpaceModule;
  16. import org.sonatype.guice.bean.binders.WireModule;
  17. import org.sonatype.guice.bean.reflect.ClassSpace;
  18. import org.sonatype.guice.bean.reflect.URLClassSpace;
  19.  
  20. import com.google.inject.AbstractModule;
  21. import com.google.inject.Guice;
  22. import com.google.inject.Injector;
  23. import com.google.inject.servlet.GuiceServletContextListener;
  24. import com.google.sitebricks.SitebricksModule;
  25.  
  26. public class IdiomSiteContextListener
  27. extends GuiceServletContextListener
  28. {
  29. private File baseDirectory;
  30.  
  31. @Override
  32. public void contextInitialized( ServletContextEvent servletContextEvent )
  33. {
  34. //
  35. // Need to run this before the injector is created
  36. //
  37. baseDirectory = new File( servletContextEvent.getServletContext().getRealPath( "/" ), "WEB-INF" );
  38.  
  39. super.contextInitialized( servletContextEvent );
  40. }
  41.  
  42. //
  43. // We need to know what base application properties file name is ==> "idiom.properties"
  44. //
  45. // Then we need to know what environment we are in ==> "dev,test,prod" so that I can pick the right properties
  46. //
  47. protected Injector getInjector()
  48. {
  49. String basePropertyName = "idiom.properties";
  50.  
  51. String mode;
  52.  
  53. mode = System.getProperty( "idiomMode" );
  54.  
  55. if ( mode == null )
  56. {
  57. mode = "prod";
  58. }
  59.  
  60. String runtimeProperties = basePropertyName + "." + mode;
  61.  
  62. ClassSpace space = new URLClassSpace( (URLClassLoader) getClass().getClassLoader() );
  63. return Guice.createInjector( new WireModule( new SpaceModule( space ), new PropertiesModule( runtimeProperties ), new SitebricksModule()
  64. {
  65. @Override
  66. protected void configureSitebricks()
  67. {
  68. scan( FormService.class.getPackage() );
  69. }
  70. }));
  71. }
  72.  
  73. //
  74. // Replace this with properties utils that must exist
  75. //
  76. public class PropertiesModule
  77. extends AbstractModule
  78. {
  79. private String propertiesPath;
  80.  
  81. public PropertiesModule( String propertiesPath )
  82. {
  83. this.propertiesPath = propertiesPath;
  84. }
  85.  
  86. @Override
  87. protected void configure()
  88. {
  89. Properties p = new Properties();
  90.  
  91. File propertiesFile = new File( baseDirectory, propertiesPath );
  92.  
  93. if ( propertiesFile.exists() )
  94. {
  95. try
  96. {
  97. p.load( new FileInputStream( propertiesFile ) );
  98. }
  99. catch ( FileNotFoundException e )
  100. {
  101. }
  102. catch ( IOException e )
  103. {
  104. }
  105. }
  106. else
  107. {
  108. try
  109. {
  110. InputStream is = getClass().getClassLoader().getResourceAsStream( propertiesPath );
  111.  
  112. if ( is != null )
  113. {
  114. p.load( is );
  115. }
  116. }
  117. catch ( IOException e )
  118. {
  119. }
  120. }
  121.  
  122. //
  123. // Derived properties
  124. //
  125. p.setProperty( "application.data.directory", System.getProperty( "application.data.directory" ) );
  126.  
  127. if ( !p.isEmpty() )
  128. {
  129. bind( ParameterKeys.PROPERTIES ).toInstance( (Map) p );
  130. System.out.println( p );
  131. }
  132. else
  133. {
  134. //
  135. // We should bail because the app is not going to work otherwise
  136. //
  137. }
  138. }
  139. }
  140. }
Add Comment
Please, Sign In to add comment