summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/util/Util.java
blob: 24f98834445bd8d6fe8f7d5199630ef7927742e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.openslx.satellitedaemon.util;

import org.apache.log4j.Logger;

public class Util
{
	private static Logger log = Logger.getLogger( Util.class );

	/**
	 * Check if the given object is null, abort program if true. An optional
	 * message to be printed can be passed. A stack trace will be printed, too.
	 * Finally the application terminates with exit code 2.
	 * 
	 * This comes in handy if something must not be null, and you want 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
	 * @param message
	 *           the message to be printed if something is null
	 */
	public static void notNullFatal( Object something, String message )
	{
		if ( something == null ) {
			if ( message != null )
				log.fatal( "[NOTNULL] " + message );
			log.warn( Thread.currentThread().getStackTrace().toString() );
			System.exit( 2 );
		}
	}
	
	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 );
		}
	}
}