GestorePolicyAttive.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.controllo_traffico.policy.driver;

  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.core.controllo_traffico.driver.IGestorePolicyAttive;
  26. import org.openspcoop2.core.controllo_traffico.driver.PolicyException;
  27. import org.openspcoop2.core.controllo_traffico.driver.PolicyGroupByActiveThreadsType;
  28. import org.slf4j.Logger;

  29. /**    
  30.  * GestorePolicyAttive
  31.  *
  32.  * @author Poli Andrea (poli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class GestorePolicyAttive {

  37.     private static Map<PolicyGroupByActiveThreadsType, IGestorePolicyAttive> staticMapInstance = null;
  38.     private static Map<PolicyGroupByActiveThreadsType, String> staticPolicyRateLimitingImage = null;
  39.     private static Map<PolicyGroupByActiveThreadsType, String> staticPolicyRateLimitingEventiImage = null;
  40.     private static TipoGestorePolicy tipo;
  41.     private static String urlService;
  42.     private static Logger logStartup;
  43.     private static Logger log;
  44.    
  45.     private static synchronized void initialize(boolean isStartupGovWay, PolicyGroupByActiveThreadsType type) throws PolicyException{
  46.         if(!GestorePolicyAttive.staticMapInstance.containsKey(type)) {
  47.            
  48.             GestorePolicyAttive.logStartup.info("Inizializzazione Gestore Policy '"+type+"' ...");
  49.             GestorePolicyAttive.log.info("Inizializzazione Gestore Policy '"+type+"' ...");
  50.            
  51.             IGestorePolicyAttive gestore = null;
  52.             if(TipoGestorePolicy.IN_MEMORY.equals(GestorePolicyAttive.tipo)){
  53.                 gestore = new GestorePolicyAttiveInMemory();
  54.                 gestore.initialize(GestorePolicyAttive.log, isStartupGovWay, type);
  55.             }
  56.             else if(TipoGestorePolicy.WS.equals(GestorePolicyAttive.tipo)){
  57.                 gestore = new GestorePolicyAttiveWS();
  58.                 gestore.initialize(GestorePolicyAttive.log, isStartupGovWay, type, GestorePolicyAttive.urlService);
  59.             }
  60.             else{
  61.                 throw new PolicyException("Tipo GestorePolicyAttive ["+GestorePolicyAttive.tipo+"] non supportato");
  62.             }
  63.             GestorePolicyAttive.staticMapInstance.put(type, gestore);
  64.             GestorePolicyAttive.staticPolicyRateLimitingImage.put(type, org.openspcoop2.core.controllo_traffico.constants.Costanti.getControlloTrafficoImage(type.name()));
  65.             GestorePolicyAttive.staticPolicyRateLimitingEventiImage.put(type, org.openspcoop2.core.controllo_traffico.constants.Costanti.getControlloTrafficoEventiImage(type.name()));
  66.            
  67.             GestorePolicyAttive.logStartup.info("Inizializzazione Gestore Policy '"+type+"' effettuata con successo");
  68.             GestorePolicyAttive.log.info("Inizializzazione Gestore Policy '"+type+"' effettuata con successo");
  69.            
  70.         }
  71.     }
  72.    
  73.     public static synchronized void initialize(Logger logStartup, Logger log, TipoGestorePolicy tipo, String urlService, List<PolicyGroupByActiveThreadsType> inMemoryTypes) throws Exception{
  74.         if(GestorePolicyAttive.staticMapInstance==null){
  75.             if(inMemoryTypes==null || inMemoryTypes.isEmpty()) {
  76.                 throw new Exception("Almeno un tipo di gestore deve essere definito");
  77.             }          
  78.             GestorePolicyAttive.staticMapInstance = new HashMap<PolicyGroupByActiveThreadsType, IGestorePolicyAttive>();
  79.             GestorePolicyAttive.staticPolicyRateLimitingImage = new HashMap<PolicyGroupByActiveThreadsType, String>();
  80.             GestorePolicyAttive.staticPolicyRateLimitingEventiImage = new HashMap<PolicyGroupByActiveThreadsType, String>();
  81.             GestorePolicyAttive.logStartup = logStartup;
  82.             GestorePolicyAttive.log = log;
  83.             GestorePolicyAttive.tipo = tipo;
  84.             GestorePolicyAttive.urlService = urlService;
  85.            
  86.             for (PolicyGroupByActiveThreadsType type : inMemoryTypes) {
  87.                 initialize(true, type);
  88.             }
  89.         }
  90.     }
  91.    
  92.     public static List<PolicyGroupByActiveThreadsType> getTipiGestoriAttivi() throws PolicyException{
  93.         if(staticMapInstance==null){
  94.             throw new PolicyException("GestorePolicyAttive non inizializzato");
  95.         }
  96.         List<PolicyGroupByActiveThreadsType> l = new ArrayList<PolicyGroupByActiveThreadsType>();
  97.         l.addAll(staticMapInstance.keySet());
  98.         return l;
  99.     }
  100.    
  101.     public static boolean isAttivo(PolicyGroupByActiveThreadsType type) {
  102.         if(staticMapInstance==null){
  103.             return false;
  104.         }
  105.         return staticMapInstance.containsKey(type);
  106.     }
  107.    
  108.     public static IGestorePolicyAttive getInstance(PolicyGroupByActiveThreadsType type) throws PolicyException{
  109.         if(staticMapInstance==null){
  110.             throw new PolicyException("GestorePolicyAttive non inizializzato");
  111.         }
  112.         IGestorePolicyAttive gestore = staticMapInstance.get(type);
  113.         if(gestore==null) {
  114.             GestorePolicyAttive.initialize(false, type);
  115.             gestore = staticMapInstance.get(type);
  116.         }
  117.         if(gestore==null) {
  118.             throw new PolicyException("GestorePolicyAttive '"+type+"' non inizializzato ??");
  119.         }
  120.         return gestore;
  121.     }
  122.    
  123.     public static String getControlloTrafficoImage(PolicyGroupByActiveThreadsType type) throws PolicyException{
  124.         if(staticPolicyRateLimitingImage==null){
  125.             throw new PolicyException("GestorePolicyAttive non inizializzato");
  126.         }
  127.         if(!staticPolicyRateLimitingImage.containsKey(type)){
  128.             throw new PolicyException("GestorePolicyAttive non inizializzato per il tipo '"+type+"'");
  129.         }
  130.         return staticPolicyRateLimitingImage.get(type);
  131.     }
  132.    
  133.     public static String getControlloTrafficoEventiImage(PolicyGroupByActiveThreadsType type) throws PolicyException{
  134.         if(staticPolicyRateLimitingEventiImage==null){
  135.             throw new PolicyException("GestorePolicyAttive non inizializzato");
  136.         }
  137.         if(!staticPolicyRateLimitingEventiImage.containsKey(type)){
  138.             throw new PolicyException("GestorePolicyAttive non inizializzato per il tipo '"+type+"'");
  139.         }
  140.         return staticPolicyRateLimitingEventiImage.get(type);
  141.     }
  142. }