
package com.exedio.cope.lib;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

final class Properties
{
	private static Properties theInstance = null;
	
	static Properties getInstance()
	{
		if(theInstance!=null)
			return theInstance;
		
		theInstance = new Properties();
		return theInstance;
	}

	private static final String FILE_NAME_PROPERTY = "com.exedio.cope.properties";
	private static final String DEFAULT_FILE_NAME = "cope.properties";
	
	private File file = null;

	private final String database;
	private final String databaseDriver;
	private final String databaseUrl;
	private final String databaseUser;
	private final String databasePassword;

	private final File mediaDirectory;
	private final String mediaUrl;

	private Properties()
	{
		final java.util.Properties properties = new java.util.Properties();
		FileInputStream stream = null;
		try
		{
			String filename = System.getProperty(FILE_NAME_PROPERTY);
			if(filename==null)
				filename = DEFAULT_FILE_NAME;
			
			file = new File(filename);
			stream = new FileInputStream(file);
			properties.load(stream);
		}
		catch(IOException e)
		{
			throw new InitializerRuntimeException(e, "ERROR: property file "+file.getAbsolutePath()+" not found.");
		}
		finally
		{
			if(stream!=null)
			{
				try
				{
					stream.close();
				}
				catch(IOException e) {}
			}
		}
		database = getPropertyNotNull(properties, "database");
		databaseDriver = getPropertyNotNull(properties, "database.driver");
		databaseUrl = getPropertyNotNull(properties, "database.url");
		databaseUser = getPropertyNotNull(properties, "database.user");
		databasePassword = getPropertyNotNull(properties, "database.password");

		final String mediaDirectoryString  = properties.getProperty("media.directory");
		if(mediaDirectoryString!=null)
		{
			final File mediaDirectoryTest = new File(mediaDirectoryString);

			if(!mediaDirectoryTest.exists())
				throw new InitializerRuntimeException("ERROR: media directory "+mediaDirectoryTest.getAbsolutePath()+" does not exist.");
			if(!mediaDirectoryTest.isDirectory())
				throw new InitializerRuntimeException("ERROR: media directory "+mediaDirectoryTest.getAbsolutePath()+" is not a directory.");
			if(!mediaDirectoryTest.canRead())
				throw new InitializerRuntimeException("ERROR: media directory "+mediaDirectoryTest.getAbsolutePath()+" is not readable.");
			if(!mediaDirectoryTest.canWrite())
				throw new InitializerRuntimeException("ERROR: media directory "+mediaDirectoryTest.getAbsolutePath()+" is not writable.");
			try
			{
				mediaDirectory = mediaDirectoryTest.getCanonicalFile();
			}
			catch(IOException e)
			{
				throw new InitializerRuntimeException(e);
			}
			mediaUrl  = getPropertyNotNull(properties, "media.url");
		}
		else
		{
			mediaDirectory = null;
			mediaUrl  = null;
		}
	}
	
	private String getPropertyNotNull(final java.util.Properties properties, final String key)
	{
		final String result = properties.getProperty(key);
		if(result==null)
			throw new InitializerRuntimeException("ERROR: property "+key+" in "+file.getAbsolutePath()+" not set.");

		return result;
	}

	public String getDatabase()
	{
		return database;
	}

	public String getDatabaseDriver()
	{
		return databaseDriver;
	}

	public String getDatabaseUrl()
	{
		return databaseUrl;
	}

	public String getDatabaseUser()
	{
		return databaseUser;
	}

	public String getDatabasePassword()
	{
		return databasePassword;
	}
	
	public File getMediaDirectory()
	{
		if(mediaDirectory==null)
			throw new InitializerRuntimeException("ERROR: property media.directory in "+file.getAbsolutePath()+" not set.");

		return mediaDirectory;
	}

	public String getMediaUrl()
	{
		if(mediaDirectory==null)
			throw new InitializerRuntimeException("ERROR: property media.directory in "+file.getAbsolutePath()+" not set.");

		return mediaUrl;
	}

}

@@ -1187,23 +1187,16 @@
 ion(e, %22
-ERROR: 
 property
@@ -1953,39 +1953,32 @@
 ntimeException(%22
-ERROR: 
 media directory 
@@ -2113,39 +2113,32 @@
 ntimeException(%22
-ERROR: 
 media directory 
@@ -2273,39 +2273,32 @@
 ntimeException(%22
-ERROR: 
 media directory 
@@ -2439,23 +2439,16 @@
 eption(%22
-ERROR: 
 media di
@@ -3005,39 +3005,32 @@
 ntimeException(%22
-ERROR: 
 property %22+key+%22
@@ -3509,39 +3509,32 @@
 ntimeException(%22
-ERROR: 
 property media.d
@@ -3719,15 +3719,8 @@
 on(%22
-ERROR: 
 prop

remove ERROR: from messages