DatasourceProperties.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.core.monitor.rs.server.config;


  21. import java.util.Properties;

  22. import org.slf4j.Logger;
  23. import org.openspcoop2.utils.LoggerWrapperFactory;
  24. import org.openspcoop2.utils.UtilsException;


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


  32. public class DatasourceProperties {

  33.     /** Logger utilizzato per errori eventuali. */
  34.     private Logger log = null;



  35.     /* ********  F I E L D S  P R I V A T I  ******** */

  36.     /** Reader delle proprieta' impostate nel file 'rs-api-monitor.datasource.properties' */
  37.     private DatasourceInstanceProperties reader;

  38.     /** Copia Statica */
  39.     private static DatasourceProperties datasourceProperties = null;


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

  41.     /**
  42.      * Viene chiamato in causa per istanziare il properties reader
  43.      *
  44.      *
  45.      */
  46.     private DatasourceProperties(String confDir,Logger log) throws Exception {

  47.         if(log!=null)
  48.             this.log = log;
  49.         else
  50.             this.log = LoggerWrapperFactory.getLogger(DatasourceProperties.class);
  51.        
  52.         /* ---- Lettura del cammino del file di configurazione ---- */
  53.         Properties propertiesReader = new Properties();
  54.         java.io.InputStream properties = null;
  55.         try{  
  56.             properties = DatasourceProperties.class.getResourceAsStream("/rs-api-monitor.datasource.properties");
  57.             if(properties==null){
  58.                 throw new Exception("File '/rs-api-monitor.datasource.properties' not found");
  59.             }
  60.             propertiesReader.load(properties);
  61.         }catch(Exception e) {
  62.             this.log.error("Riscontrato errore durante la lettura del file 'rs-api-monitor.datasource.properties': \n\n"+e.getMessage());
  63.             throw new Exception("RS Api MonitorProperties initialize error: "+e.getMessage());
  64.         }finally{
  65.             try{
  66.                 if(properties!=null)
  67.                     properties.close();
  68.             }catch(Exception er){
  69.                 // close
  70.             }
  71.         }

  72.         this.reader = new DatasourceInstanceProperties(propertiesReader, this.log, confDir);
  73.     }


  74.     /**
  75.      * Il Metodo si occupa di inizializzare il propertiesReader
  76.      *
  77.      *
  78.      */
  79.     public static boolean initialize(String confDir,Logger log){

  80.         try {
  81.             DatasourceProperties.datasourceProperties = new DatasourceProperties(confDir,log);  
  82.             return true;
  83.         }
  84.         catch(Exception e) {
  85.             log.error("Errore durante l'inizializzazione del BackendProperties: "+e.getMessage(),e);
  86.             return false;
  87.         }
  88.     }
  89.    
  90.     /**
  91.      * Ritorna l'istanza di questa classe
  92.      *
  93.      * @return Istanza di Properties
  94.      *
  95.      */
  96.     public static DatasourceProperties getInstance() throws UtilsException{
  97.         if(DatasourceProperties.datasourceProperties==null){
  98.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  99.             synchronized (DatasourceProperties.class) {
  100.                 throw new UtilsException("DatasourceProperties non inizializzato");
  101.             }
  102.         }
  103.         return DatasourceProperties.datasourceProperties;
  104.     }
  105.    
  106.     public static void updateLocalImplementation(Properties prop){
  107.         DatasourceProperties.datasourceProperties.reader.setLocalObjectImplementation(prop);
  108.     }








  109.     /* ********  M E T O D I  ******** */

  110.     private String readProperty(boolean required,String property) throws UtilsException{
  111.         String tmp = this.reader.getValueConvertEnvProperties(property);
  112.         if(tmp==null){
  113.             if(required){
  114.                 throw new UtilsException("Property ["+property+"] not found");
  115.             }
  116.             else{
  117.                 return null;
  118.             }
  119.         }else{
  120.             return tmp.trim();
  121.         }
  122.     }

  123.    

  124.    
  125.     /* ----- DB -------- */
  126.    
  127.     public boolean isShowSql() throws UtilsException{
  128.         return Boolean.parseBoolean(this.readProperty(true, "db.showSql"));
  129.     }
  130.    
  131.    
  132.    
  133.     public String getConfigDataSource() throws UtilsException{
  134.         return this.readProperty(true, "db.config.dataSource");
  135.     }
  136.    
  137.     public Properties getConfigDataSourceContext() throws UtilsException{
  138.         return this.reader.readPropertiesConvertEnvProperties("db.config.dataSource.property.");
  139.     }
  140.    
  141.     public String getConfigTipoDatabase() throws UtilsException{
  142.         return this.readProperty(true, "db.config.tipoDatabase");
  143.     }
  144.    
  145.    
  146.     public String getTracceDataSource() throws UtilsException{
  147.         return this.readProperty(true, "db.tracce.dataSource");
  148.     }
  149.    
  150.     public Properties getTracceDataSourceContext() throws UtilsException{
  151.         return this.reader.readPropertiesConvertEnvProperties("db.tracce.dataSource.property.");
  152.     }
  153.    
  154.     public String getTracceTipoDatabase() throws UtilsException{
  155.         return this.readProperty(true, "db.tracce.tipoDatabase");
  156.     }
  157.    
  158.    
  159.     public String getStatisticheDataSource() throws UtilsException{
  160.         return this.readProperty(true, "db.statistiche.dataSource");
  161.     }
  162.    
  163.     public Properties getStatisticheDataSourceContext() throws UtilsException{
  164.         return this.reader.readPropertiesConvertEnvProperties("db.statistiche.dataSource.property.");
  165.     }
  166.    
  167.     public String getStatisticheTipoDatabase() throws UtilsException{
  168.         return this.readProperty(true, "db.statistiche.tipoDatabase");
  169.     }
  170.    


  171. }