DatabaseFactory.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 java.lang.reflect.Constructor;
  22. import java.sql.Connection;
  23. import java.util.Properties;

  24. import javax.sql.DataSource;

  25. import org.slf4j.Logger;
  26. import org.openspcoop2.generic_project.exception.ServiceException;

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

  35.     private Logger log = null;
  36.     private DatabaseProperties databaseProperties = null;
  37.     private Class<T> cServiceManager = null;
  38.    
  39.     public DatabaseFactory(Class<T> cServiceManager,String PROPERTIES_LOCAL_PATH, String PROPERTIES_NAME, String nomeFileProperties,org.slf4j.Logger log) throws ServiceException{
  40.         try{
  41.             this.cServiceManager = cServiceManager;
  42.             org.openspcoop2.generic_project.utils.DatabaseProperties.initialize(PROPERTIES_LOCAL_PATH, PROPERTIES_NAME, nomeFileProperties, log);
  43.             this.log = log;
  44.             this.databaseProperties = DatabaseProperties.getInstance(log);
  45.         }catch(Exception e){
  46.             throw new ServiceException(e.getMessage(),e);
  47.         }
  48.     }
  49.    

  50.    
  51.     private T getServiceManager(DataSource ds,Connection connection,Boolean isAutocommit) throws ServiceException{
  52.        
  53.         try{
  54.        
  55.             Constructor<T> constrServiceManager = null;
  56.             T object = null;
  57.             ServiceManagerProperties smProperties = this.databaseProperties.getServiceManagerProperties();
  58.             if(isAutocommit!=null){
  59.                 smProperties.setAutomaticTransactionManagement(isAutocommit);
  60.             }
  61.            
  62.             if(ds!=null){
  63.                 constrServiceManager = this.cServiceManager
  64.                         .getConstructor(DataSource.class,
  65.                                 ServiceManagerProperties.class, Logger.class);
  66.                 object = constrServiceManager
  67.                         .newInstance(ds,
  68.                                 smProperties,this.log);
  69.             }  
  70.             else if(connection!=null){
  71.                 constrServiceManager = this.cServiceManager
  72.                         .getConstructor(Connection.class,
  73.                                 ServiceManagerProperties.class, Logger.class);
  74.                 object = constrServiceManager
  75.                         .newInstance(connection,
  76.                                 smProperties,this.log);
  77.             }
  78.             else{  
  79.                 if(this.databaseProperties.isTipoAccessoTramiteDatasource()){
  80.                     constrServiceManager = this.cServiceManager
  81.                             .getConstructor(String.class,
  82.                                     Properties.class,
  83.                                     ServiceManagerProperties.class, Logger.class);
  84.                     object = constrServiceManager
  85.                             .newInstance(this.databaseProperties.getDatasourceJNDIName(),
  86.                                     this.databaseProperties.getDatasourceJNDIContext(),
  87.                                     smProperties,this.log);
  88.                 }
  89.                 else{
  90.                     constrServiceManager = this.cServiceManager
  91.                             .getConstructor(String.class,
  92.                                     String.class,
  93.                                     String.class,
  94.                                     String.class,
  95.                                     ServiceManagerProperties.class, Logger.class);
  96.                     object = constrServiceManager
  97.                             .newInstance(this.databaseProperties.getConnectionUrl(),
  98.                                     this.databaseProperties.getConnectionDriverJDBC(),
  99.                                     this.databaseProperties.getConnectionAuthUsername(),
  100.                                     this.databaseProperties.getConnectionAuthPassword(),
  101.                                     smProperties,this.log);
  102.                 }      
  103.             }
  104.            
  105.             return object;
  106.            
  107.         }catch(Exception e){
  108.             throw new ServiceException(e.getMessage(),e);
  109.         }
  110.     }
  111.    
  112.    
  113.    
  114.     // Metodi
  115.     public Object getServiceManager() throws ServiceException{
  116.         return this.getServiceManager(null, null, null);
  117.     }
  118.     public Object getServiceManager(boolean autoCommit) throws ServiceException{
  119.         return this.getServiceManager(null, null, autoCommit);
  120.     }
  121.     public Object getServiceManager(DataSource ds) throws ServiceException{
  122.         return this.getServiceManager(ds, null, null);
  123.     }
  124.     public Object getServiceManager(DataSource ds,boolean autoCommit) throws ServiceException{
  125.         return this.getServiceManager(ds, null, autoCommit);
  126.     }
  127.     public Object getServiceManager(Connection connection) throws ServiceException{
  128.         return this.getServiceManager(null, connection, null);
  129.     }
  130.     public Object getServiceManager(Connection connection,boolean autoCommit) throws ServiceException{
  131.         return this.getServiceManager(null, connection, autoCommit);
  132.     }
  133.    
  134. }