DBConsegnePreseInCaricoManager.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.pdd.config;

  21. import java.sql.Connection;
  22. import java.sql.Statement;

  23. import javax.sql.DataSource;

  24. import org.openspcoop2.core.commons.CoreException;
  25. import org.openspcoop2.core.commons.IMonitoraggioRisorsa;
  26. import org.openspcoop2.core.constants.Costanti;
  27. import org.openspcoop2.core.constants.CostantiDB;
  28. import org.openspcoop2.core.id.IDSoggetto;
  29. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  30. import org.openspcoop2.utils.LoggerWrapperFactory;
  31. import org.openspcoop2.utils.UtilsAlreadyExistsException;
  32. import org.openspcoop2.utils.datasource.DataSourceFactory;
  33. import org.openspcoop2.utils.datasource.DataSourceParams;
  34. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  35. import org.openspcoop2.utils.resources.GestoreJNDI;
  36. import org.slf4j.Logger;

  37. /**    
  38.  * DBConsegnePreseInCaricoManager
  39.  *
  40.  * @author Poli Andrea (poli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */
  44. public class DBConsegnePreseInCaricoManager implements IMonitoraggioRisorsa {

  45.     private static DBConsegnePreseInCaricoManager staticInstanceDBSmistatoreManager;
  46.     public static synchronized void initSmistatore(DBManager dbManagerRuntimePdD,Logger alog,String tipoDatabase) throws Exception {
  47.         if(staticInstanceDBSmistatoreManager==null) {
  48.             staticInstanceDBSmistatoreManager = new DBConsegnePreseInCaricoManager(dbManagerRuntimePdD, alog, tipoDatabase,true);
  49.         }
  50.     }
  51.     public static synchronized void initSmistatore(String nomeDataSource, java.util.Properties context,Logger alog,String tipoDatabase,
  52.             boolean useOp2UtilsDatasource, boolean bindJMX) throws Exception {
  53.         if(staticInstanceDBSmistatoreManager==null) {
  54.             staticInstanceDBSmistatoreManager = new DBConsegnePreseInCaricoManager(nomeDataSource, context, alog, tipoDatabase, useOp2UtilsDatasource, bindJMX,
  55.                     true, false);
  56.         }
  57.     }
  58.     public static DBConsegnePreseInCaricoManager getInstanceSmistatore() {
  59.         // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  60.         if (staticInstanceDBSmistatoreManager == null) {
  61.             synchronized (DBConsegnePreseInCaricoManager.class) {
  62.                 if (staticInstanceDBSmistatoreManager == null) {
  63.                     return null;
  64.                 }
  65.             }
  66.         }
  67.         return staticInstanceDBSmistatoreManager;
  68.     }
  69.    
  70.    
  71.     private static DBConsegnePreseInCaricoManager staticInstanceDBRuntimeManager;
  72.     public static synchronized void initRuntime(DBManager dbManagerRuntimePdD,Logger alog,String tipoDatabase) throws Exception {
  73.         if(staticInstanceDBRuntimeManager==null) {
  74.             staticInstanceDBRuntimeManager = new DBConsegnePreseInCaricoManager(dbManagerRuntimePdD, alog, tipoDatabase,false);
  75.         }
  76.     }
  77.     public static synchronized void initRuntime(String nomeDataSource, java.util.Properties context,Logger alog,String tipoDatabase,
  78.             boolean useOp2UtilsDatasource, boolean bindJMX) throws Exception {
  79.         if(staticInstanceDBRuntimeManager==null) {
  80.             staticInstanceDBRuntimeManager = new DBConsegnePreseInCaricoManager(nomeDataSource, context, alog, tipoDatabase, useOp2UtilsDatasource, bindJMX,
  81.                     false, false);
  82.         }
  83.     }
  84.     public static DBConsegnePreseInCaricoManager getInstanceRuntime() {
  85.         // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  86.         if (staticInstanceDBRuntimeManager == null) {
  87.             synchronized (DBConsegnePreseInCaricoManager.class) {
  88.                 if (staticInstanceDBRuntimeManager == null) {
  89.                     return null;
  90.                 }
  91.             }
  92.         }
  93.         return staticInstanceDBRuntimeManager;
  94.     }
  95.    
  96.    
  97.     private static DBConsegnePreseInCaricoManager staticInstanceDBTransazioniManager;
  98.     public static synchronized void initTransazioni(DBTransazioniManager dbManagerTransazioni,Logger alog,String tipoDatabase) throws Exception {
  99.         if(staticInstanceDBTransazioniManager==null) {
  100.             staticInstanceDBTransazioniManager = new DBConsegnePreseInCaricoManager(dbManagerTransazioni, alog, tipoDatabase);
  101.         }
  102.     }
  103.     public static synchronized void initTransazioni(String nomeDataSource, java.util.Properties context,Logger alog,String tipoDatabase,
  104.             boolean useOp2UtilsDatasource, boolean bindJMX) throws Exception {
  105.         if(staticInstanceDBTransazioniManager==null) {
  106.             staticInstanceDBTransazioniManager = new DBConsegnePreseInCaricoManager(nomeDataSource, context, alog, tipoDatabase, useOp2UtilsDatasource, bindJMX,
  107.                     false, true);
  108.         }
  109.     }
  110.     public static DBConsegnePreseInCaricoManager getInstanceTransazioni() {
  111.         // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  112.         if (staticInstanceDBTransazioniManager == null) {
  113.             synchronized (DBConsegnePreseInCaricoManager.class) {
  114.                 if (staticInstanceDBTransazioniManager == null) {
  115.                     return null;
  116.                 }
  117.             }
  118.         }
  119.         return staticInstanceDBTransazioniManager;
  120.     }
  121.    
  122.    
  123.     /** Informazione sui proprietari che hanno richiesto una connessione */
  124.     private static java.util.concurrent.ConcurrentHashMap<String,Resource> risorseInGestioneSmistatore = new java.util.concurrent.ConcurrentHashMap<>();
  125.     private static java.util.concurrent.ConcurrentHashMap<String,Resource> risorseInGestioneRuntime = new java.util.concurrent.ConcurrentHashMap<>();
  126.     private static java.util.concurrent.ConcurrentHashMap<String,Resource> risorseInGestioneTransazioni = new java.util.concurrent.ConcurrentHashMap<>();
  127.    
  128.     public static String[] getStatoRisorseSmistatore() throws Exception{    
  129.         return DBManager.getStatoRisorse(DBConsegnePreseInCaricoManager.risorseInGestioneSmistatore);
  130.     }
  131.     public static String[] getStatoRisorseRuntime() throws Exception{  
  132.         return DBManager.getStatoRisorse(DBConsegnePreseInCaricoManager.risorseInGestioneRuntime);
  133.     }
  134.     public static String[] getStatoRisorseTransazioni() throws Exception{  
  135.         return DBManager.getStatoRisorse(DBConsegnePreseInCaricoManager.risorseInGestioneTransazioni);
  136.     }
  137.    
  138.     private String tipoDatabase;
  139.     public String getTipoDatabase() {
  140.         return this.tipoDatabase;
  141.     }

  142.     private Logger log;
  143.    
  144.     private DBManager dbManagerRuntimePdD;
  145.     private DBTransazioniManager dbManagerTransazioni;
  146.     public boolean useDefaultManager() {
  147.         return this.dbManagerRuntimePdD!=null || this.dbManagerTransazioni!=null;
  148.     }
  149.     public boolean useRuntimePdD() {
  150.         if(useDefaultManager()) {
  151.             return this.dbManagerRuntimePdD!=null || this.dbManagerTransazioni.useRuntimePdD();
  152.         }
  153.         return false;
  154.     }
  155.     private DataSource datasourceDedicato;
  156.     private String identita;
  157.    
  158.     private DBConsegnePreseInCaricoManager(DBManager dbManagerRuntimePdD,Logger alog,String tipoDatabase,boolean smistatore) throws Exception {
  159.         this(alog, tipoDatabase, smistatore, false);
  160.         this.dbManagerRuntimePdD = dbManagerRuntimePdD;
  161.     }
  162.     private DBConsegnePreseInCaricoManager(DBTransazioniManager dbManagerTransazioni,Logger alog,String tipoDatabase) throws Exception {
  163.         this(alog, tipoDatabase, false, true);
  164.         this.dbManagerTransazioni = dbManagerTransazioni;
  165.     }

  166.     private DBConsegnePreseInCaricoManager(String nomeDataSource, java.util.Properties context,Logger alog,String tipoDatabase,
  167.             boolean useOp2UtilsDatasource, boolean bindJMX,
  168.             boolean smistatore, boolean tracce) throws Exception {
  169.        
  170.         this(alog, tipoDatabase, smistatore, tracce);
  171.        
  172.         try {
  173.            
  174.             if(useOp2UtilsDatasource){
  175.                 DataSourceParams dsParams = Costanti.getDataSourceParamsPdD(bindJMX, tipoDatabase);
  176.                 try{
  177.                     this.datasourceDedicato = DataSourceFactory.newInstance(nomeDataSource, context, dsParams);
  178.                 }catch(UtilsAlreadyExistsException exists){
  179.                     this.datasourceDedicato = DataSourceFactory.getInstance(nomeDataSource);
  180.                     if(this.datasourceDedicato==null){
  181.                         throw new Exception("Lookup datasource non riuscita ("+exists.getMessage()+")",exists);
  182.                     }
  183.                 }
  184.             }
  185.             else{
  186.                 GestoreJNDI gestoreJNDI = new GestoreJNDI(context);
  187.                 this.datasourceDedicato = (DataSource) gestoreJNDI.lookup(nomeDataSource);
  188.             }
  189.         } catch (Exception ne) {
  190.             this.log.error("Impossibile instanziare il manager: " + ne.getMessage(),ne);
  191.             throw ne;
  192.         }
  193.     }
  194.    
  195.     private DBConsegnePreseInCaricoManager(Logger alog,String tipoDatabase,boolean smistatore, boolean tracce) throws Exception {
  196.         this.log = alog;
  197.         this.tipoDatabase = tipoDatabase;
  198.         if(smistatore) {
  199.             this.identita = "DBConsegnePreseInCaricoSmistatoreManager";
  200.         }
  201.         else {
  202.             if(tracce) {
  203.                 this.identita = "DBConsegnePreseInCaricoTransazioniManager";
  204.             }
  205.             else {
  206.                 this.identita = "DBConsegnePreseInCaricoRuntimeManager";
  207.             }
  208.         }  
  209.     }
  210.     private boolean isIamSmistatoreManager() {
  211.         return "DBConsegnePreseInCaricoSmistatoreManager".equals(this.identita);
  212.     }
  213.     private boolean isIamRuntimeManager() {
  214.         return "DBConsegnePreseInCaricoRuntimeManager".equals(this.identita);
  215.     }
  216.     private boolean isIamTransazioniManager() {
  217.         return "DBConsegnePreseInCaricoTransazioniManager".equals(this.identita);
  218.     }
  219.    
  220.    
  221.     public Resource getResource(IDSoggetto idPDD,String modulo,String idTransazione) throws Exception {
  222.         return this.getResource(idPDD, modulo, idTransazione, true);
  223.     }
  224.     public Resource getResource(IDSoggetto idPDD,String modulo,String idTransazione, boolean logError) throws Exception {
  225.         if(this.dbManagerRuntimePdD!=null) {
  226.             /**System.out.println("id ["+idTransazione+"] prendo da dbManager");*/
  227.             return this.dbManagerRuntimePdD.getResource(idPDD, modulo, idTransazione, logError);
  228.         }
  229.         else if(this.dbManagerTransazioni!=null) {
  230.             /**System.out.println("id ["+idTransazione+"] prendo da dbManagerTransazioni");*/
  231.             return this.dbManagerTransazioni.getResource(idPDD, modulo, idTransazione, logError);
  232.         }
  233.         else {
  234.             /**System.out.println("id ["+idTransazione+"] negozio");*/
  235.             try {
  236.                 StringBuilder bf = new StringBuilder();
  237.                 if(idPDD!=null) {
  238.                     bf.append(idPDD.toString());
  239.                     if(modulo!=null) {
  240.                         bf.append(".");
  241.                     }
  242.                 }
  243.                 if(modulo!=null) {
  244.                     bf.append(modulo);
  245.                 }
  246.                 if(bf.length()<=0) {
  247.                     bf.append(this.identita);
  248.                 }
  249.                 Resource risorsa = DBManager.buildResource(this.identita, this.getConnectionFromDatasource(bf.toString(), idTransazione),
  250.                         idPDD, modulo, idTransazione);  
  251.                
  252.                 if(this.isIamSmistatoreManager()) {
  253.                     DBConsegnePreseInCaricoManager.risorseInGestioneSmistatore.put(risorsa.getId(), risorsa);
  254.                 }
  255.                 else if(this.isIamRuntimeManager()) {
  256.                     DBConsegnePreseInCaricoManager.risorseInGestioneRuntime.put(risorsa.getId(), risorsa);
  257.                 }
  258.                 else if(this.isIamTransazioniManager()) {
  259.                     DBConsegnePreseInCaricoManager.risorseInGestioneTransazioni.put(risorsa.getId(), risorsa);
  260.                 }
  261.                
  262.                 return risorsa;
  263.             }
  264.             catch(Exception e) {
  265.                 this.log.error("Errore durante l'ottenimento di una connessione: "+e.getMessage(),e);
  266.                 throw e;
  267.             }
  268.         }
  269.     }
  270.    
  271.     private Connection getConnectionFromDatasource(String methodName, String idTransazione) throws Exception{
  272.         if(this.datasourceDedicato instanceof org.openspcoop2.utils.datasource.DataSource){
  273.             return ((org.openspcoop2.utils.datasource.DataSource)this.datasourceDedicato).getWrappedConnection(idTransazione, this.identita+"."+methodName);
  274.         }
  275.         else{
  276.             return this.datasourceDedicato.getConnection();
  277.         }
  278.     }
  279.    
  280.     public void releaseResource(IDSoggetto idPDD,String modulo,Resource resource){
  281.         this.releaseResource(idPDD, modulo, resource, true);
  282.     }
  283.     public void releaseResource(IDSoggetto idPDD,String modulo,Resource resource, boolean logError){
  284.         try {
  285.             if(this.dbManagerRuntimePdD!=null) {
  286.                 this.dbManagerRuntimePdD.releaseResource(idPDD, modulo, resource, logError);
  287.             }
  288.             else if(this.dbManagerTransazioni!=null) {
  289.                 this.dbManagerTransazioni.releaseResource(idPDD, modulo, resource, logError);
  290.             }
  291.             else {
  292.                 if(resource!=null){
  293.                    
  294.                     if(resource.getResource()!=null){
  295.                         Connection connectionDB = (Connection) resource.getResource();
  296.                         if(connectionDB != null && (!connectionDB.isClosed())){
  297.                             Logger logR = OpenSPCoop2Logger.getLoggerOpenSPCoopResources()!=null ? OpenSPCoop2Logger.getLoggerOpenSPCoopResources() : LoggerWrapperFactory.getLogger(DBConsegnePreseInCaricoManager.class);
  298.                             boolean checkAutocommit = (OpenSPCoop2Properties.getInstance()==null) || OpenSPCoop2Properties.getInstance().isJdbcCloseConnectionCheckAutocommit();
  299.                             boolean checkIsClosed = (OpenSPCoop2Properties.getInstance()==null) || OpenSPCoop2Properties.getInstance().isJdbcCloseConnectionCheckIsClosed();
  300.                             JDBCUtilities.closeConnection(logR, connectionDB, checkAutocommit, checkIsClosed);
  301.                         }
  302.                     }  
  303.                    
  304.                     if(this.isIamSmistatoreManager()) {
  305.                         if(DBConsegnePreseInCaricoManager.risorseInGestioneSmistatore.containsKey(resource.getId())) {
  306.                             DBConsegnePreseInCaricoManager.risorseInGestioneSmistatore.remove(resource.getId());
  307.                         }
  308.                     }
  309.                     else if(this.isIamRuntimeManager()) {
  310.                         if(DBConsegnePreseInCaricoManager.risorseInGestioneRuntime.containsKey(resource.getId())) {
  311.                             DBConsegnePreseInCaricoManager.risorseInGestioneRuntime.remove(resource.getId());
  312.                         }
  313.                     }
  314.                     else if(this.isIamTransazioniManager()) {
  315.                         if(DBConsegnePreseInCaricoManager.risorseInGestioneTransazioni.containsKey(resource.getId())) {
  316.                             DBConsegnePreseInCaricoManager.risorseInGestioneTransazioni.remove(resource.getId());
  317.                         }
  318.                     }

  319.                 }
  320.             }

  321.         }
  322.         catch(Exception e) {
  323.             this.log.error("Errore durante il rilascio di una risorsa: "+e.getMessage(),e);
  324.         }
  325.     }
  326.    
  327.    
  328.    
  329.     /**
  330.      * Metodo che verica la connessione ad una risorsa.
  331.      * Se la connessione non e' presente, viene lanciata una eccezione che contiene il motivo della mancata connessione
  332.      *
  333.      * @throws DriverException eccezione che contiene il motivo della mancata connessione
  334.      */
  335.     @Override
  336.     public void isAlive() throws CoreException{
  337.         if(this.dbManagerRuntimePdD!=null) {
  338.             this.dbManagerRuntimePdD.isAlive();
  339.         }
  340.         else if(this.dbManagerTransazioni!=null) {
  341.             this.dbManagerTransazioni.isAlive();
  342.         }
  343.         else {
  344.             // Verifico la connessione
  345.             Resource resource = null;
  346.             Statement stmtTest = null;
  347.             IDSoggetto idSoggettAlive = new IDSoggetto();
  348.             idSoggettAlive.setCodicePorta(this.identita);
  349.             idSoggettAlive.setTipo(this.identita);
  350.             idSoggettAlive.setNome(this.identita);
  351.             try {
  352.                 try{
  353.                     resource = this.getResource(idSoggettAlive, "CheckIsAlive", null);
  354.                 }catch(Exception e){
  355.                     throw e;
  356.                 }
  357.                 if(resource == null)
  358.                     throw new Exception("Resource is null");
  359.                 if(resource.getResource() == null)
  360.                     throw new Exception("Connessione is null");
  361.                 Connection con = (Connection) resource.getResource();
  362.                 // test:
  363.                 stmtTest = con.createStatement();
  364.                 stmtTest.execute("SELECT * from "+CostantiDB.DB_INFO);
  365.                        
  366.             } catch (Exception e) {
  367.                 throw new CoreException("Connessione al database 'GovWay' ("+this.identita+") non disponibile: "+e.getMessage(),e);
  368.    
  369.             }finally{
  370.                 try{
  371.                     if(stmtTest!=null)
  372.                         stmtTest.close();
  373.                 }catch(Exception e){
  374.                     // close
  375.                 }
  376.                 try{
  377.                     this.releaseResource(idSoggettAlive, "CheckIsAlive", resource);
  378.                 }catch(Exception e){
  379.                     // close
  380.                 }
  381.             }
  382.         }
  383.     }
  384. }