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

Brian Stansberry brian.stansberry at jboss.com
Tue May 22 16:14:26 EDT 2007


  User: bstansberry
  Date: 07/05/22 16:14:26

  Added:       src/org/jboss/cache   LifecycleState.java LifecycleUtil.java
  Log:
  [JBCACHE-1058] Add utility classes to help manage lifecycle
  
  Revision  Changes    Path
  1.1      date: 2007/05/22 20:14:26;  author: bstansberry;  state: Exp;JBossCache/src/org/jboss/cache/LifecycleState.java
  
  Index: LifecycleState.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  
  package org.jboss.cache;
  
  /**
   * Various states that an object that has a four stage lifecycle 
   * (i.e. <code>create()</code>, <code>start()</code>, <code>stop()</code>
   * and <code>destroy()</code>) might be in.
   * 
   * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
   * @version $Revision: 1.1 $
   */
  public enum LifecycleState 
  {
     /**
      * Object has been instantiated, but create() has not been called.
      */
     INSTANTIATED,   
     /**
      * The <code>create()</code> method has been called but <code>start()</code>
      * has not been called.
      */
     CREATED,   
     /**
      * The <code>start()</code> method has been called but has not yet completed.
      */
     STARTING,
     /**
      * The <code>start()</code> method has completed.
      */
     STARTED,
     /**
      * The <code>stop()</code> method has been called but has not yet completed.
      */
     STOPPING, 
     /**
      * The <code>stop()</code> method has completed but <code>destroy()</code> 
      * has not yet been called. Conceptually equivalent to {@link #CREATED}.
      */
     STOPPED,
     /**
      * The {@link Lifecycle#destroy() destroy()} method has completed.
      * Conceptually equivalent to {@link #INSTANTIATED}.
      */
     DESTROYED, 
     /**
      * A failure occurred during the execution of <code>start()</code> 
      * or <code>stop()</code>. The next logical transition is to call 
      * <code>destroy()</code>.
      */
     FAILED;
  }
  
  
  
  1.1      date: 2007/05/22 20:14:26;  author: bstansberry;  state: Exp;JBossCache/src/org/jboss/cache/LifecycleUtil.java
  
  Index: LifecycleUtil.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  
  package org.jboss.cache;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * Utility methods to help with lifecycle transitions.
   * 
   * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
   * @version $Revision: 1.1 $
   */
  public class LifecycleUtil
  {
     private static final Log log = LogFactory.getLog(LifecycleUtil.class);
     
     /** Prevent instantiation */
     private LifecycleUtil()
     {      
     } 
  
     public static boolean createAllowed(LifecycleState currentState)
     {
        switch(currentState)
        {
           case CREATED:
           case STARTING:
           case STARTED:
           case STOPPING:
           case STOPPED:
              log.debug("Ignoring call to create() as current state is " + currentState);
              // fall through
           case FAILED:
              return false;
           case INSTANTIATED:
           case DESTROYED:
           default:
              return true;
        }
     }
     
     public static boolean needToDestroyFailedCache(LifecycleState currentState)
     {
        if (currentState == LifecycleState.FAILED)
        {
           log.debug("need to call destroy() since current state is " + 
                     currentState);
           return true;
        }
        
        return false;      
     }
     
     public static boolean startAllowed(LifecycleState currentState)
     {
        switch(currentState)
        {
           case INSTANTIATED:
           case DESTROYED:         
           case STARTING:
           case STARTED:
           case STOPPING:
              log.debug("Ignoring call to start() as current state is " + currentState);
              // fall through
           case FAILED: 
              return false;
           case CREATED:
           case STOPPED:
           default:
              return true;
        }
     }
     
     public static boolean needCreateBeforeStart(LifecycleState currentState)
     {
        switch(currentState)
        {
           case INSTANTIATED:
           case DESTROYED:
              log.debug("start() called while current state is " + 
                        currentState + " -- calling create() first");
              return true;
           default:
              return false;
        }
     }
     
     public static boolean stopAllowed(LifecycleState currentState)
     {
        switch(currentState)
        {
           case INSTANTIATED:
           case CREATED:
           case STOPPED:
           case DESTROYED:
              log.debug("Ignoring call to stop() as current state is " + currentState);
              // fall through
           case FAILED:
              return false;
           case STARTING:
           case STOPPING:
              log.warn("Ignoring call to stop() while cache is starting/stopping -- current state is " + currentState);
              return false;
           case STARTED:
           default:
              return true;
        }
        
     }
     
     public static boolean destroyAllowed(LifecycleState currentState)
     {
        switch(currentState)
        {
           case INSTANTIATED:
           case DESTROYED:
              log.debug("Ignoring call to destroy() as current state is " + currentState);
              return false;
           case STARTING:
           case STOPPING:
              log.warn("Ignoring call to destroy() while cache is starting/stopping -- current state is " + currentState);
              return false;
           case STARTED:
              // stop first
              return false;
           case CREATED:
           case STOPPED:
           case FAILED:
           default:
              return true;
        }      
     }
     
     public static boolean needStopBeforeDestroy(LifecycleState currentState)
     {
        if (currentState == LifecycleState.STARTED)
        {
           log.warn("destroy() called while current state is " + 
                     currentState + " -- calling stop() first");
           return true;
        }
        
        return false;         
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list