SondaCoda.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.utils.sonde.impl;

  21. import java.sql.Connection;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. import java.util.HashSet;
  25. import java.util.Properties;
  26. import java.util.Set;

  27. import org.openspcoop2.utils.TipiDatabase;
  28. import org.openspcoop2.utils.sonde.ParametriSonda;
  29. import org.openspcoop2.utils.sonde.Sonda;
  30. import org.openspcoop2.utils.sonde.SondaException;

  31. /**
  32.  * Classe di implementazione della Sonda per le code
  33.  *
  34.  *
  35.  * @author Bussu Giovanni (bussu@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */

  39. public class SondaCoda extends Sonda {

  40.     /**
  41.      * Costruttore per la classe SondaCoda
  42.      * @param param parametri costruttivi della sonda
  43.      * @throws Exception
  44.      */
  45.     public SondaCoda(ParametriSonda param) {
  46.         super(param);
  47.         Set<String> reserved = new HashSet<String>();
  48.         reserved.add("dimensione_coda");
  49.         this.getParam().setReserved(reserved);
  50.     }

  51.     @Override
  52.     public StatoSonda getStatoSonda(){
  53.        
  54.         long dimensione_coda = 0;
  55.         String dimensioneCodaString = super.getParam().getDatiCheck().getProperty("dimensione_coda");
  56.         try {
  57.             if(dimensioneCodaString!=null)
  58.                 dimensione_coda = Long.parseLong(dimensioneCodaString);
  59.         } catch(NumberFormatException e) {
  60.             e.printStackTrace(System.err);
  61.             System.err.println("Errore durante il parsing del parametro dimensione_coda: " + dimensioneCodaString + ". Elimino il valore");
  62.             super.getParam().getDatiCheck().remove("dimensione_coda");
  63.         }
  64.         StatoSonda statoSonda = new StatoSonda();
  65.         SimpleDateFormat format = new SimpleDateFormat(PATTERN);

  66.         //Valuto l'eventuale superamento delle soglie e calcolo lo stato
  67.         if(dimensione_coda > super.getParam().getSogliaError()) {
  68.             if(this.getParam().getDataError() == null) {
  69.                 this.getParam().setDataError(new Date());
  70.             }
  71.             statoSonda.setStato(2);
  72.             statoSonda.setDescrizione("La coda "+super.getParam().getNome()+" ha superato la soglia di errore ("+
  73.                                         super.getParam().getSogliaError()+") dal "+
  74.                                         format.format(this.getParam().getDataError())+
  75.                                         ". Dimensione attuale della coda: "+dimensione_coda+".");
  76.         } else if (dimensione_coda > super.getParam().getSogliaWarn()) {
  77.             if(this.getParam().getDataWarn() == null) {
  78.                 this.getParam().setDataWarn(new Date());
  79.             }
  80.             statoSonda.setStato(1);
  81.             statoSonda.setDescrizione("La coda "+super.getParam().getNome()+" ha superato la soglia di warn ("+
  82.                                         super.getParam().getSogliaWarn()+") dal "+
  83.                                         format.format(this.getParam().getDataWarn())+
  84.                                         ". Dimensione attuale della coda: "+dimensione_coda+".");
  85.         } else {
  86.             statoSonda.setStato(0);
  87.             statoSonda.setDescrizione("Coda "+super.getParam().getNome()+": numero elementi in coda: " + dimensione_coda);
  88.         }
  89.  
  90.         return statoSonda;
  91.     }

  92.     //retrocompatibilita
  93.     public StatoSonda aggiornaStatoSonda(long dimensioneCoda, Connection connection, TipiDatabase tipoDatabase) throws SondaException {
  94.         return this.aggiornaStatoSonda(dimensioneCoda, null, connection, tipoDatabase);
  95.     }
  96.    
  97.     /**
  98.      * @param dimensioneCoda dimensione attuale della coda
  99.      * @param connection connessione per il DB
  100.      * @param tipoDatabase tipo database
  101.      * @return lo stato attuale della sonda
  102.      * @throws SondaException
  103.      */
  104.     public StatoSonda aggiornaStatoSonda(long dimensioneCoda, Properties params, Connection connection, TipiDatabase tipoDatabase) throws SondaException {
  105.         // inserisce i dati nel properties
  106.        
  107.         super.getParam().putAllCheck(params);
  108.        
  109.         super.getParam().getDatiCheck().put("dimensione_coda", dimensioneCoda + "");
  110.         return updateSonda(connection, tipoDatabase);
  111.     }


  112. }