From b6aa208c55cc090b5c2695889cb551dc2a98755f Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Fri, 4 Jul 2014 10:54:33 +0200 Subject: Change loading and checking of properties Remove DbFtpUser Add NotNullOrEmptyFatal to Utils --- .../java/org/openslx/imagemaster/util/Util.java | 34 +++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'src/main/java/org/openslx/imagemaster/util/Util.java') 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; + } + } } -- cgit v1.2.3-55-g7522