package cz.muni.fi.lasaris.sbms.data;

import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;

import cz.muni.fi.lasaris.sbms.auth.AuthenticationFilter;

public class Application extends ResourceConfig {
	final static Logger logger = Logger.getLogger(Application.class);
	private Properties prop;
	
	private static Application instance;

	public static Application getInstance() {
		if(instance == null) {
			instance = new Application();
		}
		return instance;
	}
	
	protected Application() {
		super();
		// http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/
		this.prop = new Properties();
		try {
			String propFileName = "data.properties";

			InputStream inputStream = Application.class.getClassLoader().getResourceAsStream(propFileName);

			if (inputStream != null) {
				this.prop.load(inputStream);
			} else {
				logger.error("property file '" + propFileName + "' not found in the classpath");
			}
		} catch (Exception ex) {
			logger.error(ex);
			logger.error("Error occured when reading properties");
		}
		
		packages("cz.muni.fi.lasaris.sbms.data.api");
		
		register(RolesAllowedDynamicFeature.class);
		if (prop.getProperty("auth.enable") != null && Boolean.parseBoolean(prop.getProperty("auth.enable"))) {
			register(AuthenticationFilter.class);
		}
		
	}
	
	public Properties getSBMSProperties() {
		
		return prop;
	}
	
	public void close() {
		
	}

}
