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

  21. import java.util.Date;
  22. import java.util.HashSet;
  23. import java.util.Properties;
  24. import java.util.Set;

  25. import org.openspcoop2.utils.io.Base64Utilities;

  26. /**
  27.  * Classe contenente i parametri per le Sonde
  28.  *
  29.  *
  30.  * @author Bussu Giovanni (bussu@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */

  34. public class ParametriSonda {

  35.     private String nome;
  36.     private long sogliaWarn, sogliaError;
  37.     private Date dataOk, dataWarn, dataError, dataUltimoCheck;
  38.     private Properties datiCheck;
  39.     private Set<String> reserved = new HashSet<String>();
  40.     private int statoUltimoCheck;
  41.    
  42.     private static final String SEPARATOR = ":";
  43.     private static final String CUSTOM = "CUSTOM##";

  44.     /**
  45.      * Deserializza una stringa in una lista di properties rappresentanti i dati di check
  46.      * @param datiCheck stringa rappresentante una lista di parametri nome:valore
  47.      * @throws Exception
  48.      */
  49.     public void unmarshallDatiCheck(String datiCheck) {
  50.         this.datiCheck = new Properties();
  51.         if(datiCheck != null) {
  52.             String[] datiList = datiCheck.split("\\\n");
  53.             for(String dato: datiList) {
  54.                 if(dato.startsWith(CUSTOM)) {
  55.                     String datoReale = dato.substring(CUSTOM.length());
  56.                     String[] nameValue = datoReale.split(SEPARATOR);
  57.                     if(nameValue.length != 2){
  58.                         System.err.println("Dato Check ["+datoReale+"] non nel formato valido nome"+SEPARATOR+"valore");
  59.                     } else {
  60.                         try {
  61.                             String key = nameValue[0].trim();
  62.                             String value = nameValue[1].trim();
  63.                             byte[] decodedKey = Base64Utilities.decode(key);
  64.                             byte[] decodedValue = Base64Utilities.decode(value);
  65.                             this.datiCheck.put(new String(decodedKey), new String(decodedValue));
  66.                         } catch(Throwable t) {
  67.                             System.err.println("Errore durante il decoding del parametro ["+datoReale+"]: " + t.getMessage());  
  68.                         }
  69.                     }
  70.                    
  71.                 } else {
  72.                     String[] nameValue = dato.split(SEPARATOR);
  73.                     if(nameValue.length != 2){
  74.                         System.err.println("Dato Check ["+dato+"] non nel formato valido nome"+SEPARATOR+"valore");
  75.                     } else {
  76.                         this.datiCheck.put(nameValue[0].trim(), nameValue[1].trim());
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.     }

  82.     /**
  83.      * Serializza una lista di properties rappresentanti i dati di check in una stringa
  84.      * @return stringa rappresentante una lista di parametri nome=valore
  85.      */
  86.     public String marshallDatiCheck() {
  87.        
  88.         if(this.datiCheck != null) {
  89.             StringBuilder sb = new StringBuilder();
  90.             for(Object key: this.datiCheck.keySet()) {
  91.                 boolean contains = this.reserved.contains(key);
  92.                
  93.                 if(contains) {
  94.                     sb.append(key).append(SEPARATOR).append(this.datiCheck.get(key)).append("\n");
  95.                 } else {
  96.                     String keyEncoded = Base64Utilities.encodeAsString(key.toString().getBytes());
  97.                     String valueEncoded = Base64Utilities.encodeAsString(this.datiCheck.get(key).toString().getBytes());
  98.                     sb.append(CUSTOM).append(keyEncoded).append(SEPARATOR).append(valueEncoded).append("\n");
  99.                 }
  100.             }
  101.             return sb.toString();
  102.         } else {
  103.             return null;
  104.         }
  105.     }

  106.     public void putAllCheck(Properties checks) {
  107.         if(checks != null) {
  108.             for(Object key: checks.keySet()) {
  109.                 if(!this.reserved.contains(key)) {
  110.                     this.datiCheck.put(key, checks.get(key));
  111.                 }
  112.             }
  113.         }
  114.     }
  115.     /**
  116.      *  GETTERS E SETTERS
  117.      */
  118.     public String getNome() {
  119.         return this.nome;
  120.     }
  121.     public void setNome(String nome) {
  122.         this.nome = nome;
  123.     }
  124.     public long getSogliaWarn() {
  125.         return this.sogliaWarn;
  126.     }
  127.     public void setSogliaWarn(long sogliaWarn) {
  128.         this.sogliaWarn = sogliaWarn;
  129.     }
  130.     public long getSogliaError() {
  131.         return this.sogliaError;
  132.     }
  133.     public void setSogliaError(long sogliaError) {
  134.         this.sogliaError = sogliaError;
  135.     }
  136.     public Date getDataOk() {
  137.         return this.dataOk;
  138.     }
  139.     public void setDataOk(Date dataOk) {
  140.         this.dataOk = dataOk;
  141.     }
  142.     public Date getDataWarn() {
  143.         return this.dataWarn;
  144.     }
  145.     public void setDataWarn(Date dataWarn) {
  146.         this.dataWarn = dataWarn;
  147.     }
  148.     public Date getDataError() {
  149.         return this.dataError;
  150.     }
  151.     public void setDataError(Date dataError) {
  152.         this.dataError = dataError;
  153.     }
  154.     public Date getDataUltimoCheck() {
  155.         return this.dataUltimoCheck;
  156.     }
  157.     public void setDataUltimoCheck(Date dataUltimoCheck) {
  158.         this.dataUltimoCheck = dataUltimoCheck;
  159.     }
  160.     public Properties getDatiCheck() {
  161.         return this.datiCheck;
  162.     }
  163.     public void setDatiCheck(Properties datiCheck) {
  164.         this.datiCheck = datiCheck;
  165.     }
  166.     public int getStatoUltimoCheck() {
  167.         return this.statoUltimoCheck;
  168.     }
  169.     public void setStatoUltimoCheck(int statoUltimoCheck) {
  170.         this.statoUltimoCheck = statoUltimoCheck;
  171.     }

  172.     public Set<String> getReserved() {
  173.         return this.reserved;
  174.     }

  175.     public void setReserved(Set<String> reserved) {
  176.         this.reserved = reserved;
  177.     }
  178. }