OpenSPCoopState.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.state;

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

  23. import org.openspcoop2.core.id.IDSoggetto;
  24. import org.openspcoop2.pdd.config.DBConsegneMessageBoxManager;
  25. import org.openspcoop2.pdd.config.DBConsegnePreseInCaricoManager;
  26. import org.openspcoop2.pdd.config.DBManager;
  27. import org.openspcoop2.pdd.config.Resource;
  28. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  29. import org.openspcoop2.pdd.mdb.GenericMessage;
  30. import org.openspcoop2.protocol.sdk.state.IState;
  31. import org.openspcoop2.protocol.sdk.state.StateMessage;
  32. import org.openspcoop2.protocol.sdk.state.StatelessMessage;
  33. import org.openspcoop2.utils.UtilsException;
  34. import org.slf4j.Logger;

  35. /**
  36.  * Oggetto che rappresenta lo stato di una richiesta/risposta all'interno della PdD
  37.  *
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author Fabio Tronci (tronci@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public abstract class OpenSPCoopState implements IOpenSPCoopState {
  44.    
  45.     /* ---------- Logger ---------*/
  46.     protected Logger logger = null;
  47.    
  48.     /* ---------- Connessione al database ---------------- */
  49.     protected OpenSPCoopStateDBManager dbManager = null;
  50.     /** DBManager */
  51.     protected DBManager dbManager_runtime= null;
  52.     /** DBManager */
  53.     protected DBConsegnePreseInCaricoManager dbManager_consegnePreseInCarico = null;
  54.     /** DBManager */
  55.     protected DBConsegneMessageBoxManager dbManager_consegneMessageBox = null;
  56.     /** Resource */
  57.     protected Resource resourceDB = null;
  58.     /** Connessione al database */
  59.     protected Connection connectionDB = null;
  60.     /** Connessione inizializzata */
  61.     protected boolean connessioneInizializzata = false;
  62.    
  63.     /* ----------- Altre informazioni ------------*/
  64.     /** Identita PdD */
  65.     protected IDSoggetto identitaPdD = null;
  66.     /** Identita Modulo */
  67.     protected String idModulo = null;
  68.     /** GenericMessage LIB */
  69.     protected GenericMessage messageLib;
  70.     /** Identificativo della sessione */
  71.     protected String IDMessaggioSessione;
  72.    
  73.     /* ---------- Stato ------------ */
  74.     protected StateMessage richiestaStato = null;
  75.     protected StateMessage rispostaStato = null;
  76.    
  77.     /* ---------- Indicazione se lavorare come stateful (utilizzare la connessione) o come stateless (operazioni NOP) -------------- */
  78.     protected boolean useConnection;
  79.    
  80.    
  81.    
  82.    
  83.    
  84.    
  85.     /* ----------- Convertitori ------------*/
  86.     public static OpenSPCoopStateless toStateless(OpenSPCoopStateful stateful,boolean useConnection){
  87.         OpenSPCoopStateless stateless = new OpenSPCoopStateless();
  88.         stateless.connectionDB = stateful.connectionDB;
  89.         stateless.dbManager = stateful.dbManager;
  90.         stateless.dbManager_runtime = stateful.dbManager_runtime;
  91.         stateless.dbManager_consegnePreseInCarico = stateful.dbManager_consegnePreseInCarico;
  92.         stateless.dbManager_consegneMessageBox = stateful.dbManager_consegneMessageBox;
  93.         stateless.IDMessaggioSessione = stateful.IDMessaggioSessione;
  94.         stateless.identitaPdD = stateful.identitaPdD;
  95.         stateless.idModulo = stateful.idModulo;
  96.         stateless.logger = stateful.logger;
  97.         stateless.messageLib = stateful.messageLib;
  98.         stateless.resourceDB = stateful.resourceDB;
  99.         stateless.richiestaStato = stateful.richiestaStato;
  100.         stateless.rispostaStato = stateful.rispostaStato;
  101.         stateless.useConnection = useConnection;
  102.         stateless.connessioneInizializzata = stateful.connessioneInizializzata;
  103.        
  104.         StatelessMessage tempRichiesta = new StatelessMessage(stateful.getConnectionDB(),stateful.logger);
  105.         if(stateful.getStatoRichiesta()!=null) {
  106.             tempRichiesta.setPreparedStatement(((StateMessage) stateful.getStatoRichiesta()).getPreparedStatement());
  107.         }
  108.         stateless.setStatoRichiesta(new StatelessMessage(tempRichiesta));
  109.        
  110.         StatelessMessage tempRisposta = new StatelessMessage(stateful.getConnectionDB(),stateful.logger);
  111.         if(stateful.getStatoRisposta()!=null) {
  112.             tempRisposta.setPreparedStatement(((StateMessage) stateful.getStatoRisposta()).getPreparedStatement());
  113.         }
  114.         stateless.setStatoRisposta(new StatelessMessage(tempRisposta));
  115.        
  116.         return stateless;
  117.     }
  118.    
  119.    
  120.    


  121.    

  122.     /* ----------- Init resource ------------*/

  123.     public abstract void updateStatoRichiesta() throws UtilsException ;
  124.     public abstract void updateStatoRisposta() throws UtilsException ;
  125.        
  126.     @Override
  127.     public void initResource(IDSoggetto identitaPdD,String idModulo,String idTransazione)throws OpenSPCoopStateException{
  128.         initResource(identitaPdD,idModulo,idTransazione, OpenSPCoopStateDBManager.runtime);
  129.     }
  130.     @Override
  131.     public void initResource(IDSoggetto identitaPdD,String idModulo,String idTransazione, OpenSPCoopStateDBManager dbManager)throws OpenSPCoopStateException{
  132.        
  133.         this.dbManager = dbManager; // per update
  134.        
  135.         // Check parametri
  136.         if(identitaPdD==null){
  137.             throw new OpenSPCoopStateException("IdentitaPdD non presente");
  138.         }
  139.         this.identitaPdD = identitaPdD;
  140.         if(idModulo==null){
  141.             throw new OpenSPCoopStateException("IDModulo non presente");
  142.         }
  143.         this.idModulo = idModulo;
  144.        
  145.         // Logger
  146.         this.logger = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  147.        
  148.         if(this.useConnection){
  149.            
  150.             // Get Connessione
  151.             if(dbManager!=null) {
  152.                 switch (dbManager) {
  153.                 case runtime:
  154.                     this.dbManager_runtime = DBManager.getInstance();
  155.                     break;
  156.                 case smistatoreMessaggiPresiInCarico:
  157.                     this.dbManager_consegnePreseInCarico = DBConsegnePreseInCaricoManager.getInstanceSmistatore();
  158.                     break;
  159.                 case consegnePreseInCarico:
  160.                     this.dbManager_consegnePreseInCarico = DBConsegnePreseInCaricoManager.getInstanceRuntime();
  161.                     break;
  162.                 case messageBox:
  163.                     this.dbManager_consegneMessageBox = DBConsegneMessageBoxManager.getInstanceRuntime();
  164.                     break;
  165.                 }
  166.             }
  167.             else {
  168.                 this.dbManager_runtime = DBManager.getInstance();
  169.             }
  170.             try{
  171.                 if(this.dbManager_runtime!=null) {
  172.                     this.resourceDB = this.dbManager_runtime.getResource(identitaPdD,this.idModulo,idTransazione);
  173.                 }
  174.                 else if(this.dbManager_consegnePreseInCarico!=null) {
  175.                     this.resourceDB = this.dbManager_consegnePreseInCarico.getResource(identitaPdD,this.idModulo,idTransazione);
  176.                 }
  177.                 else {
  178.                     this.resourceDB = this.dbManager_consegneMessageBox.getResource(identitaPdD,this.idModulo,idTransazione);
  179.                 }
  180.             }catch(Exception e){
  181.                 throw new OpenSPCoopStateException("Riscontrato errore durante la richiesta di una connessione al DB",e);
  182.             }
  183.             if(this.resourceDB==null){
  184.                 throw new OpenSPCoopStateException("Riscontrato errore durante la richiesta di una connessione al DB (Risorsa is null).");
  185.             }
  186.             if(this.resourceDB.getResource() == null){
  187.                     throw new OpenSPCoopStateException("Riscontrato errore durante la richiesta di una connessione al DB (Connessione is null).");
  188.             }
  189.             this.connectionDB = (Connection) this.resourceDB.getResource();
  190.            
  191.             // Stato
  192.             try{
  193.                 this.updateStatoRichiesta();
  194.                 this.updateStatoRisposta();
  195.             }catch(Exception e){
  196.                 this.logger.error("Update stato richiesta/risposta non riuscito",e);
  197.                 if(this.dbManager_runtime!=null) {
  198.                     this.dbManager_runtime.releaseResource(this.identitaPdD,this.idModulo,this.resourceDB);
  199.                 }
  200.                 else if(this.dbManager_consegnePreseInCarico!=null) {
  201.                     this.dbManager_consegnePreseInCarico.releaseResource(this.identitaPdD,this.idModulo,this.resourceDB);
  202.                 }
  203.                 else {
  204.                     this.dbManager_consegneMessageBox.releaseResource(this.identitaPdD,this.idModulo,this.resourceDB);
  205.                 }
  206.                 throw new OpenSPCoopStateException("Update stato richiesta/risposta non riuscito",e);
  207.             }
  208.            
  209.             this.connessioneInizializzata = true;
  210.         }
  211.     }
  212.    
  213.     @Override
  214.     public void updateResource(String idTransazione) throws OpenSPCoopStateException{
  215.         if( this.useConnection ){
  216.             try{
  217.                 if(this.dbManager!=null) {
  218.                     switch (this.dbManager) {
  219.                     case runtime:
  220.                         if(this.dbManager_runtime==null) {
  221.                             this.dbManager_runtime = DBManager.getInstance();
  222.                         }
  223.                         break;
  224.                     case smistatoreMessaggiPresiInCarico:
  225.                         if(this.dbManager_consegnePreseInCarico==null) {
  226.                             this.dbManager_consegnePreseInCarico = DBConsegnePreseInCaricoManager.getInstanceSmistatore();
  227.                         }
  228.                         break;
  229.                     case consegnePreseInCarico:
  230.                         if(this.dbManager_consegnePreseInCarico==null) {
  231.                             this.dbManager_consegnePreseInCarico = DBConsegnePreseInCaricoManager.getInstanceRuntime();
  232.                         }
  233.                         break;
  234.                     case messageBox:
  235.                         if(this.dbManager_consegneMessageBox==null) {
  236.                             this.dbManager_consegneMessageBox = DBConsegneMessageBoxManager.getInstanceRuntime();
  237.                         }
  238.                         break;
  239.                     }
  240.                 }
  241.                 else {
  242.                     if(this.dbManager_runtime==null) {
  243.                         this.dbManager_runtime = DBManager.getInstance();
  244.                     }
  245.                 }
  246.                
  247.                 if(this.dbManager_runtime!=null) {
  248.                     this.resourceDB = this.dbManager_runtime.getResource(this.identitaPdD,this.idModulo,idTransazione);
  249.                 }
  250.                 else if(this.dbManager_consegnePreseInCarico!=null) {
  251.                     this.resourceDB = this.dbManager_consegnePreseInCarico.getResource(this.identitaPdD,this.idModulo,idTransazione);
  252.                 }
  253.                 else {
  254.                     this.resourceDB = this.dbManager_consegneMessageBox.getResource(this.identitaPdD,this.idModulo,idTransazione);
  255.                 }
  256.             }catch(Exception e){
  257.                 throw new OpenSPCoopStateException("Riscontrato errore durante la richiesta di una connessione al DB",e);
  258.             }
  259.             if(this.resourceDB==null){
  260.                 throw new OpenSPCoopStateException("Riscontrato errore durante la richiesta di una connessione al DB (Risorsa is null).");
  261.             }
  262.             if(this.resourceDB.getResource() == null){
  263.                 throw new OpenSPCoopStateException("Riscontrato errore durante la richiesta di una connessione al DB (Connessione is null).");
  264.             }
  265.             this.connectionDB = (Connection) this.resourceDB.getResource();
  266.            
  267.             this.richiestaStato.updateConnection(this.connectionDB);
  268.             this.rispostaStato.updateConnection(this.connectionDB);
  269.            
  270.             this.connessioneInizializzata = true;
  271.         }
  272.     }
  273.    
  274.     @Override
  275.     public void releaseResource() {
  276.         if(this.useConnection && this.connessioneInizializzata){
  277.             try{
  278.                 if(this.richiestaStato!=null && this.richiestaStato.getPreparedStatement()!=null){
  279.                     if(this.richiestaStato.getPreparedStatement().size()>0){
  280.                         List<String> l = this.richiestaStato.getPreparedStatement().keys();
  281.                         if(l!=null && !l.isEmpty()) {
  282.                             for (String key : l) {
  283.                                 this.logger.error("PREPARED STATEMENT NON CHIUSA (RICHIESTA): "+key);
  284.                             }
  285.                         }
  286.                         this.richiestaStato.closePreparedStatement();
  287.                     }
  288.                 }
  289.             }catch (Exception e) {  
  290.                 this.logger.error("Chiusure prepared statement della richiesta non riuscita",e);
  291.             }
  292.             try{
  293.                 if(this.rispostaStato!=null && this.rispostaStato.getPreparedStatement()!=null){
  294.                     if(this.rispostaStato.getPreparedStatement().size()>0){
  295.                         List<String> l = this.rispostaStato.getPreparedStatement().keys();
  296.                         if(l!=null && !l.isEmpty()) {
  297.                             for (String key : l) {
  298.                                 this.logger.error("PREPARED STATEMENT NON CHIUSA (RISPOSTA): "+key);
  299.                             }
  300.                         }
  301.                         this.rispostaStato.closePreparedStatement();
  302.                     }
  303.                 }
  304.             }catch (Exception e) {  
  305.                 this.logger.error("Chiusure prepared statement della risposta non riuscita",e);
  306.             }
  307.             try{
  308.                 if(this.resourceDB!=null){
  309.                     if(this.dbManager_runtime!=null) {
  310.                         this.dbManager_runtime.releaseResource(this.identitaPdD,this.idModulo,this.resourceDB);
  311.                     }
  312.                     else if(this.dbManager_consegnePreseInCarico!=null) {
  313.                         this.dbManager_consegnePreseInCarico.releaseResource(this.identitaPdD,this.idModulo,this.resourceDB);
  314.                     }
  315.                     else {
  316.                         this.dbManager_consegneMessageBox.releaseResource(this.identitaPdD,this.idModulo,this.resourceDB);
  317.                     }
  318.                 }
  319.                 if(this.richiestaStato!=null) {
  320.                     this.richiestaStato.updateConnection(null);
  321.                 }
  322.                 if(this.rispostaStato!=null) {
  323.                     this.rispostaStato.updateConnection(null);
  324.                 }
  325.             }catch (Exception e) {  
  326.                 this.logger.error("Rilasciate risorse con errore: "+e.getMessage(),e);
  327.                 OpenSPCoop2Logger.getLoggerOpenSPCoopConsole().error("Rilasciate risorse con errore: "+e.getMessage());
  328.             }
  329.             this.connessioneInizializzata = false;
  330.         }
  331.     }
  332.    
  333.     @Override
  334.     public boolean resourceReleased(){
  335.         return !this.connessioneInizializzata;
  336.     }
  337.    
  338.     public void forceFinallyReleaseResource() {
  339.         if(this.connessioneInizializzata){
  340.             this.useConnection = true; // force
  341.             //System.out.println("Force close ...");
  342.             this.releaseResource();
  343.         }
  344.     }
  345.    
  346.    
  347.    
  348.    
  349.    
  350.    
  351.     /* ----------- Commit / Close ------------*/
  352.    
  353.     @Override
  354.     public void commit() throws OpenSPCoopStateException{
  355.         if(this.useConnection){
  356.             if(   (this.richiestaStato!=null && this.richiestaStato.getPreparedStatement()!=null && this.richiestaStato.getPreparedStatement().size()>0)  ||
  357.                   (this.rispostaStato!=null && this.rispostaStato.getPreparedStatement()!=null && this.rispostaStato.getPreparedStatement().size()>0)
  358.             ){
  359.                 try{
  360.                     this.connectionDB.setAutoCommit(false);
  361.                    
  362. //                  System.out.println("PREPARED STATEMENT (RICHIESTA)");
  363. //                  Enumeration<String> en = this.richiestaStato.getPreparedStatement().keys();
  364. //                  while(en.hasMoreElements()){
  365. //                      String key = en.nextElement();
  366. //                      System.out.println("PREPARED STATEMENT (RICHIESTA): "+key);
  367. //                  }          
  368.                     if(this.richiestaStato!=null) {
  369.                         this.richiestaStato.executePreparedStatement();
  370.                     }
  371.                    
  372. //                  System.out.println("PREPARED STATEMENT (RISPOSTA)");
  373. //                  en = this.rispostaStato.getPreparedStatement().keys();
  374. //                  while(en.hasMoreElements()){
  375. //                      String key = en.nextElement();
  376. //                      System.out.println("PREPARED STATEMENT (RISPOSTA): "+key);
  377. //                  }  
  378.                     if(this.rispostaStato!=null) {
  379.                         this.rispostaStato.executePreparedStatement();
  380.                     }
  381.                    
  382.                     this.connectionDB.commit();
  383.                     this.connectionDB.setAutoCommit(true);
  384.                    
  385.                 }catch (Exception e) {
  386.                     // Chiudo eventuali prepared statement non chiuse
  387.                     try{
  388.                         if(this.richiestaStato!=null) {
  389.                             this.richiestaStato.closePreparedStatement();
  390.                         }
  391.                     }catch(Exception eClose){}
  392.                     try{
  393.                         if(this.rispostaStato!=null) {
  394.                             this.rispostaStato.closePreparedStatement();
  395.                         }
  396.                     }catch(Exception eClose){}
  397.                     //  Rollback quanto effettuato (se l'errore e' avvenuto sul commit, o prima nell'execute delle PreparedStatement)
  398.                     try{
  399.                         this.connectionDB.rollback();
  400.                     }catch(Exception er){}
  401.                     // Ripristino connessione
  402.                     try{
  403.                         this.connectionDB.setAutoCommit(true);
  404.                     }catch(Exception er){}
  405.                     // Rilancio l'eccezione: sara' catturata dal catch seguente, che chiudera' le preparedStatement,
  406.                     // chiude la connessione al DB ed effettua il Rollback dell'MDB(a meno del profilo sincrono).
  407.                     throw new OpenSPCoopStateException(e.getMessage(),e);
  408.                 }
  409.             }
  410.         }
  411.     }
  412.    
  413.    
  414.    
  415.    
  416.    
  417.    
  418.    
  419.    
  420.     /* ----------- GET / SET ------------*/
  421.     @Override
  422.     public GenericMessage getMessageLib() {
  423.         return this.messageLib;
  424.     }

  425.     public void setMessageLib(GenericMessage messageLib) {
  426.         this.messageLib = messageLib;
  427.     }

  428.     @Override
  429.     public String getIDMessaggioSessione() {
  430.         return this.IDMessaggioSessione;
  431.     }

  432.     public void setIDMessaggioSessione(String idSessione) {
  433.         this.IDMessaggioSessione = idSessione;
  434.     }
  435.    
  436.     @Override
  437.     public IState getStatoRichiesta(){
  438.         return this.richiestaStato;
  439.     }
  440.     @Override
  441.     public void setStatoRichiesta(IState statoRichiesta){
  442.         this.richiestaStato = (StateMessage) statoRichiesta;
  443.     }
  444.     @Override
  445.     public IState getStatoRisposta(){
  446.         return this.rispostaStato;
  447.     }
  448.     @Override
  449.     public void setStatoRisposta(IState statoRisposta){
  450.         this.rispostaStato = (StateMessage) statoRisposta;
  451.     }
  452.     public Connection getConnectionDB() {
  453.         return this.connectionDB;
  454.     }
  455.     public void setConnectionDB(Connection connectionDB) {
  456.         this.connectionDB = connectionDB;
  457.     }
  458.    
  459.     public boolean isUseConnection() {
  460.         return this.useConnection;
  461.     }

  462.     public void setUseConnection(boolean useConnection) {
  463.         this.useConnection = useConnection;
  464.     }

  465. }