summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/Globals.java
blob: 2cf594cdcd3a43f5d915fd6a99748ef816aa85bb (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11



                                    
                                     
                           




                                               

                            





                                         

                                                                    
                                                                      
                                                 
 


                                                                                
         
 




                                                                                                         




                                                                   

         


                                                               
 


                                                                            
 

                                                                                

         


                                                                   
 

                                                                       
         
 






                                                                          



                                                              

                      


                                                                            

         
           
                          
           



                                                                              
                                                                                                 




                                                                         
                 


                                                                                                   
         
 

                                                                                                         


                  

                                                                                    

                                  






                                                                                                
                                                                              

                                                                

                                                       
                                                      

                                                              
                                                  

                                                          

                                                                                                                                        
                                     
                                         

                                                 
                                                    




                                                            




                                                              
           
                                                     
           


                                               
           














                                                                                    
         
 
package org.openslx.satellitedaemon;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Properties;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

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 SSLContext context = null;

	public static final int BLOCKSIZE = 16 * 1024 * 1024; // 16 MB blocksize

	public static void init() {
	}

	/***********************************************************************************************/
	/**
	 * A call of Globals.getXXXXXX() returns the corresponding entry in config/global.properties
	 */
	
	// * Properties *//

	// Strings //
	public static String getMasterserverHost() {
		return properties.getProperty("MASTERSERVER_HOST");
	}

	public static String getKeystoreType() {
		return properties.getProperty("KEYSTORE_TYPE");
	}

	public static String getFiletransferKeystorePath() {
		return properties.getProperty("FILETRANSFER_KEYSTORE_PATH");
	}

	public static String getFiletransferKeystorePassword() {
		return properties.getProperty("FILETRANSFER_KEYSTORE_PASSWORD");
	}

	public static String getOrganizationName() {
		return properties.getProperty("ORGANIZATION_NAME");
	}

	public static String getThriftKeystoreAlias() {
		return properties.getProperty("THRIFT_KEYSTORE_ALIAS");
	}

	public static String getThriftKeystorePassword() {
		return properties.getProperty("THRIFT_KEYSTORE_PASSWORD");
	}

	public static String getThriftKeystorePath() {
		return properties.getProperty("THRIFT_KEYSTORE_PATH");
	}
	
	public static String getImageFolder() {
		return properties.getProperty("IMAGE_FOLDER");
	}

	// Integers //

	public static int getThriftPort() {
		return tryToParseInt(properties.getProperty("THRIFT_PORT"));
	}

	/**
	 * Load properties
	 */
	static {
		try {
			// Load all entries of the config file into properties
			BufferedInputStream stream = new BufferedInputStream(
					new FileInputStream("config/global.properties.example"));
			properties.load(stream);
			stream.close();
		} catch (IOException e) {
			log.error("Could not load properties. Exiting.");
			System.exit(2);
		}
		
		notNullOrEmptyFatal(getMasterserverHost(), "Masterserver Host must not be empty!");
		// TODO: check properties
	}

	/***********************************************************************************************/
	/**
	 * 
	 * @return
	 */
	public static boolean masterServerSslContextInit() {
		char[] passphrase = getFiletransferKeystorePassword().toCharArray();
		KeyStore keystore;
		try {
			keystore = KeyStore.getInstance("JKS");
			keystore.load(new FileInputStream(getFiletransferKeystorePath()),
					passphrase);
			TrustManagerFactory tmf = TrustManagerFactory
					.getInstance(TrustManagerFactory.getDefaultAlgorithm());
			tmf.init(keystore);
			context = SSLContext.getInstance("SSLv3");
			TrustManager[] trustManagers = tmf.getTrustManagers();
			context.init(null, trustManagers, null);
		} catch (KeyStoreException e) {
			log.error("KeyStoreException");
			return false;
		} catch (NoSuchAlgorithmException e) {
			log.error("NoSuchAlgorithmException");
			return false;
		} catch (CertificateException e) {
			log.error("CertificateException");
			return false;
		} catch (FileNotFoundException e) {
			log.error("Could not find the keystore for the filetransfer. Path was '" + getFiletransferKeystorePath() + "'");
			return false;
		} catch (IOException e) {
			log.error("IOException");
			return false;
		} catch (KeyManagementException e) {
			log.error("KeyManagementException");
			return false;
		}
		return true;
	}

	public static SSLContext getMasterServerSslContext() {
		return Globals.context;
	}

	/**
	 * Tries to parse an int. Returns 0 on error.
	 * 
	 * @param s
	 *            The strig to parse
	 * @return The parsed int or 0 on error
	 */
	public static int tryToParseInt(String s) {
		try {
			return Integer.parseInt(s);
		} catch (NumberFormatException e) {
			return 0;
		}
	}

	public static void notNullOrEmptyFatal(String something, String message) {
		if (something == null || something.isEmpty()) {
			if (message != null)
				log.fatal("[NOTNULL] " + message);
			log.warn(Thread.currentThread().getStackTrace().toString());
			System.exit(2);
		}
	}
}