summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/Globals.java
blob: 3eca269ff486aa17c706a7021ee7bfa890f58da8 (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
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 final static int blockSize = 16 * 1024 * 1024;

	/**
	 * Loads the properties from config/global.properties
	 * @throws IOException
	 */
	public static void loadProperties() throws IOException
	{
		if ( loadedProperties ) return;

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

	public static boolean propertiesValid()
	{
		if ( getImageDir() == null
				|| getImageDir().isEmpty()
				|| getLdapHost() == null
				|| getLdapHost().isEmpty()
				|| getLdapBindQuery() == null
				|| getLdapBindQuery().isEmpty()
				|| getLdapSearchBaseDn() == null
				|| getLdapSearchBaseDn().isEmpty()
				|| getLdapSearchFilter() == null
				|| getLdapSearchFilter().isEmpty()
				|| getLdapKeystorePassword() == null 
				|| getLdapKeystorePassword().isEmpty()
				|| getLdapKeystorePath() == null
				|| getLdapKeystorePath().isEmpty()
				|| getFtpBaseDir() == null
				|| getFtpBaseDir().isEmpty()
				|| getFtpKeystoreFile() == null
				|| getFtpKeystoreFile().isEmpty()
				|| getFtpKeystoreAlias() == null
				|| getFtpKeystoreAlias().isEmpty()
				|| getFtpKeystorePassword() == null
				|| getFtpKeystorePassword().isEmpty()

				|| getLdapPort() == 0
				|| getSessionTimeoutUser() == 0
				|| getSessionTimeoutServer() == 0
				|| getFtpPort() == 0
				|| getFtpTimeout() == 0 ) {
			return false;
		}

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

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

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

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

		return true;
	}
	
	/* INTEGERS */
	
	public static int getLdapPort() {
		return Integer.valueOf( properties.getProperty( "ldap_port" ) );
	}
	
	public static int getSessionTimeoutUser() {
		return Integer.valueOf( properties.getProperty( "session_timeout_user" ) );
	}
	
	public static int getSessionTimeoutServer() {
		return Integer.valueOf( properties.getProperty( "session_timeout_user" ) );
	}
	
	public static int getFtpPort() {
		return Integer.valueOf( properties.getProperty( "ftp_port" ) );
	}
	
	public static int getFtpTimeout() {
		return Integer.valueOf( properties.getProperty( "ftp_timeout" ) );
	}
	
	/* STRINGS */
	
	public static String getImageDir() {
		return properties.getProperty( "image_dir" );
	}
	
	public static String getFtpKeystoreFile() {
		return properties.getProperty( "ftp_keystore_file" );
	}
	
	public static String getFtpKeystoreAlias() {
		return properties.getProperty( "ftp_keystore_alias" );
	}
	
	public static String getFtpKeystorePassword() {
		return properties.getProperty( "ftp_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" );
	}
	
	public static String getFtpBaseDir() {
		return properties.getProperty( "ftp_base_dir" );
	}
	
	/* BOOLEANS */
	
	public static boolean getLdapSsl() {
		return Boolean.valueOf( properties.getProperty( "ldap_ssl" ) );
	}
}