DAOFactoryProperties.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.commons.dao;

  21. import org.openspcoop2.generic_project.beans.IProjectInfo;
  22. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;

  23. import java.util.Iterator;
  24. import java.util.Properties;

  25. import org.openspcoop2.utils.TipiDatabase;
  26. import org.slf4j.Logger;


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


  35.     /** Copia Statica */
  36.     private static DAOFactoryProperties daoFactoryProperties = null;

  37.     private static synchronized void initialize(Logger log) throws Exception{

  38.         if(DAOFactoryProperties.daoFactoryProperties==null)
  39.             DAOFactoryProperties.daoFactoryProperties = new DAOFactoryProperties(log);  

  40.     }

  41.     public static DAOFactoryProperties getInstance(Logger log) throws Exception{

  42.         if(DAOFactoryProperties.daoFactoryProperties==null) {
  43.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  44.             synchronized (DAOFactoryProperties.class) {
  45.                 DAOFactoryProperties.initialize(log);
  46.             }
  47.         }

  48.         return DAOFactoryProperties.daoFactoryProperties;
  49.     }





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

  51.     /** Reader delle proprieta' impostate nel file 'server.properties' */
  52.     private DAOFactoryInstanceProperties reader;





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

  54.     /**
  55.      * Viene chiamato in causa per istanziare il properties reader
  56.      *
  57.      *
  58.      */
  59.     protected DAOFactoryProperties(Logger log) throws Exception{

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

  61.         Properties propertiesReaded = new Properties();
  62.        
  63.         // internal (required)
  64.         java.io.InputStream propertiesInternal = null;
  65.         try{  
  66.             propertiesInternal = DAOFactoryProperties.class.getResourceAsStream("/daoFactory.internal.properties");
  67.             if(propertiesInternal==null){
  68.                 throw new Exception("Properties daoFactory.internal.properties not found");
  69.             }
  70.             propertiesReaded.load(propertiesInternal);
  71.             propertiesInternal.close();
  72.         }catch(java.io.IOException e) {
  73.             log.error("Riscontrato errore durante la lettura del file 'daoFactory.internal.properties': "+e.getMessage(),e);
  74.             try{
  75.                 if(propertiesInternal!=null)
  76.                     propertiesInternal.close();
  77.             }catch(Exception er){
  78.                 // close
  79.             }
  80.             throw e;
  81.         }
  82.        
  83.         // opzionale e sovrascrive eventuali properties
  84.         java.io.InputStream properties = null;
  85.         try{  
  86.             properties = DAOFactoryProperties.class.getResourceAsStream("/daoFactory.properties");
  87.             if(properties!=null){
  88.                 Properties tmp = new Properties();
  89.                 tmp.load(properties);
  90.                 properties.close();
  91.                 if(tmp.size()>0) {
  92.                     Iterator<?> itTmp = tmp.keySet().iterator();
  93.                     while (itTmp.hasNext()) {
  94.                         Object oKey = (Object) itTmp.next();
  95.                         if(oKey instanceof String) {
  96.                             String key = (String) oKey;
  97.                             String value = tmp.getProperty(key);
  98.                             if(propertiesReaded.containsKey(key)) {
  99.                                 propertiesReaded.remove(key);
  100.                             }
  101.                             propertiesReaded.setProperty(key, value);
  102.                         }
  103.                     }
  104.                 }
  105.             }
  106.         }catch(java.io.IOException e) {
  107.             log.error("Riscontrato errore durante la lettura del file 'daoFactory.properties': "+e.getMessage(),e);
  108.             try{
  109.                 if(properties!=null)
  110.                     properties.close();
  111.             }catch(Exception er){
  112.                 // close
  113.             }
  114.             throw e;
  115.         }  

  116.         try{
  117.             this.reader = new DAOFactoryInstanceProperties(propertiesReaded, log);
  118.         }catch(Exception e){
  119.             throw new DAOFactoryException(e.getMessage(),e);
  120.         }
  121.     }





  122.     /* ********  P R O P E R T I E S  ******** */


  123.     private String getProperty(String name,boolean required, boolean convertEnvProperty) throws Exception{
  124.         String tmp = null;
  125.         if(convertEnvProperty){
  126.             tmp = this.reader.getValueConvertEnvProperties(name);
  127.         }else{
  128.             tmp = this.reader.getValue(name);
  129.         }
  130.         if(tmp==null){
  131.             if(required){
  132.                 throw new Exception("Property ["+name+"] not found");
  133.             }
  134.         }
  135.         if(tmp!=null){
  136.             return tmp.trim();
  137.         }else{
  138.             return null;
  139.         }
  140.     }


  141.     // NOME classe DAO
  142.     public String getDAOClassName(IProjectInfo tipoDAO) throws Exception {
  143.         return this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName(), true, false);
  144.     }


  145.     // DB

  146.     private static final String PREFIX_FACTORY = "factory.";

  147.     private static final String PROP_TIPO_DATABASE = "db.tipoDatabase";
  148.     public String getTipoDatabase(IProjectInfo tipoDAO) throws Exception {
  149.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_TIPO_DATABASE, false, false);
  150.         if(v==null){
  151.             v = this.getProperty(PROP_TIPO_DATABASE, true, false);
  152.         }
  153.         return v;
  154.     }
  155.     public TipiDatabase getTipoDatabaseEnum(IProjectInfo tipoDAO) throws Exception {
  156.         return TipiDatabase.toEnumConstant(this.getTipoDatabase(tipoDAO));
  157.     }

  158.     private static final String PROP_SHOW_SQL = "db.showSql";
  159.     public boolean isShowSql(IProjectInfo tipoDAO) throws Exception {
  160.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_SHOW_SQL, false, false);
  161.         if(v==null){
  162.             v = this.getProperty(PROP_SHOW_SQL, true, false);
  163.         }
  164.         return Boolean.parseBoolean(v);
  165.     }

  166.     private static final String PROP_GENERATE_DDL = "db.generateDDL";
  167.     public boolean isGenerateDDL(IProjectInfo tipoDAO) throws Exception {
  168.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_GENERATE_DDL, false, false);
  169.         if(v==null){
  170.             v = this.getProperty(PROP_GENERATE_DDL, true, false);
  171.         }
  172.         return Boolean.parseBoolean(v);
  173.     }

  174.     private static final String PROP_AUTO_COMMIT = "db.autoCommit";
  175.     public boolean isAutoCommit(IProjectInfo tipoDAO) throws Exception {
  176.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_AUTO_COMMIT, false, false);
  177.         if(v==null){
  178.             v = this.getProperty(PROP_AUTO_COMMIT, true, false);
  179.         }
  180.         return Boolean.parseBoolean(v);
  181.     }
  182.    
  183.     private static final String PROP_SECONDS_TO_REFRESH_CONNECTION = "db.secondsToRefreshConnection";
  184.     public int getSecondsToRefreshConnection(IProjectInfo tipoDAO) throws Exception {
  185.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_SECONDS_TO_REFRESH_CONNECTION, false, false);
  186.         if(v==null){
  187.             v = this.getProperty(PROP_SECONDS_TO_REFRESH_CONNECTION, true, false);
  188.         }
  189.         return Integer.parseInt(v);
  190.     }

  191.     public ServiceManagerProperties getServiceManagerProperties(IProjectInfo tipoDAO) throws Exception{
  192.         ServiceManagerProperties sm = new ServiceManagerProperties();
  193.         sm.setDatabaseType(this.getTipoDatabase(tipoDAO));
  194.         sm.setShowSql(this.isShowSql(tipoDAO));
  195.         sm.setGenerateDdl(this.isGenerateDDL(tipoDAO));
  196.         sm.setAutomaticTransactionManagement(this.isAutoCommit(tipoDAO));
  197.         sm.setSecondsToRefreshConnection(this.getSecondsToRefreshConnection(tipoDAO));
  198.         return sm;
  199.     }

  200.     private static final String PROP_TIPO = "db.tipo";
  201.     protected static final String PROP_TIPO_VALUE_DATASOURCE = "datasource";
  202.     protected static final String PROP_TIPO_VALUE_CONNECTION = "connection";
  203.     protected String getTipoAccessoDatabase(IProjectInfo tipoDAO) throws Exception {
  204.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_TIPO, false, true);
  205.         if(v==null){
  206.             v = this.getProperty(PROP_TIPO, true, true);
  207.         }
  208.         if(!PROP_TIPO_VALUE_DATASOURCE.equals(v) && !PROP_TIPO_VALUE_CONNECTION.equals(v)){
  209.             throw new Exception("Tipo di accesso al database fornito ["+v+"] non valido (supportati "+PROP_TIPO_VALUE_DATASOURCE+","+PROP_TIPO_VALUE_CONNECTION+")");
  210.         }
  211.         return v;
  212.     }
  213.     public boolean isTipoAccessoTramiteDatasource(IProjectInfo tipoDAO) throws Exception{
  214.         return PROP_TIPO_VALUE_DATASOURCE.equals(this.getTipoAccessoDatabase(tipoDAO));
  215.     }
  216.     public boolean isTipoAccessoTramiteConnection(IProjectInfo tipoDAO) throws Exception{
  217.         return PROP_TIPO_VALUE_CONNECTION.equals(this.getTipoAccessoDatabase(tipoDAO));
  218.     }

  219.     private static final String PROP_DATASOURCE_JNDI_NAME = "db.datasource.jndiName";
  220.     public String getDatasourceJNDIName(IProjectInfo tipoDAO) throws Exception {
  221.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_DATASOURCE_JNDI_NAME, false, true);
  222.         if(v==null){
  223.             v = this.getProperty(PROP_DATASOURCE_JNDI_NAME, PROP_TIPO_VALUE_DATASOURCE.equals(this.getTipoAccessoDatabase(tipoDAO)), true);
  224.         }
  225.         return v;
  226.     }
  227.     private static final String PROP_DATASOURCE_JNDI_CONTEXT = "db.datasource.jndiContext";
  228.     public Properties getDatasourceJNDIContext(IProjectInfo tipoDAO) throws Exception {
  229.         Properties p = this.reader.readPropertiesConvertEnvProperties(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_DATASOURCE_JNDI_CONTEXT);
  230.         if(p==null || p.size()<=0){
  231.             p = this.reader.readPropertiesConvertEnvProperties(PROP_DATASOURCE_JNDI_CONTEXT);
  232.         }
  233.         return p;
  234.     }

  235.     private static final String PROP_CONNECTION_URL = "db.connection.url";
  236.     public String getConnectionUrl(IProjectInfo tipoDAO) throws Exception {
  237.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_CONNECTION_URL, false, true);
  238.         if(v==null){
  239.             v = this.getProperty(PROP_CONNECTION_URL, true, true);
  240.         }
  241.         return v;
  242.     }
  243.     private static final String PROP_CONNECTION_DRIVER = "db.connection.driver";
  244.     public String getConnectionDriverJDBC(IProjectInfo tipoDAO) throws Exception {
  245.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_CONNECTION_DRIVER, false, true);
  246.         if(v==null){
  247.             v = this.getProperty(PROP_CONNECTION_DRIVER, PROP_TIPO_VALUE_CONNECTION.equals(this.getTipoAccessoDatabase(tipoDAO)), true);
  248.         }
  249.         return v;
  250.     }
  251.     private static final String PROP_CONNECTION_AUTH_USER = "db.connection.user";
  252.     public String getConnectionAuthUsername(IProjectInfo tipoDAO) throws Exception {
  253.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_CONNECTION_AUTH_USER, false, true);
  254.         if(v==null){
  255.             v = this.getProperty(PROP_CONNECTION_AUTH_USER, PROP_TIPO_VALUE_CONNECTION.equals(this.getTipoAccessoDatabase(tipoDAO)), true);
  256.         }
  257.         return v;
  258.     }
  259.     private static final String PROP_CONNECTION_AUTH_PASSWORD = "db.connection.password";
  260.     public String getConnectionAuthPassword(IProjectInfo tipoDAO) throws Exception {
  261.         String v = this.getProperty(PREFIX_FACTORY+tipoDAO.getProjectName()+"."+PROP_CONNECTION_AUTH_PASSWORD, false, true);
  262.         if(v==null){
  263.             v = this.getProperty(PROP_CONNECTION_AUTH_PASSWORD, PROP_TIPO_VALUE_CONNECTION.equals(this.getTipoAccessoDatabase(tipoDAO)), true);
  264.         }
  265.         return v;
  266.     }

  267. }