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() || getFtpBaseDir() == null || getFtpBaseDir().isEmpty() || getFtpKeystoreFile() == null || getFtpKeystoreFile().isEmpty() || getFtpKeystoreAlias() == null || getFtpKeystoreAlias().isEmpty() || getFtpKeystorePassword() == null || getFtpKeystorePassword().isEmpty() || getLdapPort() == 0 || getSessionTimeoutUser() == 0 || getSessionTimeoutServer() == 0 || getFtpPort() == 0 || getFtpTimeout() == 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 ( !getFtpKeystoreFile().endsWith( ".jks" )) { log.error( "Keystore is not in jks format." ); return false; } // remove "/" at the end of the paths String ftp = getFtpBaseDir(); if ( ftp.endsWith( "/" ) ) { Globals.properties.put( "ftp_base_dir", ftp.substring( 0, ftp.length() - 1 ) ); } 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 getFtpPort() { return Integer.valueOf( properties.getProperty( "ftp_port" ) ); } public static int getFtpTimeout() { return Integer.valueOf( properties.getProperty( "ftp_timeout" ) ); } /* STRINGS */ public static String getImageDir() { return properties.getProperty( "image_dir" ); } public static String getFtpKeystoreFile() { return properties.getProperty( "ftp_keystore_file" ); } public static String getFtpKeystoreAlias() { return properties.getProperty( "ftp_keystore_alias" ); } public static String getFtpKeystorePassword() { return properties.getProperty( "ftp_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" ); } public static String getFtpBaseDir() { return properties.getProperty( "ftp_base_dir" ); } /* BOOLEANS */ public static boolean getLdapSsl() { return Boolean.valueOf( properties.getProperty( "ldap_ssl" ) ); } }