[jbosscache-commits] JBoss Cache SVN: r6552 - in core/trunk/src: test/java/org/jboss/cache/jmx and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Mon Aug 11 05:58:39 EDT 2008


Author: mircea.markus
Date: 2008-08-11 05:58:39 -0400 (Mon, 11 Aug 2008)
New Revision: 6552

Modified:
   core/trunk/src/main/java/org/jboss/cache/jmx/CacheJmxWrapper.java
   core/trunk/src/main/java/org/jboss/cache/jmx/JmxRegistrationManager.java
   core/trunk/src/test/java/org/jboss/cache/jmx/JmxRegistrationManagerTest.java
   core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/CacheJmxWrapperTest.java
   core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/CacheJmxWrapperTestBase.java
   core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/NotificationTest.java
Log:
added UT and javadocs


Modified: core/trunk/src/main/java/org/jboss/cache/jmx/CacheJmxWrapper.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/jmx/CacheJmxWrapper.java	2008-08-09 03:21:50 UTC (rev 6551)
+++ core/trunk/src/main/java/org/jboss/cache/jmx/CacheJmxWrapper.java	2008-08-11 09:58:39 UTC (rev 6552)
@@ -815,7 +815,7 @@
          }
          else
          {
-            cacheObjectName = JmxRegistrationManager.CLUSTERED_CACHE_PREFIX + config.getClusterName();
+            cacheObjectName = JmxRegistrationManager.REPLICATED_CACHE_PREFIX + config.getClusterName();
          }
       }
       return cacheObjectName;

Modified: core/trunk/src/main/java/org/jboss/cache/jmx/JmxRegistrationManager.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/jmx/JmxRegistrationManager.java	2008-08-09 03:21:50 UTC (rev 6551)
+++ core/trunk/src/main/java/org/jboss/cache/jmx/JmxRegistrationManager.java	2008-08-11 09:58:39 UTC (rev 6552)
@@ -21,20 +21,30 @@
  */
 package org.jboss.cache.jmx;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.jboss.cache.Cache;
+import org.jboss.cache.CacheException;
 import org.jboss.cache.CacheSPI;
-import org.jboss.cache.CacheException;
 import org.jboss.cache.config.Configuration;
 import org.jboss.cache.factories.ComponentRegistry;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 
-import javax.management.*;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
 import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
 import java.util.List;
-import java.util.ArrayList;
 
 /**
+ * Registers all the <b>MBean</b>s from an <b>Cache</b> instance to a <b>MBeanServer</b>.
+ * It iterates over all the components within <b>ComponentRegistry</b> and registers all the components
+ * annotated with <b>ManagedAttribute</b>, <b>ManagedOperation</b> or/and <b>MBean</b>.
+ * If no <b>MBean</b> server is provided, then the {@link java.lang.management.ManagementFactory#getPlatformMBeanServer()}
+ * is being used.
+ * <p/>
+ * It is immutable: both cache instance and MBeanServer are being passed as arguments to the constructor.
+ *
  * @author Mircea.Markus at jboss.com
  * @since 3.0
  */
@@ -45,7 +55,7 @@
    /**
     * default ObjectName for clusterd caches. Cluster name should pe appended.
     */
-   public static final String CLUSTERED_CACHE_PREFIX = "jboss.cache:service=JBossCache,cluster=";
+   public static final String REPLICATED_CACHE_PREFIX = "jboss.cache:service=JBossCache,cluster=";
 
    /**
     * default ObjectName for non clustered caches. An unique identifier should be appended.
@@ -63,6 +73,13 @@
 
    private CacheSPI cacheSpi;
 
+   /**
+    * C-tor.
+    *
+    * @param mBeanServer    the server where mbeans are being registered
+    * @param cache          cache that needs to be monitored
+    * @param objectNameBase path in the MBeanServer where to register cache MBeans
+    */
    public JmxRegistrationManager(MBeanServer mBeanServer, Cache cache, ObjectName objectNameBase)
    {
       this.mBeanServer = mBeanServer;
@@ -71,10 +88,8 @@
    }
 
    /**
-    * @param mBeanServer
-    * @param cache
-    * @param objectNameBase
     * @throws IllegalArgumentException if the supplied objectNameBase name isn't valid
+    * @see #JmxRegistrationManager(javax.management.MBeanServer, org.jboss.cache.Cache, javax.management.ObjectName)
     */
    public JmxRegistrationManager(MBeanServer mBeanServer, Cache cache, String objectNameBase)
    {
@@ -105,6 +120,9 @@
       this(cache, null);
    }
 
+   /**
+    * Performs the MBean registration.
+    */
    public void registerAllMBeans() throws CacheException
    {
       try
@@ -125,6 +143,9 @@
       }
    }
 
+   /**
+    * Unregisters all the MBeans registered through {@link #registerAllMBeans()}.
+    */
    public void unregisterAllMBeans() throws CacheException
    {
       log.trace("Unregistering jmx resources..");
@@ -174,11 +195,11 @@
       }
       else //the cache is clustered
       {
-         objectNameBase = CLUSTERED_CACHE_PREFIX + cacheSpi.getConfiguration().getClusterName();
+         objectNameBase = REPLICATED_CACHE_PREFIX + cacheSpi.getConfiguration().getClusterName();
       }
    }
 
-   private String getObjectName(String resourceName)
+   public String getObjectName(String resourceName)
    {
       return objectNameBase + JMX_RESOURCE_KEY + resourceName;
    }

Modified: core/trunk/src/test/java/org/jboss/cache/jmx/JmxRegistrationManagerTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/jmx/JmxRegistrationManagerTest.java	2008-08-09 03:21:50 UTC (rev 6551)
+++ core/trunk/src/test/java/org/jboss/cache/jmx/JmxRegistrationManagerTest.java	2008-08-11 09:58:39 UTC (rev 6552)
@@ -21,10 +21,81 @@
  */
 package org.jboss.cache.jmx;
 
