[jboss-cvs] JBossCache/src/org/jboss/cache ...
Brian Stansberry
brian.stansberry at jboss.com
Wed May 23 15:26:15 EDT 2007
User: bstansberry
Date: 07/05/23 15:26:15
Modified: src/org/jboss/cache CacheImpl.java
Log:
Rename LifecycleState to CacheStatus, add CREATING/DESTROYING
Merge LifecycleUtil methods into CacheStatus
Revision Changes Path
1.76 +264 -145 JBossCache/src/org/jboss/cache/CacheImpl.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CacheImpl.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/CacheImpl.java,v
retrieving revision 1.75
retrieving revision 1.76
diff -u -b -r1.75 -r1.76
--- CacheImpl.java 23 May 2007 18:44:55 -0000 1.75
+++ CacheImpl.java 23 May 2007 19:26:15 -0000 1.76
@@ -201,7 +201,7 @@
/**
* The current lifecycle state.
*/
- private LifecycleState lifecycleState;
+ private CacheStatus cacheStatus;
/**
* Buddy Manager
@@ -238,7 +238,7 @@
{
notifier = new Notifier<K, V>(this);
regionManager = new RegionManager(this);
- lifecycleState = LifecycleState.INSTANTIATED;
+ cacheStatus = CacheStatus.INSTANTIATED;
}
public StateTransferManager getStateTransferManager()
@@ -537,14 +537,55 @@
*/
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
+ {
+ internalCreate();
+ }
+ catch (Throwable t)
+ {
+ handleLifecycleTransitionFailure(t);
+ }
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * The actual create implementation.
+ *
+ * @throws CacheException
+ */
+ private void internalCreate() throws CacheException
+ {
// Include our clusterName in our log category
configureLogCategory();
@@ -599,7 +640,7 @@
getRegionManager().setDefaultInactive(configuration.isInactiveOnStartup());
- lifecycleState = LifecycleState.CREATED;
+ cacheStatus = CacheStatus.CREATED;
}
private void createTransactionManager()
@@ -661,20 +702,37 @@
*/
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
{
+ internalStart();
+ }
+ catch (Throwable t)
+ {
+ handleLifecycleTransitionFailure(t);
+ }
+ }
+
+ /**
+ * The actual start implementation.
+ *
+ * @throws CacheException
+ * @throws IllegalArgumentException
+ */
+ private void internalStart() throws CacheException, IllegalArgumentException
+ {
+ cacheStatus = CacheStatus.STARTING;
+
createTransactionManager();
// cache loaders should be initialised *before* any state transfers take place to prevent
@@ -772,13 +830,7 @@
log.info("JBoss Cache version: " + getVersion());
- lifecycleState = LifecycleState.STARTED;
- }
- catch (RuntimeException e)
- {
- lifecycleState = LifecycleState.FAILED;
- throw e;
- }
+ cacheStatus = CacheStatus.STARTED;
}
/**
@@ -786,17 +838,67 @@
*/
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
+ {
+ internalDestroy();
+ }
+ finally
+ {
+ // We always progress to destroyed
+ cacheStatus = CacheStatus.DESTROYED;
+ }
+ }
+
+ /**
+ * The actual destroy implementation.
+ *
+ */
+ private void internalDestroy()
+ {
+ cacheStatus = CacheStatus.DESTROYING;
+
regionManager = null;
notifier = null;
- lifecycleState = LifecycleState.DESTROYED;
+
+ // The rest of these should have already been taken care of in stop,
+ // but we do it here as well in case stop failed.
+
+ if (channel != null)
+ {
+ if (channel.isOpen())
+ {
+ try
+ {
+ channel.close();
+ }
+ catch (Exception toLog)
+ {
+ log.error("Problem closing channel; setting it to null", toLog);
+ }
+ }
+ channel = null;
+ configuration.getRuntimeConfig().setChannel(null);
+ }
+ disp = null;
+ tm = null;
}
/**
@@ -804,15 +906,37 @@
*/
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
{
+ internalStop();
+ }
+ catch (Throwable t)
+ {
+ if (failed)
+ {
+ log.warn("Attempted to stop() from FAILED state, " +
+ "but caught exception; try calling destroy()", t);
+ }
+ handleLifecycleTransitionFailure(t);
+ }
+ }
+
+ /**
+ * The actual stop implementation.
+ *
+ */
+ private void internalStop()
+ {
+ cacheStatus = CacheStatus.STOPPING;
+
if (channel != null)
{
log.info("stop(): closing the channel");
@@ -820,6 +944,7 @@
channel = null;
configuration.getRuntimeConfig().setChannel(null);
}
+
if (disp != null)
{
log.info("stop(): stopping the dispatcher");
@@ -856,18 +981,12 @@
// unset transaction manager reference
tm = null;
- lifecycleState = LifecycleState.STOPPED;
- }
- catch (RuntimeException e)
- {
- lifecycleState = LifecycleState.FAILED;
- throw e;
- }
+ cacheStatus = CacheStatus.STOPPED;
}
- public LifecycleState getLifecycleState()
+ public CacheStatus getCacheStatus()
{
- return lifecycleState;
+ return cacheStatus;
}
/* ----------------------- End of MBeanSupport ----------------------- */
@@ -4183,7 +4302,7 @@
public boolean isStarted()
{
- return getLifecycleState() == LifecycleState.STARTED;
+ return getCacheStatus() == CacheStatus.STARTED;
}
protected void setMessageListener(MessageListenerAdaptor ml)
More information about the jboss-cvs-commits
mailing list