CacheJMXUtils.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 org.slf4j.Logger;
  22. import org.openspcoop2.utils.UtilsException;
  23. import org.openspcoop2.utils.jmx.CostantiJMX;
  24. import org.openspcoop2.utils.jmx.RisorseJMXException;

  25. /**
  26.  * CacheJMXUtils
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class CacheJMXUtils {

  33.     private static String JMX_DOMAIN = "org.openspcoop2.utils";
  34.     private static String JMX_TYPE = CostantiJMX.JMX_TYPE;
  35.    
  36.     private static org.openspcoop2.utils.jmx.GestoreRisorseJMX gestoreRisorse = null;
  37.    
  38.     private static synchronized void initGestoreRisorseJMX(Logger log) throws RisorseJMXException{
  39.         if(gestoreRisorse==null)
  40.             gestoreRisorse = new org.openspcoop2.utils.jmx.GestoreRisorseJMX(log);
  41.     }
  42.    
  43.     public static void register(Logger log,AbstractCacheJmx cache) throws UtilsException{
  44.         register(log, cache, null, null, cache.getCacheWrapper().getCacheName());
  45.     }
  46.     public static void register(Logger log,AbstractCacheJmx cache, String jmxName) throws UtilsException{
  47.         register(log,cache, null, null, jmxName);
  48.     }
  49.     public static void register(Logger log,AbstractCacheJmx cache,String jmxDomain, String jmxName) throws UtilsException{
  50.         register(log,cache, jmxDomain, null, jmxName);
  51.     }
  52.     public static void register(Logger log,AbstractCacheJmx cache,String jmxDomain,String jmxType, String jmxName) throws UtilsException{
  53.         try{
  54.             if(jmxName==null){
  55.                 throw new Exception("JmxName undefined");
  56.             }
  57.             if(jmxDomain==null){
  58.                 jmxDomain = JMX_DOMAIN;
  59.             }
  60.             if(jmxType==null){
  61.                 jmxType = JMX_TYPE;
  62.             }
  63.             if(gestoreRisorse==null){
  64.                 initGestoreRisorseJMX(log);
  65.             }
  66.             gestoreRisorse.registerMBean(cache.getClass(), jmxDomain, jmxType, jmxName);
  67.         }catch(Exception e){
  68.             throw new UtilsException(e.getMessage(),e);
  69.         }
  70.     }
  71.    
  72.     public static void unregister(){
  73.         if(gestoreRisorse!=null){
  74.             gestoreRisorse.unregisterMBeans();
  75.         }
  76.     }
  77. }