diff options
author | Nils Schwabe | 2014-07-04 10:54:33 +0200 |
---|---|---|
committer | Nils Schwabe | 2014-07-04 10:54:33 +0200 |
commit | b6aa208c55cc090b5c2695889cb551dc2a98755f (patch) | |
tree | 67c5509e2626866cedb7e148e9b86574be3f3e4c /src | |
parent | Merge branch 'master' of git.openslx.org:bwlp/masterserver (diff) | |
download | masterserver-b6aa208c55cc090b5c2695889cb551dc2a98755f.tar.gz masterserver-b6aa208c55cc090b5c2695889cb551dc2a98755f.tar.xz masterserver-b6aa208c55cc090b5c2695889cb551dc2a98755f.zip |
Change loading and checking of properties
Remove DbFtpUser
Add NotNullOrEmptyFatal to Utils
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/App.java | 16 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/Globals.java | 132 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/db/DbFtpUser.java | 48 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/util/Util.java | 34 |
4 files changed, 90 insertions, 140 deletions
diff --git a/src/main/java/org/openslx/imagemaster/App.java b/src/main/java/org/openslx/imagemaster/App.java index 06d9938..4a4baf0 100644 --- a/src/main/java/org/openslx/imagemaster/App.java +++ b/src/main/java/org/openslx/imagemaster/App.java @@ -1,12 +1,9 @@ package org.openslx.imagemaster; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; -import org.openslx.imagemaster.serverconnection.ConnectionHandler; -import org.openslx.imagemaster.serverconnection.ImageProcessor; import org.openslx.imagemaster.thrift.server.BinaryListener; import org.slf4j.LoggerFactory; @@ -28,19 +25,6 @@ public class App { // Init logging log.info( "Starting Application" ); - - // Load properties - try { - Globals.loadProperties(); - if ( !Globals.propertiesValid() ) { - log.error( "Config file contains errors." ); - System.exit( 1 ); - } - } catch ( IOException e ) { - log.error( "Could not load config file. Quitting." ); - System.exit( 1 ); - } - log.info( "Loaded config file" ); // Create binary listener Thread t; 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 */ diff --git a/src/main/java/org/openslx/imagemaster/db/DbFtpUser.java b/src/main/java/org/openslx/imagemaster/db/DbFtpUser.java deleted file mode 100644 index b30f504..0000000 --- a/src/main/java/org/openslx/imagemaster/db/DbFtpUser.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.openslx.imagemaster.db; - -import java.sql.Timestamp; -import java.util.List; - -// TODO: Still needed? - -public class DbFtpUser -{ - public final String username; - public final String password; - public final String mode; - public final String filename; - public final String sessionId; - public final Timestamp timestamp; - - public DbFtpUser(String username, String password, String mode, String filename, String sessionId, Timestamp timestamp) - { - this.username = username; - this.password = password; - this.mode = mode; - this.filename = filename; - this.sessionId = sessionId; - this.timestamp = timestamp; - } - - public static List<DbFtpUser> getAllUsers() - { - return MySQL.findAll( DbFtpUser.class, "SELECT username, password, mode, filename, sessionid, timestamp from ftpUser" ); - } - - public static void addUser(DbFtpUser user) - { - MySQL.update( "INSERT INTO ftpUser (username, password, mode, filename, sessionid, timestamp) VALUES (?, ?, ?, ?, ?, ?)", - user.username, user.password, user.mode, user.filename, user.sessionId, user.timestamp); - } - - public static DbFtpUser getUserByName(String username) - { - return MySQL.findUniqueOrNull( DbFtpUser.class, "SELECT username, password, mode, filename, sessionid, timestamp FROM ftpUser WHERE username=?", username ); - } - - public static boolean removeUserByName(String username) - { - int result = MySQL.update( "DELETE FROM ftpUser WHERE username=? LIMIT 1", username ); - return result == 1; - } -} diff --git a/src/main/java/org/openslx/imagemaster/util/Util.java b/src/main/java/org/openslx/imagemaster/util/Util.java index fb13c86..9d5c5fb 100644 --- a/src/main/java/org/openslx/imagemaster/util/Util.java +++ b/src/main/java/org/openslx/imagemaster/util/Util.java @@ -17,7 +17,7 @@ public class Util * exit code 2. * * This comes in handy if something must not be null, and you want - * user friendsly output. A perfect example would be reading settings + * user friendly output. A perfect example would be reading settings * from a config file. You can use this on mandatory fields. * * @param something the object to compare to null @@ -32,6 +32,25 @@ public class Util System.exit( 2 ); } } + + /** + * Check if String is null or empty, abort program if so. + * An optional message to be printed can be passed. A stack trace + * will be printed, too. Finally the application terminates with + * exit code 2. + * + * @param something The string you want to check + * @param message The message to be printed if "something" is null or empty + */ + 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 ); + } + } /** * Static {@link Random} instance. @@ -70,5 +89,18 @@ public class Util } folder.delete(); } + + /** + * Tries to parse an int. Returns 0 on error. + * @param s The strig to parse + * @return The parsed int or 0 on error + */ + public static int tryToParseInt(String s) { + try { + return Integer.parseInt( s ); + } catch (NumberFormatException e) { + return 0; + } + } } |