package com.fuse;

/*
	FUSE Light Server - multiuser server application
	Copyright (C) 2000 Aapo Kyrola / Sulake Oy  Helsinki, Finland

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


import java.io.*;
import java.util.*;
import java.text.*;
import java.rmi.*;

import com.fuse.security.*;
import com.fuse.net.*;
import com.fuse.access_control.*;
import com.fuse.storage.*;


/**
  * FUSEEnvironment is as a central class for environment variables
  * @author Aapo Kyrola
  * @copyright Aapo Kyrola
  */
public class FUSEEnvironment {
	
	private static Properties properties = new Properties();
	
	private static String propertiesFile;
	
	private static FUSERegister register;
	private static FUSEAuthenticator authenticator;
	private static DatabaseProxy databaseProxy;
	private static SecretKey secretKey;
	private static Date startupTime = new Date();
	private static Object lock = new Object();
	private static AccessControl ac;
	
	public synchronized static void reloadProperties()  throws Exception {
		properties = new Properties();
		properties.load(new FileInputStream(propertiesFile));
	}
	
	public static SecretKey getSecretKey() {
		return secretKey;
	}
	
	
	
	
	/**
	  * Initialize properties
	  */
	public static void init(String tpropertiesFile) throws Exception {
		propertiesFile = tpropertiesFile;
		properties.load(new FileInputStream(propertiesFile));
	
		authenticator = new FUSEAuthenticator();
		register = new FUSERegister();

		String className = getProperty("security.secret_key.type");
		if (className != null) {
			secretKey = (SecretKey) Class.forName(className).newInstance();
		} else {
			System.out.println("Please set security.secret_key.type-property!");
		}	
		
		databaseProxy = new DatabaseProxy(getProperty("database.rmiUrl"));
		ac = new AccessControl();
		
		System.out.println(" ");
		System.out.println(" #############################################");
		System.out.println(" FUSE Light Server version 0.5, Copyright (C) 2000 Aapo Kyrola / Sulake Oy");
   		System.out.println(" FUSE Light Server comes with ABSOLUTELY NO WARRANTY; for details see file LICENSE.txt ");
    	System.out.println(" This is free software, and you are welcome to redistribute it");
    	System.out.println(" under certain conditions; see LICENSE.txt for details.");
    	System.out.println(" #############################################");
    	System.out.println(" ");
				
		System.out.println("FUSE Light Server " + getVersion() + " running");
	}
	
	/**
	  * Returns the FUSEServer version
	  */
	public static String getVersion() {
		return "v0.5";
	}
	
	/**
	  * Returns the time when the server was started up
	  */
	public static Date getStartupTime() {
		return startupTime;
	}

    
    public static FUSERegister getRegister() {
    	return register;
    }
    
    public static FUSEAuthenticator getAuthenticator() {
    	return authenticator;
    }
    
    
    public static DatabaseProxy getDatabaseProxy() {
    	return databaseProxy;
    }
    
    /**
      * Returns an integer property 
      */
    public static int getIntProperty(String name, int def) {
    	try {
    		return Integer.parseInt(getProperty(name));	
    	} catch (NumberFormatException nfe) {
    		return def;	
    	}
    }  
    
     
    /**
      * Returns an integer property 
      */
    public static int getIntProperty(String name) throws NumberFormatException
    {
    	return Integer.parseInt(getProperty(name));	
  
    }  
    
    
	/**
	  * Returns accesscontrol object
	  */
	public static AccessControl getAccessControl() {
		return ac;
	}
	
      
    /**
     * Get one named property
     * @return the value of that property, or null if it does not exist
     */
    public static String getProperty(String name) {
        return properties.getProperty(name);
    }  
    
    public static Properties getProperties() {
    	return properties;
    }
    
    /**
     * Get one named property and use default if not found
     * @param name
     * @param def default
     * @return the value of that property, or null if it does not exist
     */
    public static String getProperty(String name, String def) {
        String s = properties.getProperty(name);
        return s == null ? def : s;
    }
    
  
}
