From 859c07eba2b19a11340e473340e408c0a24380de Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Mon, 18 Aug 2014 15:34:18 +0200 Subject: Change implementation of the configuration in Global.java Add sample config file --- .../java/org/openslx/satellitedaemon/Globals.java | 248 ++++++++------------- 1 file changed, 94 insertions(+), 154 deletions(-) (limited to 'src/main/java/org/openslx/satellitedaemon/Globals.java') diff --git a/src/main/java/org/openslx/satellitedaemon/Globals.java b/src/main/java/org/openslx/satellitedaemon/Globals.java index a62ee9e..d33be1d 100644 --- a/src/main/java/org/openslx/satellitedaemon/Globals.java +++ b/src/main/java/org/openslx/satellitedaemon/Globals.java @@ -17,156 +17,77 @@ import javax.net.ssl.TrustManagerFactory; import org.apache.log4j.Logger; -public class Globals -{ - private static Logger log = Logger.getLogger( Globals.class ); +public class Globals { + private static Logger log = Logger.getLogger(Globals.class); private static final Properties properties = new Properties(); - private static boolean loadedProperties = false; private static SSLContext context = null; - /***********************************************************************************************/ - /** - * If there are more ints or Strings which should be added to config/global.properties, - * add to suiting enum, add a 'case' to getPropertyInt/String() and add checks to - * propertiesValid(). - * - * As an Example, if you want the value of the FILETRANSFERSERVERIP you have to call - * Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ) which returns a string. - */ - public static enum PropInt - { - FILETRANSFERPORT, THRIFTPORT, BLOCKSIZE // More int's? Add them separated with "," + public static final int BLOCKSIZE = 16 * 1024 * 1024; // 16 MB blocksize + + public static void init() { } - public static enum PropString - { // More strings's? Add them separated with "," - FILETRANSFERSERVERIP, KEYSTORETYPE, FILETRANSFERSKEYSTOREPATH, FILETRANSFERSKEYSTOREPWD, THRIFTORGANIZATIONNAME, - RNDSTRINGENCRYPTALIAS, RNDSTRINGENCRYPTPASSWORD, RNDSTRINGENCRYPTPATH + // * Properties *// + + // Strings // + public static String getMasterserverHost() { + return properties.getProperty("MASTERSERVER_HOST"); } - /***********************************************************************************************/ - /** - * - * @return - * @throws IOException - */ - public static boolean loadProperties() throws IOException - { - if ( loadedProperties ) - return false; + public static String getKeystoreType() { + return properties.getProperty("KEYSTORE_TYPE"); + } - // Load all entries of the config file into properties - BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/global.properties" ) ); - properties.load( stream ); - stream.close(); + public static String getFiletransferKeystorePath() { + return properties.getProperty("FILETRANSFER_KEYSTORE_PATH"); + } - return true; + public static String getFiletransferKeystorePassword() { + return properties.getProperty("FILETRANSFER_KEYSTORE_PASSWORD"); } - /***********************************************************************************************/ - /** - * - * @param props - * @return - */ - public static int getPropertyInt( Globals.PropInt props ) - { - String result = null; - - switch ( props ) { - case FILETRANSFERPORT: - result = properties.getProperty( "filetransfer_port" ); - break; - case BLOCKSIZE: - result = properties.getProperty( "blockSize" ); - break; - case THRIFTPORT: - result = properties.getProperty( "ThriftPort" ); - break; - default: - result = "0"; - break; - } - if ( result == null ) - return 0; + public static String getOrganizationName() { + return properties.getProperty("ORGANIZATION_NAME"); + } - return Integer.valueOf( result ); + public static String getThriftKeystoreAlias() { + return properties.getProperty("THRIFT_KEYSTORE_ALIAS"); } - /***********************************************************************************************/ - /** - * - * @param props - * @return - */ - public static String getPropertyString( Globals.PropString props ) - { - String result = null; - - switch ( props ) { - case FILETRANSFERSERVERIP: - result = properties.getProperty( "filetransfer_server_ip" ); - break; - case KEYSTORETYPE: - result = properties.getProperty( "keyStore_type" ); - break; - case FILETRANSFERSKEYSTOREPATH: - result = properties.getProperty( "path_to_filetransfer_keyStore" ); - break; - case FILETRANSFERSKEYSTOREPWD: - result = properties.getProperty( "filetransfer_keyStore_password" ); - break; - case THRIFTORGANIZATIONNAME: - result = properties.getProperty( "organization_name" ); - break; - case RNDSTRINGENCRYPTALIAS: - result = properties.getProperty( "RndStringEncrypt_alias" ); - break; - case RNDSTRINGENCRYPTPASSWORD: - result = properties.getProperty( "RndStringEncrypt_password" ); - break; - case RNDSTRINGENCRYPTPATH: - result = properties.getProperty( "RndStringEncrypt_path" ); - break; - default: - result = ""; - break; - } - return result; + public static String getThriftKeystorePassword() { + return properties.getProperty("THRIFT_KEYSTORE_PASSWORD"); + } + + public static String getThriftKeystorePath() { + return properties.getProperty("THRIFT_KEYSTORE_PATH"); + } + + // Integers // + public static int getFiletransferPort() { + return tryToParseInt(properties.getProperty("FILETRANSFER_PORT")); + } + + public static int getThriftPort() { + return tryToParseInt(properties.getProperty("THRIFT_PORT")); } - // TODO: add real checks - /***********************************************************************************************/ /** - * - * @return + * Load properties */ - public static boolean propertiesValid() - { - if ( Globals.getPropertyInt( PropInt.FILETRANSFERPORT ) == 0 - || Globals.getPropertyInt( PropInt.BLOCKSIZE ) == 0 - || Globals.getPropertyInt( PropInt.THRIFTPORT ) == 0 - || Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ).isEmpty() - || Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ) == null - || Globals.getPropertyString( PropString.KEYSTORETYPE ).isEmpty() - || Globals.getPropertyString( PropString.KEYSTORETYPE ) == null - || Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPATH ).isEmpty() - || Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPATH ) == null - || Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPWD ).isEmpty() - || Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPWD ) == null - || Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ).isEmpty() - || Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ) == null - || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTALIAS ).isEmpty() - || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTALIAS ) == null - || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPASSWORD ).isEmpty() - || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPASSWORD ) == null - || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPATH ).isEmpty() - || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPATH ) == null ) { - return false; - } - else { - return true; + static { + try { + // Load all entries of the config file into properties + BufferedInputStream stream = new BufferedInputStream( + new FileInputStream("config/global.properties")); + properties.load(stream); + stream.close(); + } catch (IOException e) { + log.error("Could not load properties. Exiting."); + System.exit(2); } + + notNullOrEmptyFatal(getMasterserverHost(), "Masterserver Host must not be empty!"); + // TODO: check properties } /***********************************************************************************************/ @@ -174,47 +95,66 @@ public class Globals * * @return */ - public static boolean masterServerSslContextInit() - { - char[] passphrase = Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPWD ).toCharArray(); + public static boolean masterServerSslContextInit() { + char[] passphrase = getFiletransferKeystorePassword().toCharArray(); KeyStore keystore; try { - keystore = KeyStore.getInstance( "JKS" ); - keystore.load( new FileInputStream( Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPATH ) ), passphrase ); - TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() ); - tmf.init( keystore ); - context = SSLContext.getInstance( "SSLv3" ); + keystore = KeyStore.getInstance("JKS"); + keystore.load(new FileInputStream(getFiletransferKeystorePath()), + passphrase); + TrustManagerFactory tmf = TrustManagerFactory + .getInstance(TrustManagerFactory.getDefaultAlgorithm()); + tmf.init(keystore); + context = SSLContext.getInstance("SSLv3"); TrustManager[] trustManagers = tmf.getTrustManagers(); - context.init( null, trustManagers, null ); - } catch ( KeyStoreException e ) { + context.init(null, trustManagers, null); + } catch (KeyStoreException e) { log.error("KeyStoreException"); return false; - } catch ( NoSuchAlgorithmException e ) { + } catch (NoSuchAlgorithmException e) { log.error("NoSuchAlgorithmException"); return false; - } catch ( CertificateException e ) { + } catch (CertificateException e) { log.error("CertificateException"); return false; - } catch ( FileNotFoundException e ) { - log.error("FileNotFoundException"); + } catch (FileNotFoundException e) { + log.error("Could not find the keystore for the filetransfer. Path was '" + getFiletransferKeystorePath() + "'"); return false; - } catch ( IOException e ) { + } catch (IOException e) { log.error("IOException"); return false; - } catch ( KeyManagementException e ) { + } catch (KeyManagementException e) { log.error("KeyManagementException"); return false; } return true; } - - - /***********************************************************************************************/ + + public static SSLContext getMasterServerSslContext() { + return Globals.context; + } + /** + * Tries to parse an int. Returns 0 on error. * - * @return + * @param s + * The strig to parse + * @return The parsed int or 0 on error */ - public static SSLContext getMasterServerSslContext(){ - return Globals.context; + public static int tryToParseInt(String s) { + try { + return Integer.parseInt(s); + } catch (NumberFormatException e) { + return 0; + } + } + + public static void notNullOrEmptyFatal(String something, String message) { + if (something == null || something.isEmpty()) { + if (message != null) + log.fatal("[NOTNULL] " + message); + log.warn(Thread.currentThread().getStackTrace().toString()); + System.exit(2); + } } -} +} \ No newline at end of file -- cgit v1.2.3-55-g7522