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

                                


                                   

                            
                                            

                                                      


                    

                                                                      
 
                                                                              


                                  

                                                                                       


                                     

                                                                                                 


                                   

                       
 

                                                             
           


                                                       




                                                                 
                                  



                                                                                                                          

                            



                                                                             










                                                                                                     
 



                                                                                              
                                                                                        

                                     

                                                                                                                    

                                     

                                                                                                                       

                                     
 
                                                    
                                                                                

                                                                                                       
                 
 
                                                                                

                                                                                                        
                 
 

                            


                                                                 
                                     



















                                                                                    
                 



                                     

                                                 


                                                                          
                                     






















                                                                                 
                 
 

                              


                                                                       
                                     






                                                                      
                 
 

                                                 
 
package org.openslx.imagemaster;

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

import org.apache.commons.lang3.StringUtils;
import org.openslx.imagemaster.server.MasterFtpServer;

public class Globals
{

	private static final Properties properties = new Properties();
	private static boolean loadedProperties = false;

	public static final MasterFtpServer ftpServer = new MasterFtpServer();

	public static enum PropInt
	{
		LDAPPORT, SESSIONTIMEOUTUSER, SESSIONTIMEOUTSERVER, FTPPORT, FTPTIMEOUT
	}

	public static enum PropString
	{
		IMAGEDIR, LDAPHOST, LDAPBINDQUERY, LDAPSEARCHBASEDN, LDAPSEARCHFILTER, FTPBASEDIR
	}

	public static enum PropBool
	{
		LDAPSSL
	}

	/**
	 * Loads the properties from config/global.properties
	 * 
	 * @return if the properties were loaded or not
	 * @throws IOException
	 */
	public static boolean loadProperties() throws IOException
	{
		if ( loadedProperties )
			return false;

		// Load properties
		BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/global.properties" ) );
		properties.load( stream );
		stream.close();

		return true;
	}

	public static boolean propertiesValid()
	{
		if ( Globals.getPropertyString( PropString.IMAGEDIR ) == null
				|| Globals.getPropertyString( PropString.IMAGEDIR ).isEmpty()
				|| Globals.getPropertyString( PropString.LDAPHOST ) == null
				|| Globals.getPropertyString( PropString.LDAPHOST ).isEmpty()
				|| Globals.getPropertyString( PropString.LDAPBINDQUERY ) == null
				|| Globals.getPropertyString( PropString.LDAPBINDQUERY ).isEmpty()
				|| Globals.getPropertyString( PropString.LDAPSEARCHBASEDN ) == null
				|| Globals.getPropertyString( PropString.LDAPSEARCHBASEDN ).isEmpty()
				|| Globals.getPropertyString( PropString.LDAPSEARCHFILTER ) == null
				|| Globals.getPropertyString( PropString.LDAPSEARCHFILTER ).isEmpty()
				|| Globals.getPropertyString( PropString.FTPBASEDIR ) == null
				|| Globals.getPropertyString( PropString.FTPBASEDIR ).isEmpty()

				|| Globals.getPropertyInt( PropInt.LDAPPORT ) == 0
				|| Globals.getPropertyInt( PropInt.SESSIONTIMEOUTUSER ) == 0
				|| Globals.getPropertyInt( PropInt.SESSIONTIMEOUTSERVER ) == 0
				|| Globals.getPropertyInt( PropInt.FTPPORT ) == 0
				|| Globals.getPropertyInt( PropInt.FTPTIMEOUT ) == 0 ) {
			return false;
		}

		if ( StringUtils.countMatches( Globals.getPropertyString( PropString.LDAPBINDQUERY ), "%" ) != 1 ) {
			return false;
		}

		if ( StringUtils.countMatches( Globals.getPropertyString( PropString.LDAPSEARCHFILTER ), "%" ) != 1 ) {
			return false;
		}

		// remove "/" at the end of the path
		String ftp = Globals.getPropertyString( PropString.FTPBASEDIR );
		if ( ftp.endsWith( "/" ) ) {
			Globals.properties.put( "ftp_base_dir", ftp.substring( 0, ftp.length() - 1 ) );
		}

		String image = Globals.getPropertyString( PropString.IMAGEDIR );
		if ( image.endsWith( "/" ) ) {
			Globals.properties.put( "image_dir", image.substring( 0, image.length() - 1 ) );
		}

		return true;
	}

	public static int getPropertyInt( Globals.PropInt props )
	{
		String result = null;

		switch ( props ) {
		case LDAPPORT:
			result = properties.getProperty( "ldap_port" );
			break;
		case SESSIONTIMEOUTUSER:
			result = properties.getProperty( "session_timeout_user" );
			break;
		case SESSIONTIMEOUTSERVER:
			result = properties.getProperty( "session_timeout_server" );
			break;
		case FTPPORT:
			result = properties.getProperty( "ftp_port" );
			break;
		case FTPTIMEOUT:
			result = properties.getProperty( "ftp_timeout" );
			break;
		default:
			result = "0";
			break;
		}

		if ( result == null )
			return 0;

		return Integer.valueOf( result );
	}

	public static String getPropertyString( Globals.PropString props )
	{
		String result = null;

		switch ( props ) {
		case IMAGEDIR:
			result = properties.getProperty( "image_dir" );
			break;
		case LDAPHOST:
			result = properties.getProperty( "ldap_host" );
			break;
		case LDAPBINDQUERY:
			result = properties.getProperty( "ldap_bind_query" );
			break;
		case LDAPSEARCHBASEDN:
			result = properties.getProperty( "ldap_search_base_dn" );
			break;
		case LDAPSEARCHFILTER:
			result = properties.getProperty( "ldap_search_filter" );
			break;
		case FTPBASEDIR:
			result = properties.getProperty( "ftp_base_dir" );
			break;
		default:
			result = "";
			break;
		}

		return result;
	}

	public static boolean getPropertyBool( Globals.PropBool props )
	{
		String result = null;

		switch ( props ) {
		case LDAPSSL:
			result = properties.getProperty( "ldap_ssl" );
		default:
			result = "";
			break;
		}

		return Boolean.valueOf( result );
	}
}