[infinispan-commits] Infinispan SVN: r542 - in trunk/core/src: main/java/org/infinispan/config/parsing and 3 other directories.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Fri Jul 10 04:03:01 EDT 2009
Author: vblagojevic at jboss.com
Date: 2009-07-10 04:03:00 -0400 (Fri, 10 Jul 2009)
New Revision: 542
Added:
trunk/core/src/main/java/org/infinispan/config/parsing/CustomInterceptorConfigReader.java
Modified:
trunk/core/src/main/java/org/infinispan/config/Configuration.java
trunk/core/src/main/java/org/infinispan/config/parsing/element/CustomInterceptorsElementParser.java
trunk/core/src/test/java/org/infinispan/config/parsing/XmlFileParsingTest.java
trunk/core/src/test/resources/configs/named-cache-test.xml
Log:
add parsing of custom interceptors
Modified: trunk/core/src/main/java/org/infinispan/config/Configuration.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/config/Configuration.java 2009-07-09 19:12:22 UTC (rev 541)
+++ trunk/core/src/main/java/org/infinispan/config/Configuration.java 2009-07-10 08:03:00 UTC (rev 542)
@@ -26,6 +26,7 @@
import java.util.concurrent.TimeUnit;
import org.infinispan.config.parsing.ClusteringConfigReader;
+import org.infinispan.config.parsing.CustomInterceptorConfigReader;
import org.infinispan.distribution.DefaultConsistentHash;
import org.infinispan.eviction.EvictionStrategy;
import org.infinispan.factories.annotations.Inject;
@@ -54,7 +55,7 @@
@ConfigurationElement(name = "eviction", parent = "default", description = ""),
@ConfigurationElement(name = "expiration", parent = "default", description = ""),
@ConfigurationElement(name = "unsafe", parent = "default", description = ""),
- @ConfigurationElement(name = "customInterceptors", parent = "default", description = "")
+ @ConfigurationElement(name = "customInterceptors", parent = "default", customReader=CustomInterceptorConfigReader.class)
})
public class Configuration extends AbstractNamedCacheConfigurationBean {
private static final long serialVersionUID = 5553791890144997466L;
Added: trunk/core/src/main/java/org/infinispan/config/parsing/CustomInterceptorConfigReader.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/config/parsing/CustomInterceptorConfigReader.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/config/parsing/CustomInterceptorConfigReader.java 2009-07-10 08:03:00 UTC (rev 542)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.infinispan.config.parsing;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import org.infinispan.config.AbstractConfigurationBean;
+import org.infinispan.config.Configuration;
+import org.infinispan.config.ConfigurationException;
+import org.infinispan.config.CustomInterceptorConfig;
+import org.infinispan.interceptors.base.CommandInterceptor;
+import org.infinispan.util.Util;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+public class CustomInterceptorConfigReader implements ConfigurationElementReader {
+
+ private AutomatedXmlConfigurationParserImpl parser;
+
+ public CustomInterceptorConfigReader() {
+ super();
+ }
+
+ public void setParser(AutomatedXmlConfigurationParserImpl parser) {
+ this.parser = parser;
+ }
+
+ public void process(Element e, AbstractConfigurationBean bean) {
+ NodeList interceptorNodes = e.getElementsByTagName("interceptor");
+ List<CustomInterceptorConfig> interceptorConfigs = new ArrayList<CustomInterceptorConfig>(interceptorNodes.getLength());
+ for (int i = 0; i < interceptorNodes.getLength(); i++) {
+ boolean first = false;
+ boolean last = false;
+ int index = -1;
+ String after = null;
+ String before = null;
+
+ Element interceptorElement = (Element) interceptorNodes.item(i);
+ String position = parser.getAttributeValue(interceptorElement, "position");
+ if (parser.existsAttribute(position) && "first".equalsIgnoreCase(position)) {
+ first = true;
+ }
+ if (parser.existsAttribute(position) && "last".equalsIgnoreCase(position)) {
+ last = true;
+ }
+ String indexStr = parser.getAttributeValue(interceptorElement, "index");
+ index = parser.existsAttribute(indexStr) ? parser.getInt(indexStr) : -1;
+
+ before = parser.getAttributeValue(interceptorElement, "before");
+ if (!parser.existsAttribute(before))
+ before = null;
+ after = parser.getAttributeValue(interceptorElement, "after");
+ if (!parser.existsAttribute(after))
+ after = null;
+
+ CommandInterceptor interceptor = buildCommandInterceptor(interceptorElement);
+ CustomInterceptorConfig customInterceptorConfig = new CustomInterceptorConfig(interceptor,
+ first, last, index, after, before);
+ interceptorConfigs.add(customInterceptorConfig);
+ }
+ ((Configuration) bean).setCustomInterceptors(interceptorConfigs);
+ }
+
+ /**
+ * Builds the interceptor based on the interceptor class and also sets all its attributes.
+ */
+ private CommandInterceptor buildCommandInterceptor(Element element) {
+ String interceptorClass = parser.getAttributeValue(element, "class");
+ if (!parser.existsAttribute(interceptorClass))
+ throw new ConfigurationException("Interceptor class cannot be empty!");
+ CommandInterceptor result;
+ try {
+ result = (CommandInterceptor) Util.loadClass(interceptorClass).newInstance();
+ } catch (Exception e) {
+ throw new ConfigurationException(
+ "CommandInterceptor class is not properly loaded in classloader", e);
+ }
+ Properties p = XmlConfigHelper.extractProperties(element);
+ if (p != null)
+ XmlConfigHelper.setValues(result, p, false, true);
+ return result;
+ }
+}
Modified: trunk/core/src/main/java/org/infinispan/config/parsing/element/CustomInterceptorsElementParser.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/config/parsing/element/CustomInterceptorsElementParser.java 2009-07-09 19:12:22 UTC (rev 541)
+++ trunk/core/src/main/java/org/infinispan/config/parsing/element/CustomInterceptorsElementParser.java 2009-07-10 08:03:00 UTC (rev 542)
@@ -100,7 +100,8 @@
throw new ConfigurationException("CommandInterceptor class is not properly loaded in classloader", e);
}
Properties p = XmlConfigHelper.extractProperties(element);
- XmlConfigHelper.setValues(result, p, false, true);
+ if (p != null)
+ XmlConfigHelper.setValues(result, p, false, true);
return result;
}
Modified: trunk/core/src/test/java/org/infinispan/config/parsing/XmlFileParsingTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/config/parsing/XmlFileParsingTest.java 2009-07-09 19:12:22 UTC (rev 541)
+++ trunk/core/src/test/java/org/infinispan/config/parsing/XmlFileParsingTest.java 2009-07-10 08:03:00 UTC (rev 542)
@@ -140,6 +140,10 @@
assert c.getConsistentHashClass().equals(DefaultConsistentHash.class.getName());
assert c.getNumOwners() == 3;
assert c.isL1CacheEnabled();
+
+ c = namedCaches.get("cacheWithCustomInterceptors");
+ assert !c.getCustomInterceptors().isEmpty();
+ assert c.getCustomInterceptors().size() == 5;
}
private void testConfigurationMerging(XmlConfigurationParser parser) throws IOException {
Modified: trunk/core/src/test/resources/configs/named-cache-test.xml
===================================================================
--- trunk/core/src/test/resources/configs/named-cache-test.xml 2009-07-09 19:12:22 UTC (rev 541)
+++ trunk/core/src/test/resources/configs/named-cache-test.xml 2009-07-10 08:03:00 UTC (rev 542)
@@ -116,5 +116,21 @@
</clustering>
<jmxStatistics enabled="false"/>
</namedCache>
+
+
+ <namedCache name="cacheWithCustomInterceptors">
+
+ <!--
+ Define custom interceptors. All custom interceptors need to extend org.jboss.cache.interceptors.base.CommandInterceptor
+ Here we use existing class so we can actually load it
+ -->
+ <customInterceptors>
+ <interceptor position="first" class="org.infinispan.interceptors.CallInterceptor"></interceptor>
+ <interceptor position="last" class="org.infinispan.interceptors.CallInterceptor"/>
+ <interceptor index="3" class="org.infinispan.interceptors.CallInterceptor"/>
+ <interceptor before="org.infinispan.interceptors.CallInterceptor" class="org.infinispan.interceptors.CallInterceptor"/>
+ <interceptor after="org.infinispan.interceptors.CallInterceptor" class="org.infinispan.interceptors.CallInterceptor"/>
+ </customInterceptors>
+ </namedCache>
</infinispan>
More information about the infinispan-commits
mailing list