summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/Globals.java
blob: 852d8ddc1f7b97fa8bd5a0577054e3f14f66d058 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                


                                   

                            
                               
                                         
 


                                                                                 


                    
                                                                         
                                                                      
 



                                          
                                                             
 

                                                             
           
              
         




                                                                                                                                  
 

                                                                                                  


                                                                                                                   
 

                                                                                                              





                                                                                                          

                                                 
 

                                                                                                       

                                                 
 
                                         
                                                                         
                                                                                 

                                                 
 


                                                             
                                                                                                        
                         

                                           

                                                                     
                                         
                 
                                                                
         
 
                      
 

                                                 
                                                                                              
         


                                                   
                                                                                                
         
 
                                                  
         
                                                                                               
         
 
                                                    
         
                                                                                                 
         
 
                                                  
         
                                                                                              
         
 
                                                      
         
                                                                                              
         
 
                                            
         
                                                                                         
         
 
                                              
         
                                                                                           
         
 
                     
 
                                          
         
                                                               
         
 
                                                 
         
                                                                     
         
 
                                                  
         
                                                                      
         
 
                                                     
         
                                                                         
         
 
package org.openslx.imagemaster;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.openslx.imagemaster.util.Util;

/**
 * Class to hold global constants and properties from 'config/global.properties'.
 */
public class Globals
{

	private static Logger LOGGER = Logger.getLogger( Globals.class );
	private static final Properties properties = new Properties();

	/* CONSTANTS */
	/**
	 * The blocksize used for crc'ing.
	 */
	public final static int blockSize = 16 * 1024 * 1024;

	/**
	 * Loads the properties from config/global.properties
	 */
	static
	{
		try {
			// Load properties
			BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/global.properties" ) );
			properties.load( stream );
			stream.close();

			// check properties
			Util.notNullOrEmptyFatal( getImageDir(), "Image directory must be set." );
			Util.notNullOrEmptyFatal( getSslKeystoreFile(), "SSL keystore file must be set." );
			Util.notNullOrEmptyFatal( getSslKeystoreAlias(), "SSL keystore alias must be set." );
			Util.notNullOrEmptyFatal( getSslKeystorePassword(), "SSL keystore password must be set." );

			Util.notNullFatal( getSessionTimeoutUser(), "Session timeout user must be set." );
			Util.notNullFatal( getSessionTimeoutServer(), "Session timeout server must be set." );
			if ( getFiletransferPortSsl() == 0 && getFiletransferPortPlain() == 0 ) {
				LOGGER.fatal( "either SSL or plain port for file transfer must be set." );
				System.exit( 2 );
			}
			if ( getThriftPortSsl() == 0 && getThriftPortPlain() == 0 ) {
				LOGGER.fatal( "either SSL or plain port for thrift must be set." );
				System.exit( 2 );
			}

			if ( getFiletransferRetransmits() <= 0 ) {
				LOGGER.fatal( "SSL socket transmitted times must be greater than 0." );
				System.exit( 2 );
			}

			// check keystore
			if ( !getSslKeystoreFile().endsWith( ".jks" ) ) {
				LOGGER.fatal( "Keystore is not in jks format." );
				System.exit( 2 );
			}

			// remove "/" at the end of the paths
			String image = getImageDir();
			if ( image.endsWith( "/" ) ) {
				properties.put( "image_dir", image.substring( 0, image.length() - 1 ) );
			}

		} catch ( IOException e ) {
			LOGGER.fatal( "Could not load properties!" );
			LOGGER.warn( e.getStackTrace().toString() );
			System.exit( 2 );
		}
		LOGGER.info( "Loaded properties successfully" );
	}

	/* INTEGERS */

	public static int getSessionTimeoutUser()
	{
		return Util.tryToParseInt( properties.getProperty( "session.user.timeout" ) );
	}

	public static int getSessionTimeoutServer()
	{
		return Util.tryToParseInt( properties.getProperty( "session.server.timeout" ) );
	}

	public static int getFiletransferPortSsl()
	{
		return Util.tryToParseInt( properties.getProperty( "filetransfer.port.ssl" ) );
	}

	public static int getFiletransferPortPlain()
	{
		return Util.tryToParseInt( properties.getProperty( "filetransfer.port.plain" ) );
	}

	public static int getFiletransferTimeout()
	{
		return Util.tryToParseInt( properties.getProperty( "filetransfer.timeout" ) );
	}

	public static int getFiletransferRetransmits()
	{
		return Util.tryToParseInt( properties.getProperty( "filetransfer.retries" ) );
	}

	public static int getThriftPortSsl()
	{
		return Util.tryToParseInt( properties.getProperty( "thrift.port.ssl" ) );
	}

	public static int getThriftPortPlain()
	{
		return Util.tryToParseInt( properties.getProperty( "thrift.port.plain" ) );
	}

	/* STRINGS */

	public static String getImageDir()
	{
		return properties.getProperty( "storage.dir" );
	}

	public static String getSslKeystoreFile()
	{
		return properties.getProperty( "ssl.keystore.file" );
	}

	public static String getSslKeystoreAlias()
	{
		return properties.getProperty( "ssl.keystore.alias" );
	}

	public static String getSslKeystorePassword()
	{
		return properties.getProperty( "ssl.keystore.password" );
	}
}