Java tutorial
/* * 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.pedicom.configuration; import com.dominion.salud.pedicom.negocio.configuration.XmlUnmarshaler; import com.dominion.salud.pedicom.negocio.entities.Datasources; import com.dominion.salud.pedicom.negocio.entities.Datos; import com.dominion.salud.pedicom.negocio.configuration.PEDICOMConstantes; 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.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; /** * INSERT INTO CAB_PAR_INT (CODIGO, INTERFACE, ACTIVADO) VALUES ('PEDICOM', 'Interfaz de envio de Pedidos y Existencias', 'S'); * INSERT INTO LIN_PAR_INT (COD_INTERFACE, TIPO, PARAMETRO) VALUES ('PEDICOM', 'ENCRIPTADO', 'N'); * INSERT INTO LIN_PAR_INT (COD_INTERFACE, TIPO, PARAMETRO) VALUES ('PEDICOM', 'EXISTENCIAS_ACTIVAS', 'S'); * INSERT INTO LIN_PAR_INT (COD_INTERFACE, TIPO, PARAMETRO) VALUES ('PEDICOM', 'LISTADO_DIAS', '30'); * INSERT INTO LIN_PAR_INT (COD_INTERFACE, TIPO, PARAMETRO) VALUES ('PEDICOM', 'MIN_FECHA_ENVIO', '01/01/2016'); * INSERT INTO LIN_PAR_INT (COD_INTERFACE, TIPO, PARAMETRO) VALUES ('PEDICOM', 'PEDICOM_ACTIVO', 'S'); * * @author jcgonzalez */ public class PEDICOMInitializer implements WebApplicationInitializer { private static final Logger logger = LoggerFactory.getLogger(PEDICOMInitializer.class); @Autowired private Environment environment; @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.scan("com.dominion.salud.pedicom.configuration"); ctx.setServletContext(servletContext); PEDICOMConstantes._NOMBRE_CONFIG = servletContext.getInitParameter("NOMBRE_CONFIG"); System.setProperty("pedicom.conf.home", findConfigurationAndLogger(ctx)); ctx.refresh(); // Spring Dispatcher ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); dispatcher.setInitParameter("contextClass", ctx.getClass().getName()); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); dispatcher.addMapping("/controller/*"); servletContext.addListener(new ContextLoaderListener(ctx)); // Configuracion general PEDICOMConstantes._HOME = StringUtils.endsWith(servletContext.getRealPath("/"), File.separator) ? servletContext.getRealPath("/") : servletContext.getRealPath("/") + File.separator; PEDICOMConstantes._CONF_HOME = ctx.getEnvironment().getProperty("pedicom.conf.home"); PEDICOMConstantes._TEMP = PEDICOMConstantes._HOME + "WEB-INF" + File.separator + "temp" + File.separator; PEDICOMConstantes._VERSION = ResourceBundle.getBundle("version").getString("version"); PEDICOMConstantes._LOGS = PEDICOMConstantes._HOME + "WEB-INF" + File.separator + "classes" + File.separator + "logs"; PEDICOMConstantes._CONTEXT_NAME = servletContext.getServletContextName(); PEDICOMConstantes._CONTEXT_PATH = servletContext.getContextPath(); PEDICOMConstantes._CONTEXT_SERVER = servletContext.getServerInfo(); PEDICOMConstantes._ENABLE_TECHNICAL_INFORMATION = StringUtils.isNotBlank( ResourceBundle.getBundle("application").getString("pedicom.enable.technical.information")) ? Boolean.parseBoolean(ResourceBundle.getBundle("application") .getString("pedicom.enable.technical.information")) : false; PEDICOMConstantes._SCHEDULER_SEND_MAIL_CRON = StringUtils .isNotBlank(ResourceBundle.getBundle("application").getString("pedicom_scheduler_send_mail_cron")) ? PEDICOMConstantes._SCHEDULER_SEND_MAIL_CRON = ResourceBundle.getBundle("application") .getString("pedicom_scheduler_send_mail_cron") : PEDICOMConstantes._SCHEDULER_SEND_MAIL_CRON; PEDICOMConstantes._SCHEDULER_UPDATE_EXISTENCIAS_CRON = StringUtils.isNotBlank( ResourceBundle.getBundle("application").getString("pedicom_scheduler_update_existencias_cron")) ? PEDICOMConstantes._SCHEDULER_SEND_MAIL_CRON = ResourceBundle.getBundle("application") .getString("pedicom_scheduler_update_existencias_cron") : PEDICOMConstantes._SCHEDULER_UPDATE_EXISTENCIAS_CRON; // 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(PEDICOMConstantes._HOME + "WEB-INF" + File.separator + "classes" + File.separator + "logs" + File.separator + "mpr-desktop.log"); } PEDICOMConstantes._LOGS = new File( ((FileAppender) org.apache.log4j.Logger.getRootLogger().getAppender("LOGFILE")).getFile()) .getParent(); Environment env = ctx.getEnvironment(); XmlUnmarshaler xml = new XmlUnmarshaler(); Datos datos = (Datos) xml.unmarshal(); logger.info(" Datasources"); for (Datasources dat : datos.getDatasources()) { if (dat.getNombreDatasource().equals("Central")) { PEDICOMConstantes.EXISTENCIAS_EXISTE = true; } logger.info(" codCentro: " + dat.getCodCentro()); logger.info(" nombreDatasource: " + dat.getNombreDatasource()); logger.info(" driverClassName: " + dat.getDriverClassName()); logger.info(" jndi: " + dat.getJndi()); logger.info(" url: " + dat.getUrl()); logger.info(" username: " + dat.getUsername()); logger.info(" usernameEmail: " + dat.getUsernameEmail()); logger.info(" passwordEmail: " + dat.getPasswordEmail()); logger.info(" from: " + dat.getFrom()); logger.info(" host: " + dat.getHost()); logger.info(" port: " + dat.getPort()); logger.info(" TLS: " + dat.getTLS()); logger.info(" SSL: " + dat.getSSL()); } // ctx.refresh(); // PropertyConfigurator.configureAndWatch("log4j"); logger.info(" Configuracion general del sistema"); logger.info(" pedicom.home: " + PEDICOMConstantes._HOME); logger.info(" pedicom.conf.home: " + PEDICOMConstantes._CONF_HOME); logger.info(" pedicom.temp: " + PEDICOMConstantes._TEMP); logger.info(" pedicom.version: " + PEDICOMConstantes._VERSION); logger.info(" pedicom.logs: " + PEDICOMConstantes._LOGS); logger.info(" pedicom.context.name: " + PEDICOMConstantes._CONTEXT_NAME); logger.info(" pedicom.context.path: " + PEDICOMConstantes._CONTEXT_PATH); logger.info(" pedicom.context.server: " + PEDICOMConstantes._CONTEXT_SERVER); logger.info(" Parametrizacion del sistema"); logger.info(" pedicom.enable.technical.information: " + PEDICOMConstantes._ENABLE_TECHNICAL_INFORMATION); logger.info( " pedicom_scheduler_send_mail_cron: " + PEDICOMConstantes._SCHEDULER_SEND_MAIL_CRON); logger.info(" pedicom_scheduler_update_existencias_cron: " + PEDICOMConstantes._SCHEDULER_UPDATE_EXISTENCIAS_CRON); logger.info(" Modulo configurado correctamente"); logger.info("MODULO INICIADO CORRECTAMENTE"); } private String findConfigurationAndLogger(AnnotationConfigWebApplicationContext ctx) { String pedicom_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 + PEDICOMConstantes._NOMBRE_CONFIG + File.separator + "db.properties") .exists()) { if (FileUtils.getFile(ctx.getEnvironment().getProperty("jboss.home.dir") + File.separator + "conf" + File.separator + PEDICOMConstantes._NOMBRE_CONFIG + File.separator + "log4j.properties").exists()) { PropertyConfigurator.configure(ctx.getEnvironment().getProperty("jboss.home.dir") + File.separator + "conf" + File.separator + PEDICOMConstantes._NOMBRE_CONFIG + File.separator + "log4j.properties"); } return "file:" + ctx.getEnvironment().getProperty("jboss.home.dir") + File.separator + "conf" + File.separator + PEDICOMConstantes._NOMBRE_CONFIG; } } 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 + PEDICOMConstantes._NOMBRE_CONFIG + File.separator + "db.properties") .exists()) { if (FileUtils.getFile(ctx.getEnvironment().getProperty("catalina.home") + File.separator + "conf" + File.separator + PEDICOMConstantes._NOMBRE_CONFIG + File.separator + "log4j.properties").exists()) { PropertyConfigurator.configure(ctx.getEnvironment().getProperty("catalina.home") + File.separator + "conf" + File.separator + PEDICOMConstantes._NOMBRE_CONFIG + File.separator + "log4j.properties"); } return "file:" + ctx.getEnvironment().getProperty("catalina.home") + File.separator + "conf" + File.separator + PEDICOMConstantes._NOMBRE_CONFIG; } } catch (Exception e) { logger.error(e.toString()); } } return pedicom_conf_home; } }