summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/Configuration.java
blob: 8b6caf3d396d1a57cf4b2cebed7c6920b00637aa (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
package org.openslx.bwlp.sat.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

import javax.net.ssl.SSLContext;

import org.apache.log4j.Logger;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.openslx.util.Util;

public class Configuration {

	private static final Logger LOGGER = Logger.getLogger(Configuration.class);
	private static final DateTimeFormatter subdirDate = DateTimeFormat.forPattern("yy-MM");

	private static File vmStoreBasePath;
	private static File vmStoreProdPath;
	private static String dbUri;
	private static String dbUsername;
	private static String dbPassword;
	private static String masterAddress;
	private static boolean masterSsl = true;
	private static int masterPort = 9091;
	private static String dbLocationTable;
	private static SSLContext ctx = null;

	public static boolean load() throws IOException {
		// Load configuration from java properties file
		Properties prop = new Properties();
		InputStream in = new FileInputStream("./config.properties");
		try {
			prop.load(in);
		} finally {
			in.close();
		}

		vmStoreBasePath = new File(prop.getProperty("vmstore.path"));
		vmStoreProdPath = new File(vmStoreBasePath, "bwlehrpool_store");
		dbUri = prop.getProperty("db.uri");
		dbUsername = prop.getProperty("db.username");
		dbPassword = prop.getProperty("db.password");
		dbLocationTable = prop.getProperty("db.location-table");
		masterAddress = prop.getProperty("master.address");
		if (!Util.isEmptyString(prop.getProperty("master.ssl"))) {
			masterSsl = Boolean.parseBoolean(prop.getProperty("master.ssl"));
		}
		try {
			masterPort = Integer.parseInt(prop.getProperty("master.port"));
		} catch (Exception e) {
		}

		// Currently all fields are mandatory but there might be optional settings in the future
		return vmStoreBasePath != null && dbUri != null && dbUsername != null && dbPassword != null;
	}

	// Static ("real") fields

	/**
	 * Get the path of the VM storage space. Traditionally, this is
	 * '/srv/openslx/nfs'.
	 * 
	 * @return path of the VM storage
	 */
	public static File getVmStoreBasePath() {
		return vmStoreBasePath;
	}

	public static String getDbUri() {
		return dbUri;
	}

	public static String getDbUsername() {
		return dbUsername;
	}

	public static String getDbPassword() {
		return dbPassword;
	}

	public static String getDbLocationTable() {
		return dbLocationTable;
	}

	public static File getVmStoreProdPath() {
		return vmStoreProdPath;
	}

	public static String getMasterServerAddress() {
		return masterAddress;
	}

	public static boolean getMasterServerSsl() {
		return masterSsl;
	}

	public static int getMasterServerPort() {
		return masterPort;
	}

	// Dynamically Computed fields

	public static File getCurrentVmStorePath() {
		return new File(vmStoreProdPath, subdirDate.print(System.currentTimeMillis()));
	}

	public static SSLContext getMasterServerSslContext() throws NoSuchAlgorithmException,
			KeyManagementException {
		if (!getMasterServerSsl())
			throw new RuntimeException("SSL not activated");
		if (ctx == null) {
			synchronized (LOGGER) {
				if (ctx == null) {
					ctx = SSLContext.getInstance("TLSv1.2");
					ctx.init(null, null, null);
				}
			}
		}
		return ctx;
	}

}