ConnettoreJMSProperties.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.connettori;



  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.Properties;

  24. import org.openspcoop2.core.id.IDServizio;
  25. import org.openspcoop2.core.id.IDSoggetto;
  26. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  27. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  28. import org.openspcoop2.utils.UtilsException;
  29. import org.slf4j.Logger;

  30. /**
  31.  * Reader delle proprieta' di personalizzazione della spedizione attraverso il connettore JMS
  32.  *  
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */

  37. public class ConnettoreJMSProperties {

  38.     public static final String JMS_PREFIX_PROPERTY = "org.openspcoop.pubblicazione.";
  39.    
  40.     /** Logger utilizzato per errori eventuali. */
  41.     private static Logger log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();


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

  43.     /** Reader delle proprieta' impostate nel file 'govway.jmsPublisher.properties' */
  44.     private Properties reader;

  45.     /** Copia Statica */
  46.     private static ConnettoreJMSProperties connettoreJMSProperties = null;


  47.     /* ********  C O S T R U T T O R E  ******** */

  48.     /**
  49.      * Viene chiamato in causa per istanziare il properties reader
  50.      *
  51.      *
  52.      */
  53.     private ConnettoreJMSProperties() throws UtilsException {

  54.         /* ---- Lettura del cammino del file di configurazione ---- */
  55.         this.reader = new Properties();
  56.         try(java.io.InputStream properties = ConnettoreJMSProperties.class.getResourceAsStream("/govway.jmsPublisher.properties");){  
  57.             this.reader.load(properties);
  58.         }catch(Exception e) {
  59.             doError(e);
  60.         }

  61.     }
  62.     private void doError(Exception e) throws UtilsException {
  63.         ConnettoreJMSProperties.log.error("Riscontrato errore durante la lettura del file 'govway.jmsPublisher.properties': "+e.getMessage(),e);
  64.         throw new UtilsException("ConnettoreJMSProperties initialize error: "+e.getMessage(),e);
  65.     }


  66.     /**
  67.      * Il Metodo si occupa di inizializzare il propertiesReader
  68.      *
  69.      *
  70.      */
  71.     public static boolean initialize(){

  72.         try {
  73.             ConnettoreJMSProperties.connettoreJMSProperties = new ConnettoreJMSProperties();    
  74.             return true;
  75.         }
  76.         catch(Exception e) {
  77.             return false;
  78.         }
  79.     }
  80.    
  81.     /**
  82.      * Ritorna l'istanza di questa classe
  83.      *
  84.      * @return Istanza di Properties
  85.      *
  86.      */
  87.     public static ConnettoreJMSProperties getInstance(){
  88.         if(ConnettoreJMSProperties.connettoreJMSProperties==null) {
  89.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  90.             synchronized (ConnettoreJMSProperties.class) {
  91.                 if(ConnettoreJMSProperties.connettoreJMSProperties==null) {
  92.                     ConnettoreJMSProperties.initialize();
  93.                 }
  94.             }
  95.         }
  96.         return ConnettoreJMSProperties.connettoreJMSProperties;
  97.     }
  98.    









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

  100.     /**
  101.      * Restituisce una lista di identificatori dei servizi di pubblicazione definiti
  102.      *
  103.      * @return lista di identificatori dei servizi di pubblicazione definiti
  104.      *
  105.      */
  106.     public Map<String,IDServizio> getIDServiziPubblicazione() {
  107.         Map<String,IDServizio> servizi= new HashMap<>();
  108.         try{
  109.             // Raccolta servizi
  110.             java.util.List<String> idServizi = new java.util.ArrayList<>();
  111.             java.util.Enumeration<?> en = this.reader.propertyNames();
  112.             for (; en.hasMoreElements() ;) {
  113.                 String property = (String) en.nextElement();
  114.                 if(property.startsWith(JMS_PREFIX_PROPERTY)){
  115.                 String key = (property.substring(JMS_PREFIX_PROPERTY.length()));
  116.                 int indexOf = key.indexOf(".");
  117.                 key = key.substring(0,indexOf);
  118.                 if(key != null)
  119.                     key = key.trim();
  120.                 if(!idServizi.contains(key))
  121.                     idServizi.add(key);
  122.                 }
  123.             }
  124.            
  125.             for(int i=0; i<idServizi.size(); i++){
  126.                 /**log.info("Raccolta variabili per servizio ["+idServizi.get(i)+"]");*/
  127.                
  128.                 IDSoggetto idSoggetto = new IDSoggetto();
  129.                 idSoggetto.setTipo((String)this.reader.get(JMS_PREFIX_PROPERTY+idServizi.get(i)+".tipoSoggettoErogatore"));
  130.                 idSoggetto.setNome((String)this.reader.get(JMS_PREFIX_PROPERTY+idServizi.get(i)+".soggettoErogatore"));
  131.                
  132.                 String tipoServizio = (String)this.reader.get(JMS_PREFIX_PROPERTY+idServizi.get(i)+".tipoServizio");
  133.                 String nomeServizio = (String)this.reader.get(JMS_PREFIX_PROPERTY+idServizi.get(i)+".servizio");
  134.                 Integer versioneServizio = Integer.parseInt(((String)this.reader.get(JMS_PREFIX_PROPERTY+idServizi.get(i)+".versioneServizio")));
  135.                 IDServizio idServizio = IDServizioFactory.getInstance().getIDServizioFromValues(tipoServizio, nomeServizio, idSoggetto, versioneServizio);
  136.                
  137.                 /**log.info("Servizio ["+IDServizioFactory.getInstance().getUriFromIDServizio(idServizio)+"]");*/
  138.                 servizi.put(idServizi.get(i),idServizio);
  139.             }
  140.            
  141.             return servizi;
  142.        
  143.         }catch(Throwable e) {
  144.             ConnettoreJMSProperties.log.error("Riscontrato errore durante la lettura dei servizi pubblicatori : 'org.openspcoop.pubblicazione.*'",e);
  145.             servizi = null;
  146.             return servizi;
  147.         }  
  148.     }
  149.    
  150.     /**
  151.      * Restituisce le proprieta' da utilizzare con il contesto JNDI di lookup, se impostate.
  152.      *
  153.      * @return proprieta' da utilizzare con il contesto JNDI di lookup.
  154.      *
  155.      */
  156.     public java.util.Properties getJNDIContextConfigurazione(String id) {  
  157.         java.util.Properties prop = new java.util.Properties();
  158.         try{
  159.             String ricerca = JMS_PREFIX_PROPERTY + id +".jndi.contextProperty.";
  160.             java.util.Enumeration<?> en = this.reader.propertyNames();
  161.             for (; en.hasMoreElements() ;) {
  162.                 String property = (String) en.nextElement();
  163.                 if(property.startsWith(ricerca)){
  164.                     String key = (property.substring(ricerca.length()));
  165.                     if(key != null)
  166.                         key = key.trim();
  167.                     String value = this.reader.getProperty(property);
  168.                     if(value!=null)
  169.                         value = value.trim();
  170.                     if(key!=null && value!=null){
  171.                         prop.setProperty(key,value);
  172.                         /**System.out.println("context ["+key+"] ["+value+"]");*/
  173.                     }
  174.                 }
  175.             }
  176.             return prop;

  177.         }catch(java.lang.Exception e) {
  178.             String msg = "Riscontrato errore durante la lettura delle propriete' JNDI per la configurazione: "+e.getMessage();
  179.             ConnettoreJMSProperties.log.error(msg,e);
  180.             prop = null;
  181.             return prop;
  182.         }    
  183.     }
  184.    
  185.     /**
  186.      * Restituisce le proprieta' da utilizzare con il contesto JNDI di lookup, se impostate.
  187.      *
  188.      * @return proprieta' da utilizzare con il contesto JNDI di lookup.
  189.      *
  190.      */
  191.     public java.util.Properties getJNDIPoolConfigurazione(String id) {  
  192.         java.util.Properties prop = new java.util.Properties();
  193.         try{
  194.             String ricerca = JMS_PREFIX_PROPERTY + id +".jndi.poolProperty.";
  195.             java.util.Enumeration<?> en = this.reader.propertyNames();
  196.             for (; en.hasMoreElements() ;) {
  197.                 String property = (String) en.nextElement();
  198.                 if(property.startsWith(ricerca)){
  199.                     String key = (property.substring(ricerca.length()));
  200.                     if(key != null)
  201.                         key = key.trim();
  202.                     String value = this.reader.getProperty(property);
  203.                     if(value!=null)
  204.                         value = value.trim();
  205.                     if(key!=null && value!=null){
  206.                         prop.setProperty(key,value);
  207.                         /**System.out.println("pool ["+key+"] ["+value+"]");*/
  208.                     }
  209.                 }
  210.             }
  211.             return prop;

  212.         }catch(java.lang.Exception e) {
  213.             String msg = "Riscontrato errore durante la lettura delle propriete' JNDI per la configurazione: "+e.getMessage();
  214.             ConnettoreJMSProperties.log.error(msg,e);
  215.             prop = null;
  216.             return prop;
  217.         }    
  218.     }
  219.    
  220.    
  221.     public String[] getClassNameSetPropertiesJMS(){
  222.         String [] sNull = null;
  223.         try{
  224.             String value = this.reader.getProperty("org.openspcoop.pubblicazione.setProprietaJMS.class");
  225.             if(value!=null){
  226.                 value = value.trim();
  227.                 String [] classi = value.split(",");
  228.                 if(classi!=null){
  229.                     for(int i=0;i<classi.length;i++){
  230.                         classi[i] = classi[i].trim();
  231.                     }
  232.                 }
  233.                 return classi;
  234.             }else
  235.                 return sNull;
  236.         }catch(java.lang.Exception e) {
  237.             String msg = "Riscontrato errore durante la lettura delle classi da utilizzare per il setting delle proprieta' JMS: "+e.getMessage();
  238.             ConnettoreJMSProperties.log.error(msg,e);
  239.             return sNull;
  240.         }  
  241.     }
  242.    

  243. }
  244.    
  245.