EngineGestioneToken.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.NotificationBroadcasterSupport;
  34. import javax.management.ReflectionException;

  35. import org.slf4j.Logger;
  36. import org.openspcoop2.core.commons.CoreException;
  37. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  38. import org.openspcoop2.pdd.core.token.GestoreToken;
  39. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  40. import org.openspcoop2.utils.cache.Constants;


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

  49.     /** Attributi */
  50.     private boolean cacheAbilitata = false;
  51.    
  52.     /** getAttribute */
  53.     @Override
  54.     public Object getAttribute(String attributeName) throws AttributeNotFoundException,MBeanException,ReflectionException{
  55.        
  56.         if( (attributeName==null) || (attributeName.equals("")) )
  57.             throw new IllegalArgumentException("Il nome dell'attributo e' nullo o vuoto");
  58.        
  59.         if(attributeName.equals(JMXUtils.CACHE_ATTRIBUTE_ABILITATA))
  60.             return this.cacheAbilitata;
  61.        
  62.         throw new AttributeNotFoundException("Attributo "+attributeName+" non trovato");
  63.     }
  64.    
  65.     /** getAttributes */
  66.     @Override
  67.     public AttributeList getAttributes(String [] attributesNames){
  68.        
  69.         if(attributesNames==null)
  70.             throw new IllegalArgumentException("Array nullo");
  71.        
  72.         AttributeList list = new AttributeList();
  73.         for (int i=0; i<attributesNames.length; i++){
  74.             try{
  75.                 list.add(new Attribute(attributesNames[i],getAttribute(attributesNames[i])));
  76.             }catch(JMException ex){
  77.                 // ignore
  78.             }
  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.                 // ignore
  136.             }
  137.         }
  138.        
  139.         return ret;
  140.        
  141.     }
  142.    
  143.     /** invoke */
  144.     @Override
  145.     public Object invoke(String actionName, Object[]params, String[]signature) throws MBeanException,ReflectionException{
  146.        
  147.         if( (actionName==null) || (actionName.equals("")) )
  148.             throw new IllegalArgumentException("Nessuna operazione definita");
  149.        
  150.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_RESET)){
  151.             return this.resetCache();
  152.         }
  153.        
  154.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_PRINT_STATS)){
  155.             return this.printStatCache();
  156.         }
  157.        
  158.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_DISABILITA)){
  159.             return this.disabilitaCacheConEsito();
  160.         }
  161.        
  162.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_ABILITA)){
  163.             if(params.length != 4)
  164.                 throw new MBeanException(new Exception("[AbilitaCache] Lunghezza parametri non corretta: "+params.length));
  165.            
  166.             Long param1 = null;
  167.             if(params[0]!=null && !"".equals(params[0])){
  168.                 param1 = (Long)params[0];
  169.                 if(param1<0){
  170.                     param1 = null;
  171.                 }
  172.             }
  173.            
  174.             Boolean param2 = null;
  175.             if(params[1]!=null && !"".equals(params[1])){
  176.                 param2 = (Boolean)params[1];
  177.             }
  178.            
  179.             Long param3 = null;
  180.             if(params[2]!=null && !"".equals(params[2])){
  181.                 param3 = (Long)params[2];
  182.                 if(param3<0){
  183.                     param3 = null;
  184.                 }
  185.             }
  186.            
  187.             Long param4 = null;
  188.             if(params[3]!=null && !"".equals(params[3])){
  189.                 param4 = (Long)params[3];
  190.                 if(param4<0){
  191.                     param4 = null;
  192.                 }
  193.             }
  194.                    
  195.             return this.abilitaCache(param1, param2, param3, param4 );
  196.         }
  197.        
  198.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_LIST_KEYS)){
  199.             return this.listKeysCache();
  200.         }
  201.        
  202.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_GET_OBJECT)){
  203.            
  204.             if(params.length != 1)
  205.                 throw new MBeanException(new Exception("["+JMXUtils.CACHE_METHOD_NAME_GET_OBJECT+"] Lunghezza parametri non corretta: "+params.length));
  206.            
  207.             String param1 = null;
  208.             if(params[0]!=null && !"".equals(params[0])){
  209.                 param1 = (String)params[0];
  210.             }
  211.            
  212.             return this.getObjectCache(param1);
  213.         }
  214.        
  215.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_REMOVE_OBJECT)){
  216.            
  217.             if(params.length != 1)
  218.                 throw new MBeanException(new Exception("["+JMXUtils.CACHE_METHOD_NAME_REMOVE_OBJECT+"] Lunghezza parametri non corretta: "+params.length));
  219.            
  220.             String param1 = null;
  221.             if(params[0]!=null && !"".equals(params[0])){
  222.                 param1 = (String)params[0];
  223.             }
  224.            
  225.             return this.removeObjectCache(param1);
  226.         }
  227.        
  228.         throw new UnsupportedOperationException("Operazione "+actionName+" sconosciuta");
  229.     }
  230.    
  231.     /* MBean info */
  232.     @Override
  233.     public MBeanInfo getMBeanInfo(){
  234.        
  235.         // Descrizione della classe nel MBean
  236.         String className = this.getClass().getName();
  237.         String description = "Gestione dei token transitati sul Gateway ("+OpenSPCoop2Properties.getInstance().getVersione()+")";
  238.        
  239.         // MetaData per l'attributo abilitaCache
  240.         MBeanAttributeInfo cacheAbilitataVAR = JMXUtils.MBEAN_ATTRIBUTE_INFO_CACHE_ABILITATA;
  241.        
  242.         // MetaData per l'operazione resetCache
  243.         MBeanOperationInfo resetCacheOP = JMXUtils.MBEAN_OPERATION_RESET_CACHE;
  244.        
  245.         // MetaData per l'operazione printStatCache
  246.         MBeanOperationInfo printStatCacheOP = JMXUtils.MBEAN_OPERATION_PRINT_STATS_CACHE;
  247.        
  248.         // MetaData per l'operazione disabilitaCache
  249.         MBeanOperationInfo disabilitaCacheOP = JMXUtils.MBEAN_OPERATION_DISABILITA_CACHE;
  250.        
  251.         // MetaData per l'operazione abilitaCache con parametri
  252.         MBeanOperationInfo abilitaCacheParametriOP = JMXUtils.MBEAN_OPERATION_ABILITA_CACHE_CON_PARAMETRI;
  253.        
  254.         // MetaData per l'operazione listKeysCache
  255.         MBeanOperationInfo listKeysCacheOP = JMXUtils.MBEAN_OPERATION_LIST_KEYS_CACHE;

  256.         // MetaData per l'operazione getObjectCache
  257.         MBeanOperationInfo getObjectCacheOP = JMXUtils.MBEAN_OPERATION_GET_OBJECT_CACHE;
  258.        
  259.         // MetaData per l'operazione removeObjectCache
  260.         MBeanOperationInfo removeObjectCacheOP = JMXUtils.MBEAN_OPERATION_REMOVE_OBJECT_CACHE;
  261.        
  262.         // Mbean costruttore
  263.         MBeanConstructorInfo defaultConstructor = new MBeanConstructorInfo("Default Constructor","Crea e inizializza una nuova istanza del MBean",null);

  264.         // Lista attributi
  265.         MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[]{cacheAbilitataVAR};
  266.        
  267.         // Lista Costruttori
  268.         MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[]{defaultConstructor};
  269.        
  270.         // Lista operazioni
  271.         MBeanOperationInfo[] operations = new MBeanOperationInfo[]{resetCacheOP,printStatCacheOP,disabilitaCacheOP,abilitaCacheParametriOP,listKeysCacheOP,getObjectCacheOP,removeObjectCacheOP};
  272.        
  273.         return new MBeanInfo(className,description,attributes,constructors,operations,null);
  274.     }
  275.    
  276.     /* Variabili per la gestione JMX */
  277.     private Logger log;
  278.     private void logError(String msg, Throwable e) {
  279.         if(this.log!=null) {
  280.             this.log.error(msg,e);
  281.         }
  282.     }
  283.    
  284.     /* Costruttore */
  285.     public EngineGestioneToken(){
  286.         this.log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  287.                
  288.         // Configurazione
  289.         this.cacheAbilitata = GestoreToken.isGestioneTokenCacheAbilitata();
  290.            
  291.     }
  292.    
  293.     public boolean isCacheAbilitata() {
  294.         return this.cacheAbilitata;
  295.     }
  296.    
  297.     /* Metodi di management JMX */
  298.     public String resetCache(){
  299.         try{
  300.             if(!this.cacheAbilitata)
  301.                 throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  302.             GestoreToken.resetGestioneTokenCache();
  303.             return JMXUtils.MSG_RESET_CACHE_EFFETTUATO_SUCCESSO;
  304.         }catch(Exception e){
  305.             this.logError(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  306.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  307.         }
  308.     }
  309.    
  310.     public String printStatCache(){
  311.         try{
  312.             if(!this.cacheAbilitata)
  313.                 throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  314.             return GestoreToken.printStatsGestioneTokenCache("\n");
  315.         }catch(Exception e){
  316.             this.logError(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  317.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  318.         }
  319.     }
  320.    
  321.     public String abilitaCache(){
  322.         try{
  323.             GestoreToken.abilitaGestioneTokenCache();
  324.             this.cacheAbilitata = true;
  325.             return JMXUtils.MSG_ABILITAZIONE_CACHE_EFFETTUATA;
  326.         }catch(Exception e){
  327.             this.logError(e.getMessage(),e);
  328.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  329.         }
  330.     }

  331.     public String abilitaCache(Long dimensioneCache,Boolean algoritmoCacheLRU,Long itemIdleTime,Long itemLifeSecond){
  332.         try{
  333.             GestoreToken.abilitaGestioneTokenCache(dimensioneCache,algoritmoCacheLRU,itemIdleTime,itemLifeSecond);
  334.             this.cacheAbilitata = true;
  335.             return JMXUtils.MSG_ABILITAZIONE_CACHE_EFFETTUATA;
  336.         }catch(Exception e){
  337.             this.logError(e.getMessage(),e);
  338.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  339.         }
  340.     }
  341.    
  342.     public void disabilitaCache() throws JMException{
  343.         try{
  344.             GestoreToken.disabilitaGestioneTokenCache();
  345.             this.cacheAbilitata = false;
  346.         }catch(Exception e){
  347.             this.logError(e.getMessage(),e);
  348.             throw new JMException(e.getMessage());
  349.         }
  350.     }
  351.     public String disabilitaCacheConEsito() {
  352.         try{
  353.             disabilitaCache();
  354.             return JMXUtils.MSG_DISABILITAZIONE_CACHE_EFFETTUATA;
  355.         }catch(Exception e){
  356.             this.logError(e.getMessage(),e);
  357.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  358.         }
  359.     }
  360.    
  361.     public String listKeysCache(){
  362.         try{
  363.             if(!this.cacheAbilitata)
  364.                 throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  365.             return GestoreToken.listKeysGestioneTokenCache("\n");
  366.         }catch(Exception e){
  367.             this.logError(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  368.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  369.         }
  370.     }
  371.    
  372.     public String getObjectCache(String key){
  373.         try{
  374.             if(!this.cacheAbilitata)
  375.                 throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  376.             return GestoreToken.getObjectGestioneTokenCache(key);
  377.         }catch(Exception e){
  378.             this.logError(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  379.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  380.         }
  381.     }
  382.    
  383.     public String removeObjectCache(String key){
  384.         try{
  385.             if(!this.cacheAbilitata)
  386.                 throw new CoreException(Constants.MSG_CACHE_NON_ABILITATA);
  387.             GestoreToken.removeObjectGestioneTokenCache(key);
  388.             return JMXUtils.MSG_RIMOZIONE_CACHE_EFFETTUATA;
  389.         }catch(Exception e){
  390.             this.logError(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  391.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  392.         }
  393.     }

  394. }