EngineKeystoreCaching.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.openspcoop2.pdd.config.OpenSPCoop2Properties;
  36. import org.openspcoop2.pdd.core.keystore.GestoreKeystoreCaching;
  37. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  38. import org.slf4j.Logger;


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


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

  250.         // MetaData per l'operazione getObjectCache
  251.         MBeanOperationInfo getObjectCacheOP = JMXUtils.MBEAN_OPERATION_GET_OBJECT_CACHE;
  252.        
  253.         // MetaData per l'operazione removeObjectCache
  254.         MBeanOperationInfo removeObjectCacheOP = JMXUtils.MBEAN_OPERATION_REMOVE_OBJECT_CACHE;
  255.        
  256.         // Mbean costruttore
  257.         MBeanConstructorInfo defaultConstructor = new MBeanConstructorInfo("Default Constructor","Crea e inizializza una nuova istanza del MBean",null);

  258.         // Lista attributi
  259.         MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[]{cacheAbilitataVAR};
  260.        
  261.         // Lista Costruttori
  262.         MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[]{defaultConstructor};
  263.        
  264.         // Lista operazioni
  265.         MBeanOperationInfo[] operations = new MBeanOperationInfo[]{resetCacheOP,printStatCacheOP,disabilitaCacheOP,abilitaCacheParametriOP,listKeysCacheOP,getObjectCacheOP,removeObjectCacheOP};
  266.        
  267.         return new MBeanInfo(className,description,attributes,constructors,operations,null);
  268.     }
  269.    
  270.     /* Variabili per la gestione JMX */
  271.     private Logger log;
  272.    
  273.     /* Costruttore */
  274.     public EngineKeystoreCaching(){
  275.         this.log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  276.                
  277.         // Configurazione
  278.         this.cacheAbilitata = GestoreKeystoreCaching.isCacheAbilitata();
  279.            
  280.     }
  281.    
  282.     public boolean isCacheAbilitata() {
  283.         return this.cacheAbilitata;
  284.     }
  285.    
  286.     /* Metodi di management JMX */
  287.     public String resetCache(){
  288.         try{
  289.             if(this.cacheAbilitata==false)
  290.                 throw new Exception("Cache non abilitata");
  291.             GestoreKeystoreCaching.resetCache();
  292.             return JMXUtils.MSG_RESET_CACHE_EFFETTUATO_SUCCESSO;
  293.         }catch(Throwable e){
  294.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  295.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  296.         }
  297.     }
  298.    
  299.     public String printStatCache(){
  300.         try{
  301.             if(this.cacheAbilitata==false)
  302.                 throw new Exception("Cache non abilitata");
  303.             return GestoreKeystoreCaching.printStatsCache("\n");
  304.         }catch(Throwable e){
  305.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  306.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  307.         }
  308.     }
  309.    
  310.     public String abilitaCache(){
  311.         try{
  312.             GestoreKeystoreCaching.abilitaCache();
  313.             this.cacheAbilitata = true;
  314.             return JMXUtils.MSG_ABILITAZIONE_CACHE_EFFETTUATA;
  315.         }catch(Throwable e){
  316.             this.log.error(e.getMessage(),e);
  317.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  318.         }
  319.     }

  320.     public String abilitaCache(Long dimensioneCache,Boolean algoritmoCacheLRU,Long itemIdleTime,Long itemLifeSecond){
  321.         try{
  322.             GestoreKeystoreCaching.abilitaCache(dimensioneCache,algoritmoCacheLRU,itemIdleTime,itemLifeSecond, this.log);
  323.             this.cacheAbilitata = true;
  324.             return JMXUtils.MSG_ABILITAZIONE_CACHE_EFFETTUATA;
  325.         }catch(Throwable e){
  326.             this.log.error(e.getMessage(),e);
  327.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  328.         }
  329.     }
  330.    
  331.     public void disabilitaCache() throws JMException{
  332.         try{
  333.             GestoreKeystoreCaching.disabilitaCache();
  334.             this.cacheAbilitata = false;
  335.         }catch(Throwable e){
  336.             this.log.error(e.getMessage(),e);
  337.             throw new JMException(e.getMessage());
  338.         }
  339.     }
  340.     public String disabilitaCacheConEsito() {
  341.         try{
  342.             disabilitaCache();
  343.             return JMXUtils.MSG_DISABILITAZIONE_CACHE_EFFETTUATA;
  344.         }catch(Throwable e){
  345.             this.log.error(e.getMessage(),e);
  346.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  347.         }
  348.     }
  349.    
  350.     public String listKeysCache(){
  351.         try{
  352.             if(this.cacheAbilitata==false)
  353.                 throw new Exception("Cache non abilitata");
  354.             return GestoreKeystoreCaching.listKeysCache("\n");
  355.         }catch(Throwable e){
  356.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  357.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  358.         }
  359.     }
  360.    
  361.     public String getObjectCache(String key){
  362.         try{
  363.             if(this.cacheAbilitata==false)
  364.                 throw new Exception("Cache non abilitata");
  365.             return GestoreKeystoreCaching.getObjectCache(key);
  366.         }catch(Throwable e){
  367.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  368.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  369.         }
  370.     }
  371.    
  372.     public String removeObjectCache(String key){
  373.         try{
  374.             if(this.cacheAbilitata==false)
  375.                 throw new Exception("Cache non abilitata");
  376.             GestoreKeystoreCaching.removeObjectCache(key);
  377.             return JMXUtils.MSG_RIMOZIONE_CACHE_EFFETTUATA;
  378.         }catch(Throwable e){
  379.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  380.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  381.         }
  382.     }

  383. }