AlarmThread.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.monitor.engine.alarm;

  21. import org.openspcoop2.core.allarmi.constants.StatoAllarme;
  22. import org.openspcoop2.core.allarmi.utils.AllarmiConverterUtils;
  23. import org.openspcoop2.core.plugins.constants.TipoPlugin;
  24. import org.openspcoop2.monitor.engine.dynamic.DynamicFactory;
  25. import org.openspcoop2.monitor.engine.dynamic.IDynamicLoader;
  26. import org.openspcoop2.monitor.sdk.alarm.IAlarm;
  27. import org.openspcoop2.monitor.sdk.constants.AlarmStateValues;
  28. import org.openspcoop2.monitor.sdk.exceptions.AlarmException;
  29. import org.openspcoop2.monitor.sdk.plugins.IAlarmProcessing;
  30. import org.openspcoop2.utils.Utilities;
  31. import org.openspcoop2.utils.date.DateManager;
  32. import org.openspcoop2.utils.id.IDUtilities;
  33. import org.slf4j.Logger;

  34. /**
  35.  * AlarmLibrary
  36.  *
  37.  * @author Poli Andrea (apoli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class AlarmThread implements Runnable {

  42.     private int periodMillis;
  43.     private String tipo;
  44.     private String classname;
  45.     private IAlarm alarm;
  46.     private Logger _log;
  47.     @SuppressWarnings("unused")
  48.     private AlarmEngineConfig alarmEngineConfig;
  49.     private String threadName;
  50.     private AlarmThreadStatus threadStatus = null;
  51.     private boolean terminated = false;
  52.     private boolean forceNewCheck = false;
  53.    
  54.     private AlarmLogger alarmLogger;
  55.    
  56.     public boolean isTerminated() {
  57.         return this.terminated;
  58.     }

  59.     public void setStop(boolean stop) {
  60.         this.threadStatus.setStop(stop);
  61.     }
  62.    
  63.     public void forceNewCheck() {
  64.         this.forceNewCheck = true;
  65.     }

  66.     public AlarmThread(Logger log,String tipo, String classname, IAlarm alarm, AlarmEngineConfig alarmEngineConfig)
  67.             throws AlarmException {
  68.         if (tipo == null)
  69.             throw new AlarmException(
  70.                     "parametro tipo NULL passato al thread");
  71.         if (classname == null)
  72.             throw new AlarmException(
  73.                     "parametro classname NULL passato al thread");
  74.         if (alarm == null)
  75.             throw new AlarmException(
  76.                     "parametro alarm NULL passato al thread");
  77.         this.tipo = tipo;
  78.         this.classname = classname;
  79.         this.alarm = alarm;
  80.         this._log = log;
  81.         this.alarmEngineConfig = alarmEngineConfig;
  82.         this.threadName = "T_"+IDUtilities.getUniqueSerialNumber("newAlarmThread")+"_"+DateManager.getTimeMillis();
  83.         ((AlarmImpl)this.alarm).setThreadName(this.threadName);
  84.         this.threadStatus = new AlarmThreadStatus();
  85.         ((AlarmImpl)this.alarm).setAlarmThreadStatus(this.threadStatus);
  86.        
  87.         this.alarmLogger = (AlarmLogger) alarm.getLogger();
  88.     }

  89.     public void updateState(AlarmStateValues alarmStatus){
  90.         this.alarm.getStatus().setStatus(alarmStatus);
  91.         switch (alarmStatus) {
  92.         case OK:
  93.             this.alarm.getConfigAllarme().setStato(AllarmiConverterUtils.toIntegerValue(StatoAllarme.OK));
  94.             break;
  95.         case WARNING:
  96.             this.alarm.getConfigAllarme().setStato(AllarmiConverterUtils.toIntegerValue(StatoAllarme.WARNING));
  97.             break;
  98.         case ERROR:
  99.             this.alarm.getConfigAllarme().setStato(AllarmiConverterUtils.toIntegerValue(StatoAllarme.ERROR));
  100.             break;
  101.         }
  102.     }
  103.    
  104.     public void updateAcknowledged(boolean acknoledgement){
  105.         if(acknoledgement){
  106.             this.alarm.getConfigAllarme().setAcknowledged(1);
  107.         }else{
  108.             this.alarm.getConfigAllarme().setAcknowledged(0);
  109.         }
  110.     }
  111.    
  112.     public void setPeriodByDays(int days) {
  113.         this.periodMillis = days * 24 * 60 * 60 * 1000;
  114.     }

  115.     public void setPeriodByHours(int hours) {
  116.         this.periodMillis = hours * 60 * 60 * 1000;
  117.     }

  118.     public void setPeriodByMinutes(int minutes) {
  119.         this.periodMillis = minutes * 60 * 1000;
  120.     }

  121.     public void setPeriodBySeconds(int seconds) {
  122.         this.periodMillis = seconds * 1000;
  123.     }

  124.     @Override
  125.     public void run() {
  126.        
  127.         try{
  128.        
  129.             this.alarmLogger.debug("Thread avviato ...");
  130.            
  131.             while (this.threadStatus.isStop() == false) {
  132.                 try {
  133.                    
  134.                     this.alarmLogger.info("Verifica stato in corso ...");
  135.                    
  136. //                  org.openspcoop2.monitor.sdk.alarm.AlarmStatus oldStatus = null;
  137. //                  if(this.alarm.getStatus()!=null){
  138. //                      oldStatus = (org.openspcoop2.monitor.sdk.alarm.AlarmStatus) this.alarm.getStatus().clone();
  139. //                  }
  140.                    
  141.                     TipoPlugin tipoPlugin = TipoPlugin.ALLARME;
  142.                     IDynamicLoader cAllarme = DynamicFactory.getInstance().newDynamicLoader(tipoPlugin, this.tipo, this.classname, this._log);
  143.                     IAlarmProcessing alarmProc = (IAlarmProcessing) cAllarme.newInstance();
  144.                     this.alarmLogger.debug("Invocazione plugin '"+this.tipo+"' ...");
  145.                     alarmProc.check(this.alarm);
  146.                     this.alarmLogger.debug("Invocazione plugin '"+this.tipo+"' terminato");
  147.                    
  148.                     // la notifica di cambio stato viene attuata quando si effettua il changeStatus sull'allarme
  149. //                  this.alarmLogger.debug("Invocazione plugin '"+this.tipo+"' terminata; analisi nuovo stato in corso ...");
  150. //                  
  151. //                  AlarmManager.notifyChangeStatus(this.alarmEngineConfig, this.alarm.getConfigAllarme(),
  152. //                          this.alarm, this.classname, this.threadName,
  153. //                          this.alarmLogger,
  154. //                          oldStatus, this.alarm.getStatus(),
  155. //                          true);
  156. //                  
  157. //                  this.alarmLogger.info("Analisi nuovo stato terminata correttamente");
  158.                 } catch (Exception e) {
  159.                     this.alarmLogger.error("Errore inatteso emerso durante la verifica dello stato: "
  160.                                     + e.getMessage(), e);
  161.                 }
  162.                 int sleep = 0;
  163.                 int timeSleep = 500;
  164.                 this.alarmLogger.debug("Sleep "+this.periodMillis+"ms ...");
  165.                 while(sleep<this.periodMillis && this.threadStatus.isStop()==false){
  166.                     Utilities.sleep(timeSleep);
  167.                     sleep = sleep+timeSleep;
  168.                     if(this.forceNewCheck) {
  169.                         break;
  170.                     }
  171.                 }
  172.                 if(this.forceNewCheck) {
  173.                     this.alarmLogger.debug("Sleep terminata prematuramente; รจ stato richiesto un nuovo controllo senza attendere il normale intervallo temporale");
  174.                     this.forceNewCheck = false;
  175.                 }
  176.                 else {
  177.                     this.alarmLogger.debug("Sleep "+this.periodMillis+"ms terminato");
  178.                 }
  179.             }
  180.            
  181.             this.alarmLogger.debug("Thread terminato");
  182.         }finally{
  183.             this.terminated = true;
  184.         }
  185.     }
  186.        
  187.     public String getStatoAllarme() {
  188.         return this.alarmLogger.getStatoAllarme();
  189.     }
  190. }