SbustamentoMDB.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.ProtocolException;
  43. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  44. import org.openspcoop2.utils.LoggerWrapperFactory;




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

  58. public class SbustamentoMDB implements MessageDrivenBean, MessageListener {

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

  63.    
  64.     /* ********  F I E L D S  P R I V A T I   ******** */

  65.     /** Message Driven Context */
  66.     private transient MessageDrivenContext ctxMDB;

  67.    



  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.      * Remove EJBean
  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(SbustamentoMDB.class).error("["+Sbustamento.ID_MODULO+"]"+" Logger nullo. MDB abortito");
  115.                 return;
  116.             }
  117.            
  118.             /* ----------- Controllo risorse disponibili --------------- */
  119.             if( !TimerMonitoraggioRisorseThread.isRisorseDisponibili()){
  120.                 log.error("["+Sbustamento.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("["+Sbustamento.ID_MODULO+"] Tracciatura non disponibile: "+Tracciamento.motivoMalfunzionamentoTracciamento.getMessage(),Tracciamento.motivoMalfunzionamentoTracciamento);
  126.                 this.ctxMDB.setRollbackOnly();
  127.                 return;
  128.             }
  129.             if( !MsgDiagnostico.gestoreDiagnosticaDisponibile){
  130.                 log.error("["+Sbustamento.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("["+Sbustamento.ID_MODULO+"] Sistema di dump dei contenuti applicativi non disponibile: "+Dump.getMotivoMalfunzionamentoDump().getMessage(),Dump.getMotivoMalfunzionamentoDump());
  136.                 this.ctxMDB.setRollbackOnly();
  137.                 return;
  138.             }
  139.            
  140.             Sbustamento lib = null;
  141.             try{
  142.                 lib = new Sbustamento(log);
  143.             }catch(Exception e){
  144.                 log.error("Sbustamento.instanziazione: "+e.getMessage(),e);
  145.                 this.ctxMDB.setRollbackOnly();
  146.                 return;
  147.             }
  148.            
  149.            
  150.             /* ----------- Controllo inizializzazione libreria ----------- */
  151.             if( !lib.inizializzazioneUltimata ){
  152.                 log = LoggerWrapperFactory.getLogger(SbustamentoMDB.class);
  153.                 log.error("["+Sbustamento.ID_MODULO+"] Inizializzazione MDB non riuscita");
  154.                 this.ctxMDB.setRollbackOnly();
  155.                 return;
  156.             }
  157.            
  158.            
  159.             /* ------------  Lettura parametri dalla coda associato al MDB 'Sbustamento'  ------------- */

  160.             //Logger dei messaggi diagnostici
  161.             MsgDiagnostico msgDiag = MsgDiagnostico.newInstance(TipoPdD.APPLICATIVA,Sbustamento.ID_MODULO);

  162.             msgDiag.mediumDebug("Ricezione richiesta (SbustamentoMessage)...");
  163.             ObjectMessage received = (ObjectMessage)message;
  164.             SbustamentoMessage sbustamentoMsg = null;
  165.             try{
  166.                 sbustamentoMsg = (SbustamentoMessage)received.getObject();
  167.             }   catch(javax.jms.JMSException e){
  168.                 msgDiag.logErroreGenerico(e,"received.getObject(SbustamentoMessage)");
  169.                 return;
  170.             }
  171.             if(sbustamentoMsg.getRichiestaApplicativa()!=null && sbustamentoMsg.getRichiestaApplicativa().getIdPortaApplicativa()!=null) {
  172.                
  173.                 RequestInfo requestInfo = null;
  174.                 if(sbustamentoMsg.getPddContext()!=null && sbustamentoMsg.getPddContext().containsKey(org.openspcoop2.core.constants.Costanti.REQUEST_INFO)) {
  175.                     requestInfo = (RequestInfo) sbustamentoMsg.getPddContext().getObject(org.openspcoop2.core.constants.Costanti.REQUEST_INFO);
  176.                 }
  177.                
  178.                 msgDiag.updatePorta(sbustamentoMsg.getRichiestaApplicativa().getIdPortaApplicativa().getNome(), requestInfo);
  179.             }
  180.            
  181.             // ID associato alla richiesta
  182.             String idRequest = null; //(serve anche per una validazione sincrona)
  183.             try{
  184.                 idRequest = received.getStringProperty("ID");
  185.             }   catch(javax.jms.JMSException e){
  186.                 msgDiag.logErroreGenerico(e,"received.getStringProperty(ID)");
  187.                 return;
  188.             }
  189.            
  190.             // Stato
  191.             OpenSPCoopStateful stato = null;
  192.             try {
  193.                 stato = new OpenSPCoopStateful();
  194.                 stato.setIDMessaggioSessione(idRequest);
  195.                 stato.setMessageLib(sbustamentoMsg);
  196.             }catch (Exception e) {
  197.                 log.error("["+Sbustamento.ID_MODULO+"] Invocazione della libreria non riuscita (verrà effettuato un rollback sulla coda JMS)",e);
  198.                 this.ctxMDB.setRollbackOnly();
  199.                 return;
  200.             }
  201.            
  202.             // update diagnostico
  203.             msgDiag.updateState(stato.getStatoRichiesta(),stato.getStatoRisposta());
  204.                        
  205.        
  206.             /* PddContext */
  207.             PdDContext pddContext = sbustamentoMsg.getPddContext();
  208.             try{
  209.                 msgDiag.setPddContext(pddContext, ProtocolFactoryManager.getInstance().getProtocolFactoryByName((String) pddContext.getObject(org.openspcoop2.core.constants.Costanti.PROTOCOL_NAME)));
  210.             }catch(ProtocolException e){
  211.                 msgDiag.logErroreGenerico(e,"ProtocolFactory.instanziazione");
  212.                 this.ctxMDB.setRollbackOnly();
  213.                 return;
  214.             }
  215.            
  216.             /* ------------------ Validity Check  --------------- */
  217.             msgDiag.mediumDebug("Transaction Manager...");
  218.             try{
  219.                 if(!TransactionManager.validityCheck(msgDiag,Sbustamento.ID_MODULO,idRequest,
  220.                         Costanti.INBOX,received.getJMSMessageID(),pddContext)){
  221.                     msgDiag.addKeyword(CostantiPdD.KEY_ID_MESSAGGIO_TRANSACTION_MANAGER, idRequest);
  222.                     msgDiag.addKeyword(CostantiPdD.KEY_PROPRIETARIO_MESSAGGIO, Sbustamento.ID_MODULO);
  223.                     msgDiag.logPersonalizzato(MsgDiagnosticiProperties.MSG_DIAG_ALL,"transactionManager.validityCheckError");
  224.                     return;
  225.                 }
  226.             }catch(Exception e){
  227.                 msgDiag.logErroreGenerico(e,"TransactionManager.validityCheck");
  228.                 this.ctxMDB.setRollbackOnly();
  229.                 return;
  230.             }
  231.            
  232.             EsitoLib esito = null;
  233.             try {
  234.                 esito = lib.onMessage(stato);
  235.             }catch (Exception e) {
  236.                 log.error("["+Sbustamento.ID_MODULO+"] Invocazione della libreria non riuscita (verrà effettuato un rollback sulla coda JMS)",e);
  237.                 this.ctxMDB.setRollbackOnly();
  238.                 return;
  239.             }
  240.            
  241.             if (!esito.isEsitoInvocazione()){
  242.                 log.info("["+Sbustamento.ID_MODULO+"] Invocazione della libreria terminata con esito negativo, verrà effettuato un rollback sulla coda JMS");
  243.                 this.ctxMDB.setRollbackOnly();
  244.             }  
  245.            
  246.             else {
  247.                 log.debug("["+Sbustamento.ID_MODULO+"] Invocazione della libreria terminata correttamente");
  248.             }
  249.            
  250.            
  251.         }
  252.     }
  253. }
  254.            
  255.            
  256.