ControlloTraffico.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.jmx;

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

  25. import javax.management.Attribute;
  26. import javax.management.AttributeList;
  27. import javax.management.AttributeNotFoundException;
  28. import javax.management.DynamicMBean;
  29. import javax.management.InvalidAttributeValueException;
  30. import javax.management.JMException;
  31. import javax.management.MBeanAttributeInfo;
  32. import javax.management.MBeanConstructorInfo;
  33. import javax.management.MBeanException;
  34. import javax.management.MBeanInfo;
  35. import javax.management.MBeanOperationInfo;
  36. import javax.management.MBeanParameterInfo;
  37. import javax.management.NotificationBroadcasterSupport;
  38. import javax.management.ReflectionException;

  39. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  40. import org.openspcoop2.core.controllo_traffico.ConfigurazioneGenerale;
  41. import org.openspcoop2.core.controllo_traffico.beans.JMXConstants;
  42. import org.openspcoop2.core.controllo_traffico.driver.PolicyGroupByActiveThreadsType;
  43. import org.openspcoop2.core.controllo_traffico.driver.PolicyNotFoundException;
  44. import org.openspcoop2.core.controllo_traffico.driver.PolicyShutdownException;
  45. import org.openspcoop2.pdd.config.ConfigurazionePdDManager;
  46. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  47. import org.openspcoop2.pdd.core.controllo_traffico.GestoreControlloTraffico;
  48. import org.openspcoop2.pdd.core.controllo_traffico.policy.GestoreCacheControlloTraffico;
  49. import org.openspcoop2.pdd.core.controllo_traffico.policy.driver.GestorePolicyAttive;
  50. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  51. import org.slf4j.Logger;


  52. /**
  53.  * Implementazione JMX per la gestione del Controllo del Traffico
  54.  *  
  55.  * @author Poli Andrea (apoli@link.it)
  56.  * @author $Author$
  57.  * @version $Rev$, $Date$
  58.  */
  59. public class ControlloTraffico extends NotificationBroadcasterSupport implements DynamicMBean {
  60.        
  61.     /** Attributi */
  62.     private String statoControlloMaxThreads;
  63.     public long maxThreads = 0;
  64.     public long activeThreads = 0;
  65.     public boolean pddCongestionata = false;

  66.     // cache informazioni statistiche
  67.     public boolean cacheAbilitata = false;
  68.    
  69.     /** getAttribute */
  70.     @Override
  71.     public Object getAttribute(String attributeName) throws AttributeNotFoundException,MBeanException,ReflectionException{
  72.         return _getAttribute(attributeName, true);
  73.     }
  74.     public Object _getAttribute(String attributeName, boolean refresh) throws AttributeNotFoundException,MBeanException,ReflectionException{
  75.        
  76.         if(refresh){
  77.             this.refreshDati();
  78.         }
  79.        
  80.         if( (attributeName==null) || (attributeName.equals("")) )
  81.             throw new IllegalArgumentException("Il nome dell'attributo e' nullo o vuoto");
  82.                
  83.         if(attributeName.equals(JMXConstants.CC_ATTRIBUTE_MAX_THREADS_STATO))
  84.             return this.statoControlloMaxThreads;
  85.        
  86.         if(attributeName.equals(JMXConstants.CC_ATTRIBUTE_MAX_THREADS_SOGLIA))
  87.             return this.maxThreads;
  88.        
  89.         if(attributeName.equals(JMXConstants.CC_ATTRIBUTE_ACTIVE_THREADS))
  90.             return this.activeThreads;
  91.        
  92.         if(attributeName.equals(JMXConstants.CC_ATTRIBUTE_PDD_CONGESTIONATA))
  93.             return this.pddCongestionata;
  94.                
  95.         if(attributeName.equals(JMXUtils.CACHE_ATTRIBUTE_ABILITATA))
  96.             return this.cacheAbilitata;
  97.        
  98.         throw new AttributeNotFoundException("Attributo "+attributeName+" non trovato");
  99.     }
  100.    
  101.     /** getAttributes */
  102.     @Override
  103.     public AttributeList getAttributes(String [] attributesNames){
  104.        
  105.         try{
  106.             this.refreshDati();
  107.         }catch(Exception e){
  108.             throw new RuntimeException(e.getMessage(),e);
  109.         }
  110.        
  111.         if(attributesNames==null)
  112.             throw new IllegalArgumentException("Array nullo");
  113.        
  114.         AttributeList list = new AttributeList();
  115.         for (int i=0; i<attributesNames.length; i++){
  116.             try{
  117.                 list.add(new Attribute(attributesNames[i],_getAttribute(attributesNames[i],false)));
  118.             }catch(JMException ex){
  119.                 // ignore
  120.             }
  121.         }
  122.         return list;
  123.     }
  124.    
  125.     /** setAttribute */
  126.     @Override
  127.     public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException{
  128.        
  129.         if( attribute==null )
  130.             throw new IllegalArgumentException("Il nome dell'attributo e' nullo");
  131.        
  132.         try{
  133.            
  134.             if(attribute.getName().equals(JMXConstants.CC_ATTRIBUTE_MAX_THREADS_STATO)){
  135.                 this.statoControlloMaxThreads = (String) attribute.getValue();
  136.             }
  137.            
  138.             if(attribute.getName().equals(JMXConstants.CC_ATTRIBUTE_MAX_THREADS_SOGLIA)){
  139.                 this.maxThreads = (Long) attribute.getValue();
  140.             }
  141.            
  142.             if(attribute.getName().equals(JMXConstants.CC_ATTRIBUTE_ACTIVE_THREADS)){
  143.                 this.activeThreads = (Long) attribute.getValue();
  144.             }
  145.                        
  146.             if(attribute.getName().equals(JMXConstants.CC_ATTRIBUTE_PDD_CONGESTIONATA)){
  147.                 this.pddCongestionata = (Boolean) attribute.getValue();
  148.             }
  149.            
  150.             if(attribute.getName().equals(JMXUtils.CACHE_ATTRIBUTE_ABILITATA)){
  151.                 boolean v = (Boolean) attribute.getValue();
  152.                 if(v){
  153.                     this.abilitaCache();
  154.                 }else{
  155.                     this.disabilitaCache();
  156.                 }
  157.             }
  158.            
  159.             else
  160.                 throw new AttributeNotFoundException("Attributo "+attribute.getName()+" non trovato");
  161.            
  162.         }catch(ClassCastException ce){
  163.             throw new InvalidAttributeValueException("il tipo "+attribute.getValue().getClass()+" dell'attributo "+attribute.getName()+" non e' valido");
  164.         }catch(JMException j){
  165.             throw new MBeanException(j);
  166.         }
  167.        
  168.     }
  169.    
  170.     /** setAttributes */
  171.     @Override
  172.     public AttributeList setAttributes(AttributeList list){
  173.        
  174.         if(list==null)
  175.             throw new IllegalArgumentException("Lista degli attributi e' nulla");
  176.        
  177.         AttributeList ret = new AttributeList();
  178.         Iterator<?> it = ret.iterator();
  179.        
  180.         while(it.hasNext()){
  181.             try{
  182.                 Attribute attribute = (Attribute) it.next();
  183.                 setAttribute(attribute);
  184.                 ret.add(attribute);
  185.             }catch(JMException ex){
  186.                 // ignore
  187.             }
  188.         }
  189.        
  190.         return ret;
  191.        
  192.     }
  193.    
  194.     /** invoke */
  195.     @Override
  196.     public Object invoke(String actionName, Object[]params, String[]signature) throws MBeanException,ReflectionException{
  197.        
  198.         if( (actionName==null) || (actionName.equals("")) )
  199.             throw new IllegalArgumentException("Nessuna operazione definita");
  200.        
  201.        
  202.         // Policy
  203.        
  204.         if(actionName.equals(JMXConstants.CC_METHOD_NAME_GET_ALL_ID_POLICY)){
  205.             return this.getAllIdPolicy();
  206.         }
  207.        
  208.         if(actionName.equals(JMXConstants.CC_METHOD_NAME_GET_STATO_POLICY)){
  209.             String param1 = null;
  210.             if(params[0]!=null && !"".equals(params[0])){
  211.                 param1 = (String)params[0];
  212.             }
  213.             return this.getStatoPolicy(param1);
  214.         }
  215.        
  216.         if(actionName.equals(JMXConstants.CC_METHOD_NAME_REMOVE_ALL_POLICY)){
  217.             return this.removeAllPolicies();
  218.         }
  219.        
  220.         if(actionName.equals(JMXConstants.CC_METHOD_NAME_RESET_ALL_POLICY_COUNTERS)){
  221.             return this.resetAllPolicies();
  222.         }
  223.        
  224.         if(actionName.equals(JMXConstants.CC_METHOD_NAME_REMOVE_POLICY)){
  225.             String param1 = null;
  226.             if(params[0]!=null && !"".equals(params[0])){
  227.                 param1 = (String)params[0];
  228.             }
  229.             return this.removePolicy(param1);
  230.         }
  231.        
  232.         if(actionName.equals(JMXConstants.CC_METHOD_NAME_RESET_POLICY_COUNTERS)){
  233.             String param1 = null;
  234.             if(params[0]!=null && !"".equals(params[0])){
  235.                 param1 = (String)params[0];
  236.             }
  237.             return this.resetPolicy(param1);
  238.         }
  239.        
  240.        
  241.         // Cache Controllo del Traffico
  242.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_RESET)){
  243.             return this.resetCache();
  244.         }
  245.        
  246.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_PRINT_STATS)){
  247.             return this.printStatCache();
  248.         }
  249.                
  250.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_DISABILITA)){
  251.             return this.disabilitaCacheConEsito();
  252.         }
  253.                
  254.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_ABILITA)){
  255.             if(params.length != 4)
  256.                 throw new MBeanException(new Exception("[AbilitaCache] Lunghezza parametri non corretta: "+params.length));
  257.            
  258.             Long param1 = null;
  259.             if(params[0]!=null && !"".equals(params[0])){
  260.                 param1 = (Long)params[0];
  261.                 if(param1<0){
  262.                     param1 = null;
  263.                 }
  264.             }
  265.            
  266.             Boolean param2 = null;
  267.             if(params[1]!=null && !"".equals(params[1])){
  268.                 param2 = (Boolean)params[1];
  269.             }
  270.            
  271.             Long param3 = null;
  272.             if(params[2]!=null && !"".equals(params[2])){
  273.                 param3 = (Long)params[2];
  274.                 if(param3<0){
  275.                     param3 = null;
  276.                 }
  277.             }
  278.            
  279.             Long param4 = null;
  280.             if(params[3]!=null && !"".equals(params[3])){
  281.                 param4 = (Long)params[3];
  282.                 if(param4<0){
  283.                     param4 = null;
  284.                 }
  285.             }
  286.                    
  287.             return this.abilitaCache(param1, param2, param3, param4 );
  288.         }
  289.        
  290.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_LIST_KEYS)){
  291.             return this.listKeysCache();
  292.         }
  293.        
  294.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_GET_OBJECT)){
  295.            
  296.             if(params.length != 1)
  297.                 throw new MBeanException(new Exception("["+JMXUtils.CACHE_METHOD_NAME_GET_OBJECT+"] Lunghezza parametri non corretta: "+params.length));
  298.            
  299.             String param1 = null;
  300.             if(params[0]!=null && !"".equals(params[0])){
  301.                 param1 = (String)params[0];
  302.             }
  303.            
  304.             return this.getObjectCache(param1);
  305.         }
  306.        
  307.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_REMOVE_OBJECT)){
  308.            
  309.             if(params.length != 1)
  310.                 throw new MBeanException(new Exception("["+JMXUtils.CACHE_METHOD_NAME_REMOVE_OBJECT+"] Lunghezza parametri non corretta: "+params.length));
  311.            
  312.             String param1 = null;
  313.             if(params[0]!=null && !"".equals(params[0])){
  314.                 param1 = (String)params[0];
  315.             }
  316.            
  317.             return this.removeObjectCache(param1);
  318.         }
  319.                
  320.         throw new UnsupportedOperationException("Operazione "+actionName+" sconosciuta");
  321.     }
  322.    
  323.     /* MBean info */
  324.     @Override
  325.     public MBeanInfo getMBeanInfo(){
  326.                
  327.         // Descrizione della classe nel MBean
  328.         String className = this.getClass().getName();
  329.         String description = "Risorsa per la gestione del Controllo del Traffico ("+OpenSPCoop2Properties.getInstance().getVersione()+")";
  330.        
  331.         // *** Attributi ***
  332.        
  333.         // MetaData per l'attributo maxThreads (stato)
  334.         MBeanAttributeInfo maxThreadsStatoVAR = new MBeanAttributeInfo(JMXConstants.CC_ATTRIBUTE_MAX_THREADS_STATO,String.class.getName(),
  335.                         "Stato del controllo sul numero massimo di threads attivi",
  336.                         JMXUtils.JMX_ATTRIBUTE_READABLE,!JMXUtils.JMX_ATTRIBUTE_WRITABLE,!JMXUtils.JMX_ATTRIBUTE_IS_GETTER);
  337.        
  338.         // MetaData per l'attributo maxThreads (soglia)
  339.         MBeanAttributeInfo maxThreadsSogliaVAR = new MBeanAttributeInfo(JMXConstants.CC_ATTRIBUTE_MAX_THREADS_SOGLIA,long.class.getName(),
  340.                         "Numero massimo di threads attivi",
  341.                         JMXUtils.JMX_ATTRIBUTE_READABLE,!JMXUtils.JMX_ATTRIBUTE_WRITABLE,!JMXUtils.JMX_ATTRIBUTE_IS_GETTER);
  342.        
  343.         // MetaData per l'attributo activeThreads
  344.         MBeanAttributeInfo activeThreadsVAR = new MBeanAttributeInfo(JMXConstants.CC_ATTRIBUTE_ACTIVE_THREADS,long.class.getName(),
  345.                         "Numero di threads attivi",
  346.                         JMXUtils.JMX_ATTRIBUTE_READABLE,!JMXUtils.JMX_ATTRIBUTE_WRITABLE,!JMXUtils.JMX_ATTRIBUTE_IS_GETTER);
  347.        
  348.         // MetaData per l'attributo pddCongestionata
  349.         MBeanAttributeInfo pddCongestionataVAR = new MBeanAttributeInfo(JMXConstants.CC_ATTRIBUTE_PDD_CONGESTIONATA,boolean.class.getName(),
  350.                         "Indicazione se la PdD risulta congestionata dalle richieste",
  351.                         JMXUtils.JMX_ATTRIBUTE_READABLE,!JMXUtils.JMX_ATTRIBUTE_WRITABLE,!JMXUtils.JMX_ATTRIBUTE_IS_GETTER);
  352.        
  353.        
  354.         // MetaData per l'attributo abilitaCache
  355.         MBeanAttributeInfo cacheAbilitataVAR = JMXUtils.MBEAN_ATTRIBUTE_INFO_CACHE_ABILITATA;
  356.        
  357.         // *** Operazioni ***
  358.        
  359.         // MetaData per l'operazione  getAllIdPolicy
  360.         MBeanOperationInfo getAllIdPolicyOP = new MBeanOperationInfo(JMXConstants.CC_METHOD_NAME_GET_ALL_ID_POLICY,
  361.                 "Ritorna gli identificativi delle policy attivate tramite Rate Limiting",
  362.                 null,
  363.                 //new MBeanParameterInfo[]{new MBeanParameterInfo("param",String.class.getName())}
  364.                 String.class.getName(),
  365.                 MBeanOperationInfo.ACTION);
  366.        
  367.         // MetaData per l'operazione  getStatoThreadsAttiviPortaDelegata
  368.         MBeanOperationInfo getStatoPolicyOP = new MBeanOperationInfo(JMXConstants.CC_METHOD_NAME_GET_STATO_POLICY,
  369.                 "Ritorna lo stato della policy identificata dal parametro",
  370.                 new MBeanParameterInfo[]{new MBeanParameterInfo("id",String.class.getName(),"identificativo della policy")},
  371.                 String.class.getName(),
  372.                 MBeanOperationInfo.ACTION);
  373.        
  374.         // MetaData per l'operazione  removeAllPolicies
  375.         MBeanOperationInfo removeAllPoliciesOP = new MBeanOperationInfo(JMXConstants.CC_METHOD_NAME_REMOVE_ALL_POLICY,
  376.                 "Elimina le istanza attive di tutte le policy",
  377.                 null,
  378.                 //new MBeanParameterInfo[]{new MBeanParameterInfo("param",String.class.getName())}
  379.                 String.class.getName(),
  380.                 MBeanOperationInfo.ACTION);
  381.        
  382.         // MetaData per l'operazione  removePolicy
  383.         MBeanOperationInfo removePolicyOP = new MBeanOperationInfo(JMXConstants.CC_METHOD_NAME_REMOVE_POLICY,
  384.                 "Elimina l'istanza attiva della policy di Rate Limiting identificata dal parametro",
  385.                 new MBeanParameterInfo[]{new MBeanParameterInfo("id",String.class.getName(),"identificativo della policy")},
  386.                 String.class.getName(),
  387.                 MBeanOperationInfo.ACTION);
  388.        
  389.         // MetaData per l'operazione  removeAllPolicies
  390.         MBeanOperationInfo resetAllPoliciesOP = new MBeanOperationInfo(JMXConstants.CC_METHOD_NAME_RESET_ALL_POLICY_COUNTERS,
  391.                 "Effettua i reset dei contatori di tutte le policy attivate tramite Rate Limiting",
  392.                 null,
  393.                 //new MBeanParameterInfo[]{new MBeanParameterInfo("param",String.class.getName())}
  394.                 String.class.getName(),
  395.                 MBeanOperationInfo.ACTION);
  396.        
  397.         // MetaData per l'operazione  removePolicy
  398.         MBeanOperationInfo resetPolicyOP = new MBeanOperationInfo(JMXConstants.CC_METHOD_NAME_RESET_POLICY_COUNTERS,
  399.                 "Effettua i reset dei contatori presenti nella policy di Rate Limiting identificata dal parametro",
  400.                 new MBeanParameterInfo[]{new MBeanParameterInfo("id",String.class.getName(),"identificativo della policy")},
  401.                 String.class.getName(),
  402.                 MBeanOperationInfo.ACTION);
  403.        
  404.         // MetaData per l'operazione resetCache
  405.         MBeanOperationInfo resetCacheOP = JMXUtils.MBEAN_OPERATION_RESET_CACHE;
  406.        
  407.         // MetaData per l'operazione printStatCache
  408.         MBeanOperationInfo printStatCacheOP = JMXUtils.MBEAN_OPERATION_PRINT_STATS_CACHE;
  409.        
  410.         // MetaData per l'operazione disabilitaCache
  411.         MBeanOperationInfo disabilitaCacheOP = JMXUtils.MBEAN_OPERATION_DISABILITA_CACHE;
  412.        
  413.         // MetaData per l'operazione abilitaCache con parametri
  414.         MBeanOperationInfo abilitaCacheParametriOP = JMXUtils.MBEAN_OPERATION_ABILITA_CACHE_CON_PARAMETRI;
  415.        
  416.         // MetaData per l'operazione listKeysCache
  417.         MBeanOperationInfo listKeysCacheOP = JMXUtils.MBEAN_OPERATION_LIST_KEYS_CACHE;

  418.         // MetaData per l'operazione getObjectCache
  419.         MBeanOperationInfo getObjectCacheOP = JMXUtils.MBEAN_OPERATION_GET_OBJECT_CACHE;
  420.        
  421.         // MetaData per l'operazione removeObjectCache
  422.         MBeanOperationInfo removeObjectCacheOP = JMXUtils.MBEAN_OPERATION_REMOVE_OBJECT_CACHE;
  423.        
  424.        
  425.         // *** Costruttore ***
  426.        
  427.         // Mbean costruttore
  428.         MBeanConstructorInfo defaultConstructor = new MBeanConstructorInfo("Default Constructor","Crea e inizializza una nuova istanza del MBean",null);

  429.         // Lista attributi
  430.         MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[]{maxThreadsStatoVAR, maxThreadsSogliaVAR, activeThreadsVAR,
  431.                 pddCongestionataVAR,
  432.                 cacheAbilitataVAR};
  433.        
  434.         // Lista Costruttori
  435.         MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[]{defaultConstructor};
  436.        
  437.         // Lista operazioni
  438.         MBeanOperationInfo[] operations = new MBeanOperationInfo[]{
  439.                 getAllIdPolicyOP, getStatoPolicyOP, removeAllPoliciesOP, removePolicyOP, resetAllPoliciesOP, resetPolicyOP,
  440.                 resetCacheOP,printStatCacheOP,disabilitaCacheOP,abilitaCacheParametriOP,listKeysCacheOP,getObjectCacheOP,removeObjectCacheOP};
  441.        
  442.         return new MBeanInfo(className,description,attributes,constructors,operations,null);
  443.     }
  444.    
  445.     /* Variabili per la gestione JMX */
  446.     private Logger log;
  447.     private GestoreControlloTraffico gestoreControlloTraffico;
  448.    
  449.     /* Costruttore */
  450.     public ControlloTraffico() throws Exception{

  451.         boolean force = true;
  452.         this.log = OpenSPCoop2Logger.getLoggerOpenSPCoopControlloTraffico(force);  // per gli errori
  453.        
  454.         this.refreshDati();
  455.        
  456.         this.cacheAbilitata = GestoreCacheControlloTraffico.isCacheAbilitata();
  457.            
  458.        
  459.     }
  460.    
  461.     private void refreshDati() throws MBeanException{
  462.         ConfigurazioneGenerale configurazioneGenerale = null;
  463.         try{
  464.        
  465.             ConfigurazionePdDManager configPdDManager = ConfigurazionePdDManager.getInstance();
  466.             configurazioneGenerale = configPdDManager.getConfigurazioneControlloTraffico(null);
  467.            
  468.             if(configurazioneGenerale.getControlloTraffico().isControlloMaxThreadsEnabled()) {
  469.                 if(configurazioneGenerale.getControlloTraffico().isControlloMaxThreadsWarningOnly()) {
  470.                     this.statoControlloMaxThreads = CostantiConfigurazione.STATO_CON_WARNING_WARNING_ONLY.getValue();
  471.                 }
  472.                 else {
  473.                     this.statoControlloMaxThreads = CostantiConfigurazione.STATO_CON_WARNING_ABILITATO.getValue();
  474.                 }
  475.             }
  476.             else {
  477.                 this.statoControlloMaxThreads = CostantiConfigurazione.STATO_CON_WARNING_DISABILITATO.getValue();
  478.             }
  479.            
  480.             this.maxThreads = configurazioneGenerale.getControlloTraffico().getControlloMaxThreadsSoglia();
  481.        
  482.             this.gestoreControlloTraffico = GestoreControlloTraffico.getInstance();
  483.             this.activeThreads = this.gestoreControlloTraffico.sizeActiveThreads();
  484.            
  485.             this.pddCongestionata = this.gestoreControlloTraffico.isPortaDominioCongestionata(this.maxThreads,configurazioneGenerale.getControlloTraffico().getControlloCongestioneThreshold());
  486.            
  487.         }catch(Exception e){
  488.             try{
  489.                 this.log.error("Configurazione non disponibile: "+e.getMessage(),e);
  490.             }catch(Exception eLog){
  491.                 // ignore
  492.             }
  493.             throw new MBeanException(e, "Configurazione non disponibile: "+e.getMessage());
  494.         }
  495.     }
  496.    
  497.    
  498.     public boolean isCacheAbilitata() {
  499.         return this.cacheAbilitata;
  500.     }
  501.    
  502.     /* Metodi di management JMX */

  503.     public String getAllIdPolicy(){
  504.         try{
  505.             List<PolicyGroupByActiveThreadsType> tipiGestorePolicyAttivi = GestorePolicyAttive.getTipiGestoriAttivi();
  506.             if(tipiGestorePolicyAttivi==null || tipiGestorePolicyAttivi.isEmpty()) {
  507.                 throw new Exception("GestorePolicyAttive non abilitato");
  508.             }
  509.             StringBuilder sb = new StringBuilder();
  510.             for (PolicyGroupByActiveThreadsType type : tipiGestorePolicyAttivi) {
  511.                 String p = GestorePolicyAttive.getInstance(type).printKeysPolicy("\n");
  512.                 if(sb.length()>0) {
  513.                     sb.append("\n");
  514.                 }
  515.                 sb.append(p);
  516.             }
  517.             return sb.toString();
  518.            
  519.         }catch(PolicyShutdownException notFound){
  520.             return "GestorePolicyAttive in shutdown; controllare lo stato di GovWay";
  521.         }catch(Throwable e){
  522.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  523.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  524.         }
  525.     }
  526.    
  527.     public String getStatoPolicy(String id){
  528.         try{
  529.             List<PolicyGroupByActiveThreadsType> tipiGestorePolicyAttivi = GestorePolicyAttive.getTipiGestoriAttivi();
  530.             if(tipiGestorePolicyAttivi==null || tipiGestorePolicyAttivi.isEmpty()) {
  531.                 throw new Exception("GestorePolicyAttive non abilitato");
  532.             }
  533.             if(id==null)
  534.                 throw new Exception("Parametro non fornito");
  535.             try{
  536.                 Map<PolicyGroupByActiveThreadsType, String> infos = new HashMap<PolicyGroupByActiveThreadsType, String>();
  537.                 PolicyNotFoundException notFound = null;
  538.                 for (PolicyGroupByActiveThreadsType type : tipiGestorePolicyAttivi) {
  539.                     try {
  540.                         infos.put(type, GestorePolicyAttive.getInstance(type).printInfoPolicy(id, "================================================================"));        
  541.                     }catch(PolicyNotFoundException notFoundE){
  542.                         notFound = notFoundE;
  543.                     }
  544.                 }
  545.                 if(infos.isEmpty()) {
  546.                     if(notFound!=null) {
  547.                         throw notFound;
  548.                     }
  549.                     else {
  550.                         throw new PolicyNotFoundException();
  551.                     }
  552.                 }
  553.                 else if(infos.size()==1) {
  554.                     return infos.get(infos.keySet().iterator().next());
  555.                 }
  556.                 else {
  557.                     StringBuilder sb = new StringBuilder();
  558.                     for (PolicyGroupByActiveThreadsType policyGroupByActiveThreadsType : infos.keySet()) {
  559.                         if(sb.length()>0) {
  560.                             sb.append("\n\n");
  561.                         }
  562.                         sb.append("*** Gestore '").append(policyGroupByActiveThreadsType.name()).append("' ***\n");
  563.                         sb.append(infos.get(policyGroupByActiveThreadsType));
  564.                     }
  565.                     return sb.toString();
  566.                 }
  567.             }catch(PolicyNotFoundException notFound){
  568.                 return "Informazioni sulla Policy non disponibili; non sono ancora transitate richieste che soddisfano i criteri di filtro impostati";
  569.             }catch(PolicyShutdownException notFound){
  570.                 return "GestorePolicyAttive in shutdown; controllare lo stato di GovWay";
  571.             }
  572.         }catch(Throwable e){
  573.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  574.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  575.         }
  576.     }
  577.    
  578.     public String removeAllPolicies(){
  579.         try{
  580.             List<PolicyGroupByActiveThreadsType> tipiGestorePolicyAttivi = GestorePolicyAttive.getTipiGestoriAttivi();
  581.             if(tipiGestorePolicyAttivi==null || tipiGestorePolicyAttivi.isEmpty()) {
  582.                 throw new Exception("GestorePolicyAttive non abilitato");
  583.             }
  584.             for (PolicyGroupByActiveThreadsType type : tipiGestorePolicyAttivi) {
  585.                 GestorePolicyAttive.getInstance(type).removeAllActiveThreadsPolicy();
  586.             }
  587.             return "Operazione 'remove' effettuata con successo";
  588.         }catch(PolicyShutdownException notFound){
  589.             return "GestorePolicyAttive in shutdown; controllare lo stato di GovWay";
  590.         }catch(Throwable e){
  591.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  592.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  593.         }
  594.     }
  595.    
  596.     public String resetAllPolicies(){
  597.         try{
  598.             List<PolicyGroupByActiveThreadsType> tipiGestorePolicyAttivi = GestorePolicyAttive.getTipiGestoriAttivi();
  599.             if(tipiGestorePolicyAttivi==null || tipiGestorePolicyAttivi.isEmpty()) {
  600.                 throw new Exception("GestorePolicyAttive non abilitato");
  601.             }
  602.             for (PolicyGroupByActiveThreadsType type : tipiGestorePolicyAttivi) {
  603.                 GestorePolicyAttive.getInstance(type).resetCountersAllActiveThreadsPolicy();
  604.             }
  605.             return JMXUtils.MSG_RESET_CACHE_EFFETTUATO_SUCCESSO;
  606.         }catch(PolicyShutdownException notFound){
  607.             return "GestorePolicyAttive in shutdown; controllare lo stato di GovWay";
  608.         }catch(Throwable e){
  609.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  610.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  611.         }
  612.     }
  613.    
  614.     public String removePolicy(String id){
  615.         try{
  616.             List<PolicyGroupByActiveThreadsType> tipiGestorePolicyAttivi = GestorePolicyAttive.getTipiGestoriAttivi();
  617.             if(tipiGestorePolicyAttivi==null || tipiGestorePolicyAttivi.isEmpty()) {
  618.                 throw new Exception("GestorePolicyAttive non abilitato");
  619.             }
  620.             if(id==null)
  621.                 throw new Exception("Parametro non fornito");
  622.             for (PolicyGroupByActiveThreadsType type : tipiGestorePolicyAttivi) {
  623.                 GestorePolicyAttive.getInstance(type).removeActiveThreadsPolicy(id);
  624.             }
  625.             return "Operazione 'remove' effettuata con successo";
  626.         }catch(PolicyShutdownException notFound){
  627.             return "GestorePolicyAttive in shutdown; controllare lo stato di GovWay";
  628.         }catch(Throwable e){
  629.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  630.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  631.         }
  632.     }
  633.    
  634.     public String resetPolicy(String id){
  635.         try{
  636.             List<PolicyGroupByActiveThreadsType> tipiGestorePolicyAttivi = GestorePolicyAttive.getTipiGestoriAttivi();
  637.             if(tipiGestorePolicyAttivi==null || tipiGestorePolicyAttivi.isEmpty()) {
  638.                 throw new Exception("GestorePolicyAttive non abilitato");
  639.             }
  640.             if(id==null)
  641.                 throw new Exception("Parametro non fornito");
  642.             for (PolicyGroupByActiveThreadsType type : tipiGestorePolicyAttivi) {
  643.                 GestorePolicyAttive.getInstance(type).resetCountersActiveThreadsPolicy(id);
  644.             }
  645.             return JMXUtils.MSG_RESET_CACHE_EFFETTUATO_SUCCESSO;
  646.         }catch(PolicyShutdownException notFound){
  647.             return "GestorePolicyAttive in shutdown; controllare lo stato di GovWay";
  648.         }catch(Throwable e){
  649.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  650.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  651.         }
  652.     }
  653.    
  654.     public String resetCache(){
  655.         try{
  656.             if(this.cacheAbilitata==false)
  657.                 throw new Exception("Cache non abilitata");
  658.             GestoreCacheControlloTraffico.resetCache();
  659.             return JMXUtils.MSG_RESET_CACHE_EFFETTUATO_SUCCESSO;
  660.         }catch(Throwable e){
  661.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  662.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  663.         }
  664.     }
  665.    
  666.     public String printStatCache(){
  667.         try{
  668.             if(this.cacheAbilitata==false)
  669.                 throw new Exception("Cache non abilitata");
  670.             return GestoreCacheControlloTraffico.printStatsCache("\n");
  671.         }catch(Throwable e){
  672.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  673.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  674.         }
  675.     }
  676.    
  677.     public void abilitaCache() throws JMException{
  678.         try{
  679.             GestoreCacheControlloTraffico.abilitaCache();
  680.             this.cacheAbilitata = true;
  681.         }catch(Throwable e){
  682.             this.log.error(e.getMessage(),e);
  683.         }
  684.     }
  685.    
  686.     public String abilitaCache(Long dimensioneCache,Boolean algoritmoCacheLRU,Long itemIdleTime,Long itemLifeSecond){
  687.         try{
  688.             GestoreCacheControlloTraffico.abilitaCache(dimensioneCache,algoritmoCacheLRU,itemIdleTime,itemLifeSecond,this.log);
  689.             this.cacheAbilitata = true;
  690.             return JMXUtils.MSG_ABILITAZIONE_CACHE_EFFETTUATA;
  691.         }catch(Throwable e){
  692.             this.log.error(e.getMessage(),e);
  693.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  694.         }
  695.     }
  696.        
  697.    
  698.     public void disabilitaCache() throws JMException{
  699.         try{
  700.             GestoreCacheControlloTraffico.disabilitaCache();
  701.             this.cacheAbilitata = false;
  702.         }catch(Throwable e){
  703.             this.log.error(e.getMessage(),e);
  704.             throw new JMException(e.getMessage());
  705.         }
  706.     }
  707.     public String disabilitaCacheConEsito() {
  708.         try{
  709.             disabilitaCache();
  710.             return JMXUtils.MSG_DISABILITAZIONE_CACHE_EFFETTUATA;
  711.         }catch(Throwable e){
  712.             this.log.error(e.getMessage(),e);
  713.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  714.         }
  715.     }
  716.    
  717.     public String listKeysCache(){
  718.         try{
  719.             if(this.cacheAbilitata==false)
  720.                 throw new Exception("Cache non abilitata");
  721.             return GestoreCacheControlloTraffico.listKeysCache("\n");
  722.         }catch(Throwable e){
  723.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  724.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  725.         }
  726.     }
  727.    
  728.     public String getObjectCache(String key){
  729.         try{
  730.             if(this.cacheAbilitata==false)
  731.                 throw new Exception("Cache non abilitata");
  732.             return GestoreCacheControlloTraffico.getObjectCache(key);
  733.         }catch(Throwable e){
  734.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  735.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  736.         }
  737.     }
  738.    
  739.     public String removeObjectCache(String key){
  740.         try{
  741.             if(this.cacheAbilitata==false)
  742.                 throw new Exception("Cache non abilitata");
  743.             GestoreCacheControlloTraffico.removeObjectCache(key);
  744.             return JMXUtils.MSG_RIMOZIONE_CACHE_EFFETTUATA;
  745.         }catch(Throwable e){
  746.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  747.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  748.         }
  749.     }

  750. }