Author: manik.surtani(a)jboss.com
Date: 2007-12-20 09:53:11 -0500 (Thu, 20 Dec 2007)
New Revision: 4896
Modified:
core/trunk/src/main/java/org/jboss/cache/CacheImpl.java
core/trunk/src/main/java/org/jboss/cache/CacheSPI.java
core/trunk/src/main/java/org/jboss/cache/interceptors/MethodDispacherInterceptor.java
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java
core/trunk/src/test/java/org/jboss/cache/misc/TestingUtil.java
core/trunk/src/test/java/org/jboss/cache/optimistic/AbstractOptimisticTestCase.java
core/trunk/src/test/java/org/jboss/cache/optimistic/CacheTest.java
core/trunk/src/test/java/org/jboss/cache/optimistic/OptimisticReplicationInterceptorTest.java
core/trunk/src/test/java/org/jboss/cache/optimistic/TxInterceptorTest.java
Log:
Ability to add and remove interceptors based on interceptor type, not just position
Modified: core/trunk/src/main/java/org/jboss/cache/CacheImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/CacheImpl.java 2007-12-20 14:19:44 UTC (rev
4895)
+++ core/trunk/src/main/java/org/jboss/cache/CacheImpl.java 2007-12-20 14:53:11 UTC (rev
4896)
@@ -3735,6 +3735,54 @@
setInterceptorChain(factory.correctInterceptorChaining(i));
}
+ public synchronized void removeInterceptor(Class<? extends Interceptor>
interceptorType)
+ {
+ InterceptorChainFactory factory =
componentRegistry.getComponent(InterceptorChainFactory.class);
+ List<Interceptor> interceptors = getInterceptors();
+ int position = -1;
+ boolean found = false;
+ for (Interceptor interceptor : interceptors)
+ {
+ position++;
+ if (interceptor.getClass().equals(interceptorType))
+ {
+ found = true;
+ break;
+ }
+ }
+
+ if (found)
+ {
+ interceptors.remove(position);
+ setInterceptorChain(factory.correctInterceptorChaining(interceptors));
+ }
+ }
+
+ public synchronized void addInterceptor(Interceptor i, Class<? extends
Interceptor> afterInterceptor)
+ {
+ InterceptorChainFactory factory =
componentRegistry.getComponent(InterceptorChainFactory.class);
+ List<Interceptor> interceptors = getInterceptors();
+ int position = -1;
+ boolean found = false;
+ for (Interceptor interceptor : interceptors)
+ {
+ position++;
+ if (interceptor.getClass().equals(afterInterceptor))
+ {
+ found = true;
+ break;
+ }
+ }
+
+ if (found)
+ {
+ componentRegistry.registerComponent(i);
+ interceptors.add(position++, i);
+ setInterceptorChain(factory.correctInterceptorChaining(interceptors));
+ }
+ }
+
+
public RPCManager getRPCManager()
{
return configuration.getRuntimeConfig().getRPCManager();
Modified: core/trunk/src/main/java/org/jboss/cache/CacheSPI.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/CacheSPI.java 2007-12-20 14:19:44 UTC (rev
4895)
+++ core/trunk/src/main/java/org/jboss/cache/CacheSPI.java 2007-12-20 14:53:11 UTC (rev
4896)
@@ -100,6 +100,15 @@
void addInterceptor(Interceptor i, int position);
/**
+ * Adds a custom interceptor to the interceptor chain, after an instance of the
specified interceptor type. Throws a
+ * cache exception if it cannot find an interceptor of the specified type.
+ *
+ * @param i interceptor to add
+ * @param afterInterceptor interceptor type after which to place custom interceptor
+ */
+ void addInterceptor(Interceptor i, Class<? extends Interceptor>
afterInterceptor);
+
+ /**
* Removes the interceptor at a specified position, where the first interceptor in the
chain
* is at position 0 and the last one at getInterceptorChain().size() - 1.
*
@@ -108,6 +117,13 @@
void removeInterceptor(int position);
/**
+ * Removes the interceptor of specified type.
+ *
+ * @param interceptorType type of interceptor to remove
+ */
+ void removeInterceptor(Class<? extends Interceptor> interceptorType);
+
+ /**
* Retrieves the current CacheCacheLoaderManager instance associated with the current
Cache instance.
* <p/>
* From 2.1.0, Interceptor authors should obtain this by injection rather than this
method. See the
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/MethodDispacherInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/MethodDispacherInterceptor.java 2007-12-20
14:19:44 UTC (rev 4895)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/MethodDispacherInterceptor.java 2007-12-20
14:53:11 UTC (rev 4896)
@@ -27,11 +27,11 @@
* <p/>
* Implementation notes:
* Current implementation checks to see the methods that are overwritten and does only
perform calls to those methods.
- * This is for avoiding the casts needed to build parameter list. If a non overwritten
method is invoked,
+ * This is for avoiding the casts needed to build parameter list. If a non-overridden
method is invoked,
* then next interceptor will be called.
*
* @author Mircea.Markus(a)jboss.com
- * @version 2.2
+ * @version 2.1.0
* todo - Refactor stuff in txint
* todo - revisit backward compatibility
*/
Modified:
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java 2007-12-20
14:19:44 UTC (rev 4895)
+++
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java 2007-12-20
14:53:11 UTC (rev 4896)
@@ -121,11 +121,21 @@
cache.addInterceptor(i, position);
}
+ public void addInterceptor(Interceptor i, Class<? extends Interceptor>
afterInterceptor)
+ {
+ cache.addInterceptor(i, afterInterceptor);
+ }
+
public void removeInterceptor(int position)
{
cache.removeInterceptor(position);
}
+ public void removeInterceptor(Class<? extends Interceptor> interceptorType)
+ {
+ cache.removeInterceptor(interceptorType);
+ }
+
public CacheLoaderManager getCacheLoaderManager()
{
return cacheLoaderManager;
Modified: core/trunk/src/test/java/org/jboss/cache/misc/TestingUtil.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/misc/TestingUtil.java 2007-12-20 14:19:44 UTC
(rev 4895)
+++ core/trunk/src/test/java/org/jboss/cache/misc/TestingUtil.java 2007-12-20 14:53:11 UTC
(rev 4896)
@@ -13,7 +13,6 @@
import org.jboss.cache.CacheStatus;
import org.jboss.cache.Fqn;
import org.jboss.cache.factories.ComponentRegistry;
-import org.jboss.cache.factories.InterceptorChainFactory;
import org.jboss.cache.interceptors.Interceptor;
import org.jboss.cache.invocation.RemoteCacheInvocationDelegate;
import org.jboss.cache.loader.CacheLoader;
@@ -527,15 +526,8 @@
ComponentRegistry cr = extractComponentRegistry(cache);
// This will replace the previous interceptor chain in the component registry
+ // as well as update dependencies!
cr.registerComponent(Interceptor.class.getName(), interceptor);
-
- // update all component dependencies
- cr.updateDependencies();
-
- InterceptorChainFactory factory = cr.getComponent(InterceptorChainFactory.class);
-
- // make sure the new interceptors have their deps satisfied.
- factory.correctInterceptorChaining(interceptor);
}
/**
Modified:
core/trunk/src/test/java/org/jboss/cache/optimistic/AbstractOptimisticTestCase.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/optimistic/AbstractOptimisticTestCase.java 2007-12-20
14:19:44 UTC (rev 4895)
+++
core/trunk/src/test/java/org/jboss/cache/optimistic/AbstractOptimisticTestCase.java 2007-12-20
14:53:11 UTC (rev 4896)
@@ -10,12 +10,13 @@
import org.jboss.cache.config.CacheLoaderConfig;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.factories.XmlConfigurationParser;
+import org.jboss.cache.interceptors.CacheMgmtInterceptor;
+import org.jboss.cache.interceptors.CallInterceptor;
import org.jboss.cache.interceptors.Interceptor;
-import org.jboss.cache.interceptors.InvocationContextInterceptor;
-import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
+import org.jboss.cache.interceptors.NotificationInterceptor;
+import org.jboss.cache.interceptors.OptimisticLockingInterceptor;
import org.jboss.cache.interceptors.OptimisticNodeInterceptor;
-import org.jboss.cache.interceptors.OptimisticReplicationInterceptor;
-import org.jboss.cache.interceptors.TxInterceptor;
+import org.jboss.cache.interceptors.OptimisticValidatorInterceptor;
import org.jboss.cache.loader.DummyInMemoryCacheLoader;
import org.jboss.cache.loader.DummySharedInMemoryCacheLoader;
import org.jboss.cache.lock.IsolationLevel;
@@ -284,37 +285,14 @@
}
}
- protected Interceptor getAlteredInterceptorChain(Interceptor newLast,
CacheSPI<Object, Object> spi, boolean replicated)
+ protected void setAlteredInterceptorChain(Interceptor newLast, CacheSPI<Object,
Object> spi)
{
- Interceptor ici = new InvocationContextInterceptor();
- ici.setCache(spi);
-
- Interceptor txInterceptor = new TxInterceptor();
- txInterceptor.setCache(spi);
-
- Interceptor replicationInterceptor = new OptimisticReplicationInterceptor();
- replicationInterceptor.setCache(spi);
-
- Interceptor createInterceptor = new OptimisticCreateIfNotExistsInterceptor();
- createInterceptor.setCache(spi);
-
- Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
- nodeInterceptor.setCache(spi);
-
- ici.setNext(txInterceptor);
- if (replicated)
- {
- txInterceptor.setNext(replicationInterceptor);
- replicationInterceptor.setNext(createInterceptor);
- }
- else
- {
- txInterceptor.setNext(createInterceptor);
- }
- createInterceptor.setNext(nodeInterceptor);
- nodeInterceptor.setNext(newLast);
-
- return ici;
+ spi.removeInterceptor(CacheMgmtInterceptor.class);
+ spi.removeInterceptor(NotificationInterceptor.class);
+ spi.removeInterceptor(OptimisticLockingInterceptor.class);
+ spi.removeInterceptor(OptimisticValidatorInterceptor.class);
+ spi.removeInterceptor(CallInterceptor.class);
+ spi.addInterceptor(newLast, OptimisticNodeInterceptor.class);
}
public abstract class ExceptionThread extends Thread
Modified: core/trunk/src/test/java/org/jboss/cache/optimistic/CacheTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/optimistic/CacheTest.java 2007-12-20 14:19:44
UTC (rev 4895)
+++ core/trunk/src/test/java/org/jboss/cache/optimistic/CacheTest.java 2007-12-20 14:53:11
UTC (rev 4896)
@@ -131,7 +131,7 @@
public void testLocalTransaction() throws Exception
{
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(c, getAlteredInterceptorChain(dummy, c,
true));
+ setAlteredInterceptorChain(dummy, c);
TransactionManager mgr =
c.getConfiguration().getRuntimeConfig().getTransactionManager();
assertNull(mgr.getTransaction());
@@ -166,7 +166,7 @@
c = createCacheWithListener();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(c, getAlteredInterceptorChain(dummy, c,
true));
+ setAlteredInterceptorChain(dummy, c);
TransactionManager mgr =
c.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -196,7 +196,7 @@
c = createCacheWithListener();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(c, getAlteredInterceptorChain(dummy, c,
true));
+ setAlteredInterceptorChain(dummy, c);
TransactionManager mgr =
c.getConfiguration().getRuntimeConfig().getTransactionManager();
Modified:
core/trunk/src/test/java/org/jboss/cache/optimistic/OptimisticReplicationInterceptorTest.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/optimistic/OptimisticReplicationInterceptorTest.java 2007-12-20
14:19:44 UTC (rev 4895)
+++
core/trunk/src/test/java/org/jboss/cache/optimistic/OptimisticReplicationInterceptorTest.java 2007-12-20
14:53:11 UTC (rev 4896)
@@ -55,7 +55,7 @@
public void testLocalTransaction() throws Exception
{
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
assertNull(mgr.getTransaction());
@@ -86,7 +86,7 @@
public void testRollbackTransaction() throws Exception
{
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
assertNull(mgr.getTransaction());
@@ -112,7 +112,7 @@
public void testRemotePrepareTransaction() throws Exception
{
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
@@ -177,7 +177,7 @@
public void testRemoteRollbackTransaction() throws Exception
{
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
@@ -256,7 +256,7 @@
public void testRemoteCommitNoPrepareTransaction() throws Exception
{
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
@@ -317,7 +317,7 @@
public void testRemoteRollbackNoPrepareTransaction() throws Throwable
{
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
@@ -372,7 +372,7 @@
public void testRemoteCommitTransaction() throws Exception
{
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
@@ -455,11 +455,11 @@
cache = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
CacheSPI cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
MockInterceptor dummy2 = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache2, getAlteredInterceptorChain(dummy2,
cache2, true));
+ setAlteredInterceptorChain(dummy, cache);
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
@@ -508,14 +508,14 @@
cache = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
CacheSPI cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
MockFailureInterceptor dummy2 = new MockFailureInterceptor();
List failures = new ArrayList();
failures.add(MethodDeclarations.optimisticPrepareMethod);
dummy2.setFailurelist(failures);
- TestingUtil.replaceInterceptorChain(cache2, getAlteredInterceptorChain(dummy2,
cache2, true));
+ setAlteredInterceptorChain(dummy, cache);
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
@@ -571,11 +571,11 @@
cache = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
MockFailureInterceptor dummy = new MockFailureInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
CacheSPI cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
MockInterceptor dummy2 = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache2, getAlteredInterceptorChain(dummy2,
cache2, true));
+ setAlteredInterceptorChain(dummy, cache);
List failures = new ArrayList();
failures.add(MethodDeclarations.optimisticPrepareMethod);
Modified: core/trunk/src/test/java/org/jboss/cache/optimistic/TxInterceptorTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/optimistic/TxInterceptorTest.java 2007-12-20
14:19:44 UTC (rev 4895)
+++ core/trunk/src/test/java/org/jboss/cache/optimistic/TxInterceptorTest.java 2007-12-20
14:53:11 UTC (rev 4896)
@@ -38,7 +38,7 @@
{
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
false));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
assertNull(mgr.getTransaction());
assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
@@ -66,7 +66,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
false));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -103,7 +103,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
false));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -134,7 +134,7 @@
{
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
false));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -162,7 +162,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
false));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -190,7 +190,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
false));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -233,7 +233,7 @@
{
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
false));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -270,7 +270,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
false));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -339,7 +339,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
false));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -421,7 +421,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -510,7 +510,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -598,7 +598,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -690,7 +690,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
true));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
@@ -777,7 +777,7 @@
CacheSPI<Object, Object> cache = createCache();
MockInterceptor dummy = new MockInterceptor();
- TestingUtil.replaceInterceptorChain(cache, getAlteredInterceptorChain(dummy, cache,
false));
+ setAlteredInterceptorChain(dummy, cache);
TransactionManager mgr =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();