/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Bosquet - charles.bosquet@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.web;

import fr.gouv.finances.cp.xemelios.data.impl.MySqlDataLayer;
import fr.gouv.finances.dgfip.xemelios.common.Constants;
import fr.gouv.finances.dgfip.xemelios.services.XemeliosHelper;
import fr.gouv.finances.dgfip.xemelios.web.utils.ParametreManager;
import java.io.File;
import javax.servlet.ServletContextEvent;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

/**
 * @author CBO
 */
public class Application {

    private static Logger logger = null;
    static private final String configFileParameterName = "lo4j";

    static public void start(ServletContextEvent event) {
        try {
            initLog4j(event);
        } catch (RuntimeException re) {
            re.printStackTrace();
            throw (re);
        }
        try {
            XemeliosHelper.init(event);
        } catch (Exception e) {
            logger.error(e, e);
            throw new RuntimeException(e);
        }
    }

    static public void stop(ServletContextEvent event) {
        XemeliosHelper.shutdown();
        LogManager.shutdown();
    }

    static private void initLog4j(ServletContextEvent event) {
        String confFileAsString = event.getServletContext().getInitParameter(configFileParameterName);
        if (confFileAsString == null) {
            confFileAsString = "./WEB-INF/log4j.xml";
        }
        File confFile = null;
        if (confFileAsString.startsWith("./")) {
            confFile = new File(event.getServletContext().getRealPath("/"), confFileAsString.substring(2));
        } else {
            confFile = new File(confFileAsString);
        }
        event.getServletContext().log("Initialisation de Log4J à  partir du fichier : " + confFile);
        if (!confFile.exists()) {
            throw new IllegalArgumentException("Fichier de configuration log4j spécifié non trouvable : " + confFile.getAbsolutePath());
        }
        System.setProperty("webApp.home", event.getServletContext().getRealPath("/"));
        System.setProperty("webApp.name", new File(event.getServletContext().getRealPath("/")).getName());
        DOMConfigurator.configureAndWatch(confFile.getAbsolutePath());
        logger = Logger.getLogger(Application.class);
    }
}
