AbstractCacheJmx.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.utils.cache;

  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. /**
  36.  * CacheJmx
  37.  *  
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public abstract class AbstractCacheJmx extends NotificationBroadcasterSupport implements DynamicMBean {

  43.     public abstract AbstractCacheWrapper getCacheWrapper();
  44.    
  45.     public abstract String getJmxDescription();
  46.    
  47.    
  48.    
  49.     /** Nomi proprieta' */
  50.     public static final String CACHE_NAME = "cacheName";
  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(Constants.CACHE_ATTRIBUTE_ABILITATA))
  60.             return this.getCacheWrapper().isCacheEnabled();
  61.        
  62.         if(attributeName.equals(AbstractCacheJmx.CACHE_NAME))
  63.             return this.getCacheWrapper().getCacheName();
  64.        
  65.         throw new AttributeNotFoundException("Attribute "+attributeName+" not found");
  66.     }
  67.    
  68.     /** getAttributes */
  69.     @Override
  70.     public AttributeList getAttributes(String [] attributesNames){
  71.        
  72.         if(attributesNames==null)
  73.             throw new IllegalArgumentException("Attributes Names undefined");
  74.        
  75.         AttributeList list = new AttributeList();
  76.         for (int i=0; i<attributesNames.length; i++){
  77.             try{
  78.                 list.add(new Attribute(attributesNames[i],getAttribute(attributesNames[i])));
  79.             }catch(JMException ex){
  80.                 // ignore
  81.             }
  82.         }
  83.         return list;
  84.     }
  85.    
  86.     /** setAttribute */
  87.     @Override
  88.     public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException{
  89.        
  90.         if( attribute==null )
  91.             throw new IllegalArgumentException("Attribute name undefined");
  92.        
  93.         try{
  94.            
  95.             if(attribute.getName().equals(Constants.CACHE_ATTRIBUTE_ABILITATA)){
  96.                 boolean v = (Boolean) attribute.getValue();
  97.                 if(v){
  98.                     // la cache DEVE essere abilitata
  99.                     if(!this.getCacheWrapper().isCacheEnabled()){
  100.                         this.abilitaCache();
  101.                     }
  102.                 }
  103.                 else{
  104.                     // la cache DEVE essere disabilitata
  105.                     if(this.getCacheWrapper().isCacheEnabled()){
  106.                         this.disabilitaCache();
  107.                     }
  108.                 }
  109.             }
  110.                        
  111.             else
  112.                 throw new AttributeNotFoundException("Attributo "+attribute.getName()+" non trovato");
  113.            
  114.         }catch(ClassCastException ce){
  115.             throw new InvalidAttributeValueException("Type "+attribute.getValue().getClass()+" for attribute "+attribute.getName()+" is not valid");
  116.         }catch(JMException j){
  117.             throw new MBeanException(j);
  118.         }
  119.        
  120.     }
  121.    
  122.     /** setAttributes */
  123.     @Override
  124.     public AttributeList setAttributes(AttributeList list){
  125.        
  126.         if(list==null)
  127.             throw new IllegalArgumentException("Lista degli attributi e' nulla");
  128.        
  129.         AttributeList ret = new AttributeList();
  130.         Iterator<?> it = ret.iterator();
  131.        
  132.         while(it.hasNext()){
  133.             try{
  134.                 Attribute attribute = (Attribute) it.next();
  135.                 setAttribute(attribute);
  136.                 ret.add(attribute);
  137.             }catch(JMException ex){}
  138.         }
  139.        
  140.         return ret;
  141.        
  142.     }
  143.    
  144.     /** invoke */
  145.     @Override
  146.     public Object invoke(String actionName, Object[]params, String[]signature) throws MBeanException,ReflectionException{
  147.        
  148.         if( (actionName==null) || (actionName.equals("")) )
  149.             throw new IllegalArgumentException("Operation undefined");
  150.        
  151.         if(actionName.equals(Constants.CACHE_METHOD_NAME_RESET)){
  152.             return this.resetCache();
  153.         }
  154.        
  155.         if(actionName.equals(Constants.CACHE_METHOD_NAME_PRINT_STATS)){
  156.             return this.printStatCache();
  157.         }
  158.                
  159.         if(actionName.equals(Constants.CACHE_METHOD_NAME_DISABILITA)){
  160.             return this.disabilitaCacheConEsito();
  161.         }
  162.                
  163.         if(actionName.equals(Constants.CACHE_METHOD_NAME_ABILITA)){
  164.             if(params.length != 4)
  165.                 throw new MBeanException(new Exception("["+Constants.CACHE_METHOD_NAME_ABILITA+"] Parameter size uncorrect: "+params.length));
  166.            
  167.             Integer param1 = null;
  168.             if(params[0]!=null && !"".equals(params[0])){
  169.                 param1 = (Integer)params[0];
  170.                 if(param1<0){
  171.                     param1 = null;
  172.                 }
  173.             }
  174.            
  175.             Boolean param2 = null;
  176.             if(params[1]!=null && !"".equals(params[1])){
  177.                 param2 = (Boolean)params[1];
  178.             }
  179.            
  180.             Integer param3 = null;
  181.             if(params[2]!=null && !"".equals(params[2])){
  182.                 param3 = (Integer)params[2];
  183.                 if(param3<0){
  184.                     param3 = null;
  185.                 }
  186.             }
  187.            
  188.             Integer param4 = null;
  189.             if(params[3]!=null && !"".equals(params[3])){
  190.                 param4 = (Integer)params[3];
  191.                 if(param4<0){
  192.                     param4 = null;
  193.                 }
  194.             }
  195.                    
  196.             return this.abilitaCache(param1, param2, param3, param4 );
  197.         }
  198.        
  199.         if(actionName.equals(Constants.CACHE_METHOD_NAME_LIST_KEYS)){
  200.             return this.listKeysCache();
  201.         }
  202.        
  203.         if(actionName.equals(Constants.CACHE_METHOD_NAME_GET_OBJECT)){
  204.            
  205.             if(params.length != 1)
  206.                 throw new MBeanException(new Exception("["+Constants.CACHE_METHOD_NAME_GET_OBJECT+"] Parameter size uncorrect: "+params.length));
  207.            
  208.             String param1 = null;
  209.             if(params[0]!=null && !"".equals(params[0])){
  210.                 param1 = (String)params[0];
  211.             }
  212.            
  213.             return this.getObjectCache(param1);
  214.         }
  215.        
  216.         if(actionName.equals(Constants.CACHE_METHOD_NAME_REMOVE_OBJECT)){
  217.            
  218.             if(params.length != 1)
  219.                 throw new MBeanException(new Exception("["+Constants.CACHE_METHOD_NAME_REMOVE_OBJECT+"] Parameter size uncorrect: "+params.length));
  220.            
  221.             String param1 = null;
  222.             if(params[0]!=null && !"".equals(params[0])){
  223.                 param1 = (String)params[0];
  224.             }
  225.            
  226.             return this.removeObjectCache(param1);
  227.         }
  228.        
  229.         throw new UnsupportedOperationException("Operation "+actionName+" unknown");
  230.     }
  231.    
  232.     /* MBean info */
  233.     @Override
  234.     public MBeanInfo getMBeanInfo(){
  235.                
  236.         // Descrizione della classe nel MBean
  237.         String className = this.getClass().getName();
  238.         String description = this.getJmxDescription();
  239.        
  240.         // MetaData per l'attributo abilitaCache
  241.         MBeanAttributeInfo cacheAbilitataVAR = Constants.MBEAN_ATTRIBUTE_INFO_CACHE_ABILITATA;
  242.        
  243.         // MetaData per l'attributo registriServizi
  244.         MBeanAttributeInfo registriServiziVAR
  245.             = new MBeanAttributeInfo(AbstractCacheJmx.CACHE_NAME,String[].class.getName(),
  246.                         "Elenco dei registri dei servizi utilizzati a RunTime dalla Porta di Dominio",
  247.                             Constants.JMX_ATTRIBUTE_READABLE,!Constants.JMX_ATTRIBUTE_WRITABLE,!Constants.JMX_ATTRIBUTE_IS_GETTER);
  248.        
  249.         // MetaData per l'operazione resetCache
  250.         MBeanOperationInfo resetCacheOP = Constants.MBEAN_OPERATION_RESET_CACHE;
  251.        
  252.         // MetaData per l'operazione printStatCache
  253.         MBeanOperationInfo printStatCacheOP = Constants.MBEAN_OPERATION_PRINT_STATS_CACHE;
  254.        
  255.         // MetaData per l'operazione disabilitaCache
  256.         MBeanOperationInfo disabilitaCacheOP = Constants.MBEAN_OPERATION_DISABILITA_CACHE;
  257.        
  258.         // MetaData per l'operazione abilitaCache con parametri
  259.         MBeanOperationInfo abilitaCacheParametriOP = Constants.MBEAN_OPERATION_ABILITA_CACHE_CON_PARAMETRI;
  260.        
  261.         // MetaData per l'operazione listKeysCache
  262.         MBeanOperationInfo listKeysCacheOP = Constants.MBEAN_OPERATION_LIST_KEYS_CACHE;

  263.         // MetaData per l'operazione getObjectCache
  264.         MBeanOperationInfo getObjectCacheOP = Constants.MBEAN_OPERATION_GET_OBJECT_CACHE;
  265.        
  266.         // MetaData per l'operazione removeObjectCache
  267.         MBeanOperationInfo removeObjectCacheOP = Constants.MBEAN_OPERATION_REMOVE_OBJECT_CACHE;
  268.        
  269.         // Mbean costruttore
  270.         MBeanConstructorInfo defaultConstructor = new MBeanConstructorInfo("Default Constructor","New Instance of MBean",null);

  271.         // Lista attributi
  272.         MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[]{cacheAbilitataVAR,registriServiziVAR};
  273.        
  274.         // Lista Costruttori
  275.         MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[]{defaultConstructor};
  276.        
  277.         // Lista operazioni
  278.         MBeanOperationInfo[] operations = new MBeanOperationInfo[]{resetCacheOP,printStatCacheOP,disabilitaCacheOP,abilitaCacheParametriOP,listKeysCacheOP,getObjectCacheOP,removeObjectCacheOP};
  279.        
  280.         return new MBeanInfo(className,description,attributes,constructors,operations,null);
  281.     }
  282.    
  283.     /* Variabili per la gestione JMX */

  284.     /* Costruttore */
  285.     public AbstractCacheJmx(){
  286.     }
  287.    
  288.    
  289.     /* Metodi di management JMX */
  290.     public String resetCache(){
  291.         try{
  292.             if(this.getCacheWrapper().isCacheEnabled()==false)
  293.                 throw new Exception("Cache disabled");
  294.             this.getCacheWrapper().resetCache();
  295.             return Constants.MSG_RESET_CACHE_EFFETTUATO_SUCCESSO;
  296.         }catch(Throwable e){
  297.             this.getCacheWrapper().getLog().error(Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  298.             return Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  299.         }
  300.     }
  301.    
  302.     public String printStatCache(){
  303.         try{
  304.             if(this.getCacheWrapper().isCacheEnabled()==false)
  305.                 throw new Exception("Cache non abilitata");
  306.             return this.getCacheWrapper().printStatsCache("\n");
  307.         }catch(Throwable e){
  308.             this.getCacheWrapper().getLog().error(Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  309.             return Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  310.         }
  311.     }
  312.    
  313.     public void abilitaCache() throws JMException{
  314.         try{
  315.             this.getCacheWrapper().enableCache();
  316.         }catch(Throwable e){
  317.             this.getCacheWrapper().getLog().error(e.getMessage(),e);
  318.         }
  319.     }
  320.    
  321.     public String abilitaCache(Integer dimensioneCache,Boolean algoritmoCacheLRU,Integer itemIdleTime,Integer itemLifeSecond){
  322.         try{
  323.             this.getCacheWrapper().enableCache(dimensioneCache,algoritmoCacheLRU,itemIdleTime,itemLifeSecond);
  324.             return Constants.MSG_ABILITAZIONE_CACHE_EFFETTUATA;
  325.         }catch(Throwable e){
  326.             this.getCacheWrapper().getLog().error(e.getMessage(),e);
  327.             return Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  328.         }
  329.     }
  330.        
  331.    
  332.     public void disabilitaCache() throws JMException{
  333.         try{
  334.             this.getCacheWrapper().disableCache();
  335.         }catch(Throwable e){
  336.             this.getCacheWrapper().getLog().error(e.getMessage(),e);
  337.             throw new JMException(e.getMessage());
  338.         }
  339.     }
  340.     public String disabilitaCacheConEsito() {
  341.         try{
  342.             disabilitaCache();
  343.             return Constants.MSG_DISABILITAZIONE_CACHE_EFFETTUATA;
  344.         }catch(Throwable e){
  345.             this.getCacheWrapper().getLog().error(e.getMessage(),e);
  346.             return Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  347.         }
  348.     }
  349.    
  350.     public String listKeysCache(){
  351.         try{
  352.             if(this.getCacheWrapper().isCacheEnabled()==false)
  353.                 throw new Exception("Cache disabled");
  354.             return this.getCacheWrapper().listKeysCache("\n");
  355.         }catch(Throwable e){
  356.             this.getCacheWrapper().getLog().error(Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  357.             return Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  358.         }
  359.     }
  360.    
  361.     public String getObjectCache(String key){
  362.         try{
  363.             if(this.getCacheWrapper().isCacheEnabled()==false)
  364.                 throw new Exception("Cache disabled");
  365.             return this.getCacheWrapper().getObjectCache(key);
  366.         }catch(Throwable e){
  367.             this.getCacheWrapper().getLog().error(Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  368.             return Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  369.         }
  370.     }
  371.    
  372.     public String removeObjectCache(String key){
  373.         try{
  374.             if(this.getCacheWrapper().isCacheEnabled()==false)
  375.                 throw new Exception("Cache disabled");
  376.             return this.getCacheWrapper().removeObjectCache(key);
  377.         }catch(Throwable e){
  378.             this.getCacheWrapper().getLog().error(Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  379.             return Constants.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  380.         }
  381.     }
  382.    
  383. }