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

  21. import java.util.List;

  22. import org.openspcoop2.pdd.config.ClassNameProperties;
  23. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  24. import org.openspcoop2.utils.io.notifier.NotifierInputStream;
  25. import org.openspcoop2.utils.io.notifier.StreamingHandler;
  26. import org.openspcoop2.utils.resources.Loader;
  27. import org.openspcoop2.message.OpenSPCoop2Message;

  28. /**
  29.  * NotifierUtilities
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class NotifierUtilities {

  36.     private static INotifierCallback notifierCallback = null;
  37.     private static boolean notifierCallbackInitialized = false;
  38.     private static synchronized void initNotifierCallback() throws Exception{
  39.         if(notifierCallback==null){
  40.             OpenSPCoop2Properties properties = OpenSPCoop2Properties.getInstance();
  41.             if(properties.isNotifierInputStreamEnabled()) {
  42.                 String notifierInputStreamCallback = properties.getNotifierInputStreamCallback();
  43.                 ClassNameProperties classNameProperties = ClassNameProperties.getInstance();
  44.                 notifierCallback = (INotifierCallback) Loader.getInstance().newInstance(classNameProperties.getNotifierCallback(notifierInputStreamCallback));
  45.             }
  46.             notifierCallbackInitialized = true;
  47.         }
  48.     }
  49.     private static INotifierCallback getNotifierCallback() throws Exception{
  50.         if(notifierCallbackInitialized==false){
  51.             initNotifierCallback();
  52.         }
  53.         return notifierCallback;
  54.     }
  55.    
  56.     public static void updateNotifierState(OpenSPCoop2Message message, NotifierType notifierType, Object context) throws Exception{
  57.        
  58.         if(message!=null && message.getNotifierInputStream()!=null){
  59.            
  60.             NotifierInputStream nis = message.getNotifierInputStream();
  61.             INotifierCallback notifierCallback = getNotifierCallback(); // viene inizializzato
  62.            
  63.             if(notifierCallback!=null){        
  64.            
  65.                 // Notify
  66.                 NotifierResult notifierResult = notifierCallback.notify(notifierType, context);
  67.                
  68.                 // Nuovi Streaming Handler (possono essere aggiunti in corsa solo se il buffer e' abilitato)
  69.                 if(nis.isBufferEnabled()){
  70.                
  71.                     List<StreamingHandler> streamingHandlers = notifierResult.getStreamingHandlers();
  72.                     if(streamingHandlers!=null){
  73.                         for (StreamingHandler streamingHandler : streamingHandlers) {
  74.                             nis.addStreamingHandler(streamingHandler);
  75.                         }
  76.                     }
  77.                    
  78.                 }
  79.                                    
  80.                 // Situazione Buffer
  81.                 if(nis.isBufferEnabled()){
  82.                        
  83.                     NotifierBufferState nuovoStatoBuffer = notifierResult.getBufferState();
  84.                     if(NotifierBufferState.DISABLE_AND_RELEASE_BUFFER_READED.equals(nuovoStatoBuffer)){
  85.                         // puo' succedere che durante la callback, sia stato chiamata il serializeAndConsume. Questo rilascia anche il buffer.
  86.                         // E' quindi inutile rilasciarlo nuovamente.
  87.                         if(nis.isBufferEnabled()){
  88.                             nis.setOFFBuffering(true);
  89.                         }
  90.                     }
  91.                     else if(NotifierBufferState.DISABLE.equals(nuovoStatoBuffer)){
  92.                         // puo' succedere che durante la callback, sia stato chiamata il serializeAndConsume. Questo rilascia anche il buffer.
  93.                         // E' quindi inutile rilasciarlo nuovamente.
  94.                         if(nis.isBufferEnabled()){
  95.                             nis.setOFFBuffering(false);
  96.                         }
  97.                     }
  98.                    
  99.                 }
  100.             }
  101.            
  102.         }
  103.        
  104.     }
  105.    
  106. }