summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/Globals.java
blob: 6e5893e02fe65e4e3ad2628e00a8ecb0d3c49b89 (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.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.openslx.imagemaster.util.Util;

public class Globals
{

	private static Logger log = Logger.getLogger( Globals.class );
	private static final Properties properties = new Properties();
	public final static int blockSize = 16 * 1024 * 1024;

	/**
	 * Loads the properties from config/global.properties
	 * @throws IOException
	 */
	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( getLdapHost(), "Ldap host must be set." );
			Util.notNullOrEmptyFatal( getLdapBindQuery(), "Ldap bind query must be set." );
			Util.notNullOrEmptyFatal( getLdapSearchBaseDn(), "Ldap search base dn must be set." );
			Util.notNullOrEmptyFatal( getLdapSearchFilter(), "Ldap search filter must be set." );
			Util.notNullFatal( getLdapKeystorePassword(), "Ldap keystore password must be set." );
			Util.notNullOrEmptyFatal( getLdapKeystorePath(), "Ldap keystore path 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( getLdapPort(), "Ldap port must be set." );
			Util.notNullFatal( getSessionTimeoutUser(), "Session timeout user must be set." );
			Util.notNullFatal( getSessionTimeoutServer(), "Session timeout server must be set." );
			Util.notNullFatal( getSslPort(), "SSL socket port must be set." );
			Util.notNullFatal( getSslTimeout(), "SSL socket timeout must be set." );
			
			// check ldap_bind_query
			if ( StringUtils.countMatches( getLdapBindQuery(), "%" ) == 0 ) {
				log.fatal( "ldap_bind_query does not contain '%'" );
				System.exit( 2 );
			}

			// check ldap_search_filter
			if ( StringUtils.countMatches( getLdapSearchFilter(), "%" ) == 0) {
				log.fatal( "ldap_search_filter does not contain '%'" );
				System.exit( 2 );
			}
			
			// check keystore
			if ( !getSslKeystoreFile().endsWith( ".jks" )) {
				log.fatal( "Keystore is not in jks format." );
				System.exit( 2 );
			}

			// remove "/" at the end of the paths
			String image = getImageDir();
			if ( image.endsWith( "/" ) ) {
				Globals.properties.put( "image_dir", image.substring( 0, image.length() - 1 ) );
			}
			
		} catch (IOException e) {
			log.fatal( "Could not load properties!" );
			log.warn( e.getStackTrace().toString() );
			System.exit( 2 );
		}
	}
	
	/* INTEGERS */
	
	public static int getLdapPort() {
		return Util.tryToParseInt( properties.getProperty( "ldap_port" ) );
	}
	
	public static int getSessionTimeoutUser() {
		return Util.tryToParseInt( properties.getProperty( "session_timeout_user" ) );
	}
	
	public static int getSessionTimeoutServer() {
		return Util.tryToParseInt( properties.getProperty( "session_timeout_user" ) );
	}
	
	public static int getSslPort() {
		return Util.tryToParseInt( properties.getProperty( "ssl_port" ) );
	}
	
	public static int getSslTimeout() {
		return Util.tryToParseInt( properties.getProperty( "ssl_timeout" ) );
	}
	
	/* STRINGS */
	
	public static String getImageDir() {
		return properties.getProperty( "image_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" );
	}
	
	public static String getLdapHost() {
		return properties.getProperty( "ldap_host" );
	}
	
	public static String getLdapBindQuery() {
		return properties.getProperty( "ldap_bind_query" );
	}
	
	public static String getLdapSearchBaseDn() {
		return properties.getProperty( "ldap_search_base_dn" );
	}
	
	public static String getLdapSearchFilter() {
		return properties.getProperty( "ldap_search_filter" );
	}
	
	public static String getLdapKeystorePassword() {
		return properties.getProperty( "ldap_keystore_password" );
	}
	
	public static String getLdapKeystorePath() {
		return properties.getProperty( "ldap_keystore_path" );
	}
	
	/* BOOLEANS */
	
	public static boolean getLdapSsl() {
		return Boolean.valueOf( properties.getProperty( "ldap_ssl" ) );
	}
}