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.web.ctrlstat.config;


  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Properties;

  24. import org.slf4j.Logger;
  25. import org.openspcoop2.pdd.config.OpenSPCoop2ConfigurationException;
  26. import org.openspcoop2.utils.LoggerWrapperFactory;
  27. import org.openspcoop2.utils.UtilsException;


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


  35. public class DatasourceProperties {

  36.     /** Logger utilizzato per errori eventuali. */
  37.     private Logger log = null;



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

  39.     /** Reader delle proprieta' impostate nel file 'console.datasource.properties' */
  40.     private DatasourceInstanceProperties reader;

  41.     /** Copia Statica */
  42.     private static DatasourceProperties datasourceProperties = null;


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

  44.     /**
  45.      * Viene chiamato in causa per istanziare il properties reader
  46.      *
  47.      *
  48.      */
  49.     private DatasourceProperties(String confDir, String confPropertyName, String confLocalPathPrefix,Logger log) throws Exception {

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

  75.         this.reader = new DatasourceInstanceProperties(propertiesReader, this.log, confDir, confPropertyName, confLocalPathPrefix);
  76.     }
  77.     private DatasourceProperties(Properties properties) throws Exception {
  78.         this.reader = new DatasourceInstanceProperties(properties, this.log, "undefined", "undefined", "undefined");
  79.     }


  80.     /**
  81.      * Il Metodo si occupa di inizializzare il propertiesReader
  82.      *
  83.      *
  84.      */
  85.     public static synchronized boolean initialize(String confDir, String confPropertyName, String confLocalPathPrefix,Logger log){

  86.         try {
  87.             if(DatasourceProperties.datasourceProperties==null){
  88.                 DatasourceProperties.datasourceProperties = new DatasourceProperties(confDir,confPropertyName,confLocalPathPrefix,log);
  89.             }
  90.             return true;
  91.         }
  92.         catch(Exception e) {
  93.             log.error("Inizializzazione fallita: "+e.getMessage(),e);
  94.             return false;
  95.         }
  96.     }
  97.     public static synchronized boolean initialize(Properties properties,Logger log){
  98.         try {
  99.             if(DatasourceProperties.datasourceProperties==null){
  100.                 DatasourceProperties.datasourceProperties = new DatasourceProperties(properties);
  101.             }
  102.             return true;
  103.         }
  104.         catch(Exception e) {
  105.             log.error("Inizializzazione fallita: "+e.getMessage(),e);
  106.             return false;
  107.         }
  108.     }
  109.    
  110.     /**
  111.      * Ritorna l'istanza di questa classe
  112.      *
  113.      * @return Istanza di ClassNameProperties
  114.      *
  115.      */
  116.     public static DatasourceProperties getInstance() throws OpenSPCoop2ConfigurationException{
  117.         if(DatasourceProperties.datasourceProperties==null){
  118.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  119.             synchronized (DatasourceProperties.class) {
  120.                 throw new OpenSPCoop2ConfigurationException("DatasourceProperties non inizializzato");
  121.             }
  122.         }
  123.         return DatasourceProperties.datasourceProperties;
  124.     }
  125.    
  126.     public static void updateLocalImplementation(Properties prop){
  127.         DatasourceProperties.datasourceProperties.reader.setLocalObjectImplementation(prop);
  128.     }








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

  130.     private String readProperty(boolean required,String property) throws UtilsException{
  131.         String tmp = this.reader.getValueConvertEnvProperties(property);
  132.         if(tmp==null){
  133.             if(required){
  134.                 throw new UtilsException("Property ["+property+"] not found");
  135.             }
  136.             else{
  137.                 return null;
  138.             }
  139.         }else{
  140.             return tmp.trim();
  141.         }
  142.     }
  143.     private Boolean readBooleanProperty(boolean required,String property) throws UtilsException{
  144.         String tmp = this.readProperty(required, property);
  145.         if(!"true".equalsIgnoreCase(tmp) && !"false".equalsIgnoreCase(tmp)){
  146.             throw new UtilsException("Property ["+property+"] with uncorrect value ["+tmp+"] (true/value expected)");
  147.         }
  148.         return Boolean.parseBoolean(tmp);
  149.     }

  150.    

  151.    
  152.     /* ----- Database della Console -------- */
  153.    
  154.     public String getDataSource() throws UtilsException{
  155.         return this.readProperty(true, "dataSource");
  156.     }
  157.    
  158.     public Properties getDataSourceContext() throws UtilsException{
  159.         return this.reader.readPropertiesConvertEnvProperties("dataSource.property.");
  160.     }
  161.    
  162.     public String getTipoDatabase() throws UtilsException{
  163.         return this.readProperty(true, "tipoDatabase");
  164.     }
  165.    
  166.    
  167.     /* ----- Database di Monitoraggio ------- */
  168.    
  169.     public List<String> getSinglePddMonitorSorgentiDati() throws UtilsException{
  170.         List<String> l = new ArrayList<>();
  171.         String p = this.readProperty(false, "singlePdD.monitor.sorgentiDati");
  172.         if(p!=null && !"".equals(p.trim())){
  173.             String [] tmp = p.trim().split(",");
  174.             for (int i = 0; i < tmp.length; i++) {
  175.                 tmp[i] = tmp[i].trim();
  176.                 l.add(tmp[i]);
  177.             }
  178.         }
  179.         return l;
  180.     }
  181.    
  182.     public String getSinglePddMonitorLabel(String source) throws UtilsException{
  183.         return this.readProperty(true, "singlePdD.monitor."+source+".label");
  184.     }
  185.    
  186.     public String getSinglePddMonitorDataSource(String source) throws UtilsException{
  187.         return this.readProperty(true, "singlePdD.monitor."+source+".dataSource");
  188.     }
  189.    
  190.     public Properties getSinglePddMonitorDataSourceContext(String source) throws UtilsException{
  191.         return this.reader.readPropertiesConvertEnvProperties("singlePdD.monitor."+source+".dataSource.property.");
  192.     }
  193.    
  194.     public String getSinglePddMonitorTipoDatabase(String source) throws UtilsException{
  195.         return this.readProperty(true, "singlePdD.monitor."+source+".tipoDatabase");
  196.     }
  197.    
  198.    
  199.     /* ----- Database di Tracciamento ------- */
  200.        
  201.     public Boolean isSinglePddTracceStessoDBConsole() throws UtilsException{
  202.         return this.readBooleanProperty(true, "singlePdD.tracce.sameDBWebUI");
  203.     }
  204.    
  205.     public String getSinglePddTracceDataSource() throws UtilsException{
  206.         return this.readProperty(true, "singlePdD.tracce.dataSource");
  207.     }
  208.    
  209.     public Properties getSinglePddTracceDataSourceContext() throws UtilsException{
  210.         return this.reader.readPropertiesConvertEnvProperties("singlePdD.tracce.dataSource.property.");
  211.     }
  212.    
  213.     public String getSinglePddTracceTipoDatabase() throws UtilsException{
  214.         return this.readProperty(true, "singlePdD.tracce.tipoDatabase");
  215.     }
  216.    
  217.    
  218.     /* ----- Database dei Messaggi Diagnostici ------- */
  219.    
  220.     public Boolean isSinglePddMessaggiDiagnosticiStessoDBConsole() throws UtilsException{
  221.         return this.readBooleanProperty(true, "singlePdD.msgDiagnostici.sameDBWebUI");
  222.     }
  223.    
  224.     public String getSinglePddMessaggiDiagnosticiDataSource() throws UtilsException{
  225.         return this.readProperty(true, "singlePdD.msgDiagnostici.dataSource");
  226.     }
  227.    
  228.     public Properties getSinglePddMessaggiDiagnosticiDataSourceContext() throws UtilsException{
  229.         return this.reader.readPropertiesConvertEnvProperties("singlePdD.msgDiagnostici.dataSource.property.");
  230.     }
  231.    
  232.     public String getSinglePddMessaggiDiagnosticiTipoDatabase() throws UtilsException{
  233.         return this.readProperty(true, "singlePdD.msgDiagnostici.tipoDatabase");
  234.     }
  235.    
  236.    

  237. }