SbustamentoRisposteMDB.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.mdb;

  21. import javax.ejb.EJBException;
  22. import javax.ejb.MessageDrivenBean;
  23. import javax.ejb.MessageDrivenContext;
  24. import javax.jms.Message;
  25. import javax.jms.MessageListener;
  26. import javax.jms.ObjectMessage;

  27. import org.slf4j.Logger;
  28. import org.openspcoop2.core.constants.TipoPdD;
  29. import org.openspcoop2.pdd.core.CostantiPdD;
  30. import org.openspcoop2.pdd.core.PdDContext;
  31. import org.openspcoop2.pdd.core.node.TransactionManager;
  32. import org.openspcoop2.pdd.core.state.OpenSPCoopStateful;
  33. import org.openspcoop2.pdd.logger.Dump;
  34. import org.openspcoop2.pdd.logger.MsgDiagnosticiProperties;
  35. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  36. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  37. import org.openspcoop2.pdd.logger.Tracciamento;
  38. import org.openspcoop2.pdd.services.OpenSPCoop2Startup;
  39. import org.openspcoop2.pdd.timers.TimerMonitoraggioRisorseThread;
  40. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  41. import org.openspcoop2.protocol.engine.constants.Costanti;
  42. import org.openspcoop2.protocol.sdk.Busta;
  43. import org.openspcoop2.protocol.sdk.ProtocolException;
  44. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  45. import org.openspcoop2.utils.LoggerWrapperFactory;



  46. /**
  47.  * Contiene la definizione di un Message Driven Bean 'SbustamentoRisposte', il quale e' un
  48.  * modulo dell'infrastruttura OpenSPCoop.
  49.  * <p>
  50.  * Inoltre la classe contiene due MessageFactory per la costruzione di un oggetto {@link SbustamentoRisposteMessage}
  51.  * utilizzato per la spedizione di messaggi JMS al mdb, tramita una coda JMS utilizzata in ricezione.
  52.  *
  53.  *
  54.  * @author Poli Andrea (apoli@link.it)
  55.  * @author Tronci Fabio (tronci@link.it)
  56.  * @author $Author$
  57.  * @version $Rev$, $Date$
  58.  */

  59. public class SbustamentoRisposteMDB implements MessageDrivenBean, MessageListener {

  60.      /**
  61.      * serialVersionUID
  62.      */
  63.     private static final long serialVersionUID = 1L;

  64.     /* ********  F I E L D S  P R I V A T I   ******** */
  65.    
  66.     /** Message Driven Context */
  67.     private transient MessageDrivenContext ctxMDB;

  68.     /* ********  M E T O D I   ******** */

  69.     /**
  70.      * Imposta il Contesto del Message Driven Bean.
  71.      * Metodo necessario per l'implementazione dell'interfaccia <code>MessageDrivenBean</code>.
  72.      *
  73.      * @param mdc Contesto del Message Driven Bean.
  74.      *
  75.      */
  76.     @Override
  77.     public void setMessageDrivenContext(MessageDrivenContext mdc) throws EJBException {
  78.         this.ctxMDB = mdc;
  79.     }




  80.     /**
  81.      * Metodo necessario per l'implementazione dell'interfaccia <code>MessageDrivenBean</code>.
  82.      *
  83.      *
  84.      */
  85.     public void ejbCreate() {
  86.         // nop
  87.     }



  88.     /**
  89.      * Metodo necessario per l'implementazione dell'interfaccia <code>MessageDrivenBean</code>.
  90.      *
  91.      */
  92.     @Override
  93.     public void ejbRemove() {
  94.         // nop
  95.     }







  96.     /**
  97.      * Attivato,  quando viene ricevuto sulla coda associata al mdb (coda RequestOUT_S)
  98.      * un messaggio JMS. Questo metodo implementa la logica del modulo IdentificazionePortaDelegata
  99.      * dell'infrastruttura OpenSPCoop.
  100.      *
  101.      */
  102.     @Override
  103.     public void onMessage(Message message) {

  104.         if(message instanceof ObjectMessage) {

  105.             /* ------------ Controllo inizializzazione OpenSPCoop -------------------- */
  106.             if( !OpenSPCoop2Startup.initialize){
  107.                 this.ctxMDB.setRollbackOnly();
  108.                 return;
  109.             }
  110.            
  111.             /* logger */
  112.             Logger log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  113.             if (log == null) {
  114.                 LoggerWrapperFactory.getLogger(SbustamentoRisposteMDB.class).error("["+SbustamentoRisposte.ID_MODULO+"]"+" Logger nullo. MDB abortito");
  115.                 return;
  116.             }
  117.            
  118.             /* ----------- Controllo risorse disponibili --------------- */
  119.             if( !TimerMonitoraggioRisorseThread.isRisorseDisponibili()){
  120.                 log.error("["+SbustamentoRisposte.ID_MODULO+"] Risorse di sistema non disponibili: "+TimerMonitoraggioRisorseThread.getRisorsaNonDisponibile().getMessage(),TimerMonitoraggioRisorseThread.getRisorsaNonDisponibile());
  121.                 this.ctxMDB.setRollbackOnly();
  122.                 return;
  123.             }
  124.             if( !Tracciamento.tracciamentoDisponibile){
  125.                 log.error("["+SbustamentoRisposte.ID_MODULO+"] Tracciatura non disponibile: "+Tracciamento.motivoMalfunzionamentoTracciamento.getMessage(),Tracciamento.motivoMalfunzionamentoTracciamento);
  126.                 this.ctxMDB.setRollbackOnly();
  127.                 return;
  128.             }
  129.             if( !MsgDiagnostico.gestoreDiagnosticaDisponibile){
  130.                 log.error("["+SbustamentoRisposte.ID_MODULO+"] Sistema di diagnostica non disponibile: "+MsgDiagnostico.motivoMalfunzionamentoDiagnostici.getMessage(),MsgDiagnostico.motivoMalfunzionamentoDiagnostici);
  131.                 this.ctxMDB.setRollbackOnly();
  132.                 return;
  133.             }
  134.             if( !Dump.isSistemaDumpDisponibile()){
  135.                 log.error("["+SbustamentoRisposte.ID_MODULO+"] Sistema di dump dei contenuti applicativi non disponibile: "+Dump.getMotivoMalfunzionamentoDump().getMessage(),Dump.getMotivoMalfunzionamentoDump());
  136.                 this.ctxMDB.setRollbackOnly();
  137.                 return;
  138.             }

  139.             SbustamentoRisposte lib = null;
  140.             try{
  141.                 lib = new SbustamentoRisposte(log);
  142.             }catch(Exception e){
  143.                 log.error("SbustamentoRisposte.instanziazione: "+e.getMessage(),e);
  144.                 this.ctxMDB.setRollbackOnly();
  145.                 return;
  146.             }

  147.                        
  148.             /* ----------- Controllo inizializzazione lib ----------- */
  149.             if( !lib.inizializzazioneUltimata ){
  150.                 log = LoggerWrapperFactory.getLogger(SbustamentoRisposteMDB.class);
  151.                 log.error("["+SbustamentoRisposte.ID_MODULO+"] Inizializzazione MDB non riuscita");
  152.                 this.ctxMDB.setRollbackOnly();
  153.                 return;
  154.             }


  155.             /* ------------  Lettura parametri della richiesta  ------------- */

  156.             // Logger dei messaggi diagnostici
  157.             MsgDiagnostico msgDiag = MsgDiagnostico.newInstance(TipoPdD.DELEGATA, SbustamentoRisposte.ID_MODULO);

  158.             //  Ricezione Messaggio
  159.             msgDiag.mediumDebug("Ricezione richiesta (SbustamentoRisposteMessage)...");
  160.             ObjectMessage received = (ObjectMessage)message;
  161.             SbustamentoRisposteMessage sbustamentoRisposteMsg = null;
  162.             try{
  163.                 sbustamentoRisposteMsg = (SbustamentoRisposteMessage)received.getObject();
  164.             }   catch(javax.jms.JMSException e){
  165.                 msgDiag.logErroreGenerico(e,"received.getObject(SbustamentoRisposteMessage)");
  166.                 return;
  167.             }  
  168.             if(sbustamentoRisposteMsg.getRichiestaDelegata()!=null && sbustamentoRisposteMsg.getRichiestaDelegata().getIdPortaDelegata()!=null) {
  169.                
  170.                 RequestInfo requestInfo = null;
  171.                 if(sbustamentoRisposteMsg.getPddContext()!=null && sbustamentoRisposteMsg.getPddContext().containsKey(org.openspcoop2.core.constants.Costanti.REQUEST_INFO)) {
  172.                     requestInfo = (RequestInfo) sbustamentoRisposteMsg.getPddContext().getObject(org.openspcoop2.core.constants.Costanti.REQUEST_INFO);
  173.                 }
  174.                
  175.                 msgDiag.updatePorta(sbustamentoRisposteMsg.getRichiestaDelegata().getIdPortaDelegata().getNome(),requestInfo);  
  176.             }
  177.            
  178.             // ID associato alla richiesta
  179.             String idMessageRequest = null; //(serve anche per una validazione sincrona)
  180.             try{
  181.                 idMessageRequest = received.getStringProperty("ID");
  182.             }   catch(javax.jms.JMSException e){
  183.                 msgDiag.logErroreGenerico(e,"received.getStringProperty(ID)");
  184.                 return;
  185.             }
  186.            
  187.             // Stato
  188.             OpenSPCoopStateful stato = null;
  189.             try {
  190.                 stato = new OpenSPCoopStateful();
  191.                 stato.setIDMessaggioSessione(idMessageRequest);
  192.                 stato.setMessageLib(sbustamentoRisposteMsg);
  193.             }catch (Exception e) {
  194.                 log.error("["+SbustamentoRisposte.ID_MODULO+"] Invocazione della libreria non riuscita (verrà effettuato un rollback sulla coda JMS)",e);
  195.                 this.ctxMDB.setRollbackOnly();
  196.                 return;
  197.             }
  198.            
  199.             // update diagnostico
  200.             msgDiag.updateState(stato.getStatoRichiesta(),stato.getStatoRisposta());
  201.            
  202.            
  203.             /* PddContext */
  204.             PdDContext pddContext = sbustamentoRisposteMsg.getPddContext();
  205.             try{
  206.                 msgDiag.setPddContext(pddContext,
  207.                         ProtocolFactoryManager.getInstance().getProtocolFactoryByName((String) pddContext.getObject(org.openspcoop2.core.constants.Costanti.PROTOCOL_NAME)));
  208.             }catch(ProtocolException e){
  209.                 msgDiag.logErroreGenerico(e,"ProtocolFactory.instanziazione");
  210.                 this.ctxMDB.setRollbackOnly();
  211.                 return;
  212.             }
  213.            

  214.            
  215.             Busta bustaRisposta = sbustamentoRisposteMsg.getBusta();
  216.             String idMessageResponse = bustaRisposta.getID();
  217.            
  218.             /* ------------------ Validity Check  --------------- */
  219.             msgDiag.mediumDebug("Transaction Manager...");
  220.             try{
  221.                 if(!TransactionManager.validityCheck(msgDiag,SbustamentoRisposte.ID_MODULO,idMessageResponse,
  222.                         Costanti.INBOX,received.getJMSMessageID(),pddContext)){
  223.                     msgDiag.addKeyword(CostantiPdD.KEY_ID_MESSAGGIO_TRANSACTION_MANAGER, idMessageRequest);
  224.                     msgDiag.addKeyword(CostantiPdD.KEY_PROPRIETARIO_MESSAGGIO, SbustamentoRisposte.ID_MODULO);
  225.                     msgDiag.logPersonalizzato(MsgDiagnosticiProperties.MSG_DIAG_ALL,"transactionManager.validityCheckError");
  226.                     return;
  227.                 }
  228.             }catch(Exception e){
  229.                 msgDiag.logErroreGenerico(e,"TransactionManager.validityCheck");
  230.                 this.ctxMDB.setRollbackOnly();
  231.                 return;
  232.             }
  233.            
  234.             EsitoLib esito = null;
  235.             try {
  236.                 esito = lib.onMessage(stato);
  237.             }catch (Exception e) {
  238.                 log.error("["+SbustamentoRisposte.ID_MODULO+"] Invocazione della libreria non riuscita (verrà effettuato un rollback sulla coda JMS)",e);
  239.                 this.ctxMDB.setRollbackOnly();
  240.                 return;
  241.             }
  242.            
  243.             if (!esito.isEsitoInvocazione()){
  244.                 log.info("["+SbustamentoRisposte.ID_MODULO+"] Invocazione della libreria terminata con esito negativo, verrà effettuato un rollback sulla coda JMS");
  245.                 this.ctxMDB.setRollbackOnly();
  246.             }  
  247.            
  248.             else {
  249.                 log.debug("["+SbustamentoRisposte.ID_MODULO+"] Invocazione della libreria terminata correttamente");
  250.             }
  251.            
  252.         }
  253.     }
  254. }