package cz.hsrs.rest;

import cz.hsrs.db.pool.SQLExecutor;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.Logger;

import static io.senslog.core.ApplicationInfo.appVersion;
import static io.senslog.core.ApplicationInfo.buildVersion;

/**
 * 
 * @author mkepka
 *
 */
public class SCListener implements ServletContextListener {

    private static final Logger logger = Logger.getLogger(SCListener.class.getSimpleName());

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info(String.format("Running application version: %s build: %s.", appVersion(), buildVersion()));

        String propFile = sce.getServletContext().getRealPath("WEB-INF/database.properties");
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream(propFile));
        } catch (Exception e) {
            e.printStackTrace();
        }
        sce.getServletContext().setAttribute("props", prop);
        SQLExecutor.setProperties(prop);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        SQLExecutor.close();
    }
}