summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/util/Util.java
blob: 9805c21bf9dcc6619c9ccebf264a03db89a9391d (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package org.openslx.imagemaster.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.Random;

import org.apache.log4j.Logger;
import org.openslx.imagemaster.Globals;
import org.openslx.imagemaster.Globals.PropString;

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 friendsly 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 );
		}
	}

	/**
	 * Static {@link Random} instance.
	 */
	private static final Random random = new Random();

	/**
	 * Return a random integer in the range of 0 (inclusive) and
	 * n (exclusive). Uses the internal static instance of {@link Random},
	 * so you don't have to deal with instances everywhere.
	 * 
	 * @param n the upper bound (exclusive)
	 * @return number between 0 (inclusive) and n (exclusive)
	 */
	public static int randomInt( int n )
	{
		return random.nextInt( n );
	}

	/**
	 * Remove a folder and all contents
	 * 
	 * @param folder
	 */
	public static void deleteFolder( File folder )
	{
		File[] files = folder.listFiles();
		if ( files != null ) {
			for ( File f : files ) {
				if ( f.isDirectory() ) {
					deleteFolder( f );
				} else {
					f.delete();
				}
			}
		}
		folder.delete();
	}
	
}