[infinispan-commits] Infinispan SVN: r1317 - in trunk/core/src: test/java/org/infinispan/config and 1 other directory.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Wed Dec 23 14:17:32 EST 2009


Author: manik.surtani at jboss.com
Date: 2009-12-23 14:17:31 -0500 (Wed, 23 Dec 2009)
New Revision: 1317

Added:
   trunk/core/src/test/java/org/infinispan/config/CustomInterceptorConfigTest.java
Modified:
   trunk/core/src/main/java/org/infinispan/factories/InterceptorChainFactory.java
Log:
[ISPN-321] (NPE if custom interceptor is created from xml configuration) Test and fix

Modified: trunk/core/src/main/java/org/infinispan/factories/InterceptorChainFactory.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/factories/InterceptorChainFactory.java	2009-12-22 16:16:49 UTC (rev 1316)
+++ trunk/core/src/main/java/org/infinispan/factories/InterceptorChainFactory.java	2009-12-23 19:17:31 UTC (rev 1317)
@@ -29,6 +29,7 @@
 import org.infinispan.factories.annotations.DefaultFactoryFor;
 import org.infinispan.interceptors.*;
 import org.infinispan.interceptors.base.CommandInterceptor;
+import org.infinispan.util.Util;
 
 import java.util.List;
 
@@ -40,6 +41,7 @@
  */
 @DefaultFactoryFor(classes = InterceptorChain.class)
 public class InterceptorChainFactory extends AbstractNamedCacheComponentFactory implements AutoInstantiableFactory {
+
    public CommandInterceptor createInterceptor(Class<? extends CommandInterceptor> clazz) throws IllegalAccessException, InstantiationException {
       CommandInterceptor chainedInterceptor = componentRegistry.getComponent(clazz);
       if (chainedInterceptor == null) {
@@ -136,7 +138,6 @@
             //Nothing...
       }
 
-
       CommandInterceptor callInterceptor = createInterceptor(CallInterceptor.class);
       interceptorChain.appendIntereceptor(callInterceptor);
       if (log.isTraceEnabled()) log.trace("Finished building default interceptor chain.");
@@ -144,31 +145,45 @@
       return interceptorChain;
    }
 
-   private void buildCustomInterceptors(InterceptorChain interceptorChain, List<CustomInterceptorConfig> customInterceptors) {
+   @SuppressWarnings("unchecked")
+   private Class<? extends CommandInterceptor> getCustomInterceptorType(CustomInterceptorConfig cfg) throws ClassNotFoundException {
+      if (cfg.getInterceptor() != null) return cfg.getInterceptor().getClass();
+      return Util.loadClass(cfg.getClassName());
+   }
+
+   private CommandInterceptor getOrCreateCustomInterceptor(CustomInterceptorConfig cfg) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
+      if (cfg.getInterceptor() != null) return cfg.getInterceptor();
+      return (CommandInterceptor) Util.getInstance(cfg.getClassName());
+   }
+
+   private void buildCustomInterceptors(InterceptorChain interceptorChain, List<CustomInterceptorConfig> customInterceptors) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
+
       for (CustomInterceptorConfig config : customInterceptors) {
-         if (interceptorChain.containsInstance(config.getInterceptor())) continue;
-         if (config.isFirst()) {
-            interceptorChain.addInterceptor(config.getInterceptor(), 0);
-         }
-         if (config.isLast()) interceptorChain.appendIntereceptor(config.getInterceptor());
-         if (config.getIndex() >= 0) interceptorChain.addInterceptor(config.getInterceptor(), config.getIndex());
-         if (config.getAfter() != null) {
+         if (interceptorChain.containsInterceptorType(getCustomInterceptorType(config))) continue;
+         if (config.isFirst())
+            interceptorChain.addInterceptor(getOrCreateCustomInterceptor(config), 0);
+         else if (config.isLast())
+            interceptorChain.appendIntereceptor(getOrCreateCustomInterceptor(config));
+         else if (config.getIndex() >= 0)
+            interceptorChain.addInterceptor(getOrCreateCustomInterceptor(config), config.getIndex());
+         else if (config.getAfter() != null) {
             List<CommandInterceptor> withClassName = interceptorChain.getInterceptorsWithClassName(config.getAfter());
             if (withClassName.isEmpty()) {
                throw new ConfigurationException("Cannot add after class: " + config.getAfter()
-                     + " as no such iterceptor exists in the default chain");
+                     + " as no such interceptor exists in the default chain");
             }
-            interceptorChain.addInterceptorAfter(config.getInterceptor(), withClassName.get(0).getClass());
+            interceptorChain.addInterceptorAfter(getOrCreateCustomInterceptor(config), withClassName.get(0).getClass());
          }
-         if (config.getBefore() != null) {
+         else if (config.getBefore() != null) {
             List<CommandInterceptor> withClassName = interceptorChain.getInterceptorsWithClassName(config.getBefore());
             if (withClassName.isEmpty()) {
                throw new ConfigurationException("Cannot add before class: " + config.getAfter()
-                     + " as no such iterceptor exists in the default chain");
+                     + " as no such interceptor exists in the default chain");
             }
-            interceptorChain.addInterceptorBefore(config.getInterceptor(), withClassName.get(0).getClass());
+            interceptorChain.addInterceptorBefore(getOrCreateCustomInterceptor(config), withClassName.get(0).getClass());
          }
       }
+      
    }
 
    @Override

Added: trunk/core/src/test/java/org/infinispan/config/CustomInterceptorConfigTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/config/CustomInterceptorConfigTest.java	                        (rev 0)
+++ trunk/core/src/test/java/org/infinispan/config/CustomInterceptorConfigTest.java	2009-12-23 19:17:31 UTC (rev 1317)
@@ -0,0 +1,45 @@
+package org.infinispan.config;
+
+import org.infinispan.Cache;
+import org.infinispan.interceptors.InvocationContextInterceptor;
+import org.infinispan.interceptors.base.CommandInterceptor;
+import org.infinispan.manager.CacheManager;
+import org.infinispan.manager.DefaultCacheManager;
+import org.infinispan.test.TestingUtil;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+ at Test(testName = "config.CustomInterceptorConfigTest", groups = "functional")
+public class CustomInterceptorConfigTest {
+   Cache c;
+   CacheManager cm;
+
+   public void testCustomInterceptors() throws IOException {
+      String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
+            "<infinispan xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:infinispan:config:4.0\">" +
+            "<default><customInterceptors> \n" +
+            "<interceptor after=\""+ InvocationContextInterceptor.class.getName()+"\" class=\""+DummyInterceptor.class.getName()+"\"/> \n" +
+            "</customInterceptors> </default></infinispan>";
+
+      InputStream stream = new ByteArrayInputStream(xml.getBytes());
+      cm = new DefaultCacheManager(stream);
+      c = cm.getCache();
+      DummyInterceptor i = TestingUtil.findInterceptor(c, DummyInterceptor.class);
+      assert i != null;
+   }
+
+   @AfterMethod
+   public void tearDown() {
+      if (cm != null) cm.stop();
+   }
+
+   public static class DummyInterceptor extends CommandInterceptor {
+
+   }
+}
+
+


Property changes on: trunk/core/src/test/java/org/infinispan/config/CustomInterceptorConfigTest.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF



More information about the infinispan-commits mailing list