+import org.testng.annotations.Test;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.AfterMethod;
+import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheFactory;
+import org.jboss.cache.interceptors.CacheMgmtInterceptor;
+
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.ObjectName;
+
 /**
+ * Tester class for {@link JmxRegistrationManager}.
+ *
  * @author Mircea.Markus at jboss.com
  * @since 3.0
  */
+ at Test (groups = "functional")
 public class JmxRegistrationManagerTest
 {
+   private DefaultCacheFactory cacheFactory = new DefaultCacheFactory();
+   private MBeanServer mBeanServer;
+
+   @BeforeMethod
+   public void setUp()
+   {
+      mBeanServer = MBeanServerFactory.createMBeanServer();
+   }
+
+   @AfterMethod
+   public void tearDown()
+   {
+      MBeanServerFactory.releaseMBeanServer(mBeanServer);
+   }
+
+   public void testRegisterLocalCache() throws Exception
+   {
+      Configuration localConfig = UnitTestCacheConfigurationFactory.createConfiguration(Configuration.CacheMode.LOCAL);
+      localConfig.setExposeManagementStatistics(true);
+      Cache cache = cacheFactory.createCache(localConfig);
+      JmxRegistrationManager regManager = new JmxRegistrationManager(mBeanServer, cache, (ObjectName)null);
+      assert regManager.getObjectNameBase().indexOf(JmxRegistrationManager.LOCAL_CACHE_PREFIX) == 0;
+      regManager.registerAllMBeans();
+      String name = regManager.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
+      assert mBeanServer.isRegistered(new ObjectName(name));
+      regManager.unregisterAllMBeans();
+      assert !mBeanServer.isRegistered(new ObjectName(name));
+      cache.stop();
+   }
+
+   public void testRegisterReplicatedCache() throws Exception
+   {
+      Configuration localConfig = UnitTestCacheConfigurationFactory.createConfiguration(Configuration.CacheMode.REPL_SYNC);
+      localConfig.setExposeManagementStatistics(true);
+      Cache cache = cacheFactory.createCache(localConfig);
+      JmxRegistrationManager regManager = new JmxRegistrationManager(mBeanServer, cache, (ObjectName)null);
+      assert regManager.getObjectNameBase().indexOf(JmxRegistrationManager.REPLICATED_CACHE_PREFIX) == 0;
+      regManager.registerAllMBeans();
+      String name = regManager.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
+      assert mBeanServer.isRegistered(new ObjectName(name));
+      regManager.unregisterAllMBeans();
+      assert !mBeanServer.isRegistered(new ObjectName(name));
+      cache.stop();
+   }
+
+   public static void main(String[] args)
+   {
+      Configuration localConfig = UnitTestCacheConfigurationFactory.createConfiguration(Configuration.CacheMode.REPL_SYNC);
+      localConfig.setExposeManagementStatistics(true);
+      CacheFactory cacheFactory = new DefaultCacheFactory();
+      Cache cache = cacheFactory.createCache(localConfig);
+      JmxRegistrationManager regManager = new JmxRegistrationManager(cache);
+      while (true){}
+   }
 }

Modified: core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/CacheJmxWrapperTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/CacheJmxWrapperTest.java	2008-08-09 03:21:50 UTC (rev 6551)
+++ core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/CacheJmxWrapperTest.java	2008-08-11 09:58:39 UTC (rev 6552)
@@ -85,7 +85,7 @@
 
       // Go back to the default
       wrapper.setCacheObjectName(null);
-      assertEquals("Got default ObjectName", JmxRegistrationManager.CLUSTERED_CACHE_PREFIX + CLUSTER_NAME, wrapper.getCacheObjectName());
+      assertEquals("Got default ObjectName", JmxRegistrationManager.REPLICATED_CACHE_PREFIX + CLUSTER_NAME, wrapper.getCacheObjectName());
 
       registerWrapper(wrapper);
       assertEquals("Returns standard name", mBeanName, new ObjectName(wrapper.getCacheObjectName()));

Modified: core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/CacheJmxWrapperTestBase.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/CacheJmxWrapperTestBase.java	2008-08-09 03:21:50 UTC (rev 6551)
+++ core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/CacheJmxWrapperTestBase.java	2008-08-11 09:58:39 UTC (rev 6552)
@@ -41,7 +41,7 @@
    {
       mBeanServer = MBeanServerFactory.createMBeanServer("CacheMBeanTest");
 
-      mBeanNameStr = JmxRegistrationManager.CLUSTERED_CACHE_PREFIX + CLUSTER_NAME;
+      mBeanNameStr = JmxRegistrationManager.REPLICATED_CACHE_PREFIX + CLUSTER_NAME;
       mBeanName = new ObjectName(mBeanNameStr);
    }
 

Modified: core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/NotificationTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/NotificationTest.java	2008-08-09 03:21:50 UTC (rev 6551)
+++ core/trunk/src/test/java/org/jboss/cache/jmx/deprecated/NotificationTest.java	2008-08-11 09:58:39 UTC (rev 6552)
@@ -116,7 +116,7 @@
 
    protected ObjectName getWrapperObjectName() throws Exception
    {
-      return new ObjectName(JmxRegistrationManager.CLUSTERED_CACHE_PREFIX + CLUSTER_NAME);
+      return new ObjectName(JmxRegistrationManager.REPLICATED_CACHE_PREFIX + CLUSTER_NAME);
    }
 
    public void testNotifications() throws Exception




More information about the jbosscache-commits mailing list