RepositoryMessaggi.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.pdd.config.OpenSPCoop2Properties;
  37. import org.openspcoop2.pdd.core.GestoreMessaggi;
  38. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;


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

  47.     /** Nomi proprieta' */
  48.     public static final String TIPO_REPOSITORY = "tipoRepository";

  49.    
  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(RepositoryMessaggi.TIPO_REPOSITORY))
  62.             return OpenSPCoop2Properties.getInstance().getRepositoryType();
  63.        
  64.         if(attributeName.equals(JMXUtils.CACHE_ATTRIBUTE_ABILITATA))
  65.             return this.cacheAbilitata;
  66.        
  67.         throw new AttributeNotFoundException("Attributo "+attributeName+" non trovato");
  68.     }
  69.    
  70.     /** getAttributes */
  71.     @Override
  72.     public AttributeList getAttributes(String [] attributesNames){
  73.        
  74.         if(attributesNames==null)
  75.             throw new IllegalArgumentException("Array nullo");
  76.        
  77.         AttributeList list = new AttributeList();
  78.         for (int i=0; i<attributesNames.length; i++){
  79.             try{
  80.                 list.add(new Attribute(attributesNames[i],getAttribute(attributesNames[i])));
  81.             }catch(JMException ex){}
  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("Il nome dell'attributo e' nullo");
  92.        
  93.         try{
  94.            
  95.             if(attribute.getName().equals(JMXUtils.CACHE_ATTRIBUTE_ABILITATA)){
  96.                 boolean v = (Boolean) attribute.getValue();
  97.                 if(v){
  98.                     // la cache DEVE essere abilitata
  99.                     if(!this.cacheAbilitata){
  100.                         this.abilitaCache();
  101.                     }
  102.                 }
  103.                 else{
  104.                     // la cache DEVE essere disabilitata
  105.                     if(this.cacheAbilitata){
  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("il tipo "+attribute.getValue().getClass()+" dell'attributo "+attribute.getName()+" non e' valido");
  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("Nessuna operazione definita");
  150.        
  151.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_RESET)){
  152.             return this.resetCache();
  153.         }
  154.        
  155.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_PRINT_STATS)){
  156.             return this.printStatCache();
  157.         }
  158.        
  159.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_DISABILITA)){
  160.             return this.disabilitaCacheConEsito();
  161.         }
  162.        
  163.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_ABILITA)){
  164.             if(params.length != 4)
  165.                 throw new MBeanException(new Exception("[AbilitaCache] Lunghezza parametri non corretta: "+params.length));
  166.             return this.abilitaCache((Long)params[0], (Boolean)params[1], (Long)params[2], (Long)params[3] );
  167.         }
  168.        
  169.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_LIST_KEYS)){
  170.             return this.listKeysCache();
  171.         }
  172.        
  173.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_GET_OBJECT)){
  174.            
  175.             if(params.length != 1)
  176.                 throw new MBeanException(new Exception("["+JMXUtils.CACHE_METHOD_NAME_GET_OBJECT+"] Lunghezza parametri non corretta: "+params.length));
  177.            
  178.             String param1 = null;
  179.             if(params[0]!=null && !"".equals(params[0])){
  180.                 param1 = (String)params[0];
  181.             }
  182.            
  183.             return this.getObjectCache(param1);
  184.         }
  185.        
  186.         if(actionName.equals(JMXUtils.CACHE_METHOD_NAME_REMOVE_OBJECT)){
  187.            
  188.             if(params.length != 1)
  189.                 throw new MBeanException(new Exception("["+JMXUtils.CACHE_METHOD_NAME_REMOVE_OBJECT+"] Lunghezza parametri non corretta: "+params.length));
  190.            
  191.             String param1 = null;
  192.             if(params[0]!=null && !"".equals(params[0])){
  193.                 param1 = (String)params[0];
  194.             }
  195.            
  196.             return this.removeObjectCache(param1);
  197.         }
  198.        
  199.         throw new UnsupportedOperationException("Operazione "+actionName+" sconosciuta");
  200.     }
  201.    
  202.     /* MBean info */
  203.     @Override
  204.     public MBeanInfo getMBeanInfo(){
  205.        
  206.         // Per determinare se l'attributo e' leggibile/scrivibile
  207.         final boolean READABLE = true;
  208.         final boolean WRITABLE = true;
  209.        
  210.         // Per determinare se l'attributo e' ricavabile nella forma booleana isAttribute()
  211.         final boolean IS_GETTER = true;
  212.        
  213.         // Descrizione della classe nel MBean
  214.         String className = this.getClass().getName();
  215.         String description = "Repository dei Messaggi ("+OpenSPCoop2Properties.getInstance().getVersione()+")";

  216.         // MetaData per l'attributo tipoRepositoryMessaggi
  217.         MBeanAttributeInfo tipoRepositoryVAR
  218.             = new MBeanAttributeInfo(RepositoryMessaggi.TIPO_REPOSITORY,String.class.getName(),
  219.                         "Tipo di repository messaggi",
  220.                             READABLE,!WRITABLE,!IS_GETTER);
  221.        
  222.         // MetaData per l'attributo abilitaCache
  223.         MBeanAttributeInfo cacheAbilitataVAR = JMXUtils.MBEAN_ATTRIBUTE_INFO_CACHE_ABILITATA;
  224.        
  225.         // MetaData per l'operazione resetCache
  226.         MBeanOperationInfo resetCacheOP = JMXUtils.MBEAN_OPERATION_RESET_CACHE;
  227.        
  228.         // MetaData per l'operazione printStatCache
  229.         MBeanOperationInfo printStatCacheOP = JMXUtils.MBEAN_OPERATION_PRINT_STATS_CACHE;
  230.        
  231.         // MetaData per l'operazione disabilitaCache
  232.         MBeanOperationInfo disabilitaCacheOP = JMXUtils.MBEAN_OPERATION_DISABILITA_CACHE;
  233.        
  234.         // MetaData per l'operazione abilitaCache con parametri
  235.         MBeanOperationInfo abilitaCacheParametriOP = JMXUtils.MBEAN_OPERATION_ABILITA_CACHE_CON_PARAMETRI;
  236.        
  237.         // MetaData per l'operazione listKeysCache
  238.         MBeanOperationInfo listKeysCacheOP = JMXUtils.MBEAN_OPERATION_LIST_KEYS_CACHE;

  239.         // MetaData per l'operazione getObjectCache
  240.         MBeanOperationInfo getObjectCacheOP = JMXUtils.MBEAN_OPERATION_GET_OBJECT_CACHE;
  241.        
  242.         // MetaData per l'operazione removeObjectCache
  243.         MBeanOperationInfo removeObjectCacheOP = JMXUtils.MBEAN_OPERATION_REMOVE_OBJECT_CACHE;
  244.        
  245.         // Mbean costruttore
  246.         MBeanConstructorInfo defaultConstructor = new MBeanConstructorInfo("Default Constructor","Crea e inizializza una nuova istanza del MBean",null);

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

  310.     public String abilitaCache(long dimensioneCache,boolean algoritmoCacheLRU,long itemIdleTime,long itemLifeSecond){
  311.         try{
  312.             GestoreMessaggi.abilitaCache(dimensioneCache,algoritmoCacheLRU,itemIdleTime,itemLifeSecond);
  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.    
  321.     public void disabilitaCache() throws JMException{
  322.         try{
  323.             GestoreMessaggi.disabilitaCache();
  324.             this.cacheAbilitata = false;
  325.         }catch(Throwable e){
  326.             this.log.error(e.getMessage(),e);
  327.             throw new JMException(e.getMessage());
  328.         }
  329.     }
  330.     public String disabilitaCacheConEsito() {
  331.         try{
  332.             disabilitaCache();
  333.             return JMXUtils.MSG_DISABILITAZIONE_CACHE_EFFETTUATA;
  334.         }catch(Throwable e){
  335.             this.log.error(e.getMessage(),e);
  336.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  337.         }
  338.     }
  339.    
  340.     public String listKeysCache(){
  341.         try{
  342.             if(this.cacheAbilitata==false)
  343.                 throw new Exception("Cache non abilitata");
  344.             return GestoreMessaggi.listKeysCache("\n");
  345.         }catch(Throwable e){
  346.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  347.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  348.         }
  349.     }
  350.    
  351.     public String getObjectCache(String key){
  352.         try{
  353.             if(this.cacheAbilitata==false)
  354.                 throw new Exception("Cache non abilitata");
  355.             return GestoreMessaggi.getObjectCache(key);
  356.         }catch(Throwable e){
  357.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  358.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  359.         }
  360.     }
  361.    
  362.     public String removeObjectCache(String key){
  363.         try{
  364.             if(this.cacheAbilitata==false)
  365.                 throw new Exception("Cache non abilitata");
  366.             GestoreMessaggi.removeObjectCache(key);
  367.             return JMXUtils.MSG_RIMOZIONE_CACHE_EFFETTUATA;
  368.         }catch(Throwable e){
  369.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  370.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  371.         }
  372.     }

  373. }