CAMEL-11410: camel-spring - Should not list uris as spring bean ids from Camel routes
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/89114841 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/89114841 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/89114841 Branch: refs/heads/master Commit: 891148417e1e82120b9220d83fa53fe391676f36 Parents: 78fea48 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Jun 14 16:01:07 2017 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Jun 14 16:01:07 2017 +0200 ---------------------------------------------------------------------- .../camel/spring/CamelContextFactoryBean.java | 16 ++++++ .../spring/handler/CamelNamespaceHandler.java | 15 +++++- .../camel/spring/NodeIdReferenceLegacyTest.java | 52 ++++++++++++++++++++ .../NodeIdReferenceLegacyTest-context.xml | 36 ++++++++++++++ 4 files changed, 117 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/89114841/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java index eb79196..fd47daf 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java @@ -121,6 +121,9 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Spr private String autoStartup; @XmlAttribute @Metadata(defaultValue = "true") private String shutdownEager; + @XmlAttribute @Metadata(defaultValue = "false") + @Deprecated + private String registerEndpointIdsFromRoute; @XmlAttribute private String useMDCLogging; @XmlAttribute @@ -758,6 +761,19 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Spr this.shutdownEager = shutdownEager; } + public String getRegisterEndpointIdsFromRoute() { + return registerEndpointIdsFromRoute; + } + + /** + * Sets whether to register endpoints that has id attribute assigned in the Spring registry. + * <p/> + * This mode is by default false, but can be turned on for backwards compatibility. + */ + public void setRegisterEndpointIdsFromRoute(String registerEndpointIdsFromRoute) { + this.registerEndpointIdsFromRoute = registerEndpointIdsFromRoute; + } + public String getUseMDCLogging() { return useMDCLogging; } http://git-wip-us.apache.org/repos/asf/camel/blob/89114841/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java index 43afb2b..9ecdf6c 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java @@ -350,6 +350,7 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport { String contextId = element.getAttribute("id"); boolean implicitId = false; + boolean registerEndpointIdsFromRoute = false; // lets avoid folks having to explicitly give an ID to a camel context if (ObjectHelper.isEmpty(contextId)) { @@ -407,6 +408,8 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport { builder.addPropertyValue("hystrixConfigurations", factoryBean.getHystrixConfigurations()); // add any depends-on addDependsOn(factoryBean, builder); + + registerEndpointIdsFromRoute = "true".equalsIgnoreCase(factoryBean.getRegisterEndpointIdsFromRoute()); } NodeList list = element.getChildNodes(); @@ -439,8 +442,11 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport { } } - // register as endpoint defined indirectly in the routes by from/to types having id explicit set - // registerEndpointsWithIdsDefinedInFromOrToTypes(element, parserContext, contextId, binder); + if (registerEndpointIdsFromRoute) { + // register as endpoint defined indirectly in the routes by from/to types having id explicit set + LOG.debug("Registering endpoint with ids defined in Camel routes"); + registerEndpointsWithIdsDefinedInFromOrToTypes(element, parserContext, contextId, binder); + } // register templates if not already defined registerTemplates(element, parserContext, contextId); @@ -689,6 +695,11 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport { String id = childElement.getAttribute("id"); // must have an id to be registered if (ObjectHelper.isNotEmpty(id)) { + // skip underscore as they are internal naming and should not be registered + if (id.startsWith("_")) { + LOG.debug("Skip registering endpoint starting with underscore: {}", id); + return; + } BeanDefinition definition = endpointParser.parse(childElement, parserContext); definition.getPropertyValues().addPropertyValue("camelContext", new RuntimeBeanReference(contextId)); // Need to add this dependency of CamelContext for Spring 3.0 http://git-wip-us.apache.org/repos/asf/camel/blob/89114841/components/camel-spring/src/test/java/org/apache/camel/spring/NodeIdReferenceLegacyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/NodeIdReferenceLegacyTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/NodeIdReferenceLegacyTest.java new file mode 100644 index 0000000..044169d --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/spring/NodeIdReferenceLegacyTest.java @@ -0,0 +1,52 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.spring; + +import org.apache.camel.Endpoint; +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; + +/** + * @version + */ +@ContextConfiguration +public class NodeIdReferenceLegacyTest extends SpringRunWithTestSupport { + protected String expectedBody = "Bye World"; + + @Autowired + protected ProducerTemplate producer; + + @EndpointInject(ref = "foo") + protected Endpoint start; + + @EndpointInject(uri = "mock:result") + protected MockEndpoint result; + + @Test + public void testNodeIdReferenceLegacy() throws Exception { + result.expectedBodiesReceived(expectedBody); + + producer.sendBody(start, expectedBody); + + result.assertIsSatisfied(); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/89114841/components/camel-spring/src/test/resources/org/apache/camel/spring/NodeIdReferenceLegacyTest-context.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/NodeIdReferenceLegacyTest-context.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/NodeIdReferenceLegacyTest-context.xml new file mode 100644 index 0000000..fefb292 --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/NodeIdReferenceLegacyTest-context.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. + +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext registerEndpointIdsFromRoute="true" xmlns="http://camel.apache.org/schema/spring"> + <template id="camelTemplate"/> + + <route> + <from id="foo" uri="direct:start"/> + <to uri="mock:result"/> + </route> + </camelContext> + +</beans>