package yami.configuration;

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

import javax.xml.bind.*;

import org.apache.log4j.*;

import yami.model.*;

public class ConfigurationManager
{
	private static final Logger log = Logger.getLogger(ConfigurationManager.class);
	private static ConfigurationManager instance = new ConfigurationManager();
	private Yami configuration = null;
	private Date updateTime = null;
	
	private ConfigurationManager()
	{
		try
		{
			configuration = getConfFromFile(Constants.getConfPath());
			applySystemPropertiesOverXML();
			updateTime = new Date();
		}
		catch (RuntimeException e)
		{
			log.warn("Failed to read new configuration from " + Constants.getConfPath() + ". Using original from " + DateFormat.getDateTimeInstance().format(updateTime), e);
		}
	}
	
	private void applySystemPropertiesOverXML()
	{
/*		if (System.getProperty("client.port") != null)
		{
			configuration.conf.clientport = Integer.parseInt(System.getProperty("client.port"));
		}
		if (System.getProperty("server.port") != null)
		{
			configuration.conf.serverport = Integer.parseInt(System.getProperty("server.port"));
		}
		if (System.getProperty("client.path") != null)
		{
			configuration.conf.clientpath = System.getProperty("client.path");
		}
		if (System.getProperty("yami.conf") != null)
		{
			configuration.conf.conffile = System.getProperty("yami.conf");
		}
*/	}
	
	public Yami getConfFromFile(String confPath) throws RuntimeException
	{
		try
		{
			Object o = JAXBContext.newInstance(Yami.class).createUnmarshaller().unmarshal(new File(Constants.getConfPath()));
			return (Yami)o;
		}
		catch (Exception ex)
		{
			throw new RuntimeException(ex);
		}
	}
	
	public static ConfigurationManager getInstance()
	{
		return instance;
	}
	
	synchronized public Yami getCurrentConfiguration()
	{
		return configuration;
	}
	
	synchronized public void setCurrentConfiguration(Yami newConf)
	{
		configuration = newConf;
	}
	
	synchronized public Project getConfiguredProject()
	{
		if (null == configuration)
		{
			return null;
		}
		return configuration.project;
	}
	
	synchronized public GlobalConfiguration getCurrentGlobalConfiguration()
	{
		if (null == configuration)
		{
			return null;
		}
		return configuration.conf;
	}
	
}

@@ -37,17 +37,20 @@
 java.io.
-*
+File
 ;%0Aimport
@@ -60,17 +60,26 @@
 va.text.
-*
+DateFormat
 ;%0Aimport
@@ -89,17 +89,20 @@
 va.util.
-*
+Date
 ;%0A%0Aimpor
@@ -118,17 +118,27 @@
 ml.bind.
-*
+JAXBContext
 ;%0A%0Aimpor
@@ -156,17 +156,22 @@
 e.log4j.
-*
+Logger
 ;%0A%0Aimpor
@@ -183,17 +183,25 @@
 i.model.
-*
+Constants
 ;%0A%0Apubli
@@ -490,24 +490,51 @@
 anager()%0A%09%7B%0A
+%09%09updateTime = new Date();%0A
 %09%09try%0A%09%09%7B%0A%09%09
@@ -631,36 +631,8 @@
 ();%0A
-%09%09%09updateTime = new Date();%0A
 %09%09%7D%0A
@@ -829,24 +829,124 @@
 e), e);%0A%09%09%7D%0A
+%09%09catch (Exception e)%0A%09%09%7B%0A%09%09%09log.warn(%22Configuration manager failed to start, aborting.%22,e);%0A%09%09%7D%0A%09%09%0A
 %09%7D%0A%09%0A%09privat

move getDate() to beginning to prevent null date if Exception occurs.