SysPropsJMXResource.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.pdd.config.SystemPropertiesManager;
  38. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;


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

  47.     /** Nomi proprietà */
  48.     public static final String OPENSPCOOP2_SYSTEM_PROPERTIES = "OpenSPCoop2Properties";
  49.    
  50.     /** Nomi metodi */
  51.     public static final String REFRESH_PERSISTENT_SYSTEM_PROPERTIES = "refreshPersistentConfiguration";
  52.     public static final String READ_ALL_SYSTEM_PROPERTIES = "readAllProperties";
  53.     public static final String READ_OPENSPCOOP2_SYSTEM_PROPERTIES = "readOpenSPCoop2Properties";
  54.     public static final String GET_SYSTEM_PROPERTY_VALUE = "getPropertyValue";
  55.     public static final String REMOVE_SYSTEM_PROPERTY = "removeProperty";
  56.     public static final String UPDATE_SYSTEM_PROPERTY = "updateProperty";
  57.     public static final String INSERT_SYSTEM_PROPERTY = "insertProperty";
  58.    
  59.     public static final String EFFETTUATO_SUCCESSO = "Operazione effettuata con successo";
  60.     public static final String EFFETTUATO_SUCCESSO_INFO = "Operazione effettuata con successo (Nota la Modifica non è persistente, inoltre in caso di invocazione del pulsante 'refreshPersistentConfiguration' questa impostazione non viene mantenuta)";

  61.        
  62.     /** getAttribute */
  63.     @Override
  64.     public Object getAttribute(String attributeName) throws AttributeNotFoundException,MBeanException,ReflectionException{
  65.        
  66.         if( (attributeName==null) || (attributeName.equals("")) )
  67.             throw new IllegalArgumentException("Il nome dell'attributo e' nullo o vuoto");
  68.        
  69.         if(attributeName.equals(SysPropsJMXResource.OPENSPCOOP2_SYSTEM_PROPERTIES)){
  70.             String v = this.spm.getPropertyValue(SystemPropertiesManager.SYSTEM_PROPERTIES);
  71.             if(v==null || v.length()<=0){
  72.                 return "";
  73.             }
  74.             else{
  75.                 return v;
  76.             }
  77.         }
  78.                
  79.         throw new AttributeNotFoundException("Attributo "+attributeName+" non trovato");
  80.     }
  81.    
  82.     /** getAttributes */
  83.     @Override
  84.     public AttributeList getAttributes(String [] attributesNames){
  85.        
  86.         if(attributesNames==null)
  87.             throw new IllegalArgumentException("Array nullo");
  88.        
  89.         AttributeList list = new AttributeList();
  90.         for (int i=0; i<attributesNames.length; i++){
  91.             try{
  92.                 list.add(new Attribute(attributesNames[i],getAttribute(attributesNames[i])));
  93.             }catch(JMException ex){}
  94.         }
  95.         return list;
  96.     }
  97.    
  98.     /** setAttribute */
  99.     @Override
  100.     public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException{
  101.        
  102.         if( attribute==null )
  103.             throw new IllegalArgumentException("Il nome dell'attributo e' nullo");
  104.        
  105.         try{
  106.            
  107.             if(attribute.getName().equals(SysPropsJMXResource.OPENSPCOOP2_SYSTEM_PROPERTIES)){
  108.                 // nop;
  109.            
  110.             }else
  111.                 throw new AttributeNotFoundException("Attributo "+attribute.getName()+" non trovato");
  112.            
  113.         }catch(ClassCastException ce){
  114.             throw new InvalidAttributeValueException("il tipo "+attribute.getValue().getClass()+" dell'attributo "+attribute.getName()+" non e' valido");
  115.         }catch(JMException j){
  116.             throw new MBeanException(j);
  117.         }
  118.        
  119.     }
  120.    
  121.     /** setAttributes */
  122.     @Override
  123.     public AttributeList setAttributes(AttributeList list){
  124.        
  125.         if(list==null)
  126.             throw new IllegalArgumentException("Lista degli attributi e' nulla");
  127.        
  128.         AttributeList ret = new AttributeList();
  129.         Iterator<?> it = ret.iterator();
  130.        
  131.         while(it.hasNext()){
  132.             try{
  133.                 Attribute attribute = (Attribute) it.next();
  134.                 setAttribute(attribute);
  135.                 ret.add(attribute);
  136.             }catch(JMException ex){}
  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(SysPropsJMXResource.REFRESH_PERSISTENT_SYSTEM_PROPERTIES)){
  151.             return this.refreshPersistentConfigSystemProperty();
  152.         }
  153.        
  154.         if(actionName.equals(SysPropsJMXResource.READ_ALL_SYSTEM_PROPERTIES)){
  155.             return this.readAllSystemProperties();
  156.         }
  157.        
  158.         if(actionName.equals(SysPropsJMXResource.READ_OPENSPCOOP2_SYSTEM_PROPERTIES)){
  159.             return this.readOpenSPCoop2SystemProperties();
  160.         }
  161.        
  162.         if(actionName.equals(SysPropsJMXResource.GET_SYSTEM_PROPERTY_VALUE)){
  163.            
  164.             if(params.length != 1)
  165.                 throw new MBeanException(new Exception("["+SysPropsJMXResource.GET_SYSTEM_PROPERTY_VALUE+"] Lunghezza parametri non corretta: "+params.length));
  166.            
  167.             String param1 = null;
  168.             if(params[0]!=null && !"".equals(params[0])){
  169.                 param1 = (String)params[0];
  170.             }
  171.            
  172.             if(param1==null) {
  173.                 throw new MBeanException(new Exception("["+SysPropsJMXResource.GET_SYSTEM_PROPERTY_VALUE+"] parametro richiesto non fornito"));
  174.             }
  175.             return this.getSystemPropertyValue(param1);
  176.         }
  177.        
  178.         if(actionName.equals(SysPropsJMXResource.REMOVE_SYSTEM_PROPERTY)){
  179.            
  180.             if(params.length != 1)
  181.                 throw new MBeanException(new Exception("["+SysPropsJMXResource.REMOVE_SYSTEM_PROPERTY+"] Lunghezza parametri non corretta: "+params.length));
  182.            
  183.             String param1 = null;
  184.             if(params[0]!=null && !"".equals(params[0])){
  185.                 param1 = (String)params[0];
  186.             }
  187.            
  188.             if(param1==null) {
  189.                 throw new MBeanException(new Exception("["+SysPropsJMXResource.REMOVE_SYSTEM_PROPERTY+"] parametro richiesto non fornito"));
  190.             }
  191.             return this.removeSystemProperty(param1);
  192.         }
  193.        
  194.         if(actionName.equals(SysPropsJMXResource.UPDATE_SYSTEM_PROPERTY)){
  195.            
  196.             if(params.length != 2)
  197.                 throw new MBeanException(new Exception("["+SysPropsJMXResource.UPDATE_SYSTEM_PROPERTY+"] Lunghezza parametri non corretta: "+params.length));
  198.            
  199.             String param1 = null;
  200.             if(params[0]!=null && !"".equals(params[0])){
  201.                 param1 = (String)params[0];
  202.             }
  203.            
  204.             String param2 = null;
  205.             if(params[1]!=null && !"".equals(params[1])){
  206.                 param2 = (String)params[1];
  207.             }
  208.            
  209.             if(param1==null) {
  210.                 throw new MBeanException(new Exception("["+SysPropsJMXResource.UPDATE_SYSTEM_PROPERTY+"] parametro richiesto non fornito"));
  211.             }
  212.             return this.updateSystemProperty(param1,param2);
  213.         }
  214.        
  215.         if(actionName.equals(SysPropsJMXResource.INSERT_SYSTEM_PROPERTY)){
  216.            
  217.             if(params.length != 2)
  218.                 throw new MBeanException(new Exception("["+SysPropsJMXResource.INSERT_SYSTEM_PROPERTY+"] 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.             String param2 = null;
  226.             if(params[1]!=null && !"".equals(params[1])){
  227.                 param2 = (String)params[1];
  228.             }
  229.            
  230.             if(param1==null) {
  231.                 throw new MBeanException(new Exception("["+SysPropsJMXResource.INSERT_SYSTEM_PROPERTY+"] parametro richiesto non fornito"));
  232.             }
  233.             return this.insertSystemProperty(param1,param2);
  234.         }
  235.        
  236.         throw new UnsupportedOperationException("Operazione "+actionName+" sconosciuta");
  237.     }
  238.    
  239.     /* MBean info */
  240.     @Override
  241.     public MBeanInfo getMBeanInfo(){
  242.        
  243.         // Descrizione della classe nel MBean
  244.         String className = this.getClass().getName();
  245.         String description = "Risorsa per la gestione delle proprietà di sistema ("+this.openspcoopProperties.getVersione()+")";

  246.         // MetaData per l'attributo propertiesVAR
  247.         MBeanAttributeInfo propertiesVAR
  248.             = new MBeanAttributeInfo(SysPropsJMXResource.OPENSPCOOP2_SYSTEM_PROPERTIES,String.class.getName(),
  249.                         "proprietà di sistema impostate tramite la configurazione",
  250.                             JMXUtils.JMX_ATTRIBUTE_READABLE,!JMXUtils.JMX_ATTRIBUTE_WRITABLE,!JMXUtils.JMX_ATTRIBUTE_IS_GETTER);

  251.         // MetaData per l'operazione refreshPersistentConfigurazioneOP
  252.         MBeanOperationInfo refreshPersistentConfigurazioneOP
  253.             = new MBeanOperationInfo(SysPropsJMXResource.REFRESH_PERSISTENT_SYSTEM_PROPERTIES,"Reimposta le proprietà di sistema indicate nella configurazione",
  254.                     null,
  255.                     //new MBeanParameterInfo[]{new MBeanParameterInfo("param",String.class.getName())}
  256.                     String.class.getName(),
  257.                     MBeanOperationInfo.ACTION);
  258.        
  259.         // MetaData per l'operazione readAllPropertiesOP
  260.         MBeanOperationInfo readAllPropertiesOP
  261.             = new MBeanOperationInfo(SysPropsJMXResource.READ_ALL_SYSTEM_PROPERTIES,"Visualizza tutte le proprietà di sistema",
  262.                     null,
  263.                     //new MBeanParameterInfo[]{new MBeanParameterInfo("param",String.class.getName())}
  264.                     String.class.getName(),
  265.                     MBeanOperationInfo.ACTION);
  266.        
  267.         // MetaData per l'operazione readOpenSPCoop2PropertiesOP
  268.         MBeanOperationInfo readOpenSPCoop2PropertiesOP
  269.             = new MBeanOperationInfo(SysPropsJMXResource.READ_OPENSPCOOP2_SYSTEM_PROPERTIES,"Visualizza le proprietà di sistema impostate tramite la configurazione",
  270.                     null,
  271.                     //new MBeanParameterInfo[]{new MBeanParameterInfo("param",String.class.getName())}
  272.                     String.class.getName(),
  273.                     MBeanOperationInfo.ACTION);
  274.                
  275.         // MetaData per l'operazione getPropertyValueOP con parametri
  276.         MBeanOperationInfo getPropertyValueOP
  277.             = new MBeanOperationInfo(SysPropsJMXResource.GET_SYSTEM_PROPERTY_VALUE,"Ritorna il valore di una proprietà di sistema",
  278.                     new MBeanParameterInfo[]{
  279.                         new MBeanParameterInfo("nome",String.class.getName(),"Nome della proprietà"),
  280.                     },
  281.                     String.class.getName(),
  282.                     MBeanOperationInfo.ACTION);
  283.        
  284.         // MetaData per l'operazione removePropertyValueOP con parametri
  285.         MBeanOperationInfo removePropertyValueOP
  286.             = new MBeanOperationInfo(SysPropsJMXResource.REMOVE_SYSTEM_PROPERTY,"Elimina una proprietà di sistema",
  287.                     new MBeanParameterInfo[]{
  288.                         new MBeanParameterInfo("nome",String.class.getName(),"Nome della proprietà"),
  289.                     },
  290.                     String.class.getName(),
  291.                     MBeanOperationInfo.ACTION);
  292.        
  293.         // MetaData per l'operazione updatePropertyValueOP con parametri
  294.         MBeanOperationInfo updatePropertyValueOP
  295.             = new MBeanOperationInfo(SysPropsJMXResource.UPDATE_SYSTEM_PROPERTY,"Aggiorna una proprietà di sistema",
  296.                     new MBeanParameterInfo[]{
  297.                         new MBeanParameterInfo("nome",String.class.getName(),"Nome della proprietà"),
  298.                         new MBeanParameterInfo("valore",String.class.getName(),"Valore della proprietà"),
  299.                     },
  300.                     String.class.getName(),
  301.                     MBeanOperationInfo.ACTION);
  302.        
  303.         // MetaData per l'operazione insertPropertyValueOP con parametri
  304.         MBeanOperationInfo insertPropertyValueOP
  305.             = new MBeanOperationInfo(SysPropsJMXResource.INSERT_SYSTEM_PROPERTY,"Crea una nuova proprietà di sistema",
  306.                     new MBeanParameterInfo[]{
  307.                         new MBeanParameterInfo("nome",String.class.getName(),"Nome della proprietà"),
  308.                         new MBeanParameterInfo("valore",String.class.getName(),"Valore della proprietà"),
  309.                     },
  310.                     String.class.getName(),
  311.                     MBeanOperationInfo.ACTION);
  312.        
  313.         // Mbean costruttore
  314.         MBeanConstructorInfo defaultConstructor = new MBeanConstructorInfo("Default Constructor","Crea e inizializza una nuova istanza del MBean",null);

  315.         // Lista attributi
  316.         MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[]{propertiesVAR};
  317.        
  318.         // Lista Costruttori
  319.         MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[]{defaultConstructor};
  320.        
  321.         // Lista operazioni
  322.         MBeanOperationInfo[] operations = new MBeanOperationInfo[]{refreshPersistentConfigurazioneOP,
  323.                 readAllPropertiesOP,readOpenSPCoop2PropertiesOP,getPropertyValueOP,removePropertyValueOP,
  324.                 updatePropertyValueOP,insertPropertyValueOP};
  325.        
  326.         return new MBeanInfo(className,description,attributes,constructors,operations,null);
  327.     }
  328.    
  329.     /* Variabili per la gestione JMX */
  330.     private Logger log;
  331.     org.openspcoop2.pdd.config.ConfigurazionePdDManager configReader = null;
  332.     org.openspcoop2.pdd.config.OpenSPCoop2Properties openspcoopProperties = null;
  333.     private SystemPropertiesManager spm = null;
  334.    
  335.     /* Costruttore */
  336.     public SysPropsJMXResource(){
  337.         this.log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  338.         this.configReader = org.openspcoop2.pdd.config.ConfigurazionePdDManager.getInstance();
  339.         this.openspcoopProperties = org.openspcoop2.pdd.config.OpenSPCoop2Properties.getInstance();
  340.         this.spm = new SystemPropertiesManager(this.configReader, this.log);
  341.        
  342.     }
  343.    
  344.     /* Metodi di management JMX */
  345.    
  346.     public String readAllSystemProperties(){
  347.         try{
  348.             String p = this.spm.readAllSystemProperties("\n");
  349.             if(p==null || p.length()<=0){
  350.                 return "proprietà non presenti";
  351.             }
  352.             else{
  353.                 return p;
  354.             }
  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 readOpenSPCoop2SystemProperties(){
  362.         try{
  363.             String p = this.spm.readOpenSPCoop2SystemProperties("\n");
  364.             if(p==null || p.length()<=0){
  365.                 return "proprietà impostate tramite OpenSPCoop2 non trovate";
  366.             }
  367.             else{
  368.                 return p;
  369.             }
  370.         }catch(Throwable e){
  371.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  372.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  373.         }
  374.     }
  375.    
  376.     public String getSystemPropertyValue(String key){
  377.         try{
  378.             String v = this.spm.getPropertyValue(key);
  379.             if(v==null || v.length()<=0){
  380.                 return "proprietà ["+key+"] non presente";
  381.             }
  382.             else{
  383.                 return "["+key+"]=["+v+"]";
  384.             }
  385.         }catch(Throwable e){
  386.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  387.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  388.         }
  389.     }
  390.    
  391.     public String removeSystemProperty(String key){
  392.         try{
  393.             this.spm.removeProperty(key);
  394.             return SysPropsJMXResource.EFFETTUATO_SUCCESSO_INFO;
  395.         }catch(Throwable e){
  396.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  397.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  398.         }
  399.     }
  400.    
  401.     public String updateSystemProperty(String key,String value){
  402.         try{
  403.             this.spm.updateProperty(key,value);
  404.             return SysPropsJMXResource.EFFETTUATO_SUCCESSO_INFO;
  405.         }catch(Throwable e){
  406.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  407.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  408.         }
  409.     }
  410.    
  411.     public String insertSystemProperty(String key,String value){
  412.         try{
  413.             this.spm.insertProperty(key,value);
  414.             return SysPropsJMXResource.EFFETTUATO_SUCCESSO_INFO;
  415.         }catch(Throwable e){
  416.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  417.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  418.         }
  419.     }
  420.    
  421.     public String refreshPersistentConfigSystemProperty(){
  422.         try{
  423.             this.spm.updateSystemProperties();
  424.             return SysPropsJMXResource.EFFETTUATO_SUCCESSO;
  425.         }catch(Throwable e){
  426.             this.log.error(JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage(),e);
  427.             return JMXUtils.MSG_OPERAZIONE_NON_EFFETTUATA+e.getMessage();
  428.         }
  429.     }
  430.    
  431. }