com.dominion.salud.mpr.configuration.MPRInitializer.java Source code

Java tutorial

Introduction

Here is the source code for com.dominion.salud.mpr.configuration.MPRInitializer.java

Source

/*
 * Copyright (C) 2016 Dominion Global
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.dominion.salud.mpr.configuration;

import com.dominion.salud.mpr.negocio.configuration.MPRConstantes;
import java.io.File;
import java.util.ResourceBundle;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.FileAppender;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * Inicializador para aplicaciones web. Se ejecuta en el momento del despliegue.
 *
 * @author jcgonzalez
 */
public class MPRInitializer implements WebApplicationInitializer {

    private static final Logger logger = LoggerFactory.getLogger(MPRInitializer.class);

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.scan("com.dominion.salud.mpr.configuration");
        ctx.setServletContext(servletContext);
        System.setProperty("mpr.conf.home", findConfigurationAndLogger(ctx));
        ctx.refresh();

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
                new DispatcherServlet(ctx));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
        dispatcher.addMapping("/controller/*");
        dispatcher.addMapping("/services/*");
        servletContext.addListener(new ContextLoaderListener(ctx));

        // Configuracion GENERAL DEL MODULO
        MPRConstantes._MPR_HOME = StringUtils.endsWith(servletContext.getRealPath("/"), File.separator)
                ? servletContext.getRealPath("/")
                : servletContext.getRealPath("/") + File.separator;
        MPRConstantes._MPR_CONF_HOME = ctx.getEnvironment().getProperty("mpr.conf.home");
        MPRConstantes._MPR_VERSION = ResourceBundle.getBundle("version").getString("version");
        MPRConstantes._MPR_RESOURCES = MPRConstantes._MPR_HOME + "resources" + File.separator;
        MPRConstantes._MPR_TEMP = MPRConstantes._MPR_HOME + "WEB-INF" + File.separator + "temp" + File.separator;
        MPRConstantes._MPR_CONTEXT_NAME = servletContext.getServletContextName();
        MPRConstantes._MPR_CONTEXT_PATH = servletContext.getContextPath();
        MPRConstantes._MPR_CONTEXT_SERVER = servletContext.getServerInfo();

        // Configuracion de LOGS DEL MODULO
        if (StringUtils.isBlank(
                ((FileAppender) org.apache.log4j.Logger.getRootLogger().getAppender("LOGFILE")).getFile())) {
            ((FileAppender) org.apache.log4j.Logger.getRootLogger().getAppender("LOGFILE"))
                    .setFile(MPRConstantes._MPR_HOME + "WEB-INF" + File.separator + "classes" + File.separator
                            + "logs" + File.separator + "mpr-desktop.log");
        }
        MPRConstantes._MPR_LOGS = new File(
                ((FileAppender) org.apache.log4j.Logger.getRootLogger().getAppender("LOGFILE")).getFile())
                        .getParent();

        // Parametrizacion GENERAL DEL SISTEMA
        MPRConstantes._ENABLE_TECHNICAL_INFORMATION = StringUtils
                .isNotBlank(ctx.getEnvironment().getProperty("mpr.enable.technical.information"))
                        ? Boolean.parseBoolean(ctx.getEnvironment().getProperty("mpr.enable.technical.information"))
                        : false;

        // Parametrizacion de CONEXION A EMPI
        MPRConstantes._EMPI_ENABLE = StringUtils.isNotBlank(ctx.getEnvironment().getProperty("mpr.empi.enable"))
                ? Boolean.parseBoolean(ctx.getEnvironment().getProperty("mpr.empi.enable"))
                : false;
        MPRConstantes._EMPI_USUARIO = StringUtils.isNotBlank(ctx.getEnvironment().getProperty("mpr.empi.usuario"))
                ? ctx.getEnvironment().getProperty("mpr.empi.usuario")
                : "";
        MPRConstantes._EMPI_SISTEMA = StringUtils.isNotBlank(ctx.getEnvironment().getProperty("mpr.empi.sistema"))
                ? ctx.getEnvironment().getProperty("mpr.empi.sistema")
                : "";
        MPRConstantes._EMPI_URL = StringUtils.isNotBlank(ctx.getEnvironment().getProperty("mpr.empi.url"))
                ? ctx.getEnvironment().getProperty("mpr.empi.url")
                : "";

        // Parametrizacion de TAREAS PROGRAMADAS
        MPRConstantes._TASK_BUZON_IN_PROCESS_MESSAGES = StringUtils
                .isNotBlank(ctx.getEnvironment().getProperty("mpr.task.buzon.in.process.messages"))
                        ? ctx.getEnvironment().getProperty("mpr.task.buzon.in.process.messages")
                        : MPRConstantes._TASK_BUZON_IN_PROCESS_MESSAGES;
        MPRConstantes._TASK_BUZON_OUT_PROCESS_MESSAGES = StringUtils
                .isNotBlank(ctx.getEnvironment().getProperty("mpr.task.buzon.out.process.messages"))
                        ? ctx.getEnvironment().getProperty("mpr.task.buzon.out.process.messages")
                        : MPRConstantes._TASK_BUZON_OUT_PROCESS_MESSAGES;
        MPRConstantes._TASK_BUZON_IN_HIS_CLEAN = StringUtils
                .isNotBlank(ctx.getEnvironment().getProperty("mpr.task.buzon.in.his.clean"))
                        ? ctx.getEnvironment().getProperty("mpr.task.buzon.in.his.clean")
                        : MPRConstantes._TASK_BUZON_IN_HIS_CLEAN;
        MPRConstantes._TASK_BUZON_OUT_HIS_CLEAN = StringUtils
                .isNotBlank(ctx.getEnvironment().getProperty("mpr.task.buzon.out.his.clean"))
                        ? ctx.getEnvironment().getProperty("mpr.task.buzon.out.his.clean")
                        : MPRConstantes._TASK_BUZON_OUT_HIS_CLEAN;
        MPRConstantes._TASK_BUZON_IN_HIS_CLEAN_OLD = StringUtils
                .isNotBlank(ctx.getEnvironment().getProperty("mpr.task.buzon.in.his.clean.old"))
                        ? Integer.parseInt(ctx.getEnvironment().getProperty("mpr.task.buzon.in.his.clean.old"))
                        : MPRConstantes._TASK_BUZON_IN_HIS_CLEAN_OLD;
        MPRConstantes._TASK_BUZON_OUT_HIS_CLEAN_OLD = StringUtils
                .isNotBlank(ctx.getEnvironment().getProperty("mpr.task.buzon.out.his.clean.old"))
                        ? Integer.parseInt(ctx.getEnvironment().getProperty("mpr.task.buzon.out.his.clean.old"))
                        : MPRConstantes._TASK_BUZON_OUT_HIS_CLEAN_OLD;
        MPRConstantes._TASK_BUZON_IN_CLEAN = StringUtils
                .isNotBlank(ctx.getEnvironment().getProperty("mpr.task.buzon.in.clean"))
                        ? ctx.getEnvironment().getProperty("mpr.task.buzon.in.clean")
                        : MPRConstantes._TASK_BUZON_IN_CLEAN;
        MPRConstantes._TASK_BUZON_OUT_CLEAN = StringUtils
                .isNotBlank(ctx.getEnvironment().getProperty("mpr.task.buzon.out.clean"))
                        ? ctx.getEnvironment().getProperty("mpr.task.buzon.out.clean")
                        : MPRConstantes._TASK_BUZON_OUT_CLEAN;
        MPRConstantes._TASK_BUZON_ERRORES_CLEAN = StringUtils
                .isNotBlank(ctx.getEnvironment().getProperty("mpr.task.buzon.errores.clean"))
                        ? ctx.getEnvironment().getProperty("mpr.task.buzon.errores.clean")
                        : MPRConstantes._TASK_BUZON_ERRORES_CLEAN;

        logger.info("Iniciando el modulo de [" + MPRConstantes._MPR_CONTEXT_NAME + "]");
        logger.debug("     Configuracion GENERAL DEL MODULO");
        logger.debug("          mpr.home: " + MPRConstantes._MPR_HOME);
        logger.debug("          mpr.conf.home: " + MPRConstantes._MPR_CONF_HOME);
        logger.debug("          mpr.version: " + MPRConstantes._MPR_VERSION);
        logger.debug("          mpr.resources: " + MPRConstantes._MPR_RESOURCES);
        logger.debug("          mpr.temp: " + MPRConstantes._MPR_TEMP);
        logger.debug("          mpr.logs: " + MPRConstantes._MPR_LOGS);
        logger.debug("          mpr.logs.file: "
                + ((FileAppender) org.apache.log4j.Logger.getRootLogger().getAppender("LOGFILE")).getFile());
        logger.debug("          mpr.context.name: " + MPRConstantes._MPR_CONTEXT_NAME);
        logger.debug("          mpr.context.path: " + MPRConstantes._MPR_CONTEXT_PATH);
        logger.debug("          mpr.context.server: " + MPRConstantes._MPR_CONTEXT_SERVER);
        logger.debug("          java.version: " + ctx.getEnvironment().getProperty("java.version"));
        logger.debug("");
        logger.debug("     Parametrizacion GENERAL DEL SISTEMA");
        logger.debug("          mpr.enable.technical.information: " + MPRConstantes._ENABLE_TECHNICAL_INFORMATION);
        logger.debug("     Parametrizacion de CONEXION A EMPI");
        logger.debug("          mpr.empi.enable: " + MPRConstantes._EMPI_ENABLE);
        logger.debug("          mpr.empi.usuario: " + MPRConstantes._EMPI_USUARIO);
        logger.debug("          mpr.empi.sistema: " + MPRConstantes._EMPI_SISTEMA);
        logger.debug("          mpr.empi.url: " + MPRConstantes._EMPI_URL);
        logger.debug("     Parametrizacion de TAREAS PROGRAMADAS");
        logger.debug(
                "          mpr.task.buzon.in.process.messages: " + MPRConstantes._TASK_BUZON_IN_PROCESS_MESSAGES);
        logger.debug(
                "          mpr.task.buzon.out.process.messages: " + MPRConstantes._TASK_BUZON_OUT_PROCESS_MESSAGES);
        logger.debug("          mpr.task.buzon.in.his.clean: " + MPRConstantes._TASK_BUZON_IN_HIS_CLEAN);
        logger.debug("          mpr.task.buzon.out.his.clean: " + MPRConstantes._TASK_BUZON_OUT_HIS_CLEAN);
        logger.debug("          mpr.task.buzon.in.his.clean.old: " + MPRConstantes._TASK_BUZON_IN_HIS_CLEAN_OLD);
        logger.debug("          mpr.task.buzon.out.his.clean.old: " + MPRConstantes._TASK_BUZON_OUT_HIS_CLEAN_OLD);
        logger.debug("          mpr.task.buzon.in.clean: " + MPRConstantes._TASK_BUZON_IN_CLEAN);
        logger.debug("          mpr.task.buzon.out.clean: " + MPRConstantes._TASK_BUZON_OUT_CLEAN);
        logger.debug("          mpr.task.buzon.errores.clean: " + MPRConstantes._TASK_BUZON_ERRORES_CLEAN);
        logger.debug("     Variables de ENTORNO de utilidad");
        logger.debug("          catalina.home: " + ctx.getEnvironment().getProperty("catalina.home"));
        logger.debug("          jboss.home.dir: " + ctx.getEnvironment().getProperty("jboss.home.dir"));
        logger.info("Modulo [" + MPRConstantes._MPR_CONTEXT_NAME + "] iniciado correctamente");
    }

    private String findConfigurationAndLogger(AnnotationConfigWebApplicationContext ctx) {
        String mpr_conf_home = "classpath:";
        PropertyConfigurator.configureAndWatch("log4j");

        if (StringUtils.isNotBlank(ctx.getEnvironment().getProperty("jboss.home.dir"))) { //Jboss
            try {
                if (FileUtils.getFile(ctx.getEnvironment().getProperty("jboss.home.dir") + File.separator + "conf"
                        + File.separator + "mpr" + File.separator + "db.properties").exists()) {
                    if (FileUtils.getFile(ctx.getEnvironment().getProperty("jboss.home.dir") + File.separator
                            + "conf" + File.separator + "mpr" + File.separator + "log4j.properties").exists()) {
                        PropertyConfigurator
                                .configure(ctx.getEnvironment().getProperty("jboss.home.dir") + File.separator
                                        + "conf" + File.separator + "mpr" + File.separator + "log4j.properties");
                    }
                    return "file:" + ctx.getEnvironment().getProperty("jboss.home.dir") + File.separator + "conf"
                            + File.separator + "mpr";
                }
            } catch (Exception e) {
                logger.error(e.toString());
            }
        } else if (StringUtils.isNotBlank(ctx.getEnvironment().getProperty("catalina.home"))
                && StringUtils.isBlank(ctx.getEnvironment().getProperty("jboss.home.dir"))) { //Apache Tomcat
            try {
                if (FileUtils.getFile(ctx.getEnvironment().getProperty("catalina.home") + File.separator + "conf"
                        + File.separator + "mpr" + File.separator + "db.properties").exists()) {
                    if (FileUtils.getFile(ctx.getEnvironment().getProperty("catalina.home") + File.separator
                            + "conf" + File.separator + "mpr" + File.separator + "log4j.properties").exists()) {
                        PropertyConfigurator
                                .configure(ctx.getEnvironment().getProperty("catalina.home") + File.separator
                                        + "conf" + File.separator + "mpr" + File.separator + "log4j.properties");
                    }
                    return "file:" + ctx.getEnvironment().getProperty("catalina.home") + File.separator + "conf"
                            + File.separator + "mpr";
                }
            } catch (Exception e) {
                logger.error(e.toString());
            }
        }
        return mpr_conf_home;
    }
}