com.dominion.salud.nomenclator.configuration.NOMENCLATORInitializer.java Source code

Java tutorial

Introduction

Here is the source code for com.dominion.salud.nomenclator.configuration.NOMENCLATORInitializer.java

Source

/*
 * Copyright (C) 2015 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.nomenclator.configuration;

import com.dominion.salud.nomenclator.negocio.configuracion.NOMENCLATORConstantes;
import java.io.File;
import java.util.ResourceBundle;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.apache.commons.lang3.StringUtils;
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;

/**
 *
 * @author jcgonzalez
 */
public class NOMENCLATORInitializer implements WebApplicationInitializer {

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

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.scan("com.dominion.salud.nomenclator.configuration");
        ctx.setServletContext(servletContext);
        ctx.refresh();

        logger.info("     Registrando servlets de configuracion");
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
                new DispatcherServlet(ctx));
        dispatcher.setInitParameter("contextClass", ctx.getClass().getName());
        dispatcher.setLoadOnStartup(1);
        logger.info("          Agregando mapping: /");
        dispatcher.addMapping("/");
        logger.info("          Agregando mapping: /controller/*");
        dispatcher.addMapping("/controller/*");
        logger.info("          Agregando mapping: /services/*");
        dispatcher.addMapping("/services/*");
        servletContext.addListener(new ContextLoaderListener(ctx));
        logger.info("     Servlets de configuracion registrados correctamente");

        // Configuracion general
        logger.info("     Iniciando la configuracion del modulo");
        NOMENCLATORConstantes._HOME = StringUtils.endsWith(servletContext.getRealPath("/"), File.separator)
                ? servletContext.getRealPath("/")
                : servletContext.getRealPath("/") + File.separator;
        NOMENCLATORConstantes._TEMP = NOMENCLATORConstantes._HOME + "WEB-INF" + File.separator + "temp"
                + File.separator;
        NOMENCLATORConstantes._VERSION = ResourceBundle.getBundle("version").getString("version");
        NOMENCLATORConstantes._LOGS = NOMENCLATORConstantes._HOME + "WEB-INF" + File.separator + "classes"
                + File.separator + "logs";
        NOMENCLATORConstantes._CONTEXT_NAME = servletContext.getServletContextName();
        NOMENCLATORConstantes._CONTEXT_PATH = servletContext.getContextPath();
        NOMENCLATORConstantes._CONTEXT_SERVER = servletContext.getServerInfo();
        NOMENCLATORConstantes._ENABLE_TECHNICAL_INFORMATION = StringUtils.isNotBlank(
                ResourceBundle.getBundle("application").getString("nomenclator.enable.technical.information"))
                        ? Boolean.parseBoolean(ResourceBundle.getBundle("application")
                                .getString("nomenclator.enable.technical.information"))
                        : false;
        PropertyConfigurator.configureAndWatch("log4j");

        logger.info("          Configuracion general del sistema");
        logger.info("               nomenclator.home: " + NOMENCLATORConstantes._HOME);
        logger.info("               nomenclator.temp: " + NOMENCLATORConstantes._TEMP);
        logger.info("               nomenclator.version: " + NOMENCLATORConstantes._VERSION);
        logger.info("               nomenclator.logs: " + NOMENCLATORConstantes._LOGS);
        logger.info("               nomenclator.context.name: " + NOMENCLATORConstantes._CONTEXT_NAME);
        logger.info("               nomenclator.context.path: " + NOMENCLATORConstantes._CONTEXT_PATH);
        logger.info("               nomenclator.context.server: " + NOMENCLATORConstantes._CONTEXT_SERVER);
        logger.info("          Parametrizacion del sistema");
        logger.info("               nomenclator.enable.technical.information: "
                + NOMENCLATORConstantes._ENABLE_TECHNICAL_INFORMATION);
        logger.info("     Modulo configurado correctamente");
        logger.info("MODULO INICIADO CORRECTAMENTE");
    }
}