ca.qc.cegepoutaouais.tge.pige.server.Startup.java Source code

Java tutorial

Introduction

Here is the source code for ca.qc.cegepoutaouais.tge.pige.server.Startup.java

Source

/*
 * Copyright 2010, 2011 Renaud Brub
 *
 * This file is part of PIGE.
 *
 * PIGE 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 3 of the License, or
 * (at your option) any later version.
 *
 * PIGE 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 PIGE.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.qc.cegepoutaouais.tge.pige.server;

import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Renaud Brub
 */
public class Startup implements ServletContextListener {

    private static final Logger logger = LogHelper.getLogger(Startup.class);
    private SessionFactory sf = null;
    private LoanTermVerificatorTask ltf = null;
    private MaintenanceRecallServiceTask mrs = null;
    private Timer ltfTimer = null;
    private Timer mrsTimer = null;
    private Configuration hConfig = null;

    @Override
    public void contextInitialized(ServletContextEvent sce) {

        logger.info("Initialisations des ressources du serveur...");
        // Obtenir le fichier contenant les configurations du serveur.
        String key = sce.getServletContext().getInitParameter("configurations");
        InputStream is = sce.getServletContext().getResourceAsStream(key);
        Configurations.init(is);

        logger.info("Configuration du module Hibernate...");
        // Charger les configurations contenu dans le fichier:
        // WEB-INF/classes/hibernate.cfg.xml
        hConfig = new Configuration().configure();
        assert hConfig != null : "hConfig ne doit pas tre nul!";
        sf = hConfig.buildSessionFactory();

        // Obtenir les paramtres pour la vrification des chances des emprunts.
        String startHour = Configurations.getProperties().getProperty("server.loan_verificator.run.start_time");
        Integer interval = Integer
                .parseInt(Configurations.getProperties().getProperty("server.loan_verificator.run.interval"));
        if (interval <= 0 || interval >= 168) {
            logger.info("L'inverval d'excution de la vrification des chances "
                    + "des prts est en dehors des valeurs autorises." + "L'inverval a donc t fix  1.");
            interval = 1;
        }
        // Convertir en millisecondes.
        final int MS_FACTOR = 60 * 60 * 1000;
        interval = interval * MS_FACTOR;
        // Convertir en date.
        Date startDate = ServerUtil.getStartDate(startHour);
        ltf = new LoanTermVerificatorTask();
        logger.info("Dmarrage du processus de vrification des chances des emprunts...\n"
                + "   Dmarrage cdul pour: " + startDate.toString() + "\n" + "   Interval d'excution: "
                + (interval / MS_FACTOR) + " heure(s)");
        ltfTimer = new Timer(LoanTermVerificatorTask.class.getSimpleName());
        ltfTimer.scheduleAtFixedRate(ltf, startDate, interval);

        Boolean startVerificationOnStartup = Boolean.parseBoolean(
                Configurations.getProperties().getProperty("server.loan_verificator.run.on_startup", "false"));
        if (startVerificationOnStartup) {
            logger.info("Dmarrage immdiat du vrificateur des chances des " + "emprunts...");
            Timer t = new Timer(LoanTermVerificatorTask.class.getSimpleName());
            t.schedule(new LoanTermVerificatorTask(), new Date()); // Maintenant.
        }

        // MRS
        // Obtenir les paramtres pour le service de rappel des maintenances.
        startHour = Configurations.getProperties().getProperty("server.maintenance_recall_service.run.start_time");
        interval = Integer.parseInt(
                Configurations.getProperties().getProperty("server.maintenance_recall_service.run.interval"));
        if (interval <= 0 || interval >= 168) {
            logger.info("L'inverval d'excution du service de rappel des "
                    + "maintenances est en dehors des valeurs autorises." + "L'inverval a donc t fix  1.");
            interval = 1;
        }
        interval = interval * MS_FACTOR;
        startDate = ServerUtil.getStartDate(startHour);
        mrs = new MaintenanceRecallServiceTask();
        logger.info("Dmarrage du service de rappel des maintenances...\n" + "   Dmarrage cdul pour: "
                + startDate.toString() + "\n" + "   Interval d'excution: " + (interval / MS_FACTOR) + " heure(s)");
        mrsTimer = new Timer(LoanTermVerificatorTask.class.getSimpleName());
        mrsTimer.scheduleAtFixedRate(mrs, startDate, interval);

        Boolean startExecutionOnStartup = Boolean.parseBoolean(Configurations.getProperties()
                .getProperty("server.maintenance_recall_service.run.on_startup", "false"));
        if (startExecutionOnStartup) {
            logger.info("Dmarrage immdiat du service de rappel des " + "maintenances...");
            Timer t = new Timer(MaintenanceRecallServiceTask.class.getSimpleName());
            t.schedule(new MaintenanceRecallServiceTask(), new Date()); // Maintenant.
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

        if (mrsTimer != null) {
            logger.info("Arrt du service de rappel des maintenances. [Timer]");
            mrsTimer.cancel();
        }

        if (mrs != null) {
            logger.info("Arrt du service de rappel des maintenances. [TimerTask]");
            mrs.cancel();
        }

        if (ltfTimer != null) {
            logger.info("Arrt du service de vrification des chances des emprunts. [Timer]");
            ltfTimer.cancel();
        }

        if (ltf != null) {
            logger.info("Arrt du service de vrification des chances des emprunts. [TimerTask]");
            ltf.cancel();
        }

        if (sf != null) {
            logger.info("Fermeture de l'usine  session d'Hibernate.");
            sf.close();
            sf = null;
        }

        try {
            logger.info("Dsenregistrement du pilote JDBC.");
            if (hConfig != null) {
                DriverManager
                        .deregisterDriver(DriverManager.getDriver(hConfig.getProperty("hibernate.connection.url")));
            }
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }

    }

}