summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/Globals.java
blob: f120abce14bd4f3f5945d1a12cd536aadd5b8fa7 (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
package org.openslx.satellitedaemon;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Globals
{
	private static final Properties properties = new Properties();
	private static boolean loadedProperties = false;

	/**
	 * If there are more ints or Strings which should be added to config/global.properties,
	 * add to suiting enum, add a 'case' to getPropertyInt/String() and add checks to
	 * propertiesValid().
	 * 
	 * As an Example, if you want the value of the FTPSERVERIP you have to call
	 * Globals.getPropertyString( PropString.FTPSERVERIP ) which returns a string.
	 */
	public static enum PropInt
	{
		FTPPORT		// More int's? Add them separated with ","
	}

	public static enum PropString
	{ // More strings's? Add them separated with ","
		FTPSERVERIP, KEYSTORETYPE, FTPSKEYSTOREPATH, FTPSKEYSTOREPWD, THRIFTORGANIZATIONNAME, 
		RNDSTRINGENCRYPTALIAS, RNDSTRINGENCRYPTPASSWORD, RNDSTRINGENCRYPTPATH
	}

	public static boolean loadProperties() throws IOException
	{
		if ( loadedProperties )
			return false;

		// Load all entries of the config file into properties
		BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/global.properties" ) );
		properties.load( stream );
		stream.close();

		return true;
	}

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

		switch ( props ) {
		case FTPPORT:
			result = properties.getProperty( "ftp_port" );
			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 FTPSERVERIP:
			result = properties.getProperty( "ftp_server_ip" );
			break;
		case KEYSTORETYPE:
			result = properties.getProperty( "keyStore_type" );
			break;
		case FTPSKEYSTOREPATH:
			result = properties.getProperty( "path_to_ftps_keyStore" );
			break;
		case FTPSKEYSTOREPWD:
			result = properties.getProperty( "ftps_keyStore_password" );
			break;
		case THRIFTORGANIZATIONNAME:
			result = properties.getProperty( "organization_name" );
			break;
		case RNDSTRINGENCRYPTALIAS:
			result = properties.getProperty( "RndStringEncrypt_alias" );
			break;
		case RNDSTRINGENCRYPTPASSWORD:
			result = properties.getProperty( "RndStringEncrypt_password" );
			break;
		case RNDSTRINGENCRYPTPATH:
			result = properties.getProperty( "RndStringEncrypt_path" );
			break;
		default:
			result = "";
			break;
		}
		return result;
	}

	public static boolean propertiesValid()
	{
		if ( Globals.getPropertyInt( PropInt.FTPPORT ) == 0
				|| Globals.getPropertyString( PropString.FTPSERVERIP ).isEmpty()
				|| Globals.getPropertyString( PropString.FTPSERVERIP ) == null
				|| Globals.getPropertyString( PropString.KEYSTORETYPE ).isEmpty()
				|| Globals.getPropertyString( PropString.KEYSTORETYPE ) == null
				|| Globals.getPropertyString( PropString.FTPSKEYSTOREPATH ).isEmpty()
				|| Globals.getPropertyString( PropString.FTPSKEYSTOREPATH ) == null
				|| Globals.getPropertyString( PropString.FTPSKEYSTOREPWD ).isEmpty()
				|| Globals.getPropertyString( PropString.FTPSKEYSTOREPWD ) == null 
				|| Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ).isEmpty()
				|| Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ) == null
				|| Globals.getPropertyString( PropString.RNDSTRINGENCRYPTALIAS ).isEmpty()
				|| Globals.getPropertyString( PropString.RNDSTRINGENCRYPTALIAS ) == null
				|| Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPASSWORD ).isEmpty()
				|| Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPASSWORD ) == null
				|| Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPATH ).isEmpty()
				|| Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPATH ) == null) {
			return false;
		}
		else {
			return true;
		}
	}
}