HealthCheckUtils.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.core.behaviour.built_in.load_balance.health_check;

  21. import org.openspcoop2.core.config.PortaApplicativa;
  22. import org.openspcoop2.core.config.Proprieta;
  23. import org.openspcoop2.pdd.core.behaviour.BehaviourException;
  24. import org.openspcoop2.pdd.core.behaviour.BehaviourPropertiesUtils;
  25. import org.slf4j.Logger;

  26. /**
  27.  * HealthCheckUtils
  28.  *
  29.  * @author Andrea Poli (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class HealthCheckUtils  {
  34.        
  35.     public static HealthCheckConfigurazione read(PortaApplicativa pa, Logger log) throws BehaviourException {
  36.         HealthCheckConfigurazione config = new HealthCheckConfigurazione();
  37.         if(pa.getBehaviour()==null || pa.getBehaviour().sizeProprietaList()<=0) {
  38.             throw new BehaviourException("Configurazione sticky non disponibile");
  39.         }
  40.        
  41.         for (Proprieta p : pa.getBehaviour().getProprietaList()) {
  42.            
  43.             String nome = p.getNome();
  44.             String valore = p.getValore().trim();
  45.            
  46.             try {
  47.                 if(HealthCheckCostanti.PASSIVE_HEALTH_CHECK.equals(nome)) {
  48.                     config.setPassiveCheckEnabled("true".equals(valore));
  49.                 }
  50.                 else if(HealthCheckCostanti.PASSIVE_HEALTH_CHECK_SECONDS.equals(nome)) {
  51.                     config.setPassiveHealthCheck_excludeForSeconds(Integer.valueOf(valore));
  52.                 }
  53.             }catch(Exception e) {
  54.                 throw new BehaviourException("Configurazione health check non corretta (proprietà:"+p.getNome()+" valore:'"+p.getValore()+"'): "+e.getMessage(),e);
  55.             }
  56.            
  57.         }

  58.         return config;
  59.     }
  60.    

  61.     public static void save(PortaApplicativa pa, HealthCheckConfigurazione configurazione) throws BehaviourException {
  62.        
  63.         if(pa.getBehaviour()==null) {
  64.             throw new BehaviourException("Configurazione behaviour non abilitata");
  65.         }
  66.         if(configurazione==null) {
  67.             throw new BehaviourException("Configurazione health check non fornita");
  68.         }
  69.         BehaviourPropertiesUtils.addProprieta(pa.getBehaviour(),HealthCheckCostanti.PASSIVE_HEALTH_CHECK, configurazione.isPassiveCheckEnabled()+"");
  70.        
  71.         BehaviourPropertiesUtils.removeProprieta(pa.getBehaviour(),HealthCheckCostanti.PASSIVE_HEALTH_CHECK_SECONDS);
  72.        
  73.         if(configurazione.isPassiveCheckEnabled()) {
  74.             if(configurazione.getPassiveHealthCheck_excludeForSeconds()!=null && configurazione.getPassiveHealthCheck_excludeForSeconds().intValue()>0) {
  75.                 BehaviourPropertiesUtils.addProprieta(pa.getBehaviour(),HealthCheckCostanti.PASSIVE_HEALTH_CHECK_SECONDS, configurazione.getPassiveHealthCheck_excludeForSeconds().intValue()+"");
  76.             }
  77.         }
  78.        
  79.     }
  80.    
  81. }