NotifierCallback.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.handlers.notifier.engine;

  21. import org.slf4j.Logger;
  22. import org.openspcoop2.core.constants.TipoPdD;
  23. import org.openspcoop2.message.OpenSPCoop2Message;
  24. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  25. import org.openspcoop2.pdd.core.handlers.notifier.INotifierCallback;
  26. import org.openspcoop2.pdd.core.handlers.notifier.NotifierException;
  27. import org.openspcoop2.pdd.core.handlers.notifier.NotifierResult;
  28. import org.openspcoop2.pdd.core.handlers.notifier.NotifierType;
  29. import org.openspcoop2.pdd.logger.LoggerUtility;
  30. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;

  31. /**    
  32.  * NotifierCallback
  33.  *
  34.  * @author Poli Andrea (poli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class NotifierCallback implements INotifierCallback {
  39.    
  40.     private LoggerUtility logUtility = null;
  41.     private synchronized void initializeLogUtility() throws Exception{
  42.         if(this.logUtility==null){
  43.             boolean forceGetLogTransazioni = true;
  44.             this.logUtility = new LoggerUtility(OpenSPCoop2Logger.getLoggerOpenSPCoopTransazioni(forceGetLogTransazioni), // use forceGetLogTransazioni poiche' il debug e' gestito da LoggerUtility
  45.                     OpenSPCoop2Properties.getInstance().isTransazioniDebug());
  46.         }
  47.     }
  48.     private LoggerUtility getLoggerUtility() {
  49.         try{
  50.             if(this.logUtility==null){
  51.                 this.initializeLogUtility();
  52.             }
  53.             return this.logUtility;
  54.         }catch(Exception e){
  55.             System.err.println("[NotifierCallback] Errore durante l'inizializzazione del logger: "+e.getMessage());
  56.             throw new RuntimeException(e.getMessage(),e);
  57.         }
  58.     }
  59.    
  60.     public NotifierCallback(){
  61. //      try{
  62. //          this.logUtility = new LoggerUtility(LoggerManager.getInstance().getLogTransazioni(),PddInterceptorConfig.isDebugEnabled_generazioneTransazioni());          
  63. //      }catch(Exception e){
  64. //          System.err.println("[NotifierCallback] Errore durante l'inizializzazione del logger: "+e.getMessage());
  65. //      }
  66.     }
  67.    
  68.     public void debug(String msg){
  69.         this.getLoggerUtility().debug("[NotifierCallback] "+msg);
  70.     }
  71.    
  72.     public void error(NotifierType notifierType,Object context,String methodName,String message,Exception e){
  73.         Logger log = NotifierUtilities.getLogger(notifierType, context);
  74.         String idTransazione = NotifierUtilities.getIdTransazione(notifierType, context);
  75.         TipoPdD tipoPorta = NotifierUtilities.getTipoPorta(notifierType, context);
  76.         StringBuilder bf = new StringBuilder();
  77.         bf.append("[NotifierCallback] [").append(idTransazione).append("]-[")
  78.             .append(tipoPorta.name()).append("] (")
  79.             .append(notifierType.name()).append(") @")
  80.             .append(methodName).append("@ : ")
  81.             .append(message);
  82.         log.error(message,e); // log openspcoop_core
  83.         this.getLoggerUtility().error(message, e);
  84.     }
  85.    
  86.     public void error(String msg,Throwable t){
  87.         this.getLoggerUtility().error("[NotifierCallback] "+msg, t);
  88.     }
  89.    
  90.     private void emitLog(NotifierType notifierType,Object context,String methodName){
  91.         StringBuilder bf = new StringBuilder();
  92.         String idTransazione = NotifierUtilities.getIdTransazione(notifierType, context);
  93.         bf.append("[NotifierCallback] \n");
  94.         bf.append("[NotifierCallback] [").append(idTransazione).append("] --------------------------- @"+methodName+"@ (start) ------------------------------\n");          
  95.         OpenSPCoop2Message msg = NotifierUtilities.getOpenSPCoopMessage(notifierType, context);
  96.         String notifierBufferState = "UNDEFINED";
  97.         if(msg!=null && msg.getNotifierInputStream()!=null){
  98.             if(msg.getNotifierInputStream().isBufferEnabled()){
  99.                 notifierBufferState = "ON";
  100.             }
  101.             else{
  102.                 notifierBufferState = "OFF";
  103.             }
  104.         }
  105.        
  106.         bf.append("[NotifierCallback] [").append(idTransazione).append("[").append(NotifierUtilities.getTipoPorta(notifierType, context)).append("]").
  107.             append(" STATE:").append(notifierType.name()).append(" (StatoBuffer:").
  108.             append(notifierBufferState).append(")\n");
  109.         this.getLoggerUtility().debug(bf.toString());
  110.     }
  111.     private void emitLogEnd(NotifierType notifierType,Object context,String methodName){
  112.         StringBuilder bf = new StringBuilder();
  113.         String idTransazione = NotifierUtilities.getIdTransazione(notifierType, context);
  114.         bf.append("[NotifierCallback] [").append(idTransazione).append("] --------------------------- @"+methodName+"@ (end) ------------------------------\n");        
  115.         this.getLoggerUtility().debug(bf.toString());
  116.     }
  117.    

  118.    
  119.    
  120.    
  121.     // **** interface ***
  122.    
  123.     // ** Metodi utilizzati nelle fasi iniziali di creazione del NotifierInputStream **
  124.    
  125.     @Override
  126.     public boolean enableNotifierInputStream(NotifierType notifierType,
  127.             Object context) throws NotifierException {
  128.         this.emitLog(notifierType, context, "enableNotifierInputStream");
  129.         try{
  130.             return NotifierCallbackEnableUtils.enableNotifierInputStream(this, notifierType, context);
  131.         }catch(Exception e){
  132.             error(notifierType, context, "enableNotifierInputStream", e.getMessage(), e);
  133.             throw new NotifierException(e.getMessage(),e);
  134.         }
  135.         finally{
  136.             this.emitLogEnd(notifierType, context, "enableNotifierInputStream");
  137.         }

  138.     }

  139.     @Override
  140.     public boolean throwStreamingHandlerException(NotifierType notifierType,
  141.             Object context) throws NotifierException {
  142.         this.emitLog(notifierType, context, "throwStreamingHandlerException");
  143.         try{
  144.             return OpenSPCoop2Properties.getInstance().isDumpNonRealtimeThrowStreamingHandlerException();
  145.         }catch(Exception e){
  146.             error(notifierType, context, "throwStreamingHandlerException", e.getMessage(), e);
  147.             throw new NotifierException(e.getMessage(),e);
  148.         }
  149.         finally{
  150.             this.emitLogEnd(notifierType, context, "throwStreamingHandlerException");
  151.         }
  152.     }

  153.    
  154.    
  155.     // ** Metodi utilizzati in tutte le fasi **
  156.    
  157.     @Override
  158.     public NotifierResult notify(NotifierType notifierType, Object context)
  159.             throws NotifierException {
  160.         this.emitLog(notifierType, context, "notify");
  161.         try{
  162.             return NotifierCallbackEnableUtils.notify(this, notifierType, context);
  163.         }catch(Exception e){
  164.             error(notifierType, context, "notify", e.getMessage(), e);
  165.             throw new NotifierException(e.getMessage(),e);
  166.         }
  167.         finally{
  168.             this.emitLogEnd(notifierType, context, "notify");
  169.         }
  170.     }

  171.    
  172.    
  173.    
  174.     public String getClassNamePropertiesName(){
  175.         return "org.openspcoop2.notifierCallback.pddOE";
  176.     }
  177.    
  178.     public String [] getOpenSPCoopPropertiesNames(){
  179.         return new String [] {"org.openspcoop2.pdd.services.notifierInputStreamCallback"};
  180.     }
  181.     public String [] getOpenSPCoopPropertiesValues(){
  182.         return new String [] {"pddOE"};
  183.     }
  184. }