[jboss-cvs] JBossCache/src/org/jboss/cache/pojo/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/pojo/jmx PojoCacheJmxWrapper.java
Log:
[JBCACHE-928] Expose CacheStatus
Rename LifecycleState to CacheStatus, add CREATING/DESTROYING
Merge LifecycleUtil methods into CacheStatus
Revision Changes Path
1.10 +104 -53 JBossCache/src/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapper.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PojoCacheJmxWrapper.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapper.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- PojoCacheJmxWrapper.java 23 May 2007 10:28:56 -0000 1.9
+++ PojoCacheJmxWrapper.java 23 May 2007 19:26:53 -0000 1.10
@@ -24,8 +24,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.CacheException;
-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.jmx.CacheJmxWrapper;
@@ -55,14 +54,14 @@
private CacheJmxWrapper plainCacheWrapper;
private boolean registerPlainCache = true;
private boolean plainCacheRegistered;
- private LifecycleState lifecycleState;
+ private CacheStatus cacheStatus;
/**
* Default constructor.
*/
public PojoCacheJmxWrapper()
{
- lifecycleState = LifecycleState.INSTANTIATED;
+ cacheStatus = CacheStatus.INSTANTIATED;
}
/**
@@ -72,7 +71,7 @@
*/
public PojoCacheJmxWrapper(PojoCache toWrap)
{
- lifecycleState = LifecycleState.INSTANTIATED;
+ cacheStatus = CacheStatus.INSTANTIATED;
setPojoCache(toWrap);
}
@@ -100,14 +99,18 @@
public void create() throws PojoCacheException
{
- if (LifecycleUtil.createAllowed(lifecycleState) == false)
+ if (cacheStatus.createAllowed() == false)
{
- if (LifecycleUtil.needToDestroyFailedCache(lifecycleState))
+ if (cacheStatus.needToDestroyFailedCache())
destroy();
else
return;
}
+ try
+ {
+ cacheStatus = CacheStatus.CREATING;
+
if (pojoCache == null)
{
if (config == null)
@@ -125,70 +128,85 @@
plainCacheWrapper.create();
- lifecycleState = LifecycleState.CREATED;
+ cacheStatus = CacheStatus.CREATED;
+ }
+ catch (Throwable t)
+ {
+ handleLifecycleTransitionFailure(t);
+ }
}
public void start() throws PojoCacheException
{
- if (LifecycleUtil.startAllowed(lifecycleState) == false)
+ if (cacheStatus.startAllowed() == false)
{
- if (LifecycleUtil.needToDestroyFailedCache(lifecycleState))
+ if (cacheStatus.needToDestroyFailedCache())
destroy();
- if (LifecycleUtil.needCreateBeforeStart(lifecycleState))
+ if (cacheStatus.needCreateBeforeStart())
create();
else
return;
}
- lifecycleState = LifecycleState.STARTING;
-
try
{
+ cacheStatus = CacheStatus.STARTING;
+
pojoCache.start();
plainCacheWrapper.start();
- lifecycleState = LifecycleState.STARTED;
+ 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;
+
pojoCache.stop();
plainCacheWrapper.stop();
- lifecycleState = LifecycleState.STOPPED;
+ cacheStatus = CacheStatus.STOPPED;
}
- catch (RuntimeException e)
+ catch (Throwable t)
+ {
+ if (failed)
{
- lifecycleState = LifecycleState.FAILED;
- throw e;
+ 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 (LifecycleUtil.needStopBeforeDestroy(lifecycleState))
+ if (cacheStatus.needStopBeforeDestroy())
stop();
else
return;
}
+ try
+ {
+ cacheStatus = CacheStatus.DESTROYING;
+
pojoCache.destroy();
// The cache is destroyed, so we shouldn't leave it registered
@@ -196,13 +214,22 @@
unregisterPlainCache();
plainCacheWrapper.destroy();
+ }
+ finally
+ {
+ // We always proceed to DESTROYED
+ cacheStatus = CacheStatus.DESTROYED;
+ }
+ }
- lifecycleState = LifecycleState.DESTROYED;
+ public CacheStatus getCacheStatus()
+ {
+ return cacheStatus;
}
- public LifecycleState getLifecycleState()
+ public CacheStatus getState()
{
- return lifecycleState;
+ return getCacheStatus();
}
public boolean getRegisterPlainCache()
@@ -315,8 +342,8 @@
public void setPojoCache(PojoCache 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()");
this.pojoCache = cache;
@@ -390,4 +417,28 @@
plainCacheRegistered = false;
}
}
+
+ /**
+ * 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 PojoCacheException
+ * @throws RuntimeException
+ * @throws Error
+ */
+ private void handleLifecycleTransitionFailure(Throwable t)
+ throws PojoCacheException, RuntimeException, Error
+ {
+ cacheStatus = CacheStatus.FAILED;
+ if (t instanceof CacheException)
+ throw (PojoCacheException) t;
+ else if (t instanceof RuntimeException)
+ throw (RuntimeException) t;
+ else if (t instanceof Error)
+ throw (Error) t;
+ else
+ throw new PojoCacheException(t);
+ }
}
More information about the jboss-cvs-commits
mailing list