Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.sonatype.idiom.site;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URLClassLoader;
- import java.util.Map;
- import java.util.Properties;
- import javax.servlet.ServletContextEvent;
- import org.sonatype.guice.bean.binders.ParameterKeys;
- import org.sonatype.guice.bean.binders.SpaceModule;
- import org.sonatype.guice.bean.binders.WireModule;
- import org.sonatype.guice.bean.reflect.ClassSpace;
- import org.sonatype.guice.bean.reflect.URLClassSpace;
- import com.google.inject.AbstractModule;
- import com.google.inject.Guice;
- import com.google.inject.Injector;
- import com.google.inject.servlet.GuiceServletContextListener;
- import com.google.sitebricks.SitebricksModule;
- public class IdiomSiteContextListener
- extends GuiceServletContextListener
- {
- private File baseDirectory;
- @Override
- public void contextInitialized( ServletContextEvent servletContextEvent )
- {
- //
- // Need to run this before the injector is created
- //
- baseDirectory = new File( servletContextEvent.getServletContext().getRealPath( "/" ), "WEB-INF" );
- super.contextInitialized( servletContextEvent );
- }
- //
- // We need to know what base application properties file name is ==> "idiom.properties"
- //
- // Then we need to know what environment we are in ==> "dev,test,prod" so that I can pick the right properties
- //
- protected Injector getInjector()
- {
- String basePropertyName = "idiom.properties";
- String mode;
- mode = System.getProperty( "idiomMode" );
- if ( mode == null )
- {
- mode = "prod";
- }
- String runtimeProperties = basePropertyName + "." + mode;
- ClassSpace space = new URLClassSpace( (URLClassLoader) getClass().getClassLoader() );
- return Guice.createInjector( new WireModule( new SpaceModule( space ), new PropertiesModule( runtimeProperties ), new SitebricksModule()
- {
- @Override
- protected void configureSitebricks()
- {
- scan( FormService.class.getPackage() );
- }
- }));
- }
- //
- // Replace this with properties utils that must exist
- //
- public class PropertiesModule
- extends AbstractModule
- {
- private String propertiesPath;
- public PropertiesModule( String propertiesPath )
- {
- this.propertiesPath = propertiesPath;
- }
- @Override
- protected void configure()
- {
- Properties p = new Properties();
- File propertiesFile = new File( baseDirectory, propertiesPath );
- if ( propertiesFile.exists() )
- {
- try
- {
- p.load( new FileInputStream( propertiesFile ) );
- }
- catch ( FileNotFoundException e )
- {
- }
- catch ( IOException e )
- {
- }
- }
- else
- {
- try
- {
- InputStream is = getClass().getClassLoader().getResourceAsStream( propertiesPath );
- if ( is != null )
- {
- p.load( is );
- }
- }
- catch ( IOException e )
- {
- }
- }
- //
- // Derived properties
- //
- p.setProperty( "application.data.directory", System.getProperty( "application.data.directory" ) );
- if ( !p.isEmpty() )
- {
- bind( ParameterKeys.PROPERTIES ).toInstance( (Map) p );
- System.out.println( p );
- }
- else
- {
- //
- // We should bail because the app is not going to work otherwise
- //
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment