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

Brian Stansberry brian.stansberry at jboss.com
Tue May 22 18:52:52 EDT 2007


  User: bstansberry
  Date: 07/05/22 18:52:52

  Modified:    src/org/jboss/cache/jmx    CacheJmxWrapperMBean.java
                        CacheJmxWrapper.java
  Removed:     src/org/jboss/cache/jmx    LifeCycle.java
  Log:
  [JBCACHE-1068] Get rid of LifeCycle interface
  [JBCACHE-1047] Revert manageCacheLifecycle attribute
  
  Revision  Changes    Path
  1.6       +13 -42    JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapperMBean.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheJmxWrapperMBean.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapperMBean.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -b -r1.5 -r1.6
  --- CacheJmxWrapperMBean.java	21 May 2007 20:19:49 -0000	1.5
  +++ CacheJmxWrapperMBean.java	22 May 2007 22:52:52 -0000	1.6
  @@ -9,6 +9,7 @@
   import java.util.List;
   
   import org.jboss.cache.Cache;
  +import org.jboss.cache.CacheException;
   import org.jboss.cache.config.Configuration;
   import org.jgroups.Address;
   
  @@ -17,15 +18,23 @@
    * set of operations are exposed via JMX:
    * <p/>
    * <ol>
  - * <li> {@link Lifecycle} methods - create, start, stop, destroy</li>
  + * <li> Lifecycle methods - create, start, stop, destroy</li>
    * <li> Configuration (read-only) getter - which retrieves a String (or formatted HTML for web based JMX consoles) representation of the configuration</li>
    * <li> Setters for a specific subset of config elements which may be changed at runtime</li>
    * <li> Cache information methods (numNodes, numAttributes, lockInfo, printDetails) which print as Strings or as formatted HTML (for web based JMX consoles)</li>
    *
    * @since 2.0.0
    */
  -public interface CacheJmxWrapperMBean extends LifeCycle
  +public interface CacheJmxWrapperMBean
   {
  +   void create() throws CacheException;
  +
  +   void start() throws CacheException;
  +
  +   void stop();
  +
  +   void destroy();
  +   
      /**
       * Retrieves a reference to the underlying {@link Cache}
       */
  @@ -96,7 +105,7 @@
   
      /**
       * Gets whether this object should register the cache's interceptors
  -    * with JMX during {@link LifeCycle#create() create()}.
  +    * with JMX during {@link {@link #create()}.
       * <p/>
       * Default is <code>true</code>.
       */
  @@ -104,47 +113,9 @@
   
      /**
       * Sets whether this object should register the cache's interceptors
  -    * with JMX during {@link LifeCycle#create() create()}.
  +    * with JMX during {@link #create()}.
       * <p/>
       * Default is <code>true</code>.
       */
      void setRegisterInterceptors(boolean register);
  -   
  -   /**
  -    * Gets whether this object should call through to the underlying
  -    * {@link Cache} when any of the {@link Lifecycle} methods are invoked.
  -    * <p>
  -    * The value returned from this method will be determined as follows:
  -    * 
  -    * <ol>
  -    * <li>If {@link #setManageCacheLifecycle(boolean)} has been called, the
  -    * provided value will be returned.</li>
  -    * <li>If a <code>Cache</code> has been injected into the object implementing
  -    * this interface, this method will return <code>false</code> (i.e. assume
  -    * whatever code injected the <code>Cache</code> will manage its lifecycle.)</li>
  -    * <li>If the object implementing this interface itself constructed the
  -    * <code>Cache</code> (using an injected <code>Configuration</code>), this 
  -    * method will return <code>true</code> (i.e. assume since we created
  -    * the <code>Cache</code> we will manage its lifecycle.)</li>
  -    * </ol>
  -    * </p>
  -    * 
  -    * @return   <code>true</code> if calls to the lifecycle methods will be 
  -    *           passed through to the underlying cache, <code>false</code>
  -    *           otherwise
  -    */
  -   boolean getManageCacheLifecycle();
  -   
  -   /**
  -    * Sets whether this object should call through to the underlying
  -    * {@link Cache} when any of the {@link Lifecycle} methods are invoked.
  -    * <p>
  -    * See {@link #getManageCacheLifecycle()} for an explanation of the
  -    * default values of this property.
  -    * 
  -    * @param manage <code>true</code> if calls to the lifecycle methods should
  -    *               be passed through to the underlying cache, <code>false</code>
  -    *               otherwise
  -    */
  -   void setManageCacheLifecycle(boolean manage);
   }
  
  
  
  1.20      +101 -41   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.19
  retrieving revision 1.20
  diff -u -b -r1.19 -r1.20
  --- CacheJmxWrapper.java	21 May 2007 20:19:49 -0000	1.19
  +++ CacheJmxWrapper.java	22 May 2007 22:52:52 -0000	1.20
  @@ -24,6 +24,7 @@
   import java.util.List;
   import java.util.concurrent.atomic.AtomicInteger;
   
  +import javax.management.JMException;
   import javax.management.ListenerNotFoundException;
   import javax.management.MBeanNotificationInfo;
   import javax.management.MBeanRegistration;
  @@ -37,20 +38,23 @@
   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.CacheImpl;
   import org.jboss.cache.DefaultCacheFactory;
  +import org.jboss.cache.LifecycleUtil;
  +import org.jboss.cache.LifecycleState;
   import org.jboss.cache.config.Configuration;
   import org.jboss.cache.config.ConfigurationException;
   import org.jboss.cache.interceptors.Interceptor;
   import org.jgroups.Address;
   
   /**
  - * Wrapper class that provides exposes a 
  + * Wrapper class that exposes a
    * {@link CacheJmxWrapperMBean JMX management interface} for an instance of 
    * {@link CacheImpl}.
    * 
    * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
  - * @version $Revision: 1.19 $
  + * @version $Revision: 1.20 $
    */
   public class CacheJmxWrapper 
      extends NotificationBroadcasterSupport
  @@ -62,19 +66,18 @@
      private String cacheObjectName;
      private boolean interceptorsRegistered;
      private CacheImpl<?, ?> cache;
  -   private boolean selfConstructed = false;
      private Configuration config;
  -   private boolean created;
  -   private Boolean manageCacheLifecycle;
      private boolean registerInterceptors = true;
      private final AtomicInteger listenerCount = new AtomicInteger(0);
      private final CacheNotificationListener cacheNotificationListener;
  +   private LifecycleState lifecycleState;
      
      // ----------------------------------------------------------- Constructors
      
      public CacheJmxWrapper()
      {
         cacheNotificationListener = new CacheNotificationListener(this);
  +      lifecycleState = LifecycleState.INSTANTIATED;
      }
   
      public CacheJmxWrapper(Cache cache)
  @@ -117,6 +120,11 @@
         return cache == null ? "Cache is null" : formatHtml(cache.printDetails());
      }
   
  +   public LifecycleState getLifecycleState()
  +   {
  +      return lifecycleState;
  +   }
  +
      public Address getLocalAddress()
      {      
         return cache == null ? null : cache.getLocalAddress();
  @@ -157,20 +165,18 @@
         this.registerInterceptors = register;
      }
   
  -   public boolean getManageCacheLifecycle()
  -   {
  -      return manageCacheLifecycle == null ? selfConstructed : manageCacheLifecycle.booleanValue();
  -   }
  +   // --------------------------------------------------------------  Lifecycle
   
  -   public void setManageCacheLifecycle(boolean manageCacheLifecycle)
  +   public void create() throws CacheException
      {
  -      this.manageCacheLifecycle = manageCacheLifecycle ? Boolean.TRUE : Boolean.FALSE;
  +      if (LifecycleUtil.createAllowed(lifecycleState) == false)
  +      {
  +         if (LifecycleUtil.needToDestroyFailedCache(lifecycleState))
  +            destroy();
  +         else
  +            return;
      }
      
  -   // --------------------------------------------------------------  Lifecycle
  -   
  -   public void create() throws Exception
  -   {
         if (cache == null)
         {
            if (config == null)
  @@ -181,44 +187,91 @@
            constructCache();
         }
   
  -      if (getManageCacheLifecycle())
  -      {
            cache.create();
  +      
  +      lifecycleState = LifecycleState.CREATED;
  +   }
  +
  +   public void start() throws CacheException
  +   {
  +      if (LifecycleUtil.startAllowed(lifecycleState) == false)
  +      {
  +         if (LifecycleUtil.needToDestroyFailedCache(lifecycleState))
  +            destroy();
  +         if (LifecycleUtil.needCreateBeforeStart(lifecycleState))
  +            create();
  +         else
  +            return;
         }
         
  +      lifecycleState = LifecycleState.STARTING;
  +      
  +      try
  +      {
  +         cache.start();
  +         
         registerInterceptors();
         
  -      created = true;
  +         lifecycleState = LifecycleState.STARTED;
      }
  -
  -   public void start() throws Exception
  +      catch (CacheException e)
      {
  -      if (getManageCacheLifecycle())
  +         lifecycleState = LifecycleState.FAILED;
  +         throw e;
  +      }
  +      catch (RuntimeException e)
         {
  -         cache.start();
  +         lifecycleState = LifecycleState.FAILED;
  +         throw e;
         }
      }
   
      public void stop()
      {
  -      if (getManageCacheLifecycle())
  +      if (LifecycleUtil.stopAllowed(lifecycleState) == false)
  +      {
  +         if (LifecycleUtil.needToDestroyFailedCache(lifecycleState))
  +            destroy();
  +         else
  +            return;
  +      }
  +      
  +      lifecycleState = LifecycleState.STOPPING;
  +      try
         {
            cache.stop();
  +         lifecycleState = LifecycleState.STOPPED;
  +      }
  +      catch (RuntimeException e)
  +      {
  +         lifecycleState = LifecycleState.FAILED;
  +         throw e;
  +      }
  +      
  +      if (cache.getLifecycleState() == LifecycleState.DESTROYED)
  +      {
  +         // Cache was already destroyed externally; follow suit
  +         destroy();
         }
      }
   
      public void destroy()
      {
  -      if (getManageCacheLifecycle())
  +      if (LifecycleUtil.destroyAllowed(lifecycleState) == false)
         {
  +         if (LifecycleUtil.needStopBeforeDestroy(lifecycleState))
  +            stop();
  +         else
  +            return;
  +      }
  +      
            cache.destroy();
            
            // 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;
  +      lifecycleState = LifecycleState.DESTROYED;
      }
   
      // ------------------------------------------------------  MBeanRegistration
  @@ -313,7 +366,8 @@
       */
      public void setCache(Cache cache)
      {
  -      if (created)
  +      if (lifecycleState != LifecycleState.INSTANTIATED 
  +            && lifecycleState != LifecycleState.DESTROYED)
         {
            throw new IllegalStateException("Cannot set underlying cache after call to create()");
         }
  @@ -405,10 +459,9 @@
      {
         log.debug("Constructing Cache");
         setCache(DefaultCacheFactory.getInstance().createCache(config, false));
  -      selfConstructed = true;
      }
   
  -   protected boolean registerInterceptors() throws Exception
  +   protected boolean registerInterceptors() throws CacheException
      {
         if (registerInterceptors && !interceptorsRegistered && server != null)
         {
  @@ -416,10 +469,17 @@
            List<Interceptor> interc = cache.getInterceptorChain();
            if (interc != null && interc.size() > 0)
            {
  +            try
  +            {
               JmxUtil.registerInterceptors(server, interc, cacheObjectName);
               interceptorsRegistered = true;
               return true;
            }
  +            catch (JMException e)
  +            {
  +               throw new CacheException("Failed to register interceptors", e);
  +            }
  +         }
         }
         return false;
      }
  
  
  



More information about the jboss-cvs-commits mailing list