JMSSender.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;

  21. import javax.jms.JMSException;
  22. import javax.jms.ObjectMessage;
  23. import javax.jms.MessageProducer;
  24. import javax.jms.Queue;

  25. import org.slf4j.Logger;
  26. import org.openspcoop2.core.id.IDSoggetto;
  27. import org.openspcoop2.pdd.config.JMSObject;
  28. import org.openspcoop2.pdd.config.QueueManager;
  29. import org.openspcoop2.pdd.config.Resource;

  30. /**
  31.  * Classe utilizzata per spedire messaggi JMS ai componenti dell'architettura di OpenSPCoop.
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */

  37. public class JMSSender {

  38.     /** QueueManager */
  39.     private QueueManager qmanager;
  40.     /** Indicazione sul modulo che utilizza il Sender  */
  41.     private String idModulo = null;
  42.     /** Indicazione sul codice porta del Sender */
  43.     private IDSoggetto codicePorta = null;


  44.     /** motivo di un eventuale errore */
  45.     private String errore;
  46.     /** eventuale eccezione */
  47.     private Exception exception;


  48.     /** Logger */
  49.     private Logger log;
  50.    
  51.     /** IDTransazione */
  52.     private String idTransazione;
  53.    

  54.     /**
  55.      * Costruttore
  56.      *
  57.      * @param aCodicePorta Codice del dominio che sta gestendo la richiesta.
  58.      * @param aIDModulo Identificativo del Sender.
  59.      *
  60.      */
  61.     public JMSSender(IDSoggetto aCodicePorta,String aIDModulo,Logger log,String idTransazione) throws Exception {
  62.         this.codicePorta = aCodicePorta;
  63.         this.idModulo = "JMSSender."+aIDModulo;
  64.         this.qmanager = QueueManager.getInstance();
  65.         this.log = log;
  66.         this.idTransazione = idTransazione;
  67.     }


  68.     /**
  69.      * Spedizione di un messaggio  
  70.      *
  71.      * @param destinatario Nodo destinatario a cui spedire il messaggio.
  72.      * @param objectToSend Oggetto da spedire.
  73.      * @param idBusta Identificativo da impostare come proprieta;
  74.      * @return true se la spedizione JMS e' andata a buon fine, false altrimenti.
  75.      *
  76.      */
  77.     public boolean send(String destinatario,java.io.Serializable objectToSend,String idBusta){
  78.         java.util.Properties prop = new java.util.Properties();
  79.         prop.put("ID",idBusta);
  80.         return send(destinatario,objectToSend,prop);
  81.     }
  82.     /**
  83.      * Spedizione di un messaggio  
  84.      *
  85.      * @param destinatario Nodo destinatario a cui spedire il messaggio.
  86.      * @param objectToSend Oggetto da spedire.
  87.      * @param properties Proprieta' da impostare.
  88.      * @return true se la spedizione JMS e' andata a buon fine, false altrimenti.
  89.      *
  90.      */
  91.     public boolean send(String destinatario,java.io.Serializable objectToSend,java.util.Properties properties){

  92.         Resource resource = null;
  93.         MessageProducer sender = null;
  94.         try{

  95.             if(destinatario == null)
  96.                 return true; // non deve essere spedito nulla.
  97.            
  98.             JMSObject jmsObject = null;
  99.             try{
  100.                 resource = this.qmanager.getResource(this.codicePorta,this.idModulo,this.idTransazione);
  101.                 if(resource == null)
  102.                     throw new JMSException("Resource is null");
  103.                 if(resource.getResource() == null)
  104.                     throw new JMSException("JMSObject is null");
  105.                 jmsObject = (JMSObject) resource.getResource();
  106.                 if(jmsObject.getConnection()==null)
  107.                     throw new Exception("Connessione is null");
  108.                 if(jmsObject.getSession()==null)
  109.                     throw new Exception("Sessione is null");
  110.             }catch(Exception e){
  111.                 this.log.error("JMSObject non ottenibile dal Pool: "+e.getMessage(),e);
  112.                 this.errore ="JMSObject non ottenibile dal Pool: "+e.getMessage();
  113.                 this.exception = e;
  114.                 return false;
  115.             }


  116.             // Get Coda
  117.             Queue queue = this.qmanager.getQueue(destinatario);
  118.             if(queue == null){
  119.                 this.qmanager.releaseResource(this.codicePorta,this.idModulo,resource);
  120.                 this.errore="La coda ["+destinatario+"] non e' tra quelle registrate per OpenSPCoop";
  121.                 return false;
  122.             }

  123.             // Sender
  124.             try{
  125.                 sender = jmsObject.getSession().createProducer(queue);
  126.             }catch(javax.jms.JMSException e){
  127.                 this.qmanager.releaseResource(this.codicePorta,this.idModulo,resource);
  128.                 this.log.error("Riscontrato errore durante la creazione del sender ["+destinatario+"] :"+e.getMessage(),e);
  129.                 this.errore = "Riscontrato errore durante la creazione del sender ["+destinatario+"] :"+e.getMessage();
  130.                 this.exception = e;
  131.                 return false;
  132.             }

  133.             // Oggetto da Spedire
  134.             ObjectMessage message = jmsObject.getSession().createObjectMessage(objectToSend);
  135.             java.util.Enumeration<?> en = properties.propertyNames();
  136.             for (; en.hasMoreElements() ;) {
  137.                 String key = (String) en.nextElement();
  138.                 String value = properties.getProperty(key);
  139.                 // Controllo boolean
  140.                 if( "true".equalsIgnoreCase(value) ){
  141.                     message.setBooleanProperty(key,true);
  142.                 }else if("false".equalsIgnoreCase(value)){
  143.                     message.setBooleanProperty(key,false);
  144.                 }else{
  145.                     Long testLong = null;
  146.                     try{
  147.                         testLong = Long.valueOf(value);
  148.                     }catch(Exception e){
  149.                         testLong = null;
  150.                     }
  151.                     // controllo long
  152.                     if(testLong != null)
  153.                         message.setLongProperty(key,testLong.longValue());
  154.                     // string
  155.                     else{
  156.                         message.setStringProperty(key,value);
  157.                     }
  158.                 }
  159.             }

  160.             // Send Message
  161.             sender.send(message);
  162.            
  163.             //  Rilascio producer
  164.             sender.close();

  165.             this.qmanager.releaseResource(this.codicePorta,this.idModulo,resource);
  166.             return true;

  167.         }catch(Exception e){
  168.             //  Rilascio producer
  169.             try{
  170.                 if(sender!=null)
  171.                     sender.close();
  172.             }catch(Exception eClose){
  173.                 // close
  174.             }
  175.             if(resource!=null){
  176.                 try{
  177.                     this.qmanager.releaseResource(this.codicePorta,this.idModulo,resource);
  178.                 }catch(Exception eClose){
  179.                     // close
  180.                 }  
  181.             }
  182.             this.log.error("Riscontrato errore durante la creazione/spedizione dell'oggetto :"+e.getMessage(),e);
  183.             this.errore = "Riscontrato errore durante la creazione/spedizione dell'oggetto :"+e.getMessage();
  184.             this.exception = e;
  185.             return false;
  186.         }

  187.     }

  188.     /**
  189.      * In caso di avvenuto errore in fase di consegna, questo metodo ritorna il motivo dell'errore.
  190.      *
  191.      * @return motivo dell'errore (se avvenuto in fase di consegna).
  192.      *
  193.      */
  194.     public String getErrore(){
  195.         return this.errore;
  196.     }

  197.    
  198.     public Exception getException() {
  199.         return this.exception;
  200.     }
  201. }