summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/Globals.java
blob: a03d401fdfcee4dbd320f1662f70cfd5e06c0694 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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;

public class Globals
{

	private static Logger log = Logger.getLogger( Globals.class );
	private static final Properties properties = new Properties();
	private static boolean loadedProperties = false;

	public static enum PropInt
	{
		LDAPPORT, SESSIONTIMEOUTUSER, SESSIONTIMEOUTSERVER, FTPPORT, FTPTIMEOUT
	}

	public static enum PropString
	{
		IMAGEDIR, FTPKEYSTOREFILE, FTPKEYSTOREALIAS, FTPKEYSTOREPASSWORD, LDAPHOST, LDAPBINDQUERY, LDAPSEARCHBASEDN, LDAPSEARCHFILTER, LDAPKEYSTOREPASSWORD, LDAPKEYSTOREPATH, FTPBASEDIR
	}

	public static enum PropBool
	{
		LDAPSSL
	}

	/**
	 * Loads the properties from config/global.properties
	 * 
	 * @return if the properties were loaded or not
	 * @throws IOException
	 */
	public static boolean loadProperties() throws IOException
	{
		if ( loadedProperties )
			return false;

		// Load properties
		BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/global.properties" ) );
		properties.load( stream );
		stream.close();

		return true;
	}

	public static boolean propertiesValid()
	{
		if ( Globals.getPropertyString( PropString.IMAGEDIR ) == null
				|| Globals.getPropertyString( PropString.IMAGEDIR ).isEmpty()
				|| Globals.getPropertyString( PropString.LDAPHOST ) == null
				|| Globals.getPropertyString( PropString.LDAPHOST ).isEmpty()
				|| Globals.getPropertyString( PropString.LDAPBINDQUERY ) == null
				|| Globals.getPropertyString( PropString.LDAPBINDQUERY ).isEmpty()
				|| Globals.getPropertyString( PropString.LDAPSEARCHBASEDN ) == null
				|| Globals.getPropertyString( PropString.LDAPSEARCHBASEDN ).isEmpty()
				|| Globals.getPropertyString( PropString.LDAPSEARCHFILTER ) == null
				|| Globals.getPropertyString( PropString.LDAPSEARCHFILTER ).isEmpty()
				|| Globals.getPropertyString( PropString.LDAPKEYSTOREPASSWORD ) == null 
				|| Globals.getPropertyString( PropString.LDAPKEYSTOREPASSWORD ).isEmpty()
				|| Globals.getPropertyString( PropString.LDAPKEYSTOREPATH ) == null
				|| Globals.getPropertyString( PropString.LDAPKEYSTOREPATH ).isEmpty()
				|| Globals.getPropertyString( PropString.FTPBASEDIR ) == null
				|| Globals.getPropertyString( PropString.FTPBASEDIR ).isEmpty()
				|| Globals.getPropertyString( PropString.FTPKEYSTOREFILE ) == null
				|| Globals.getPropertyString( PropString.FTPKEYSTOREFILE ).isEmpty()
				|| Globals.getPropertyString( PropString.FTPKEYSTOREALIAS ) == null
				|| Globals.getPropertyString( PropString.FTPKEYSTOREALIAS ).isEmpty()
				|| Globals.getPropertyString( PropString.FTPKEYSTOREPASSWORD ) == null
				|| Globals.getPropertyString( PropString.FTPKEYSTOREPASSWORD ).isEmpty()

				|| Globals.getPropertyInt( PropInt.LDAPPORT ) == 0
				|| Globals.getPropertyInt( PropInt.SESSIONTIMEOUTUSER ) == 0
				|| Globals.getPropertyInt( PropInt.SESSIONTIMEOUTSERVER ) == 0
				|| Globals.getPropertyInt( PropInt.FTPPORT ) == 0
				|| Globals.getPropertyInt( PropInt.FTPTIMEOUT ) == 0 ) {
			return false;
		}

		// check ldap_bind_query
		if ( StringUtils.countMatches( Globals.getPropertyString( PropString.LDAPBINDQUERY ), "%" ) == 0 ) {
			log.error( "ldap_bind_query does not contain '%'" );
			return false;
		}

		// check ldap_search_filter
		if ( StringUtils.countMatches( Globals.getPropertyString( PropString.LDAPSEARCHFILTER ), "%" ) == 0) {
			log.error( "ldap_search_filter does not contain '%'" );
			return false;
		}
		
		// check keystore
		if ( !Globals.getPropertyString( PropString.FTPKEYSTOREFILE ).endsWith( ".jks" )) {
			log.error( "Keystore is not in jks format." );
			return false;
		}

		// remove "/" at the end of the paths
		String ftp = Globals.getPropertyString( PropString.FTPBASEDIR );
		if ( ftp.endsWith( "/" ) ) {
			Globals.properties.put( "ftp_base_dir", ftp.substring( 0, ftp.length() - 1 ) );
		}

		String image = Globals.getPropertyString( PropString.IMAGEDIR );
		if ( image.endsWith( "/" ) ) {
			Globals.properties.put( "image_dir", image.substring( 0, image.length() - 1 ) );
		}

		return true;
	}

	public static int getPropertyInt( Globals.PropInt props )
	{
		String result = null;

		switch ( props ) {
		case LDAPPORT:
			result = properties.getProperty( "ldap_port" );
			break;
		case SESSIONTIMEOUTUSER:
			result = properties.getProperty( "session_timeout_user" );
			break;
		case SESSIONTIMEOUTSERVER:
			result = properties.getProperty( "session_timeout_server" );
			break;
		case FTPPORT:
			result = properties.getProperty( "ftp_port" );
			break;
		case FTPTIMEOUT:
			result = properties.getProperty( "ftp_timeout" );
			break;
		default:
			result = "0";
			break;
		}

		if ( result == null )
			return 0;

		return Integer.valueOf( result );
	}

	public static String getPropertyString( Globals.PropString props )
	{
		String result = null;

		switch ( props ) {
		case IMAGEDIR:
			result = properties.getProperty( "image_dir" );
			break;
		case FTPKEYSTOREFILE:
			result = properties.getProperty( "ftp_keystore_file" );
			break;
		case FTPKEYSTOREALIAS:
			result = properties.getProperty( "ftp_keystore_alias" );
			break;
		case FTPKEYSTOREPASSWORD:
			result = properties.getProperty( "ftp_keystore_password" );
			break;
		case LDAPHOST:
			result = properties.getProperty( "ldap_host" );
			break;
		case LDAPBINDQUERY:
			result = properties.getProperty( "ldap_bind_query" );
			break;
		case LDAPSEARCHBASEDN:
			result = properties.getProperty( "ldap_search_base_dn" );
			break;
		case LDAPSEARCHFILTER:
			result = properties.getProperty( "ldap_search_filter" );
			break;
		case LDAPKEYSTOREPASSWORD:
			result = properties.getProperty( "ldap_keystore_password" );
			break;
		case LDAPKEYSTOREPATH:
			result = properties.getProperty( "ldap_keystore_path" );
			break;
		case FTPBASEDIR:
			result = properties.getProperty( "ftp_base_dir" );
			break;
		default:
			result = "";
			break;
		}

		return result;
	}

	public static boolean getPropertyBool( Globals.PropBool props )
	{
		String result = null;

		switch ( props ) {
		case LDAPSSL:
			result = properties.getProperty( "ldap_ssl" );
			break;
		default:
			result = "";
			break;
		}

		return Boolean.valueOf( result );
	}
}