TimerGestoreChiaviPDND.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.timers.pdnd;


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

  24. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  25. import org.openspcoop2.pdd.config.PDNDConfig;
  26. import org.openspcoop2.pdd.config.PDNDConfigUtilities;
  27. import org.openspcoop2.pdd.core.CostantiPdD;
  28. import org.openspcoop2.pdd.logger.MsgDiagnosticiProperties;
  29. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  30. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  31. import org.openspcoop2.pdd.services.OpenSPCoop2Startup;
  32. import org.openspcoop2.pdd.timers.TimerException;
  33. import org.openspcoop2.utils.Utilities;
  34. import org.openspcoop2.utils.threads.BaseThread;
  35. import org.slf4j.Logger;


  36. /**    
  37.  * TimerGestoreChiaviPDND
  38.  *
  39.  * @author Poli Andrea (poli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class TimerGestoreChiaviPDND extends BaseThread{

  44.     public static final String ID_MODULO = "TimerGestoreChiaviPDND";
  45.    
  46.     static final String CONNESSIONE_NON_DISPONIBILE = "Connessione al database della configurazione non disponibile";
  47.    
  48.     /** Logger utilizzato per debug. */
  49.     private Logger logTimer = null;
  50.     private void logError(String msgErrore, Exception e) {
  51.         if(this.logTimer!=null) {
  52.             this.logTimer.error(msgErrore,e);
  53.         }
  54.     }
  55.     private MsgDiagnostico msgDiag = null;

  56.     /** OpenSPCoop2Properties */
  57.     private OpenSPCoop2Properties op2Properties = null;
  58.    
  59.     private List<PDNDConfig> remoteStores;
  60.     private Map<String, String> mapUrlCheckEventi;
  61.    
  62.    
  63.    
  64.     /** Costruttore */
  65.     public TimerGestoreChiaviPDND(long timeout, List<PDNDConfig> remoteStores) throws TimerException{
  66.    
  67.         // Aspetto inizializzazione di OpenSPCoop (aspetto mezzo minuto e poi segnalo errore)
  68.         int attesa = 90;
  69.         int secondi = 0;
  70.         while( (!OpenSPCoop2Startup.initialize) && (secondi<attesa) ){
  71.             Utilities.sleep(1000);
  72.             secondi++;
  73.         }
  74.         if(secondi>= 90){
  75.             throw new TimerException("Riscontrata inizializzazione OpenSPCoop non effettuata");
  76.         }  

  77.         this.logTimer = OpenSPCoop2Logger.getLoggerOpenSPCoopTimers();
  78.        
  79.         try {
  80.             this.msgDiag = MsgDiagnostico.newInstance(ID_MODULO);
  81.             this.msgDiag.setPrefixMsgPersonalizzati(MsgDiagnosticiProperties.MSG_DIAG_TIMER_GESTORE_CHIAVI_PDND);
  82.             this.msgDiag.addKeyword(CostantiPdD.KEY_TIMER, ID_MODULO);
  83.         } catch (Exception e) {
  84.             String msgErrore = "Riscontrato Errore durante l'inizializzazione del MsgDiagnostico";
  85.             this.logError(msgErrore,e);
  86.             throw new TimerException(msgErrore,e);
  87.         }

  88.         this.msgDiag.logPersonalizzato("avvioInCorso");
  89.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("avvioInCorso"));
  90.        
  91.         try {
  92.             this.op2Properties = OpenSPCoop2Properties.getInstance();
  93.         } catch (Exception e) {
  94.             this.msgDiag.logErroreGenerico(e,"InizializzazioneTimer");
  95.             String msgErrore = "Riscontrato errore durante l'inizializzazione del Reader delle Properties di OpenSPCoop: "+e.getMessage();
  96.             this.logError(msgErrore,e);
  97.             throw new TimerException(msgErrore,e);
  98.         }
  99.        
  100.         this.remoteStores = remoteStores;
  101.        
  102.         initMapUrlCheckEventi();
  103.        

  104.         this.setTimeout((int)timeout);
  105.         String sec = "secondi";
  106.         if(this.getTimeout() == 1)
  107.             sec = "secondo";
  108.         this.msgDiag.addKeyword(CostantiPdD.KEY_TIMEOUT, this.getTimeout()+" "+sec);
  109.        
  110.         this.msgDiag.logPersonalizzato("avvioEffettuato");
  111.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("avvioEffettuato"));
  112.     }
  113.    
  114.     private void initMapUrlCheckEventi() throws TimerException {
  115.         if(this.remoteStores!=null && !this.remoteStores.isEmpty()) {
  116.             for (PDNDConfig pdndConfig : this.remoteStores) {
  117.                 String remoteStoreName = pdndConfig.getRemoteStoreConfig().getStoreName();
  118.                 try {
  119.                     String urlCheckEventi = PDNDConfigUtilities.buildUrlCheckEventi(pdndConfig.getRemoteStoreConfig(), this.op2Properties);
  120.                     if(this.mapUrlCheckEventi==null) {
  121.                         this.mapUrlCheckEventi = new HashMap<>();
  122.                     }
  123.                     this.mapUrlCheckEventi.put(remoteStoreName, urlCheckEventi);
  124.                 } catch (Exception e) {
  125.                     this.msgDiag.logErroreGenerico(e,"InizializzazioneTimer");
  126.                     String msgErrore = "Riscontrato errore durante l'inizializzazione della configurazione del timer: "+e.getMessage();
  127.                     this.logError(msgErrore,e);
  128.                     throw new TimerException(msgErrore,e);
  129.                 }
  130.             }
  131.         }
  132.     }

  133.     @Override
  134.     public void process(){
  135.        
  136.         try{
  137.             if(this.remoteStores!=null && !this.remoteStores.isEmpty()) {
  138.                 for (PDNDConfig remoteStore : this.remoteStores) {
  139.            
  140.                     String remoteStoreName = remoteStore.getRemoteStoreConfig().getStoreName();
  141.                     this.msgDiag.addKeyword(CostantiPdD.KEY_REMOTE_STORE, remoteStoreName);
  142.                    
  143.                     String urlCheckEventi = this.mapUrlCheckEventi.get(remoteStoreName);
  144.                    
  145.                     process(remoteStoreName, remoteStore, urlCheckEventi);
  146.                 }
  147.             }
  148.            
  149.         }catch(Exception e){
  150.             this.msgDiag.logErroreGenerico(e,"TimerGestoreChiaviPDNDLib.check()");
  151.             this.logError("Errore generale: "+e.getMessage(),e);
  152.         }
  153.        
  154.     }
  155.     private void process(String remoteStoreName, PDNDConfig remoteStore, String urlCheckEventi){
  156.         try {
  157.             TimerGestoreChiaviPDNDLib timer =
  158.                     new TimerGestoreChiaviPDNDLib(this.logTimer, this.msgDiag,
  159.                             remoteStore.getRemoteStoreConfig(), urlCheckEventi,
  160.                             this.getTimeout());
  161.                
  162.             timer.check();
  163.         }catch(Exception e){
  164.             this.msgDiag.logErroreGenerico(e,"TimerGestoreChiaviPDNDLib.check("+remoteStoreName+")");
  165.             this.logError("Errore generale: "+e.getMessage(),e);
  166.         }
  167.     }
  168.    
  169. }