DatabaseProperties.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.generic_project.utils;

  21. import org.openspcoop2.generic_project.exception.ServiceException;
  22. import java.util.Properties;

  23. import org.openspcoop2.utils.TipiDatabase;
  24. import org.openspcoop2.utils.UtilsException;


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

  33.    
  34.     /** Copia Statica */
  35.     private static DatabaseProperties databaseProperties = null;

  36.     public static synchronized void initialize(String propertiesLocalPath, String propertiesName, String nomeFileProperties,org.slf4j.Logger log) throws ServiceException{

  37.         if(DatabaseProperties.databaseProperties==null)
  38.             DatabaseProperties.databaseProperties = new DatabaseProperties(propertiesLocalPath,propertiesName,nomeFileProperties,log);  

  39.     }

  40.     public static DatabaseProperties getInstance(org.slf4j.Logger log) throws ServiceException{
  41.         if(log!=null) {
  42.             // unused
  43.         }
  44.         // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  45.         if(DatabaseProperties.databaseProperties==null){
  46.             synchronized (DatabaseProperties.class) {
  47.                 if(DatabaseProperties.databaseProperties==null){
  48.                     throw new ServiceException("DatabaseProperties not initialized, use initialize method");
  49.                 }
  50.             }
  51.         }
  52.         return DatabaseProperties.databaseProperties;
  53.     }
  54.    
  55.    
  56.    
  57.    
  58.    
  59.     /* ********  F I E L D S  P R I V A T I  ******** */

  60.     /** Reader delle proprieta' impostate nel file 'database.properties' */
  61.     private DatabaseInstanceProperties reader;





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

  63.     /**
  64.      * Viene chiamato in causa per istanziare il properties reader
  65.      *
  66.      *
  67.      */
  68.     private DatabaseProperties(String propertiesLocalPath, String propertiesName, String nomeFileProperties,org.slf4j.Logger log) throws ServiceException{

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

  70.         Properties propertiesReader = new Properties();
  71.         java.io.InputStream properties = null;
  72.         try{  
  73.             properties = DatabaseProperties.class.getResourceAsStream("/"+nomeFileProperties);
  74.             if(properties==null){
  75.                 throw new ServiceException("Properties "+nomeFileProperties+" not found");
  76.             }
  77.             propertiesReader.load(properties);
  78.             properties.close();
  79.         }catch(Exception e) {
  80.             doError(log, nomeFileProperties, properties, e);
  81.         }  
  82.    
  83.         try{
  84.             this.reader = new DatabaseInstanceProperties(propertiesLocalPath,propertiesName,propertiesReader, log);
  85.         }catch(Exception e){
  86.             throw new ServiceException(e.getMessage(),e);
  87.         }
  88.     }
  89.     private void doError(org.slf4j.Logger log, String nomeFileProperties, java.io.InputStream properties, Exception e) throws ServiceException {
  90.         String msg = "Riscontrato errore durante la lettura del file '"+nomeFileProperties+"': "+e.getMessage();
  91.         log.error(msg,e);
  92.         try{
  93.             if(properties!=null)
  94.                 properties.close();
  95.         }catch(Exception er){
  96.             // close
  97.         }
  98.         throw new ServiceException(e.getMessage(),e);
  99.     }

  100.    
  101.    
  102.    
  103.    
  104.     /* ********  P R O P E R T I E S  ******** */


  105.     private String getProperty(String name,boolean required, boolean convertEnvProperty) throws UtilsException{
  106.         String tmp = null;
  107.         if(convertEnvProperty){
  108.             tmp = this.reader.getValueConvertEnvProperties(name);
  109.         }else{
  110.             tmp = this.reader.getValue(name);
  111.         }
  112.         if(tmp==null &&
  113.             required){
  114.             throw new UtilsException("Property ["+name+"] not found");
  115.         }
  116.         if(tmp!=null){
  117.             return tmp.trim();
  118.         }else{
  119.             return null;
  120.         }
  121.     }
  122.    

  123.    

  124.     // DB

  125.    
  126.     private static final String PROP_TIPO_DATABASE = "db.tipoDatabase";
  127.     public String getTipoDatabase() throws UtilsException {
  128.         return this.getProperty(DatabaseProperties.PROP_TIPO_DATABASE, true, false);
  129.     }
  130.     public TipiDatabase getTipoDatabaseEnum() throws UtilsException {
  131.         return TipiDatabase.toEnumConstant(this.getTipoDatabase());
  132.     }
  133.    
  134.     private static final String PROP_SHOW_SQL = "db.showSql";
  135.     public boolean isShowSql() throws UtilsException {
  136.         return Boolean.parseBoolean(this.getProperty(DatabaseProperties.PROP_SHOW_SQL, true, false));
  137.     }
  138.    
  139.     private static final String PROP_GENERATE_DDL = "db.generateDDL";
  140.     public boolean isGenerateDDL() throws UtilsException {
  141.         return Boolean.parseBoolean(this.getProperty(DatabaseProperties.PROP_GENERATE_DDL, true, false));
  142.     }
  143.    
  144.     private static final String PROP_AUTO_COMMIT = "db.autoCommit";
  145.     public boolean isAutoCommit() throws UtilsException {
  146.         return Boolean.parseBoolean(this.getProperty(DatabaseProperties.PROP_AUTO_COMMIT, true, false));
  147.     }
  148.    
  149.     private static final String PROP_SECONDS_TO_REFRESH_CONNECTION = "db.secondsToRefreshConnection";
  150.     public int getSecondsToRefreshConnection() throws UtilsException {
  151.         return Integer.parseInt(this.getProperty(DatabaseProperties.PROP_SECONDS_TO_REFRESH_CONNECTION, true, false));
  152.     }
  153.    
  154.     public ServiceManagerProperties getServiceManagerProperties() throws UtilsException{
  155.         ServiceManagerProperties sm = new ServiceManagerProperties();
  156.         sm.setDatabaseType(this.getTipoDatabase());
  157.         sm.setShowSql(this.isShowSql());
  158.         sm.setGenerateDdl(this.isGenerateDDL());
  159.         sm.setAutomaticTransactionManagement(this.isAutoCommit());
  160.         sm.setSecondsToRefreshConnection(this.getSecondsToRefreshConnection());
  161.         return sm;
  162.     }
  163.    
  164.     private static final String PROP_TIPO = "db.tipo";
  165.     private static final String PROP_TIPO_VALUE_DATASOURCE = "datasource";
  166.     private static final String PROP_TIPO_VALUE_CONNECTION = "connection";
  167.     private String getTipoAccessoDatabase() throws UtilsException {
  168.         String v = this.getProperty(DatabaseProperties.PROP_TIPO, true, true);
  169.         if(!DatabaseProperties.PROP_TIPO_VALUE_DATASOURCE.equals(v) && !DatabaseProperties.PROP_TIPO_VALUE_CONNECTION.equals(v)){
  170.             throw new UtilsException("Tipo di accesso al database fornito ["+v+"] non valido (supportati "+DatabaseProperties.PROP_TIPO_VALUE_DATASOURCE+","+DatabaseProperties.PROP_TIPO_VALUE_CONNECTION+")");
  171.         }
  172.         return v;
  173.     }
  174.     public boolean isTipoAccessoTramiteDatasource() throws UtilsException{
  175.         return DatabaseProperties.PROP_TIPO_VALUE_DATASOURCE.equals(this.getTipoAccessoDatabase());
  176.     }
  177.     public boolean isTipoAccessoTramiteConnection() throws UtilsException{
  178.         return DatabaseProperties.PROP_TIPO_VALUE_CONNECTION.equals(this.getTipoAccessoDatabase());
  179.     }
  180.    
  181.     private static final String PROP_DATASOURCE_JNDI_NAME = "db.datasource.jndiName";
  182.     public String getDatasourceJNDIName() throws UtilsException {
  183.         return this.getProperty(DatabaseProperties.PROP_DATASOURCE_JNDI_NAME, DatabaseProperties.PROP_TIPO_VALUE_DATASOURCE.equals(this.getTipoAccessoDatabase()), true);
  184.     }
  185.     private static final String PROP_DATASOURCE_JNDI_CONTEXT = "db.datasource.jndiContext";
  186.     public Properties getDatasourceJNDIContext() throws UtilsException {
  187.         return this.reader.readPropertiesConvertEnvProperties(DatabaseProperties.PROP_DATASOURCE_JNDI_CONTEXT);
  188.     }
  189.    
  190.     private static final String PROP_CONNECTION_URL = "db.connection.url";
  191.     public String getConnectionUrl() throws UtilsException {
  192.         return this.getProperty(DatabaseProperties.PROP_CONNECTION_URL, DatabaseProperties.PROP_TIPO_VALUE_CONNECTION.equals(this.getTipoAccessoDatabase()), true);
  193.     }
  194.     private static final String PROP_CONNECTION_DRIVER = "db.connection.driver";
  195.     public String getConnectionDriverJDBC() throws UtilsException {
  196.         return this.getProperty(DatabaseProperties.PROP_CONNECTION_DRIVER, DatabaseProperties.PROP_TIPO_VALUE_CONNECTION.equals(this.getTipoAccessoDatabase()), true);
  197.     }
  198.     private static final String PROP_CONNECTION_AUTH_USER = "db.connection.user";
  199.     public String getConnectionAuthUsername() throws UtilsException {
  200.         /**return this.getProperty(PROP_CONNECTION_AUTH_USER, PROP_TIPO_VALUE_CONNECTION.equals(this.getTipoAccessoDatabase()), true);*/
  201.         return this.getProperty(DatabaseProperties.PROP_CONNECTION_AUTH_USER, false, true);
  202.     }
  203.     private static final String PROP_CONNECTION_AUTH_PASSWORD = "db.connection.password";
  204.     public String getConnectionAuthPassword() throws UtilsException {
  205.         /**return this.getProperty(PROP_CONNECTION_AUTH_PASSWORD, PROP_TIPO_VALUE_CONNECTION.equals(this.getTipoAccessoDatabase()), true);*/
  206.         return this.getProperty(DatabaseProperties.PROP_CONNECTION_AUTH_PASSWORD, false, true);
  207.     }
  208.    
  209. }