TimerGestoreOperazioniRemote.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.proxy;


  21. import java.util.Date;

  22. import org.apache.commons.lang.StringUtils;
  23. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  24. import org.openspcoop2.pdd.core.CostantiPdD;
  25. import org.openspcoop2.pdd.logger.MsgDiagnosticiProperties;
  26. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  27. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  28. import org.openspcoop2.pdd.services.OpenSPCoop2Startup;
  29. import org.openspcoop2.pdd.services.connector.proxy.IProxyOperationService;
  30. import org.openspcoop2.pdd.services.connector.proxy.ProxyOperationServiceFactory;
  31. import org.openspcoop2.pdd.timers.TimerException;
  32. import org.openspcoop2.utils.Utilities;
  33. import org.openspcoop2.utils.date.DateManager;
  34. import org.openspcoop2.utils.date.DateUtils;
  35. import org.openspcoop2.utils.threads.BaseThread;
  36. import org.slf4j.Logger;


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

  45.     public static final String ID_MODULO = "TimerGestoreOperazioniRemote";
  46.    
  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 Date dataUltimoAggiornamento = null;
  60.    
  61.     private String urlPrefix;

  62.     private IProxyOperationService proxyOperationService = null;
  63.    
  64.    
  65.     /** Costruttore */
  66.     public TimerGestoreOperazioniRemote(long timeout) throws TimerException{
  67.    
  68.         // Aspetto inizializzazione di OpenSPCoop (aspetto mezzo minuto e poi segnalo errore)
  69.         int attesa = 90;
  70.         int secondi = 0;
  71.         while( (!OpenSPCoop2Startup.initialize) && (secondi<attesa) ){
  72.             Utilities.sleep(1000);
  73.             secondi++;
  74.         }
  75.         if(secondi>= 90){
  76.             throw new TimerException("Riscontrata inizializzazione OpenSPCoop non effettuata");
  77.         }  

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

  89.         this.msgDiag.logPersonalizzato("avvioInCorso");
  90.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("avvioInCorso"));
  91.        
  92.         try {
  93.             this.op2Properties = OpenSPCoop2Properties.getInstance();
  94.         } catch (Exception e) {
  95.             this.msgDiag.logErroreGenerico(e,"InizializzazioneTimer");
  96.             String msgErrore = "Riscontrato errore durante l'inizializzazione del Reader delle Properties di OpenSPCoop: "+e.getMessage();
  97.             this.logError(msgErrore,e);
  98.             throw new TimerException(msgErrore,e);
  99.         }
  100.        
  101.         String className = this.op2Properties.getProxyReadJMXResourcesAsyncProcessByTimerServiceImplClass();
  102.         try {
  103.             this.proxyOperationService = ProxyOperationServiceFactory.newInstance(className, this.logTimer);
  104.         }catch(Exception e) {
  105.             this.msgDiag.logErroreGenerico(e,"InizializzazioneTimer-ProxyOperationService");
  106.             String msgErrore = "Riscontrato errore durante l'inizializzazione del ProxyOperationService: "+e.getMessage();
  107.             this.logError(msgErrore,e);
  108.             throw new TimerException(msgErrore,e);
  109.         }
  110.        
  111.         this.urlPrefix = buildUlrPrefix(this.op2Properties);
  112.        
  113.         this.dataUltimoAggiornamento = DateManager.getDate();
  114.        
  115.         this.setTimeout((int)timeout);
  116.         String sec = "secondi";
  117.         if(this.getTimeout() == 1)
  118.             sec = "secondo";
  119.         this.msgDiag.addKeyword(CostantiPdD.KEY_TIMEOUT, this.getTimeout()+" "+sec);
  120.        
  121.         this.msgDiag.logPersonalizzato("avvioEffettuato");
  122.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("avvioEffettuato"));
  123.        
  124.     }
  125.    
  126.     private static String buildUlrPrefix(OpenSPCoop2Properties op2Properties) {
  127.         // schema
  128.         String protocolSchema = op2Properties.getProxyReadJMXResourcesSchema();
  129.         String tmp = op2Properties.getProxyReadJMXResourcesAsyncProcessByTimerSchema();
  130.         if(tmp!=null && StringUtils.isNotEmpty(tmp)) {
  131.             protocolSchema = tmp;
  132.         }
  133.         String protocol = (protocolSchema!=null && protocolSchema.trim().toLowerCase().startsWith("https")) ? "https://" : "http://";
  134.        
  135.         // hostname (solo in caso async-mode)
  136.         String hostnameAsync = op2Properties.getProxyReadJMXResourcesAsyncProcessByTimerHostname();
  137.         if(hostnameAsync==null || StringUtils.isEmpty(hostnameAsync)) {
  138.             hostnameAsync = "localhost";
  139.         }
  140.        
  141.         // port
  142.         int port = 8080;
  143.         if(op2Properties.getProxyReadJMXResourcesAsyncProcessByTimerPort()!=null && op2Properties.getProxyReadJMXResourcesAsyncProcessByTimerPort().intValue()>0) {
  144.             port = op2Properties.getProxyReadJMXResourcesAsyncProcessByTimerPort().intValue();
  145.         }
  146.         else if(op2Properties.getProxyReadJMXResourcesPort()!=null && op2Properties.getProxyReadJMXResourcesPort().intValue()>0) {
  147.             port = op2Properties.getProxyReadJMXResourcesPort().intValue();
  148.         }
  149.        
  150.         String hostnameLocale = op2Properties.getClusterHostname();
  151.         if(hostnameLocale!=null) {
  152.             Integer portHostname = op2Properties.getProxyReadJMXResourcesPort(hostnameLocale);
  153.             if(portHostname!=null && portHostname.intValue()>0) {
  154.                 port = portHostname.intValue();
  155.             }
  156.         }
  157.        
  158.         return protocol+hostnameAsync+":"+port;
  159.     }

  160.     @Override
  161.     public void process(){
  162.        
  163.         try{
  164.            
  165.             this.msgDiag.addKeyword(CostantiPdD.DATA_AGGIORNAMENTO, DateUtils.getSimpleDateFormatMs().format(this.dataUltimoAggiornamento));
  166.            
  167.             Date now = DateManager.getDate();
  168.            
  169.             // Prendo la gestione
  170.             TimerGestoreOperazioniRemoteLib timer = new TimerGestoreOperazioniRemoteLib(this.logTimer, this.msgDiag,
  171.                     this.proxyOperationService,
  172.                     now, this.dataUltimoAggiornamento, this.urlPrefix);
  173.            
  174.             timer.check();
  175.            
  176.             this.dataUltimoAggiornamento = now;
  177.            
  178.         }catch(Exception e){
  179.             this.msgDiag.logErroreGenerico(e,"TimerGestoreOperazioniRemote.check()");
  180.             this.logError("Errore generale: "+e.getMessage(),e);
  181.         }
  182.        
  183.     }
  184.    
  185. }