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

Brian Stansberry brian.stansberry at jboss.com
Wed May 23 15:26:53 EDT 2007


  User: bstansberry
  Date: 07/05/23 15:26:53

  Modified:    src/org/jboss/cache/jmx  CacheJmxWrapper.java
  Log:
  [JBCACHE-928] Expose CacheStatus
  Rename LifecycleState to CacheStatus, add CREATING/DESTROYING
  Merge LifecycleUtil methods into CacheStatus
  
  Revision  Changes    Path
  1.23      +117 -58   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.22
  retrieving revision 1.23
  diff -u -b -r1.22 -r1.23
  --- CacheJmxWrapper.java	23 May 2007 10:28:48 -0000	1.22
  +++ CacheJmxWrapper.java	23 May 2007 19:26:53 -0000	1.23
  @@ -27,8 +27,7 @@
   import org.jboss.cache.CacheException;
   import org.jboss.cache.CacheImpl;
   import org.jboss.cache.DefaultCacheFactory;
  -import org.jboss.cache.LifecycleState;
  -import org.jboss.cache.LifecycleUtil;
  +import org.jboss.cache.CacheStatus;
   import org.jboss.cache.config.Configuration;
   import org.jboss.cache.config.ConfigurationException;
   import org.jboss.cache.interceptors.Interceptor;
  @@ -53,7 +52,7 @@
    * {@link CacheImpl}.
    *
    * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
  - * @version $Revision: 1.22 $
  + * @version $Revision: 1.23 $
    */
   public class CacheJmxWrapper
         extends NotificationBroadcasterSupport
  @@ -69,14 +68,14 @@
      private boolean registerInterceptors = true;
      private final AtomicInteger listenerCount = new AtomicInteger(0);
      private final CacheNotificationListener cacheNotificationListener;
  -   private LifecycleState lifecycleState;
  +   private CacheStatus cacheStatus;
   
      // ----------------------------------------------------------- Constructors
   
      public CacheJmxWrapper()
      {
         cacheNotificationListener = new CacheNotificationListener(this);
  -      lifecycleState = LifecycleState.INSTANTIATED;
  +      cacheStatus = CacheStatus.INSTANTIATED;
      }
   
      public CacheJmxWrapper(Cache cache)
  @@ -119,9 +118,14 @@
         return cache == null ? "Cache is null" : formatHtml(cache.printDetails());
      }
   
  -   public LifecycleState getLifecycleState()
  +   public CacheStatus getCacheStatus()
      {
  -      return lifecycleState;
  +      return cacheStatus;
  +   }
  +   
  +   public CacheStatus getState()
  +   {
  +      return getCacheStatus();
      }
   
      public Address getLocalAddress()
  @@ -168,14 +172,18 @@
   
      public void create() throws CacheException
      {
  -      if (LifecycleUtil.createAllowed(lifecycleState) == false)
  +      if (cacheStatus.createAllowed() == false)
         {
  -         if (LifecycleUtil.needToDestroyFailedCache(lifecycleState))
  +         if (cacheStatus.needToDestroyFailedCache())
               destroy();
            else
               return;
         }
   
  +      try
  +      {
  +         cacheStatus = CacheStatus.CREATING;
  +         
         if (cache == null)
         {
            if (config == null)
  @@ -188,86 +196,113 @@
   
         cache.create();
   
  -      lifecycleState = LifecycleState.CREATED;
  +         cacheStatus = CacheStatus.CREATED;
  +      }
  +      catch (Throwable t)
  +      {
  +         handleLifecycleTransitionFailure(t);
  +      }
      }
   
      public void start() throws CacheException
      {
  -      if (LifecycleUtil.startAllowed(lifecycleState) == false)
  +      if (cacheStatus.startAllowed() == false)
         {
  -         if (LifecycleUtil.needToDestroyFailedCache(lifecycleState))
  -            destroy();
  -         if (LifecycleUtil.needCreateBeforeStart(lifecycleState))
  +         if (cacheStatus.needToDestroyFailedCache())
  +            destroy(); // this will take us back to DESTROYED
  +          
  +         if (cacheStatus.needCreateBeforeStart())
               create();
            else
               return;
         }
   
  -      lifecycleState = LifecycleState.STARTING;
  -
         try
         {
  +         cacheStatus = CacheStatus.STARTING;
  +         
            cache.start();
   
            registerInterceptors();
   
  -         lifecycleState = LifecycleState.STARTED;
  -      }
  -      catch (CacheException e)
  -      {
  -         lifecycleState = LifecycleState.FAILED;
  -         throw e;
  +         cacheStatus = CacheStatus.STARTED;
         }
  -      catch (RuntimeException e)
  +      catch (Throwable t)
         {
  -         lifecycleState = LifecycleState.FAILED;
  -         throw e;
  +         handleLifecycleTransitionFailure(t);
         }
      }
   
      public void stop()
      {
  -      if (LifecycleUtil.stopAllowed(lifecycleState) == false)
  +      if (cacheStatus.stopAllowed() == false)
         {
            return;
         }
   
  -      lifecycleState = LifecycleState.STOPPING;
  +      // Trying to stop() from FAILED is valid, but may not work
  +      boolean failed = cacheStatus == CacheStatus.FAILED;
  +      
         try
         {
  +         cacheStatus = CacheStatus.STOPPING;
            cache.stop();
  -         lifecycleState = LifecycleState.STOPPED;
  -      }
  -      catch (RuntimeException e)
  +
  +         if (cache.getCacheStatus() == CacheStatus.DESTROYED)
         {
  -         lifecycleState = LifecycleState.FAILED;
  -         throw e;
  +            // Cache was already destroyed externally; 
  +            // so get rid of the interceptor mbeans
  +            unregisterInterceptors();
         }
   
  -      if (cache.getLifecycleState() == LifecycleState.DESTROYED)
  +         cacheStatus = CacheStatus.STOPPED;
  +      }
  +      catch (Throwable t)
         {
  -         // Cache was already destroyed externally; follow suit
  -         destroy();
  +         if (failed)
  +         {
  +            log.warn("Attempted to stop() from FAILED state, " +
  +                      "but caught exception; try calling destroy()", t);
  +         }
  +         handleLifecycleTransitionFailure(t);
         }
      }
   
      public void destroy()
      {
  -      if (LifecycleUtil.destroyAllowed(lifecycleState) == false)
  +      if (cacheStatus.destroyAllowed() == false)
  +      {
  +         if (cacheStatus.needStopBeforeDestroy())
  +         {
  +            try
         {
  -         if (LifecycleUtil.needStopBeforeDestroy(lifecycleState))
               stop();
  +            }
  +            catch (CacheException e)
  +            {
  +               log.warn("Needed to call stop() before destroying but stop() " +
  +                        "threw exception. Proceeding to destroy", e);
  +            }
  +         }
            else
               return;
         }
   
  +      try
  +      {
  +         cacheStatus = CacheStatus.DESTROYING;
  +         
         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();
  -
  -      lifecycleState = LifecycleState.DESTROYED;
  +      }
  +      finally
  +      {
  +         // We always proceed to DESTROYED
  +         cacheStatus = CacheStatus.DESTROYED;
  +      }
      }
   
      // ------------------------------------------------------  MBeanRegistration
  @@ -362,8 +397,8 @@
       */
      public void setCache(Cache cache)
      {
  -      if (lifecycleState != LifecycleState.INSTANTIATED
  -          && lifecycleState != LifecycleState.DESTROYED)
  +      if (cacheStatus != CacheStatus.INSTANTIATED
  +          && cacheStatus != CacheStatus.DESTROYED)
         {
            throw new IllegalStateException("Cannot set underlying cache after call to create()");
         }
  @@ -540,4 +575,28 @@
            }
         }
      }
  +   
  +   /**
  +    * Sets the cacheStatus to FAILED and rethrows the problem as one
  +    * of the declared types. Converts any non-RuntimeException Exception
  +    * to CacheException.
  +    * 
  +    * @param t
  +    * @throws CacheException
  +    * @throws RuntimeException
  +    * @throws Error
  +    */
  +   private void handleLifecycleTransitionFailure(Throwable t) 
  +      throws CacheException, RuntimeException, Error
  +   {
  +      cacheStatus = CacheStatus.FAILED;
  +      if (t instanceof CacheException)
  +         throw (CacheException) t;
  +      else if (t instanceof RuntimeException)
  +         throw (RuntimeException) t;
  +      else if (t instanceof Error)
  +         throw (Error) t;
  +      else
  +         throw new CacheException(t);
  +   }
   }
  
  
  



More information about the jboss-cvs-commits mailing list