ModISignalHubConfig.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.protocol.modipa.config;

  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Properties;

  24. import org.apache.commons.lang.StringUtils;
  25. import org.openspcoop2.protocol.sdk.ProtocolException;

  26. /**
  27.  * ModISignalHubConfig
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class ModISignalHubConfig {

  34.     public static final String PROPERTY_PARAMS = "params";
  35.    
  36.     private List<ModISignalHubParamConfig> params;
  37.        
  38.     public ModISignalHubConfig copyNewInstance() {
  39.         ModISignalHubConfig config = new ModISignalHubConfig();
  40.        
  41.         config.params = new ArrayList<>();
  42.         if(this.params!=null && !this.params.isEmpty()) {          
  43.             for (ModISignalHubParamConfig modISignalHubParamConfig : this.params) {
  44.                 config.params.add(modISignalHubParamConfig.copyNewInstance());
  45.             }
  46.         }
  47.        
  48.         return config;
  49.     }
  50.    
  51.     private ModISignalHubConfig() {}
  52.     ModISignalHubConfig(String prefix, Properties p) throws ProtocolException {
  53.        
  54.         String claimsP = getProperty(prefix, p, PROPERTY_PARAMS, true);
  55.         String [] tmp = claimsP.split(",");
  56.         if(tmp==null || tmp.length<=0) {
  57.             throw new ProtocolException("Property '"+PROPERTY_PARAMS+"' empty");
  58.         }
  59.         this.params = new ArrayList<>();
  60.         for (String c : tmp) {
  61.             c = c.trim();
  62.             this.params.add(new ModISignalHubParamConfig(prefix, c, p));
  63.         }
  64.        
  65.         // Verifico univocita
  66.         if(!this.params.isEmpty()) {
  67.             verify();
  68.         }
  69.        
  70.     }
  71.     private void verify() throws ProtocolException {
  72.         for (ModISignalHubParamConfig modISignalHubParamConfig : this.params) {
  73.             String name = modISignalHubParamConfig.getNome();
  74.             int count = 0;
  75.             for (ModISignalHubParamConfig modISignalHubParamConfigCheck : this.params) {
  76.                 if(name.equals(modISignalHubParamConfigCheck.getNome())) {
  77.                     count++;
  78.                 }
  79.             }
  80.             if(count>1) {
  81.                 throw new ProtocolException("Property "+PROPERTY_PARAMS+".xx."+ModISignalHubParamConfig.PROPERTY_NOME+"="+name+" defined more then one time ("+count+")");
  82.             }
  83.         }
  84.     }
  85.    
  86.    
  87.     static String getProperty(String prefixProperty, Properties p, String name, boolean required) throws ProtocolException {
  88.         String tmp = p.getProperty(name);
  89.         if(tmp!=null) {
  90.             return tmp.trim();
  91.         }
  92.         else {
  93.             if(required) {
  94.                 throw new ProtocolException("Property '"+prefixProperty+"."+name+"' notFound");
  95.             }
  96.             return null;
  97.         }
  98.     }
  99.     static boolean getBooleanProperty(String prefixProperty, Properties p, String name, boolean required, boolean defaultValue) throws ProtocolException {
  100.         String tmp = getProperty(prefixProperty, p, name, required);
  101.         if(tmp!=null && StringUtils.isNotEmpty(tmp)) {
  102.             try {
  103.                 return Boolean.valueOf(tmp);
  104.             }catch(Exception t) {
  105.                 throw new ProtocolException("Boolean property '"+prefixProperty+"."+name+"' invalid (found value:["+tmp+"]): "+t.getMessage(),t);
  106.             }
  107.         }
  108.         return defaultValue;
  109.     }
  110.     static int getIntProperty(String prefixProperty, Properties p, String name, boolean required, int defaultValue) throws ProtocolException {
  111.         String tmp = getProperty(prefixProperty, p, name, required);
  112.         if(tmp!=null && StringUtils.isNotEmpty(tmp)) {
  113.             try {
  114.                 return Integer.valueOf(tmp);
  115.             }catch(Exception t) {
  116.                 throw new ProtocolException("Int property '"+prefixProperty+"."+name+"' invalid (found value:["+tmp+"]): "+t.getMessage(),t);
  117.             }
  118.         }
  119.         return defaultValue;
  120.     }
  121.    

  122.     public List<ModISignalHubParamConfig> getClaims() {
  123.         return this.params;
  124.     }
  125.     public void setClaims(List<ModISignalHubParamConfig> claims) {
  126.         this.params = claims;
  127.     }

  128. }