Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 178   Methods: 7
NCLOC: 107   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JmxUtil.java 68.8% 85.3% 71.4% 78.9%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    * Copyright 2005, JBoss Inc., and individual contributors as indicated
 4    * by the @authors tag. See the copyright.txt in the distribution for a
 5    * full listing of individual contributors.
 6    *
 7    * This is free software; you can redistribute it and/or modify it
 8    * under the terms of the GNU Lesser General Public License as
 9    * published by the Free Software Foundation; either version 2.1 of
 10    * the License, or (at your option) any later version.
 11    *
 12    * This software is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15    * Lesser General Public License for more details.
 16    *
 17    * You should have received a copy of the GNU Lesser General Public
 18    * License along with this software; if not, write to the Free
 19    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21    */
 22    package org.jboss.cache.jmx;
 23   
 24    import org.jboss.cache.config.Configuration;
 25    import org.jboss.cache.interceptors.Interceptor;
 26   
 27    import javax.management.JMException;
 28    import javax.management.MBeanServer;
 29    import javax.management.ObjectName;
 30    import java.util.List;
 31   
 32    /**
 33    * Various JMX related utilities
 34    *
 35    * @author Jerry Gauthier
 36    * @author Manik Surtani
 37    * @version $Id: JmxUtil.java,v 1.17 2007/06/11 12:58:19 msurtani Exp $
 38    */
 39    public class JmxUtil
 40    {
 41    public static final String JBOSS_SERVER_DOMAIN = "jboss";
 42    public static final String JBOSS_CACHE_DOMAIN = "jboss.cache";
 43    public static final String SERVICE_KEY_NAME = "service";
 44    public static final String BASE_PREFIX = JBOSS_CACHE_DOMAIN + ":" + SERVICE_KEY_NAME + "=JBossCache";
 45    public static final String CLUSTER_KEY = "cluster";
 46    public static final String PREFIX = BASE_PREFIX + "," + CLUSTER_KEY + "=";
 47    public static final String UNIQUE_ID_KEY = "uniqueId";
 48    public static final String NO_CLUSTER_PREFIX = BASE_PREFIX + "," + UNIQUE_ID_KEY + "=";
 49    public static final String CACHE_TYPE_KEY = "cacheType";
 50    public static final String PLAIN_CACHE_TYPE = "Cache";
 51    public static final String MBEAN_CLASS_SUFFIX = "MBean";
 52    public static final String INTERCEPTOR_KEY = ",cache-interceptor=";
 53   
 54  59 public static void registerCacheMBean(MBeanServer server, CacheJmxWrapperMBean cache, String cacheObjectName)
 55    throws JMException
 56    {
 57  59 ObjectName on = new ObjectName(cacheObjectName);
 58  59 if (!server.isRegistered(on))
 59    {
 60  59 server.registerMBean(cache, on);
 61    }
 62    }
 63   
 64    /*
 65    * Register the associated mbeans for cache interceptors
 66    *
 67    * @param server the mbean server with which the mbeans should be registered
 68    * @param cache the cache having the set of interceptors to be registered
 69    * @param registerCache whether the cache itself should be registered
 70    */
 71  32 public static void registerInterceptors(MBeanServer server, List<Interceptor> interceptors, String cacheObjectName)
 72    throws JMException
 73    {
 74  32 if (server == null || interceptors == null || cacheObjectName == null)
 75    {
 76  0 return;
 77    }
 78   
 79  32 for (Interceptor interceptor : interceptors)
 80    {
 81  224 if (!interceptor.getStatisticsEnabled())
 82  12 continue;
 83   
 84  212 boolean mbeanExists = true;
 85  212 try
 86    {
 87    // the mbean for interceptor AbcInterceptor will be named AbcInterceptorMBean
 88  212 Class.forName(interceptor.getClass().getName() + MBEAN_CLASS_SUFFIX);
 89    }
 90    catch (Throwable e)
 91    {
 92    // if the class can't be instantiated, no mbean is available
 93  122 mbeanExists = false;
 94    }
 95   
 96    // for JDK 1.4, must parse name and remove package prefix
 97    // for JDK 1.5, can use getSimpleName() to establish class name without package prefix
 98  212 String className = interceptor.getClass().getName();
 99  212 String serviceName = cacheObjectName + INTERCEPTOR_KEY + className.substring(className.lastIndexOf('.') + 1);
 100   
 101  212 ObjectName objName = new ObjectName(serviceName);
 102  212 if (!server.isRegistered(objName))
 103    {
 104  212 if (mbeanExists)
 105    // register associated interceptor mbean
 106    {
 107  90 server.registerMBean(interceptor, objName);
 108    }
 109    //else
 110    // register dummy interceptor mbean
 111    // server.registerMBean(new BaseInterceptor(), objName);
 112    }
 113    }
 114    }
 115   
 116  0 public static String getDefaultCacheObjectName(org.jboss.cache.Cache cache)
 117    {
 118    // get the cache's registration name
 119  0 return getDefaultCacheObjectName(cache.getConfiguration(), cache.getClass().getName());
 120    }
 121   
 122  1 public static String getDefaultCacheObjectName(Configuration config, String cacheImplClass)
 123    {
 124    // get the cache's registration name
 125  1 String tmpName = null;
 126  1 if (config.getClusterName() == null)
 127    {
 128  0 tmpName = NO_CLUSTER_PREFIX + getUniqueId(cacheImplClass);
 129    }
 130    else
 131    {
 132  1 tmpName = PREFIX + config.getClusterName();
 133    }
 134   
 135  1 return tmpName;
 136    }
 137   
 138  0 public static String getUniqueId(String cacheImplClass)
 139    {
 140  0 return cacheImplClass + System.currentTimeMillis();
 141    }
 142   
 143  26 public static void unregisterCacheMBean(MBeanServer server, String cacheObjectName)
 144    throws Exception
 145    {
 146  26 server.unregisterMBean(new ObjectName(cacheObjectName));
 147    }
 148   
 149    /*
 150    * Unregister the associated mbeans for cache interceptors
 151    *
 152    * @param server the mbean server for which the mbeans should be unregistered
 153    * @param cache the cache having the set of interceptors to be unregistered
 154    * @param unregisterCache whether the cache itself should be unregistered
 155    */
 156  32 public static void unregisterInterceptors(MBeanServer server, List<Interceptor> interceptors, String cacheObjectName)
 157    throws Exception
 158    {
 159  32 if (server == null || interceptors == null || cacheObjectName == null)
 160    {
 161  0 return;
 162    }
 163   
 164  32 for (Interceptor interceptor : interceptors)
 165    {
 166    // for JDK 1.4, must parse name and remove package prefix
 167    // for JDK 1.5, can use getSimpleName() to establish class name without package prefix
 168  224 String className = interceptor.getClass().getName();
 169  224 String serviceName = cacheObjectName + INTERCEPTOR_KEY + className.substring(className.lastIndexOf('.') + 1);
 170   
 171  224 ObjectName objName = new ObjectName(serviceName);
 172  224 if (server.isRegistered(objName))
 173    {
 174  90 server.unregisterMBean(objName);
 175    }
 176    }
 177    }
 178    }