RepositoryGestioneStateful.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.core.transazioni;

  21. import java.sql.Connection;
  22. import java.util.ArrayList;

  23. import org.openspcoop2.core.commons.dao.DAOFactory;
  24. import org.openspcoop2.core.commons.dao.DAOFactoryProperties;
  25. import org.openspcoop2.core.id.IDSoggetto;
  26. import org.openspcoop2.core.transazioni.dao.ITransazioneService;
  27. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  28. import org.openspcoop2.pdd.config.DBTransazioniManager;
  29. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  30. import org.openspcoop2.pdd.config.Resource;
  31. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  32. import org.openspcoop2.protocol.sdk.diagnostica.MsgDiagnostico;
  33. import org.openspcoop2.protocol.sdk.dump.Messaggio;
  34. import org.openspcoop2.protocol.sdk.tracciamento.Traccia;
  35. import org.openspcoop2.utils.Utilities;
  36. import org.slf4j.Logger;

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

  45.     private static final String ID_MODULO = "RepositoryGestioneStateful";
  46.    
  47.     private static Boolean gestioneStatefulAbilitata = null;
  48.    
  49.     private static String tipoDatabase = null; //tipoDatabase
  50.     private static DAOFactory daoFactory = null;
  51.     private static ServiceManagerProperties daoFactoryServiceManagerPropertiesTransazioni = null;
  52.     private static Logger log = null;
  53.     private static Logger daoFactoryLoggerTransazioni = null;
  54.    
  55.     private static OpenSPCoop2Properties openspcoopProperties = null;
  56.     private static boolean debug;
  57.    
  58.     private static GestoreTransazioniStateful gestoreTransazioniStateful = null;
  59.     private static synchronized void init() throws TransactionStatefulNotSupportedException{
  60.         if(gestioneStatefulAbilitata==null){
  61.             try{
  62.                 openspcoopProperties = OpenSPCoop2Properties.getInstance();
  63.                 gestioneStatefulAbilitata = openspcoopProperties.isTransazioniStatefulEnabled();
  64.            
  65.                 tipoDatabase = openspcoopProperties.getDatabaseType();
  66.                 //System.out.println("DS["+this.datasource+"] TIPODB["+this.tipoDatabase+"]");
  67.    
  68.                 if(tipoDatabase==null){
  69.                     throw new Exception("Tipo Database non definito");
  70.                 }
  71.    
  72.                 openspcoopProperties = OpenSPCoop2Properties.getInstance();
  73.    
  74.                 debug = openspcoopProperties.isTransazioniStatefulDebug();
  75.                
  76.                 DAOFactoryProperties daoFactoryProperties = null;
  77.                 log = OpenSPCoop2Logger.getLoggerOpenSPCoopTransazioniStateful(debug);
  78.                 daoFactoryLoggerTransazioni = OpenSPCoop2Logger.getLoggerOpenSPCoopTransazioniStatefulSql(debug);
  79.                 daoFactory = DAOFactory.getInstance(daoFactoryLoggerTransazioni);
  80.                 daoFactoryProperties = DAOFactoryProperties.getInstance(daoFactoryLoggerTransazioni);
  81.                 daoFactoryServiceManagerPropertiesTransazioni = daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance());
  82.                 daoFactoryServiceManagerPropertiesTransazioni.setShowSql(debug);    
  83.                 daoFactoryServiceManagerPropertiesTransazioni.setDatabaseType(DBTransazioniManager.getInstance().getTipoDatabase());

  84.             }catch(Exception e){
  85.                 throw new TransactionStatefulNotSupportedException("Inizializzazione risorse database non riuscita: "+e.getMessage(),e);
  86.             }
  87.             try{
  88.                 gestoreTransazioniStateful = new GestoreTransazioniStateful(log, daoFactoryLoggerTransazioni,
  89.                         tipoDatabase,
  90.                         debug);
  91.             }catch(Exception e){
  92.                 throw new TransactionStatefulNotSupportedException("Inizializzazione GestoreTransazioniStateful non riuscita: "+e.getMessage(),e);
  93.             }
  94.         }
  95.     }
  96.     private static void checkGestioneAbilitata() throws TransactionStatefulNotSupportedException{
  97.         if(gestioneStatefulAbilitata==null){
  98.             init();
  99.         }
  100.         if(gestioneStatefulAbilitata==false){
  101.             throw new TransactionStatefulNotSupportedException("Non abilitata la gestione delle transazioni stateful");
  102.         }
  103.     }
  104.    
  105.    
  106.     // TODO: Gestione realizzata su database!!!!
  107.     // Questa implementazione puo' causare OUT OF MEMORY
  108.     private static ArrayList<StatefulObject> repository =
  109.         new ArrayList<StatefulObject>();
  110.            
  111.     private static void invokeGestoreTransazioniStateful(StatefulObject s,TransactionDB transactionDB)throws TransactionStatefulNotSupportedException {
  112.         Resource dbResource = null;
  113.         DBTransazioniManager dbManager = DBTransazioniManager.getInstance();
  114.         IDSoggetto idDominio = openspcoopProperties.getIdentitaPortaDefault(s.getProtocollo(), null);
  115.         Connection con = null;
  116.         try{
  117.             dbResource = dbManager.getResource(idDominio, ID_MODULO, null);
  118.             con = (Connection) dbResource.getResource();    
  119.            
  120.             boolean autoCommit = false;
  121.             con.setAutoCommit(autoCommit);
  122.            
  123.             org.openspcoop2.core.transazioni.dao.jdbc.JDBCServiceManager jdbcServiceManager =
  124.                     (org.openspcoop2.core.transazioni.dao.jdbc.JDBCServiceManager) daoFactory.getServiceManager(org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance(),
  125.                             con, autoCommit,
  126.                             daoFactoryServiceManagerPropertiesTransazioni, daoFactoryLoggerTransazioni);
  127.             jdbcServiceManager.getJdbcProperties().setShowSql(debug);
  128.             ITransazioneService transazioneService = jdbcServiceManager.getTransazioneService();
  129.            
  130.             gestoreTransazioniStateful.gestioneStatefulObject(transazioneService, con, s, transactionDB);
  131.             con.commit();
  132.         }catch(Exception e){
  133.             try{
  134.                 if(con!=null)
  135.                     con.rollback();
  136.             }catch(Exception eRollback){
  137.                 // close
  138.             }
  139.             throw new TransactionStatefulNotSupportedException("Errore durante la gestione della transazione stateful",e);
  140.         }finally{
  141.             try{
  142.                 if(con!=null)
  143.                     con.setAutoCommit(true);
  144.             }catch(Exception eRollback){
  145.                 // ignore
  146.             }
  147.             try {
  148.                 dbManager.releaseResource(idDominio, ID_MODULO, dbResource);
  149.             } catch (Exception e) {
  150.                 // close
  151.             }
  152.         }
  153.     }
  154.    
  155.     private static TransactionDB invokeGestoreTransazioniStateful_readTransactionDB(String protocollo, String idTransazione)throws TransactionStatefulNotSupportedException {
  156.         Resource dbResource = null;
  157.         DBTransazioniManager dbManager = DBTransazioniManager.getInstance();
  158.         IDSoggetto idDominio = openspcoopProperties.getIdentitaPortaDefault(protocollo, null);
  159.         Connection con = null;
  160.         try{
  161.             dbResource = dbManager.getResource(idDominio, ID_MODULO+".read", null);
  162.             con = (Connection) dbResource.getResource();
  163.            
  164.             org.openspcoop2.core.transazioni.dao.jdbc.JDBCServiceManager jdbcServiceManager =
  165.                     (org.openspcoop2.core.transazioni.dao.jdbc.JDBCServiceManager) daoFactory.getServiceManager(org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance(), con,
  166.                             daoFactoryServiceManagerPropertiesTransazioni, daoFactoryLoggerTransazioni);
  167.             jdbcServiceManager.getJdbcProperties().setShowSql(debug);
  168.             ITransazioneService transazioneService = jdbcServiceManager.getTransazioneService();
  169.            
  170.             return gestoreTransazioniStateful.readTransazione(transazioneService, idTransazione);
  171.         }catch(Exception e){
  172.             throw new TransactionStatefulNotSupportedException("Errore durante la gestione della transazione stateful",e);
  173.         }finally{
  174.             try {
  175.                 dbManager.releaseResource(idDominio, ID_MODULO, dbResource);
  176.             } catch (Exception e) {
  177.                 // close
  178.             }
  179.         }
  180.     }
  181.    
  182.    
  183.     public static void addMessaggio(String idTransazione, Messaggio messaggio) throws TransactionStatefulNotSupportedException {
  184.        
  185.         checkGestioneAbilitata();
  186.        
  187.         StatefulObject s = new StatefulObject(messaggio.getProtocollo());
  188.         s.setIdTransazione(idTransazione);
  189.         s.setObject(messaggio);
  190.         s.setType(StatefulObjectType.MESSAGGIO);
  191.        
  192.         // OUT OF MEMORY PROBLEM
  193.         //repository.add(s);
  194.         invokeGestoreTransazioniStateful(s,null);

  195.     }
  196.    
  197.     public static void addMsgDiagnostico(String idTransazione, MsgDiagnostico msgDiag) throws TransactionStatefulNotSupportedException {
  198.        
  199.         checkGestioneAbilitata();
  200.        
  201.         StatefulObject s = new StatefulObject(msgDiag.getProtocollo());
  202.         s.setIdTransazione(idTransazione);
  203.         s.setObject(msgDiag);
  204.         s.setType(StatefulObjectType.MSGDIAGNOSTICO);
  205.        
  206.         // OUT OF MEMORY PROBLEM
  207.         //repository.add(s);
  208.         invokeGestoreTransazioniStateful(s,null);

  209.     }
  210.    
  211.     public static void addTraccia(String idTransazione, Traccia traccia) throws TransactionStatefulNotSupportedException {

  212.         checkGestioneAbilitata();
  213.        
  214.         StatefulObject s = new StatefulObject(traccia.getProtocollo());
  215.         s.setIdTransazione(idTransazione);
  216.         s.setObject(traccia);
  217.         s.setType(StatefulObjectType.TRACCIA);
  218.         // OUT OF MEMORY PROBLEM
  219.         //repository.add(s);
  220.         invokeGestoreTransazioniStateful(s,null);

  221.     }
  222.    
  223.     public static void addOutRequestStatefulObject(String protocollo,String idTransazione, OutRequestStatefulObject object) throws TransactionStatefulNotSupportedException {
  224.        
  225.         checkGestioneAbilitata();
  226.        
  227.         StatefulObject s = new StatefulObject(protocollo);
  228.         s.setIdTransazione(idTransazione);
  229.         s.setObject(object);
  230.         s.setType(StatefulObjectType.OUT_REQUEST_STATEFUL_OBJECT);
  231.            
  232.         TransactionDB tr = invokeGestoreTransazioniStateful_readTransactionDB(protocollo,idTransazione);
  233.         // Se la transazione e' null, attendo 1 secondo e attendo se deve essere terminata la sua scrittura
  234.         if(tr==null){
  235.             Utilities.sleep(500);
  236.             tr = invokeGestoreTransazioniStateful_readTransactionDB(protocollo,idTransazione);
  237.         }
  238.         if(tr==null){
  239.             Utilities.sleep(500);
  240.             tr = invokeGestoreTransazioniStateful_readTransactionDB(protocollo,idTransazione);
  241.         }
  242.         if(tr!=null){
  243.             invokeGestoreTransazioniStateful(s,tr);
  244.         }else{
  245.             // OUT OF MEMORY PROBLEM
  246.             repository.add(s);
  247.         }
  248.     }
  249.    
  250.     public static void addInResponseStatefulObject(String protocollo,String idTransazione, InResponseStatefulObject object) throws TransactionStatefulNotSupportedException {
  251.        
  252.         checkGestioneAbilitata();
  253.        
  254.         StatefulObject s = new StatefulObject(protocollo);
  255.         s.setIdTransazione(idTransazione);
  256.         s.setObject(object);
  257.         s.setType(StatefulObjectType.IN_RESPONSE_STATEFUL_OBJECT);
  258.        
  259.         TransactionDB tr = invokeGestoreTransazioniStateful_readTransactionDB(protocollo,idTransazione);
  260.         // Se la transazione e' null, attendo 1 secondo e attendo se deve essere terminata la sua scrittura
  261.         if(tr==null){
  262.             Utilities.sleep(500);
  263.             tr = invokeGestoreTransazioniStateful_readTransactionDB(protocollo,idTransazione);
  264.         }
  265.         if(tr==null){
  266.             Utilities.sleep(500);
  267.             tr = invokeGestoreTransazioniStateful_readTransactionDB(protocollo,idTransazione);
  268.         }
  269.         if(tr!=null){
  270.             invokeGestoreTransazioniStateful(s,tr);
  271.         }else{
  272.             // OUT OF MEMORY PROBLEM
  273.             repository.add(s);
  274.         }
  275.     }
  276.    
  277.     public static void addObject(StatefulObject s) throws TransactionStatefulNotSupportedException {

  278.         checkGestioneAbilitata();
  279.        
  280.         repository.add(s);
  281.     }
  282.    
  283.     // NOTA: se si realizza questa versione su database mantenere il comportamento a LISTA
  284.     // Si inserisce in fondo, si preleva in cima, tramite la order by
  285.    
  286.     public static int size(){
  287.         return repository.size();
  288.     }
  289.    
  290.     public static StatefulObject getObject(){
  291.         return repository.get(0); // oggetto in testa alla coda
  292.     }
  293.    
  294.     public static StatefulObject removeObject(){
  295.         return repository.remove(0); // oggetto in testa alla coda
  296.     }
  297. }