Log4jConfig.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.logger.config;

  21. import java.io.File;
  22. import java.net.URI;
  23. import java.net.URL;
  24. import java.util.Properties;

  25. import org.openspcoop2.utils.LoggerWrapperFactory;
  26. import org.openspcoop2.utils.UtilsException;

  27. /**
  28.  * Log4jConfig
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class Log4jConfig {

  35.     private File log4jConfigFile;
  36.     private String log4jConfigName;
  37.     private URI log4jConfigURI;
  38.     private URL log4jConfigURL;
  39.     private Properties log4jConfigProperties;
  40.    
  41.     public File getLog4jConfigFile() {
  42.         return this.log4jConfigFile;
  43.     }
  44.     public void setLog4jConfigFile(File log4jConfigFile) {
  45.         this.log4jConfigFile = log4jConfigFile;
  46.     }
  47.     public Properties getLog4jConfigProperties() {
  48.         return this.log4jConfigProperties;
  49.     }
  50.     public void setLog4jConfigProperties(Properties log4jConfigProperties) {
  51.         this.log4jConfigProperties = log4jConfigProperties;
  52.     }
  53.     public String getLog4jConfigName() {
  54.         return this.log4jConfigName;
  55.     }
  56.     public void setLog4jConfigName(String log4jConfigName) {
  57.         this.log4jConfigName = log4jConfigName;
  58.     }
  59.     public URI getLog4jConfigURI() {
  60.         return this.log4jConfigURI;
  61.     }
  62.     public void setLog4jConfigURI(URI log4jConfigURI) {
  63.         this.log4jConfigURI = log4jConfigURI;
  64.     }
  65.     public URL getLog4jConfigURL() {
  66.         return this.log4jConfigURL;
  67.     }
  68.     public void setLog4jConfigURL(URL log4jConfigURL) {
  69.         this.log4jConfigURL = log4jConfigURL;
  70.     }

  71.    
  72.     // Istanza statica per motivi di performance
  73.     private static boolean logInitialized = false;
  74.    
  75.     public static void validate(Log4jConfig config) throws UtilsException{
  76.         if(logInitialized==false){
  77.             _validate(config);
  78.         }
  79.     }
  80.     public static void setConfigurationLogger(Log4jConfig config) throws UtilsException{
  81.         if(logInitialized==false){
  82.             _setConfigurationLogger(config);
  83.         }
  84.     }
  85.    
  86.     private static void _validate(Log4jConfig config) throws UtilsException{
  87.         if(logInitialized == false){
  88.             if(config==null){
  89.                 throw new UtilsException("Log4j configuration undefined (with enabled mode)");
  90.             }
  91.             int log4jConfigMode = 0;
  92.             if(config.getLog4jConfigFile()!=null){
  93.                 log4jConfigMode++;
  94.             }
  95.             if(config.getLog4jConfigName()!=null){
  96.                 log4jConfigMode++;
  97.             }
  98.             if(config.getLog4jConfigURI()!=null){
  99.                 log4jConfigMode++;
  100.             }
  101.             if(config.getLog4jConfigURL()!=null){
  102.                 log4jConfigMode++;
  103.             }
  104.             if(config.getLog4jConfigProperties()!=null){
  105.                 log4jConfigMode++;
  106.             }  
  107.             if(log4jConfigMode==0){
  108.                 throw new UtilsException("Log4j configuration uncorrect: source log4j configuration file undefined");
  109.             }
  110.             if(log4jConfigMode>1){
  111.                 throw new UtilsException("Log4j configuration uncorrect: found multiple source log4j configuration file");
  112.             }
  113.         }
  114.     }
  115.    
  116.     private static synchronized void _setConfigurationLogger(Log4jConfig config) throws UtilsException{
  117.         if(logInitialized == false){
  118.             if(config.getLog4jConfigFile()!=null){
  119.                 LoggerWrapperFactory.setLogConfiguration(config.getLog4jConfigFile());
  120.             }
  121.             else if(config.getLog4jConfigName()!=null){
  122.                 LoggerWrapperFactory.setLogConfiguration(config.getLog4jConfigName());
  123.             }
  124.             else if(config.getLog4jConfigURI()!=null){
  125.                 LoggerWrapperFactory.setLogConfiguration(config.getLog4jConfigURI());
  126.             }
  127.             else if(config.getLog4jConfigURL()!=null){
  128.                 LoggerWrapperFactory.setLogConfiguration(config.getLog4jConfigURL());
  129.             }
  130.             else if(config.getLog4jConfigProperties()!=null){
  131.                 LoggerWrapperFactory.setLogConfiguration(config.getLog4jConfigProperties());
  132.             }
  133.             logInitialized = true;
  134.         }
  135.     }
  136. }