TracciamentoOpenSPCoopProtocolAppender.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.logger;

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

  23. import org.openspcoop2.core.commons.CoreException;
  24. import org.openspcoop2.core.config.OpenspcoopAppender;
  25. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  26. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  27. import org.openspcoop2.protocol.sdk.ProtocolException;
  28. import org.openspcoop2.protocol.sdk.tracciamento.ITracciaProducer;
  29. import org.openspcoop2.protocol.sdk.tracciamento.Traccia;
  30. import org.openspcoop2.protocol.sdk.tracciamento.TracciamentoException;
  31. import org.openspcoop2.utils.resources.MapReader;

  32. /**
  33.  * Contiene l'implementazione di un appender personalizzato,
  34.  * per la registrazione dei tracciamenti su database.
  35.  *
  36.  * @author Poli Andrea (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */

  40. public class TracciamentoOpenSPCoopProtocolAppender implements ITracciaProducer{

  41.     private static java.util.HashMap<String, ITracciaProducer> mappingProtocolToAppenders = new java.util.HashMap<String, ITracciaProducer>();
  42.    
  43.     private static synchronized void initProtocolAppender(String protocol,OpenspcoopAppender appenderProperties) throws ProtocolException{
  44.         if(TracciamentoOpenSPCoopProtocolAppender.mappingProtocolToAppenders.containsKey(protocol)==false){
  45.             IProtocolFactory<?> p = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(protocol);
  46.             ITracciaProducer tracciamento = p.createTracciaProducer();
  47.             if(tracciamento==null){
  48.                 throw new ProtocolException("ITracciaProducer not defined for protocol ["+protocol+"]");
  49.             }
  50.             try{
  51.                 tracciamento.initializeAppender(appenderProperties);
  52.             }catch(Exception e){
  53.                 throw new ProtocolException(e.getMessage(), e);
  54.             }
  55.             TracciamentoOpenSPCoopProtocolAppender.mappingProtocolToAppenders.put(protocol, tracciamento);
  56.         }
  57.     }
  58.    
  59.     private static ITracciaProducer getProtocolAppender(String protocol) throws ProtocolException{
  60.         if(TracciamentoOpenSPCoopProtocolAppender.mappingProtocolToAppenders.containsKey(protocol)==false){
  61.             throw new ProtocolException("ProtocolAppender per protocollo["+protocol+"] non inizializzato");
  62.         }
  63.         return  TracciamentoOpenSPCoopProtocolAppender.mappingProtocolToAppenders.get(protocol);
  64.     }
  65.    
  66.        
  67.     /**
  68.      * Inizializza l'engine di un appender per la registrazione
  69.      * di un tracciamento emesso da una porta di dominio.
  70.      *
  71.      * @param appenderProperties Proprieta' dell'appender
  72.      * @throws TracciamentoException
  73.      */
  74.     @Override
  75.     public void initializeAppender(OpenspcoopAppender appenderProperties) throws TracciamentoException{
  76.         try{
  77.             MapReader<String, IProtocolFactory<?>> table = ProtocolFactoryManager.getInstance().getProtocolFactories();
  78.             Enumeration<String> keys = table.keys();
  79.             while (keys.hasMoreElements()) {
  80.                 String protocol = keys.nextElement();
  81.                 TracciamentoOpenSPCoopProtocolAppender.initProtocolAppender(protocol,appenderProperties);
  82.             }
  83.         }catch(Exception e){
  84.             throw new TracciamentoException(e.getMessage(),e);
  85.         }
  86.     }

  87.    
  88.    
  89.     /**
  90.      * Registra una traccia prodotta da una porta di dominio, utilizzando le informazioni definite dalla specifica SPC.
  91.      *
  92.      * @param conOpenSPCoopPdD Connessione verso il database
  93.      * @param traccia Traccia
  94.      * @throws TracciamentoException
  95.      */
  96.     @Override
  97.     public void log(Connection conOpenSPCoopPdD, Traccia traccia) throws TracciamentoException{
  98.         try{
  99.             if(traccia.getProtocollo()!=null){
  100.                 TracciamentoOpenSPCoopProtocolAppender.getProtocolAppender(traccia.getProtocollo()).log(conOpenSPCoopPdD,traccia);
  101.             }
  102.         }catch(Exception e){
  103.             throw new TracciamentoException(e.getMessage(),e);
  104.         }
  105.     }



  106.     @Override
  107.     public void isAlive() throws CoreException {
  108.         try{
  109.             Enumeration<String> protocols = ProtocolFactoryManager.getInstance().getProtocolNames();
  110.             while(protocols.hasMoreElements()){
  111.                 String protocol = protocols.nextElement();
  112.                 TracciamentoOpenSPCoopProtocolAppender.getProtocolAppender(protocol).isAlive();
  113.             }
  114.         }catch(Exception e){
  115.             throw new CoreException(e.getMessage(),e);
  116.         }
  117.     }



  118.     @Override
  119.     public IProtocolFactory<?> getProtocolFactory() {
  120.         return null; // non e' possibile localizzarla
  121.     }

  122. }