MonitorProperties.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.config;

  21. import org.openspcoop2.monitor.engine.constants.CostantiConfigurazione;
  22. import org.openspcoop2.monitor.engine.exceptions.EngineException;

  23. import java.util.Properties;

  24. import org.openspcoop2.utils.UtilsException;


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

  33.    
  34.     /** Copia Statica */
  35.     private static MonitorProperties monitorPropertiesStaticInstance = null;

  36.     private static synchronized void initialize(org.slf4j.Logger log) throws EngineException {

  37.         if(MonitorProperties.monitorPropertiesStaticInstance==null)
  38.             MonitorProperties.monitorPropertiesStaticInstance = new MonitorProperties(log);

  39.     }

  40.     public static MonitorProperties getInstance(org.slf4j.Logger log) throws EngineException{

  41.         if(MonitorProperties.monitorPropertiesStaticInstance==null) {
  42.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  43.             synchronized (MonitorProperties.class) {
  44.                 MonitorProperties.initialize(log);
  45.             }
  46.         }

  47.         return MonitorProperties.monitorPropertiesStaticInstance;
  48.     }
  49.    
  50.    
  51.    
  52.    
  53.    
  54.     /* ********  F I E L D S  P R I V A T I  ******** */

  55.     /** Reader delle proprieta' impostate nel file 'server.properties' */
  56.     private MonitorInstanceProperties reader;





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

  58.     /**
  59.      * Viene chiamato in causa per istanziare il properties reader
  60.      *
  61.      *
  62.      */
  63.     private MonitorProperties(org.slf4j.Logger log) throws EngineException{

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

  65.         Properties propertiesReader = new Properties();
  66.         java.io.InputStream properties = null;
  67.         try{  
  68.             properties = MonitorProperties.class.getResourceAsStream("/"+CostantiConfigurazione.CONFIG_FILENAME);
  69.             if(properties==null){
  70.                 throw new EngineException("Properties "+CostantiConfigurazione.CONFIG_FILENAME+" not found");
  71.             }
  72.             propertiesReader.load(properties);
  73.             properties.close();
  74.         }catch(Exception e) {
  75.             doError(log, properties, e);
  76.         }  
  77.    
  78.         try{
  79.             this.reader = new MonitorInstanceProperties(propertiesReader, log);
  80.         }catch(Exception e){
  81.             throw new EngineException(e.getMessage(),e);
  82.         }
  83.        
  84.     }
  85.     private void doError(org.slf4j.Logger log, java.io.InputStream properties, Exception e) throws EngineException {
  86.         log.error("Riscontrato errore durante la lettura del file '"+CostantiConfigurazione.CONFIG_FILENAME+"': "+e.getMessage(),e);
  87.         try{
  88.             if(properties!=null)
  89.                 properties.close();
  90.         }catch(Exception er){
  91.             // close
  92.         }
  93.         throw new EngineException(e.getMessage(),e);
  94.     }
  95.    
  96.    
  97.    
  98.    
  99.     /* ********  P R O P E R T I E S  ******** */

  100.     public String getProperty(String name,String defaultValue, boolean convertEnvProperty) throws UtilsException{
  101.         String tmp = null;
  102.         if(convertEnvProperty){
  103.             tmp = this.reader.getValueConvertEnvProperties(name);
  104.         }else{
  105.             tmp = this.reader.getValue(name);
  106.         }
  107.         if(tmp==null){
  108.             return defaultValue;
  109.         }
  110.         else{
  111.             return tmp.trim();
  112.         }
  113.     }
  114.     public String getProperty(String name,boolean required, boolean convertEnvProperty) throws UtilsException{
  115.         String tmp = null;
  116.         if(convertEnvProperty){
  117.             tmp = this.reader.getValueConvertEnvProperties(name);
  118.         }else{
  119.             tmp = this.reader.getValue(name);
  120.         }
  121.         if(tmp==null &&
  122.                 required){
  123.             throw new UtilsException("Property ["+name+"] not found");
  124.         }
  125.         if(tmp!=null){
  126.             return tmp.trim();
  127.         }else{
  128.             return null;
  129.         }
  130.     }
  131.    
  132. }