bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
Globals.java
Go to the documentation of this file.
1 package org.openslx.imagemaster;
2 
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.util.Properties;
8 
9 import org.apache.logging.log4j.LogManager;
10 import org.apache.logging.log4j.Logger;
12 
16 public class Globals
17 {
18 
19  private static Logger LOGGER = LogManager.getLogger( Globals.class );
20  private static final Properties properties = new Properties();
21  private static File imgPath = null;
22 
23  /* CONSTANTS */
27  public final static int blockSize = 16 * 1024 * 1024;
28 
32  static
33  {
34  try {
35  // Load properties
36  BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/global.properties" ) );
37  properties.load( stream );
38  stream.close();
39 
40  // check properties
41  Util.notNullOrEmptyFatal( getSslKeystoreFile(), "SSL keystore file must be set." );
42  Util.notNullOrEmptyFatal( getSslKeystoreAlias(), "SSL keystore alias must be set." );
43  Util.notNullOrEmptyFatal( getSslKeystorePassword(), "SSL keystore password must be set." );
44 
45  Util.notNullFatal( getSessionTimeoutUser(), "Session timeout user must be set." );
46  Util.notNullFatal( getSessionTimeoutServer(), "Session timeout server must be set." );
47 
48  if ( getThriftPortSsl() == 0 && getThriftPortPlain() == 0 ) {
49  LOGGER.fatal( "either SSL or plain port for thrift must be set." );
50  System.exit( 2 );
51  }
52 
53  if ( getFiletransferRetransmits() <= 0 ) {
54  LOGGER.fatal( "SSL socket transmitted times must be greater than 0." );
55  System.exit( 2 );
56  }
57 
58  // check keystore
59  if ( !getSslKeystoreFile().endsWith( ".jks" ) ) {
60  LOGGER.fatal( "Keystore is not in jks format." );
61  System.exit( 2 );
62  }
63 
64  // remove "/" at the end of the paths
65  String image = getImageDir();
66  if ( image.endsWith( "/" ) ) {
67  properties.put( "image_dir", image.substring( 0, image.length() - 1 ) );
68  }
69 
70  } catch ( IOException e ) {
71  LOGGER.fatal( "Could not load properties!" );
72  LOGGER.warn( e.getStackTrace().toString() );
73  System.exit( 2 );
74  }
75  LOGGER.info( "Loaded properties successfully" );
76  }
77 
78  /* INTEGERS */
79 
80  public static int getSessionTimeoutUser()
81  {
82  return Util.tryToParseInt( properties.getProperty( "session.user.timeout" ) );
83  }
84 
85  public static int getSessionTimeoutServer()
86  {
87  return Util.tryToParseInt( properties.getProperty( "session.server.timeout" ) );
88  }
89 
90  public static int getFiletransferPortSsl()
91  {
92  return Util.tryToParseInt( properties.getProperty( "filetransfer.port.ssl" ) );
93  }
94 
95  public static int getFiletransferPortPlain()
96  {
97  return Util.tryToParseInt( properties.getProperty( "filetransfer.port.plain" ) );
98  }
99 
100  public static int getFiletransferTimeout()
101  {
102  return Util.tryToParseInt( properties.getProperty( "filetransfer.timeout" ) );
103  }
104 
105  public static int getFiletransferRetransmits()
106  {
107  return Util.tryToParseInt( properties.getProperty( "filetransfer.retries" ) );
108  }
109 
110  public static int getThriftPortSsl()
111  {
112  return Util.tryToParseInt( properties.getProperty( "thrift.port.ssl" ) );
113  }
114 
115  public static int getThriftPortPlain()
116  {
117  return Util.tryToParseInt( properties.getProperty( "thrift.port.plain" ) );
118  }
119 
120  public static long getImageValiditySeconds()
121  {
122  // TODO Auto-generated method stub
123  return 86400l * 500;
124  }
125 
126  public static long getOldImageExpireTimeSeconds()
127  {
128  // TODO Auto-generated method stub
129  return 86400l * 30;
130  }
131 
132  /* STRINGS */
133 
134  public static String getImageDir()
135  {
136  return properties.getProperty( "storage.dir" );
137  }
138 
139  public static String getSslKeystoreFile()
140  {
141  return properties.getProperty( "ssl.keystore.file" );
142  }
143 
144  public static String getSslKeystoreAlias()
145  {
146  return properties.getProperty( "ssl.keystore.alias" );
147  }
148 
149  public static String getSslKeystorePassword()
150  {
151  return properties.getProperty( "ssl.keystore.password" );
152  }
153 
154  public static File getImagePath()
155  {
156  if ( imgPath == null ) {
157  imgPath = new File( getImageDir() );
158  }
159  return imgPath;
160  }
161 
162  public static boolean isReadOnlyMode()
163  {
164  return ( getFiletransferPortPlain() <= 0 && getFiletransferPortSsl() <= 0 )
165  || Util.isEmpty( getImageDir() );
166  }
167 
168 }
static int getFiletransferPortSsl()
Definition: Globals.java:90
static long getImageValiditySeconds()
Definition: Globals.java:120
static int getFiletransferTimeout()
Definition: Globals.java:100
static int getFiletransferRetransmits()
Definition: Globals.java:105
static boolean isReadOnlyMode()
Definition: Globals.java:162
static final int blockSize
The blocksize used for crc'ing.
Definition: Globals.java:27
Class to hold global constants and properties from 'config/global.properties'.
Definition: Globals.java:16
static int getSessionTimeoutUser()
Definition: Globals.java:80
static int getSessionTimeoutServer()
Definition: Globals.java:85
static String getSslKeystoreFile()
Definition: Globals.java:139
static long getOldImageExpireTimeSeconds()
Definition: Globals.java:126
static String getSslKeystoreAlias()
Definition: Globals.java:144
static int getFiletransferPortPlain()
Definition: Globals.java:95
Some utilities to make our lives easier.
Definition: Util.java:18
static void notNullFatal(Object something, String message)
Check if the given object is null, abort program if true.
Definition: Util.java:36
static String getSslKeystorePassword()
Definition: Globals.java:149
static int tryToParseInt(String s)
Tries to parse an int.
Definition: Util.java:142
static boolean isEmpty(String str)
Definition: Util.java:123
static final Properties properties
Definition: Globals.java:20
static void notNullOrEmptyFatal(String something, String message)
Check if String is null or empty, abort program if so.
Definition: Util.java:76