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;
/**
* Class to hold global constants and properties from 'config/global.properties'.
*/
public class Globals
{
private static Logger log = 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;
public static void init()
{
}
/**
* 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( 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( getSslSocketPort(), "SSL socket port must be set." );
Util.notNullFatal( getSslTimeout(), "SSL socket timeout must be set." );
if ( getSslTransmitTimes() <= 0 )
log.fatal( "SSL socket transmitted times must be greater than 0." );
// 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 );
}
log.info( "Loaded properties successfully" );
}
/* 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 getSslSocketPort()
{
return Util.tryToParseInt( properties.getProperty( "ssl_socket_port" ) );
}
public static int getSslTimeout()
{
return Util.tryToParseInt( properties.getProperty( "ssl_socket_timeout" ) );
}
public static int getSslTransmitTimes()
{
return Util.tryToParseInt( properties.getProperty( "ssl_socket_transmit_times" ) );
}
public static int getCrcSchedulingInterval()
{
return Util.tryToParseInt( properties.getProperty( "crc_scheduling_interval" ) );
}
/* 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" ) );
}
}