GestoreRichieste.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.Iterator;

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

  36. import org.slf4j.Logger;
  37. import org.openspcoop2.core.constants.TipoPdD;
  38. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  39. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;


  40. /**
  41.  * Implementazione JMX per la gestione dei dati della richiesta
  42.  *  
  43.  * @author Poli Andrea (apoli@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  */
  47. public class GestoreRichieste extends NotificationBroadcasterSupport implements DynamicMBean {

  48.     public static final String CACHE_METHOD_NAME_REMOVE_DATI_CONTROLLO_TRAFFICO_GLOBALE = "removeRateLimitingGlobalConfigCache";
  49.     public static final String CACHE_METHOD_NAME_REMOVE_DATI_CONTROLLO_TRAFFICO_API = "removeRateLimitingAPIConfigCache";
  50.    
  51.     /** Attributi */
  52.     private boolean cacheAbilitata = false;
  53.    
  54.     /** getAttribute */
  55.     @Override
  56.     public Object getAttribute(String attributeName) throws AttributeNotFoundException,MBeanException,ReflectionException{
  57.        
  58.         if( (attributeName==null) || (attributeName.equals("")) )
  59.             throw new IllegalArgumentException("Il nome dell'attributo e' nullo o vuoto");
  60.        
  61.         if(attributeName.equals(JMXUtils.CACHE_ATTRIBUTE_ABILITATA))
  62.             return this.cacheAbilitata;
  63.        
  64.         throw new AttributeNotFoundException("Attributo "+attributeName+" non trovato");
  65.     }
  66.    
  67.     /** getAttributes */
  68.     @Override
  69.     public AttributeList getAttributes(String [] attributesNames){
  70.        
  71.         if(attributesNames==null)
  72.             throw new IllegalArgumentException("Array nullo");
  73.        
  74.         AttributeList list = new AttributeList();
  75.         for (int i=0; i<attributesNames.length; i++){
  76.             try{
  77.                 list.add(new Attribute(attributesNames[i],getAttribute(attributesNames[i])));
  78.             }catch(JMException ex){}
  79.         }
  80.         return list;
  81.     }
  82.    
  83.     /** setAttribute */
  84.     @Override
  85.     public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException{
  86.        
  87.         if( attribute==null )
  88.             throw new IllegalArgumentException("Il nome dell'attributo e' nullo");
  89.        
  90.         try{
  91.            
  92.             if(attribute.getName().equals(JMXUtils.CACHE_ATTRIBUTE_ABILITATA)){
  93.                 boolean v = (Boolean) attribute.getValue();
  94.                 if(v){
  95.                     // la cache DEVE essere abilitata
  96.                     if(!this.cacheAbilitata){
  97.                         this.abilitaCache();
  98.                     }
  99.                 }
  100.                 else{
  101.                     // la cache DEVE essere disabilitata
  102.                     if(this.cacheAbilitata){
  103.                         this.disabilitaCache();
  104.                     }
  105.                 }
  106.             }
  107.            
  108.             else
  109.                 throw new AttributeNotFoundException("Attributo "+attribute.getName()+" non trovato");
  110.            
  111.         }catch(ClassCastException ce){
  112.             throw new InvalidAttributeValueException("il tipo "+attribute.getValue().getClass()+" dell'attributo "+attribute.getName()+" non e' valido");
  113.         }catch(JMException j){
  114.             throw new MBeanException(j);
  115.         }
  116.        
  117.     }
  118.    
  119.     /** setAttributes */
  120.     @Override
  121.     public AttributeList setAttributes(AttributeList list){
  122.        
  123.         if(list==null)
  124.             throw new IllegalArgumentException("Lista degli attributi e' nulla");
  125.        
  126.         AttributeList ret = new AttributeList();
  127.         Iterator<?> it = ret.iterator();
  128.        
  129.         while(it.hasNext()){
  130.             try{
  131.                 Attribute attribute = (Attribute) it.next();
  132.                 setAttribute(attribute);
  133.                 ret.add(attribute);
  134.             }catch(JMException ex){}
  135.         }
  136.        
  137.         return ret;
  138.        
  139.     }
  140.    
  141.     /** invoke */
  142.     @Override
  143.     public Object invoke(String actionName, Object[]params, String[]signature) throws MBeanException,ReflectionException{
  144.        
  145.         if( (actionName==null) || (actionName.equals("")) )
  146.             throw new IllegalArgumentException("Nessuna operazione definita");
  147.        
  148.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_RESET)){
  149.             return this.resetCache();
  150.         }
  151.        
  152.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_PRINT_STATS)){
  153.             return this.printStatCache();
  154.         }
  155.        
  156.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_DISABILITA)){
  157.             return this.disabilitaCacheConEsito();
  158.         }
  159.        
  160.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_ABILITA)){
  161.             if(params.length != 4)
  162.                 throw new MBeanException(new Exception("[AbilitaCache] Lunghezza parametri non corretta: "+params.length));
  163.            
  164.             Long param1 = null;
  165.             if(params[0]!=null && !"".equals(params[0])){
  166.                 param1 = (Long)params[0];
  167.                 if(param1<0){
  168.                     param1 = null;
  169.                 }
  170.             }
  171.            
  172.             Boolean param2 = null;
  173.             if(params[1]!=null && !"".equals(params[1])){
  174.                 param2 = (Boolean)params[1];
  175.             }
  176.            
  177.             Long param3 = null;
  178.             if(params[2]!=null && !"".equals(params[2])){
  179.                 param3 = (Long)params[2];
  180.                 if(param3<0){
  181.                     param3 = null;
  182.                 }
  183.             }
  184.            
  185.             Long param4 = null;
  186.             if(params[3]!=null && !"".equals(params[3])){
  187.                 param4 = (Long)params[3];
  188.                 if(param4<0){
  189.                     param4 = null;
  190.                 }
  191.             }
  192.                    
  193.             return this.abilitaCache(param1, param2, param3, param4 );
  194.         }
  195.        
  196.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_LIST_KEYS)){
  197.             return this.listKeysCache();
  198.         }
  199.        
  200.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_GET_OBJECT)){
  201.            
  202.             if(params.length != 1)
  203.                 throw new MBeanException(new Exception("["+JMXUtils.CACHE_METHOD_NAME_GET_OBJECT+"] Lunghezza parametri non corretta: "+params.length));
  204.            
  205.             String param1 = null;
  206.             if(params[0]!=null && !"".equals(params[0])){
  207.                 param1 = (String)params[0];
  208.             }
  209.            
  210.             return this.getObjectCache(param1);
  211.         }
  212.        
  213.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_REMOVE_OBJECT)){
  214.            
  215.             if(params.length != 1)
  216.                 throw new MBeanException(new Exception("["+JMXUtils.CACHE_METHOD_NAME_REMOVE_OBJECT+"] Lunghezza parametri non corretta: "+params.length));
  217.            
  218.             String param1 = null;
  219.             if(params[0]!=null && !"".equals(params[0])){
  220.                 param1 = (String)params[0];
  221.             }
  222.            
  223.             return this.removeObjectCache(param1);
  224.         }
  225.        
  226.         if(actionName.equals(CACHE_METHOD_NAME_REMOVE_DATI_CONTROLLO_TRAFFICO_GLOBALE)){
  227.             return this.removeRateLimitingGlobalConfigCache();
  228.         }
  229.        
  230.         if(actionName.equals(CACHE_METHOD_NAME_REMOVE_DATI_CONTROLLO_TRAFFICO_API)){
  231.             if(params.length != 2)
  232.                 throw new MBeanException(new Exception("["+CACHE_METHOD_NAME_REMOVE_DATI_CONTROLLO_TRAFFICO_API+"] Lunghezza parametri non corretta: "+params.length));
  233.            
  234.             TipoPdD param1 = null;
  235.             if(params[0]!=null && !"".equals(params[0])){
  236.                 String param1_tipo = (String)params[0];
  237.                 param1 = TipoPdD.toTipoPdD(param1_tipo);
  238.                 if(param1==null) {
  239.                     try {
  240.                         param1 = TipoPdD.valueOf(param1_tipo);
  241.                     }catch(Throwable t) {}
  242.                 }
  243.                 if(param1==null || (!TipoPdD.APPLICATIVA.equals(param1) && !TipoPdD.DELEGATA.equals(param1))) {
  244.                     throw new MBeanException(new Exception("["+CACHE_METHOD_NAME_REMOVE_DATI_CONTROLLO_TRAFFICO_API+"] Primo parametro ("+param1_tipo+") non corretto; valori ammessi: "+
  245.                             TipoPdD.APPLICATIVA.getTipo()+","+TipoPdD.DELEGATA.getTipo()));
  246.                 }
  247.             }
  248.            
  249.             String param2 = null;
  250.             if(params[1]!=null && !"".equals(params[1])){
  251.                 param2 = (String)params[1];
  252.             }
  253.                    
  254.             return this.removeRateLimitingAPIConfigCache(param1, param2);
  255.         }
  256.        
  257.         throw new UnsupportedOperationException("Operazione "+actionName+" sconosciuta");
  258.     }
  259.    
  260.     /* MBean info */
  261.     @Override
  262.     public MBeanInfo getMBeanInfo(){
  263.        
  264.         // Descrizione della classe nel MBean
  265.         String className = this.getClass().getName();
  266.         String description = "Configurazione dei dati di autenticazione ai servizi esposti ("+OpenSPCoop2Properties.getInstance().getVersione()+")";
  267.        
  268.         // MetaData per l'attributo abilitaCache
  269.         MBeanAttributeInfo cacheAbilitataVAR = JMXUtils.MBEAN_ATTRIBUTE_INFO_CACHE_ABILITATA;
  270.        
  271.         // MetaData per l'operazione resetCache
  272.         MBeanOperationInfo resetCacheOP = JMXUtils.MBEAN_OPERATION_RESET_CACHE;
  273.        
  274.         // MetaData per l'operazione printStatCache
  275.         MBeanOperationInfo printStatCacheOP = JMXUtils.MBEAN_OPERATION_PRINT_STATS_CACHE;
  276.        
  277.         // MetaData per l'operazione disabilitaCache
  278.         MBeanOperationInfo disabilitaCacheOP = JMXUtils.MBEAN_OPERATION_DISABILITA_CACHE;
  279.        
  280.         // MetaData per l'operazione abilitaCache con parametri
  281.         MBeanOperationInfo abilitaCacheParametriOP = JMXUtils.MBEAN_OPERATION_ABILITA_CACHE_CON_PARAMETRI;
  282.        
  283.         // MetaData per l'operazione listKeysCache
  284.         MBeanOperationInfo listKeysCacheOP = JMXUtils.MBEAN_OPERATION_LIST_KEYS_CACHE;

  285.         // MetaData per l'operazione getObjectCache
  286.         MBeanOperationInfo getObjectCacheOP = JMXUtils.MBEAN_OPERATION_GET_OBJECT_CACHE;
  287.        
  288.         // MetaData per l'operazione removeObjectCache
  289.         MBeanOperationInfo removeObjectCacheOP = JMXUtils.MBEAN_OPERATION_REMOVE_OBJECT_CACHE;
  290.        
  291.         // MetaData per l'operazione removeRateLimitingGlobalConfigCache
  292.         MBeanOperationInfo removeRateLimitingGlobalConfigCache
  293.         = new MBeanOperationInfo(CACHE_METHOD_NAME_REMOVE_DATI_CONTROLLO_TRAFFICO_GLOBALE,"Rimuove le informazioni riguardanti policy di controllo del traffico globali",
  294.             null,
  295.             String.class.getName(),
  296.             MBeanOperationInfo.ACTION);
  297.        
  298.         // MetaData per l'operazione removeRateLimitingGlobalConfigCache
  299.         MBeanOperationInfo removeRateLimitingAPIConfigCache
  300.         = new MBeanOperationInfo(CACHE_METHOD_NAME_REMOVE_DATI_CONTROLLO_TRAFFICO_API,"Rimuove le informazioni riguardanti policy di controllo del traffico dell'erogazione/fruizione indicata",
  301.             new MBeanParameterInfo[]{
  302.                 new MBeanParameterInfo("tipoPdD",String.class.getName(),"Indicazione se il nome della porta indicato riguarda una porta delegata o applicativa"),
  303.                 new MBeanParameterInfo("nomePorta",String.class.getName(),"Nome porta"),
  304.             },
  305.             String.class.getName(),
  306.             MBeanOperationInfo.ACTION);
  307.        
  308.         // Mbean costruttore
  309.         MBeanConstructorInfo defaultConstructor = new MBeanConstructorInfo("Default Constructor","Crea e inizializza una nuova istanza del MBean",null);

  310.         // Lista attributi
  311.         MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[]{cacheAbilitataVAR};
  312.        
  313.         // Lista Costruttori
  314.         MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[]{defaultConstructor};
  315.        
  316.         // Lista operazioni
  317.         MBeanOperationInfo[] operations = new MBeanOperationInfo[]{resetCacheOP,printStatCacheOP,disabilitaCacheOP,abilitaCacheParametriOP,listKeysCacheOP,getObjectCacheOP,removeObjectCacheOP,
  318.                 removeRateLimitingGlobalConfigCache, removeRateLimitingAPIConfigCache};
  319.        
  320.         return new MBeanInfo(className,description,attributes,constructors,operations,null);
  321.     }
  322.    
  323.     /* Variabili per la gestione JMX */
  324.     private Logger log;
  325.    
  326.     /* Costruttore */
  327.     public GestoreRichieste(){
  328.         this.log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  329.                
  330.         // Configurazione
  331.         this.cacheAbilitata = org.openspcoop2.pdd.core.GestoreRichieste.isCacheAbilitata();
  332.            
  333.     }
  334.    
  335.     public boolean isCacheAbilitata() {
  336.         return this.cacheAbilitata;
  337.     }
  338.    
  339.     /* Metodi di management JMX */
  340.     public String resetCache(){
  341.         try{
  342.             if(this.cacheAbilitata==false)
  343.                 throw new Exception("Cache non abilitata");
  344.             org.openspcoop2.pdd.core.GestoreRichieste.resetCache();
  345.             return JMXUtils.MSG_RESET_CACHE_EFFETTUATO_SUCCESSO;
  346.         }catch(Throwable e){
  347.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  348.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  349.         }
  350.     }
  351.    
  352.     public String printStatCache(){
  353.         try{
  354.             if(this.cacheAbilitata==false)
  355.                 throw new Exception("Cache non abilitata");
  356.             return org.openspcoop2.pdd.core.GestoreRichieste.printStatsCache("\n");
  357.         }catch(Throwable e){
  358.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  359.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  360.         }
  361.     }
  362.    
  363.     public String abilitaCache(){
  364.         try{
  365.             org.openspcoop2.pdd.core.GestoreRichieste.abilitaCache();
  366.             this.cacheAbilitata = true;
  367.             return JMXUtils.MSG_ABILITAZIONE_CACHE_EFFETTUATA;
  368.         }catch(Throwable e){
  369.             this.log.error(e.getMessage(),e);
  370.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  371.         }
  372.     }

  373.     public String abilitaCache(Long dimensioneCache,Boolean algoritmoCacheLRU,Long itemIdleTime,Long itemLifeSecond){
  374.         try{
  375.             org.openspcoop2.pdd.core.GestoreRichieste.abilitaCache(dimensioneCache,algoritmoCacheLRU,itemIdleTime,itemLifeSecond);
  376.             this.cacheAbilitata = true;
  377.             return JMXUtils.MSG_ABILITAZIONE_CACHE_EFFETTUATA;
  378.         }catch(Throwable e){
  379.             this.log.error(e.getMessage(),e);
  380.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  381.         }
  382.     }
  383.    
  384.     public void disabilitaCache() throws JMException{
  385.         try{
  386.             org.openspcoop2.pdd.core.GestoreRichieste.disabilitaCache();
  387.             this.cacheAbilitata = false;
  388.         }catch(Throwable e){
  389.             this.log.error(e.getMessage(),e);
  390.             throw new JMException(e.getMessage());
  391.         }
  392.     }
  393.     public String disabilitaCacheConEsito() {
  394.         try{
  395.             disabilitaCache();
  396.             return JMXUtils.MSG_DISABILITAZIONE_CACHE_EFFETTUATA;
  397.         }catch(Throwable e){
  398.             this.log.error(e.getMessage(),e);
  399.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  400.         }
  401.     }
  402.    
  403.     public String listKeysCache(){
  404.         try{
  405.             if(this.cacheAbilitata==false)
  406.                 throw new Exception("Cache non abilitata");
  407.             return org.openspcoop2.pdd.core.GestoreRichieste.listKeysCache("\n");
  408.         }catch(Throwable e){
  409.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  410.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  411.         }
  412.     }
  413.    
  414.     public String getObjectCache(String key){
  415.         try{
  416.             if(this.cacheAbilitata==false)
  417.                 throw new Exception("Cache non abilitata");
  418.             return org.openspcoop2.pdd.core.GestoreRichieste.getObjectCache(key);
  419.         }catch(Throwable e){
  420.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  421.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  422.         }
  423.     }
  424.    
  425.     public String removeObjectCache(String key){
  426.         try{
  427.             if(this.cacheAbilitata==false)
  428.                 throw new Exception("Cache non abilitata");
  429.             org.openspcoop2.pdd.core.GestoreRichieste.removeObjectCache(key);
  430.             return JMXUtils.MSG_RIMOZIONE_CACHE_EFFETTUATA;
  431.         }catch(Throwable e){
  432.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  433.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  434.         }
  435.     }

  436.     public String removeRateLimitingGlobalConfigCache(){
  437.         try{
  438.             if(this.cacheAbilitata==false)
  439.                 throw new Exception("Cache non abilitata");
  440.             org.openspcoop2.pdd.core.GestoreRichieste.removeRateLimitingConfigGlobale();
  441.             return JMXUtils.MSG_RIMOZIONE_CACHE_EFFETTUATA;
  442.         }catch(Throwable e){
  443.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  444.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  445.         }
  446.     }
  447.    
  448.     public String removeRateLimitingAPIConfigCache(TipoPdD tipoPdD, String nomePorta){
  449.         try{
  450.             if(this.cacheAbilitata==false)
  451.                 throw new Exception("Cache non abilitata");
  452.             org.openspcoop2.pdd.core.GestoreRichieste.removeRateLimitingConfig(tipoPdD, nomePorta);
  453.             return JMXUtils.MSG_RIMOZIONE_CACHE_EFFETTUATA;
  454.         }catch(Throwable e){
  455.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  456.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  457.         }
  458.     }
  459.    
  460. }