[jboss-cvs] JBossCache/src/org/jboss/cache/jmx ...

Brian Stansberry brian.stansberry at jboss.com
Wed May 9 14:31:19 EDT 2007


  User: bstansberry
  Date: 07/05/09 14:31:19

  Modified:    src/org/jboss/cache/jmx  CacheJmxWrapper.java
  Log:
  [JBCACHE-1050] Don't throw NPE if getInterceptorChain() invoked before create()
  [JBCACHE-1047] JMX Wrapper classes allow explicit config of whether to control cache lifecycle
  
  Revision  Changes    Path
  1.15      +54 -32    JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapper.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheJmxWrapper.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapper.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -b -r1.14 -r1.15
  --- CacheJmxWrapper.java	12 Mar 2007 18:13:47 -0000	1.14
  +++ CacheJmxWrapper.java	9 May 2007 18:31:19 -0000	1.15
  @@ -21,15 +21,7 @@
    */
   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.CacheImpl;
  -import org.jboss.cache.DefaultCacheFactory;
  -import org.jboss.cache.config.Configuration;
  -import org.jboss.cache.config.ConfigurationException;
  -import org.jboss.cache.interceptors.CacheMgmtInterceptor;
  -import org.jboss.cache.interceptors.Interceptor;
  +import java.util.List;
   
   import javax.management.ListenerNotFoundException;
   import javax.management.MBeanNotificationInfo;
  @@ -40,18 +32,28 @@
   import javax.management.NotificationListener;
   import javax.management.ObjectName;
   
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
  +import org.jboss.cache.Cache;
  +import org.jboss.cache.CacheImpl;
  +import org.jboss.cache.DefaultCacheFactory;
  +import org.jboss.cache.config.Configuration;
  +import org.jboss.cache.config.ConfigurationException;
  +import org.jboss.cache.interceptors.CacheMgmtInterceptor;
  +import org.jboss.cache.interceptors.Interceptor;
  +
   public class CacheJmxWrapper implements CacheJmxWrapperMBean, MBeanRegistration
   {
      private Log log = LogFactory.getLog(getClass().getName());
   
      private MBeanServer server;
      private String cacheObjectName;
  -   private boolean registeredInterceptorsInCreate;
      private boolean interceptorsRegistered;
      private CacheImpl<?, ?> cache;
      private boolean selfConstructed = false;
      private Configuration config;
      private boolean created;
  +   private Boolean manageCacheLifecycle;
      private boolean registerInterceptors = true;
   
      public CacheJmxWrapper()
  @@ -70,17 +72,19 @@
   
      public Configuration getConfiguration()
      {
  -      return cache == null ? null : cache.getConfiguration();
  +      return cache == null ? config : cache.getConfiguration();
      }
   
      public String getConfigurationAsString()
      {
  -      return cache == null ? "Cache is null" : cache.getConfiguration().toString();
  +      Configuration cfg = getConfiguration();
  +      return cfg == null ? "Configuration is null" : cfg.toString();
      }
   
      public String getConfigurationAsHtmlString()
      {
  -      return cache == null ? "Cache is null" : formatHtml(cache.getConfiguration().toString());
  +      Configuration cfg = getConfiguration();
  +      return cfg == null ? "Configuration is null" : formatHtml(cfg.toString());
      }
   
      public String getCacheDetails()
  @@ -130,9 +134,14 @@
   
      private CacheMgmtInterceptor getCacheMgmtInterceptor()
      {
  -      for (Interceptor i : cache.getInterceptors())
  +      List<Interceptor> interc = cache.getInterceptorChain();
  +      if (interc != null)
  +      {
  +         for (Interceptor i : interc)
         {
  -         if (i instanceof CacheMgmtInterceptor) return (CacheMgmtInterceptor) i;
  +            if (i instanceof CacheMgmtInterceptor) 
  +               return (CacheMgmtInterceptor) i;
  +         }
         }
         throw new RuntimeException("Cache management interceptor not found");
      }
  @@ -143,7 +152,7 @@
       * @param s string to format
       * @return formatted string
       */
  -   protected String formatHtml(String s)
  +   public static String formatHtml(String s)
      {
         s = s.replaceAll("\r\n", "<br />");
         s = s.replaceAll("\r", "<br />");
  @@ -165,19 +174,19 @@
            constructCache();
         }
   
  -      if (selfConstructed)
  +      if (getManageCacheLifecycle())
         {
            cache.create();
         }
   
  -      registeredInterceptorsInCreate = registerInterceptors();
  +      registerInterceptors();
   
         created = true;
      }
   
      public void start() throws Exception
      {
  -      if (selfConstructed)
  +      if (getManageCacheLifecycle())
         {
            cache.start();
         }
  @@ -185,7 +194,7 @@
   
      public void stop()
      {
  -      if (selfConstructed)
  +      if (getManageCacheLifecycle())
         {
            cache.stop();
         }
  @@ -193,15 +202,14 @@
   
      public void destroy()
      {
  -      if (selfConstructed)
  +      if (getManageCacheLifecycle())
         {
            cache.destroy();
   
  -         if (registeredInterceptorsInCreate)
  -         {
  +         // The cache is destroyed, so we shouldn't leave the interceptors 
  +         // in JMX, even if we didn't register them in create
               unregisterInterceptors();
            }
  -      }
   
         created = false;
      }
  @@ -216,6 +224,16 @@
         this.registerInterceptors = register;
      }
   
  +   public boolean getManageCacheLifecycle()
  +   {
  +      return manageCacheLifecycle == null ? selfConstructed : manageCacheLifecycle.booleanValue();
  +   }
  +
  +   public void setManageCacheLifecycle(boolean manageCacheLifecycle)
  +   {
  +      this.manageCacheLifecycle = manageCacheLifecycle ? Boolean.TRUE : Boolean.FALSE;
  +   }
  +
      protected void constructCache() throws Exception
      {
         log.debug("Constructing Cache");
  @@ -228,10 +246,14 @@
         if (registerInterceptors && !interceptorsRegistered && server != null)
         {
            log.debug("Registering interceptors");
  -         JmxUtil.registerInterceptors(server, cache.getInterceptorChain(), cacheObjectName);
  +         List<Interceptor> interc = cache.getInterceptorChain();
  +         if (interc != null && interc.size() > 0)
  +         {
  +            JmxUtil.registerInterceptors(server, interc, cacheObjectName);
            interceptorsRegistered = true;
            return true;
         }
  +      }
         return false;
      }
   
  @@ -345,7 +367,7 @@
            throw new IllegalStateException("Cannot set underlying cache after call to create()");
         }
   
  -      // FIXME -- the only reason we need to cast here is to support printCacheDetails 
  +      // FIXME -- having to cast is ugly!!
         this.cache = (CacheImpl) cache;
         this.config = (cache == null ? null : cache.getConfiguration());
      }
  
  
  



More information about the jboss-cvs-commits mailing list