summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/Globals.java
blob: a62ee9edcb50228f3f4a7dc663fb560b794365b5 (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 boolean loadedProperties = false;
	private static SSLContext context = null;

	/***********************************************************************************************/
	/**
	 * 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 FILETRANSFERSERVERIP you have to call
	 * Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ) which returns a string.
	 */
	public static enum PropInt
	{
		FILETRANSFERPORT, THRIFTPORT, BLOCKSIZE		// More int's? Add them separated with ","
	}

	public static enum PropString
	{ // More strings's? Add them separated with ","
		FILETRANSFERSERVERIP, KEYSTORETYPE, FILETRANSFERSKEYSTOREPATH, FILETRANSFERSKEYSTOREPWD, THRIFTORGANIZATIONNAME,
		RNDSTRINGENCRYPTALIAS, RNDSTRINGENCRYPTPASSWORD, RNDSTRINGENCRYPTPATH
	}

	/***********************************************************************************************/
	/**
	 * 
	 * @return
	 * @throws IOException
	 */
	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;
	}

	/***********************************************************************************************/
	/**
	 * 
	 * @param props
	 * @return
	 */
	public static int getPropertyInt( Globals.PropInt props )
	{
		String result = null;

		switch ( props ) {
		case FILETRANSFERPORT:
			result = properties.getProperty( "filetransfer_port" );
			break;
		case BLOCKSIZE:
			result = properties.getProperty( "blockSize" );
			break;
		case THRIFTPORT:
			result = properties.getProperty( "ThriftPort" );
			break;
		default:
			result = "0";
			break;
		}
		if ( result == null )
			return 0;

		return Integer.valueOf( result );
	}

	/***********************************************************************************************/
	/**
	 * 
	 * @param props
	 * @return
	 */
	public static String getPropertyString( Globals.PropString props )
	{
		String result = null;

		switch ( props ) {
		case FILETRANSFERSERVERIP:
			result = properties.getProperty( "filetransfer_server_ip" );
			break;
		case KEYSTORETYPE:
			result = properties.getProperty( "keyStore_type" );
			break;
		case FILETRANSFERSKEYSTOREPATH:
			result = properties.getProperty( "path_to_filetransfer_keyStore" );
			break;
		case FILETRANSFERSKEYSTOREPWD:
			result = properties.getProperty( "filetransfer_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;
	}

	// TODO: add real checks
	/***********************************************************************************************/
	/**
	 * 
	 * @return
	 */
	public static boolean propertiesValid()
	{
		if ( Globals.getPropertyInt( PropInt.FILETRANSFERPORT ) == 0
				|| Globals.getPropertyInt( PropInt.BLOCKSIZE ) == 0
				|| Globals.getPropertyInt( PropInt.THRIFTPORT ) == 0
				|| Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ).isEmpty()
				|| Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ) == null
				|| Globals.getPropertyString( PropString.KEYSTORETYPE ).isEmpty()
				|| Globals.getPropertyString( PropString.KEYSTORETYPE ) == null
				|| Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPATH ).isEmpty()
				|| Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPATH ) == null
				|| Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPWD ).isEmpty()
				|| Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPWD ) == 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;
		}
	}

	/***********************************************************************************************/
	/**
	 * 
	 * @return
	 */
	public static boolean masterServerSslContextInit()
	{
		char[] passphrase = Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPWD ).toCharArray();
		KeyStore keystore;
		try {
			keystore = KeyStore.getInstance( "JKS" );
			keystore.load( new FileInputStream( Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPATH ) ), 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("FileNotFoundException");
			return false;
		} catch ( IOException e ) {
			log.error("IOException");
			return false;
		} catch ( KeyManagementException e ) {
			log.error("KeyManagementException");
			return false;
		}
		return true;
	}
	
	
	/***********************************************************************************************/
	/**
	 * 
	 * @return
	 */
	public static SSLContext getMasterServerSslContext(){
		return Globals.context;
	}
}