blob: 3cafdfb7b3ab46b7ee739cc1e0b32e1053827ac7 (
plain) (
tree)
|
|
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;
public class Globals
{
private static Logger log = Logger.getLogger( Globals.class );
private static final Properties properties = new Properties();
private static boolean loadedProperties = false;
public final static int blockSize = 16 * 1024 * 1024;
/**
* Loads the properties from config/global.properties
* @throws IOException
*/
public static void loadProperties() throws IOException
{
if ( loadedProperties ) return;
// Load properties
BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/global.properties" ) );
properties.load( stream );
stream.close();
loadedProperties = true;
}
public static boolean propertiesValid()
{
// TODO: Some of these might legitimately be empty (but not null).
// Maybe use Util.notNullFatal on those so you can easily add an error message
// telling which option is missing. Add Util.notNullOrEmptyFatal if you feel like it...
if ( getImageDir() == null
|| getImageDir().isEmpty()
|| getLdapHost() == null
|| getLdapHost().isEmpty()
|| getLdapBindQuery() == null
|| getLdapBindQuery().isEmpty()
|| getLdapSearchBaseDn() == null
|| getLdapSearchBaseDn().isEmpty()
|| getLdapSearchFilter() == null
|| getLdapSearchFilter().isEmpty()
|| getLdapKeystorePassword() == null
|| getLdapKeystorePassword().isEmpty()
|| getLdapKeystorePath() == null
|| getLdapKeystorePath().isEmpty()
|| getSslKeystoreFile() == null
|| getSslKeystoreFile().isEmpty()
|| getSslKeystoreAlias() == null
|| getSslKeystoreAlias().isEmpty()
|| getSslKeystorePassword() == null
|| getSslKeystorePassword().isEmpty()
|| getLdapPort() == 0
|| getSessionTimeoutUser() == 0
|| getSessionTimeoutServer() == 0
|| getSslPort() == 0
|| getSslTimeout() == 0 ) {
return false;
}
// check ldap_bind_query
if ( StringUtils.countMatches( getLdapBindQuery(), "%" ) == 0 ) {
log.error( "ldap_bind_query does not contain '%'" );
return false;
}
// check ldap_search_filter
if ( StringUtils.countMatches( getLdapSearchFilter(), "%" ) == 0) {
log.error( "ldap_search_filter does not contain '%'" );
return false;
}
// check keystore
if ( !getSslKeystoreFile().endsWith( ".jks" )) {
log.error( "Keystore is not in jks format." );
return false;
}
// remove "/" at the end of the paths
String image = getImageDir();
if ( image.endsWith( "/" ) ) {
Globals.properties.put( "image_dir", image.substring( 0, image.length() - 1 ) );
}
return true;
}
/* INTEGERS */
// TODO: Use parseInt not valueOf so we don't instantiate Integers all the time
// TODO: Either way might throw an exception if not parsable as integer.
// Maybe write a Util method that tries Integer.parseInt and returns 0/-1 on exception.
public static int getLdapPort() {
return Integer.valueOf( properties.getProperty( "ldap_port" ) );
}
public static int getSessionTimeoutUser() {
return Integer.valueOf( properties.getProperty( "session_timeout_user" ) );
}
public static int getSessionTimeoutServer() {
return Integer.valueOf( properties.getProperty( "session_timeout_user" ) );
}
public static int getSslPort() {
return Integer.valueOf( properties.getProperty( "ssl_port" ) );
}
public static int getSslTimeout() {
return Integer.valueOf( 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" ) );
}
}
|