summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/Globals.java
blob: 72897607dcc78fe965316cfa9f0f47077e700e2a (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package org.openslx.imagemaster;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
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();
	
	/* CONSTANTS */
	/**
	 * The blocksize used for crc'ing.
	 */
	public final static int blockSize = 16 * 1024 * 1024;

	public static void init()
	{
	}

	/**
	 * Loads the properties from config/global.properties
	 * 
	 * @throws IOException
	 */
	static
	{
		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( getSslSocketPort(), "SSL socket port must be set." );
			Util.notNullFatal( getSslTimeout(), "SSL socket timeout must be set." );

			if ( getSslTransmitTimes() <= 0 )
				log.fatal( "SSL socket transmitted times must be greater than 0." );

			// check ldap_bind_query
			if ( StringUtils.countMatches( getLdapBindQuery(), "%" ) == 0 ) {
				log.fatal( "ldap_bind_query does not contain '%'" );
				System.exit( 2 );
			}

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

			// 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 );
		}
		log.info( "Loaded properties successfully" );
	}

	/* INTEGERS */

	public static int getLdapPort()
	{
		return Util.tryToParseInt( properties.getProperty( "ldap_port" ) );
	}

	public static int getSessionTimeoutUser()
	{
		return Util.tryToParseInt( properties.getProperty( "session_timeout_user" ) );
	}

	public static int getSessionTimeoutServer()
	{
		return Util.tryToParseInt( properties.getProperty( "session_timeout_user" ) );
	}

	public static int getSslSocketPort()
	{
		return Util.tryToParseInt( properties.getProperty( "ssl_socket_port" ) );
	}

	public static int getSslTimeout()
	{
		return Util.tryToParseInt( properties.getProperty( "ssl_socket_timeout" ) );
	}

	public static int getSslTransmitTimes()
	{
		return Util.tryToParseInt( properties.getProperty( "ssl_socket_transmit_times" ) );
	}
	
	public static int getCrcSchedulingInterval()
	{
		return Util.tryToParseInt( properties.getProperty("crc_scheduling_interval") );
	}

	/* STRINGS */

	public static String getImageDir()
	{
		return properties.getProperty( "image_dir" );
	}

	public static String getSslKeystoreFile()
	{
		return properties.getProperty( "ssl_keystore_file" );
	}

	public static String getSslKeystoreAlias()
	{
		return properties.getProperty( "ssl_keystore_alias" );
	}

	public static String getSslKeystorePassword()
	{
		return properties.getProperty( "ssl_keystore_password" );
	}

	public static String getLdapHost()
	{
		return properties.getProperty( "ldap_host" );
	}

	public static String getLdapBindQuery()
	{
		return properties.getProperty( "ldap_bind_query" );
	}

	public static String getLdapSearchBaseDn()
	{
		return properties.getProperty( "ldap_search_base_dn" );
	}

	public static String getLdapSearchFilter()
	{
		return properties.getProperty( "ldap_search_filter" );
	}

	public static String getLdapKeystorePassword()
	{
		return properties.getProperty( "ldap_keystore_password" );
	}

	public static String getLdapKeystorePath()
	{
		return properties.getProperty( "ldap_keystore_path" );
	}

	/* BOOLEANS */

	public static boolean getLdapSsl()
	{
		return Boolean.valueOf( properties.getProperty( "ldap_ssl" ) );
	}
}