AlarmConfigProperties.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.monitor.engine.alarm;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.util.Properties;

  24. import org.openspcoop2.monitor.engine.exceptions.EngineException;
  25. import org.openspcoop2.utils.UtilsException;


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

  34.    
  35.     /** Copia Statica */
  36.     private static AlarmConfigProperties alarmConfigProperties = null;
  37.     private static AlarmEngineConfig alarmEngineConfig = null;

  38. //  public static AlarmEngineConfig getAlarmConfiguration(org.slf4j.Logger log, String confDir, boolean useDefaultIfNotFound) throws Exception{
  39. //      return getAlarmConfiguration(log, null, confDir, useDefaultIfNotFound);
  40. //  }
  41.     public static AlarmEngineConfig getAlarmConfiguration(org.slf4j.Logger log,String filePath, String confDir) throws Exception{
  42.         return getAlarmConfiguration(log, filePath, confDir, true);
  43.     }
  44.     public static AlarmEngineConfig getAlarmConfiguration(org.slf4j.Logger log,String filePath, String confDir, boolean useDefaultIfNotFound) throws Exception{

  45.         if(AlarmConfigProperties.alarmConfigProperties==null){
  46.             return _getAlarmConfiguration(log, filePath, confDir, useDefaultIfNotFound);
  47.         }else{
  48.             return AlarmConfigProperties.alarmEngineConfig;
  49.         }
  50.     }
  51.    
  52.     private static synchronized AlarmEngineConfig _getAlarmConfiguration(org.slf4j.Logger log,String filePath, String confDir, boolean useDefaultIfNotFound) throws Exception{

  53.         if(AlarmConfigProperties.alarmConfigProperties==null){
  54.             AlarmConfigProperties.alarmConfigProperties = new AlarmConfigProperties(log,filePath, confDir, useDefaultIfNotFound);
  55.             AlarmConfigProperties.alarmEngineConfig = AlarmEngineConfig.readAlarmEngineConfig(log, alarmConfigProperties);
  56.         }

  57.         return AlarmConfigProperties.alarmEngineConfig;
  58.        
  59.     }
  60.    
  61.    
  62.    
  63.     /* ********  F I E L D S  P R I V A T I  ******** */

  64.     /** Reader delle proprieta' impostate nel file 'alarmConfig.properties' */
  65.     private AlarmConfigInstanceProperties reader;





  66.     /* ********  C O S T R U T T O R E  ******** */

  67.     /**
  68.      * Viene chiamato in causa per istanziare il properties reader
  69.      *
  70.      *
  71.      */
  72.     public AlarmConfigProperties(org.slf4j.Logger log,String filePat, String confDir, boolean useDefaultIfNotFound) throws EngineException{

  73.         /* ---- Lettura del cammino del file di configurazione ---- */

  74.         Properties p = new Properties();
  75.         java.io.InputStream properties = null;
  76.         try{  
  77.             File f = new File(filePat);
  78.             if(f.exists()){
  79.                 properties = new FileInputStream(f);
  80.             }
  81.             else{
  82.                 properties = AlarmConfigProperties.class.getResourceAsStream(filePat);
  83.                 if(properties==null && !filePat.startsWith("/")){
  84.                     properties = AlarmConfigProperties.class.getResourceAsStream("/"+filePat);
  85.                 }
  86.             }
  87.             if(properties==null) {
  88.                 properties = AlarmConfigProperties.class.getResourceAsStream("/org/openspcoop2/monitor/engine/alarm/allarmi_configurazione.properties");
  89.             }
  90.             if(properties==null){
  91.                 throw new Exception("Properties "+filePat+" not found");
  92.             }
  93.             p.load(properties);
  94.             properties.close();
  95.         }catch(Exception e) {
  96.             log.error("Riscontrato errore durante la lettura del file '"+filePat+"': "+e.getMessage(),e);
  97.             try{
  98.                 if(properties!=null)
  99.                     properties.close();
  100.             }catch(Exception er){
  101.                 // close
  102.             }
  103.             throw new EngineException(e.getMessage(),e);
  104.         }  
  105.    
  106.         try{
  107.             //this.reader = new PropertiesReader(p, true);
  108.             this.reader = new AlarmConfigInstanceProperties(p, log, confDir);
  109.         }catch(Exception e){
  110.             throw new EngineException(e.getMessage(),e);
  111.         }
  112.        
  113.     }

  114.    
  115.    
  116.    
  117.    
  118.     /* ********  P R O P E R T I E S  ******** */

  119.     public String getProperty(String name,String defaultValue, boolean convertEnvProperty) throws UtilsException{
  120.         String tmp = null;
  121.         if(convertEnvProperty){
  122.             tmp = this.reader.getValueConvertEnvProperties(name);
  123.         }else{
  124.             tmp = this.reader.getValue(name);
  125.         }
  126.         if(tmp==null){
  127.             return defaultValue;
  128.         }
  129.         else{
  130.             return tmp.trim();
  131.         }
  132.     }
  133.     public String getProperty(String name,boolean required, boolean convertEnvProperty) throws UtilsException{
  134.         String tmp = null;
  135.         if(convertEnvProperty){
  136.             tmp = this.reader.getValueConvertEnvProperties(name);
  137.         }else{
  138.             tmp = this.reader.getValue(name);
  139.         }
  140.         if(tmp==null){
  141.             if(required){
  142.                 throw new UtilsException("Property ["+name+"] not found");
  143.             }
  144.         }
  145.         if(tmp!=null){
  146.             return tmp.trim();
  147.         }else{
  148.             return null;
  149.         }
  150.     }
  151.    
  152. }