package com.ttv.acs.util;


import java.io.IOException;
import java.io.InputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import org.apache.log4j.Logger;

public class ApplicationProperties extends Properties {

	private static final long serialVersionUID = -2440412148987996833L;

	// The Logger
	private static final Logger logger = Logger.getLogger(ApplicationProperties.class);

	// The path to the Application Properties file
	private static final String PROPERTIES_FILE_PATH = "/adi.properties";

	// Singleton Instance
	private static ApplicationProperties m_Instance;

	/*
	 * Private constructor to prevent instantiation
	 */
	private ApplicationProperties() throws InvalidPropertiesFormatException, IOException {
		this.loadProperties();
	}

	/**
	 * Singleton Accessor.
	 * 
	 * @return The Properties singleton instance
	 * @throws InvalidPropertiesFormatException
	 *             The Properties format is invalid
	 * @throws IOException
	 *             Failure to read the load the Properties
	 */
	public static synchronized ApplicationProperties getInstance() throws InvalidPropertiesFormatException, IOException {
		if (m_Instance == null) {
			m_Instance = new ApplicationProperties();
		}

		return m_Instance;
	}

	/*
	 * Initialize the Properties by loading the values from the Application Properties file.
	 */
	private void loadProperties() throws InvalidPropertiesFormatException, IOException {
		logger.info("Loading application properties from " + PROPERTIES_FILE_PATH);
		InputStream inStream = this.getClass().getResourceAsStream(PROPERTIES_FILE_PATH);
		this.load(inStream);
		logger.debug("Loaded properties: "  + this);
	}

}
