diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/Globals.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/Globals.java | 132 |
1 files changed, 57 insertions, 75 deletions
diff --git a/src/main/java/org/openslx/imagemaster/Globals.java b/src/main/java/org/openslx/imagemaster/Globals.java index 3cafdfb..6e5893e 100644 --- a/src/main/java/org/openslx/imagemaster/Globals.java +++ b/src/main/java/org/openslx/imagemaster/Globals.java @@ -7,114 +7,96 @@ 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(); - 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 + static { - if ( loadedProperties ) return; + 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 ); + } - // 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() + // 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 ); + } - || getLdapPort() == 0 - || getSessionTimeoutUser() == 0 - || getSessionTimeoutServer() == 0 - || getSslPort() == 0 - || getSslTimeout() == 0 ) { - 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 ) ); + } + + } catch (IOException e) { + log.fatal( "Could not load properties!" ); + log.warn( e.getStackTrace().toString() ); + System.exit( 2 ); } - - // 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" ) ); + return Util.tryToParseInt( properties.getProperty( "ldap_port" ) ); } public static int getSessionTimeoutUser() { - return Integer.valueOf( properties.getProperty( "session_timeout_user" ) ); + return Util.tryToParseInt( properties.getProperty( "session_timeout_user" ) ); } public static int getSessionTimeoutServer() { - return Integer.valueOf( properties.getProperty( "session_timeout_user" ) ); + return Util.tryToParseInt( properties.getProperty( "session_timeout_user" ) ); } public static int getSslPort() { - return Integer.valueOf( properties.getProperty( "ssl_port" ) ); + return Util.tryToParseInt( properties.getProperty( "ssl_port" ) ); } public static int getSslTimeout() { - return Integer.valueOf( properties.getProperty( "ssl_timeout" ) ); + return Util.tryToParseInt( properties.getProperty( "ssl_timeout" ) ); } /* STRINGS */ |