diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractIntegrationNamespaceHandler.java b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractIntegrationNamespaceHandler.java index dec25a6745..0375e2fb4d 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractIntegrationNamespaceHandler.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractIntegrationNamespaceHandler.java @@ -18,8 +18,10 @@ package org.springframework.integration.config.xml; import java.io.IOException; import java.util.Arrays; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -77,6 +79,8 @@ public abstract class AbstractIntegrationNamespaceHandler implements NamespaceHa private final NamespaceHandlerDelegate delegate = new NamespaceHandlerDelegate(); + private static final Set registriesProcessed = new HashSet(); + @Override public final BeanDefinition parse(Element element, ParserContext parserContext) { @@ -199,6 +203,8 @@ public abstract class AbstractIntegrationNamespaceHandler implements NamespaceHa } private void registerBuiltInBeans(ParserContext parserContext) { + int registryId = System.identityHashCode(parserContext.getRegistry()); + String jsonPathBeanName = "jsonPath"; boolean alreadyRegistered = false; if (parserContext.getRegistry() instanceof ListableBeanFactory) { @@ -207,7 +213,7 @@ public abstract class AbstractIntegrationNamespaceHandler implements NamespaceHa else { alreadyRegistered = parserContext.getRegistry().isBeanNameInUse(jsonPathBeanName); } - if (!alreadyRegistered) { + if (!alreadyRegistered && !registriesProcessed.contains(registryId)) { Class jsonPathClass = null; try { jsonPathClass = ClassUtils.forName("com.jayway.jsonpath.JsonPath", parserContext.getReaderContext().getBeanClassLoader()); @@ -230,7 +236,7 @@ public abstract class AbstractIntegrationNamespaceHandler implements NamespaceHa else { alreadyRegistered = parserContext.getRegistry().isBeanNameInUse(xpathBeanName); } - if (!alreadyRegistered) { + if (!alreadyRegistered && !registriesProcessed.contains(registryId)) { Class xpathClass = null; try { xpathClass = ClassUtils.forName(IntegrationNamespaceUtils.BASE_PACKAGE + ".xml.xpath.XPathUtils", @@ -246,6 +252,7 @@ public abstract class AbstractIntegrationNamespaceHandler implements NamespaceHa } } + registriesProcessed.add(registryId); this.doRegisterBuiltInBeans(parserContext); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/IntegrationNamespaceHandlerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/IntegrationNamespaceHandlerTests.java new file mode 100644 index 0000000000..a82b24110f --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/IntegrationNamespaceHandlerTests.java @@ -0,0 +1,79 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.config; + +import org.apache.commons.logging.Log; +import org.junit.Test; +import org.mockito.Mockito; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.support.BeanDefinitionReader; +import org.springframework.beans.factory.xml.NamespaceHandler; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.factory.xml.XmlReaderContext; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.integration.config.xml.IntegrationNamespaceHandler; +import org.springframework.integration.test.util.TestUtils; + +/** + * @author Artem Bilan + * @since 3.0 + */ +public class IntegrationNamespaceHandlerTests { + + @Test + public void testRegisterBuiltInBeansOnlyOnce() { + NamespaceHandler namespaceHandler = new IntegrationNamespaceHandler(); + + Log log = Mockito.spy(TestUtils.getPropertyValue(namespaceHandler, "logger", Log.class)); + DirectFieldAccessor dfa = new DirectFieldAccessor(namespaceHandler); + dfa.setPropertyValue("logger", log); + + GenericApplicationContext testApplicationContext = TestUtils.createTestApplicationContext(); + + XmlReaderContext readerContext = Mockito.mock(XmlReaderContext.class); + BeanDefinitionReader beanDefinitionReader = Mockito.mock(XmlBeanDefinitionReader.class); + DirectFieldAccessor bDefReaderDfa = new DirectFieldAccessor(beanDefinitionReader); + bDefReaderDfa.setPropertyValue("registry", testApplicationContext); + + DirectFieldAccessor readerContextDfa = new DirectFieldAccessor(readerContext); + readerContextDfa.setPropertyValue("reader", beanDefinitionReader); + + Element element = Mockito.mock(Element.class); + Mockito.when(element.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation")) + .thenReturn("http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"); + Document document = Mockito.mock(Document.class); + Mockito.when(element.getOwnerDocument()).thenReturn(document); + Mockito.when(document.getDocumentElement()).thenReturn(element); + + + for (int i = 0; i < 3; i++) { + try { + namespaceHandler.parse(element, new ParserContext(readerContext, null)); + } + catch (NullPointerException e) { + //ignore it as it is out of scope of this test. + } + } + + Mockito.verify(log).debug("SpEL function '#xpath' isn't registered: there is no spring-integration-xml.jar on the classpath."); + } + +}