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

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

import org.apache.log4j.Logger;
import org.openslx.imagemaster.util.Util;

/**
 * Class to hold global constants and properties from 'config/global.properties'.
 */
public class Globals
{

	private static Logger LOGGER = Logger.getLogger( Globals.class );
	private static final Properties properties = new Properties();
	private static File imgPath = null;

	/* CONSTANTS */
	/**
	 * The blocksize used for crc'ing.
	 */
	public final static int blockSize = 16 * 1024 * 1024;

	/**
	 * Loads the properties from config/global.properties
	 */
	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( 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( getSessionTimeoutUser(), "Session timeout user must be set." );
			Util.notNullFatal( getSessionTimeoutServer(), "Session timeout server must be set." );
			if ( getFiletransferPortSsl() == 0 && getFiletransferPortPlain() == 0 ) {
				LOGGER.fatal( "either SSL or plain port for file transfer must be set." );
				System.exit( 2 );
			}
			if ( getThriftPortSsl() == 0 && getThriftPortPlain() == 0 ) {
				LOGGER.fatal( "either SSL or plain port for thrift must be set." );
				System.exit( 2 );
			}

			if ( getFiletransferRetransmits() <= 0 ) {
				LOGGER.fatal( "SSL socket transmitted times must be greater than 0." );
				System.exit( 2 );
			}

			// check keystore
			if ( !getSslKeystoreFile().endsWith( ".jks" ) ) {
				LOGGER.fatal( "Keystore is not in jks format." );
				System.exit( 2 );
			}

			// remove "/" at the end of the paths
			String image = getImageDir();
			if ( image.endsWith( "/" ) ) {
				properties.put( "image_dir", image.substring( 0, image.length() - 1 ) );
			}

		} catch ( IOException e ) {
			LOGGER.fatal( "Could not load properties!" );
			LOGGER.warn( e.getStackTrace().toString() );
			System.exit( 2 );
		}
		LOGGER.info( "Loaded properties successfully" );
	}

	/* INTEGERS */

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

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

	public static int getFiletransferPortSsl()
	{
		return Util.tryToParseInt( properties.getProperty( "filetransfer.port.ssl" ) );
	}

	public static int getFiletransferPortPlain()
	{
		return Util.tryToParseInt( properties.getProperty( "filetransfer.port.plain" ) );
	}

	public static int getFiletransferTimeout()
	{
		return Util.tryToParseInt( properties.getProperty( "filetransfer.timeout" ) );
	}

	public static int getFiletransferRetransmits()
	{
		return Util.tryToParseInt( properties.getProperty( "filetransfer.retries" ) );
	}

	public static int getThriftPortSsl()
	{
		return Util.tryToParseInt( properties.getProperty( "thrift.port.ssl" ) );
	}

	public static int getThriftPortPlain()
	{
		return Util.tryToParseInt( properties.getProperty( "thrift.port.plain" ) );
	}

	public static long getImageValiditySeconds()
	{
		// TODO Auto-generated method stub
		return 86400l * 500;
	}

	/* STRINGS */

	public static String getImageDir()
	{
		return properties.getProperty( "storage.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 File getImagePath()
	{
		if ( imgPath == null ) {
			imgPath = new File( getImageDir() );
		}
		return imgPath;
	}

}