You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/11/01 15:56:57 UTC
[5/6] git commit: CAMEL-7986: Add duplicate check when adding routes
that are using auto assigned names,
which could clash with end user assigned names using same naming pattern.
CAMEL-7986: Add duplicate check when adding routes that are using auto assigned names, which could clash with end user assigned names using same naming pattern.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2aab7c39
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2aab7c39
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2aab7c39
Branch: refs/heads/camel-2.13.x
Commit: 2aab7c39131297f42003ca2e2c19055a515489d0
Parents: d2bf0f8
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Nov 1 15:23:53 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Nov 1 15:56:32 2014 +0100
----------------------------------------------------------------------
.../apache/camel/impl/DefaultCamelContext.java | 9 ++-
.../camel/model/RouteDefinitionHelper.java | 24 +++++++-
.../issues/RouteIdAutoAssignClashIssue.java | 63 ++++++++++++++++++++
.../SpringRouteIdAutoAssignClashIssue.java | 33 ++++++++++
.../issues/RouteIdAutoAssignClashIssue.xml | 40 +++++++++++++
5 files changed, 163 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/2aab7c39/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index 9ade095..865c1ef 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -749,9 +749,12 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
}
public synchronized void removeRouteDefinition(RouteDefinition routeDefinition) throws Exception {
- String id = routeDefinition.idOrCreate(nodeIdFactory);
- stopRoute(id);
- removeRoute(id);
+ String id = routeDefinition.getId();
+ if (id != null) {
+ // remove existing route
+ stopRoute(id);
+ removeRoute(id);
+ }
this.routeDefinitions.remove(routeDefinition);
}
http://git-wip-us.apache.org/repos/asf/camel/blob/2aab7c39/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java b/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
index 5c67d57..da218e1 100644
--- a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
@@ -19,6 +19,7 @@ package org.apache.camel.model;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
@@ -118,10 +119,10 @@ public final class RouteDefinitionHelper {
* @throws Exception is thrown if error force assign ids to the routes
*/
public static void forceAssignIds(CamelContext context, List<RouteDefinition> routes) throws Exception {
- for (RouteDefinition route : routes) {
- // force id on the route
- route.idOrCreate(context.getNodeIdFactory());
+ // handle custom assigned id's first, and then afterwards assign auto generated ids
+ Set<String> customIds = new HashSet<>();
+ for (RouteDefinition route : routes) {
// if there was a custom id assigned, then make sure to support property placeholders
if (route.hasCustomIdAssigned()) {
String id = route.getId();
@@ -130,6 +131,23 @@ public final class RouteDefinitionHelper {
if (!route.getId().equals(id)) {
route.setId(id);
}
+ customIds.add(id);
+ }
+ }
+
+ // auto assign route ids
+ for (RouteDefinition route : routes) {
+ if (route.getId() == null) {
+ // keep assigning id's until we find a free name
+ boolean done = false;
+ String id = null;
+ while (!done) {
+ id = context.getNodeIdFactory().createId(route);
+ done = !customIds.contains(id);
+ }
+ route.setId(id);
+ route.setCustomId(false);
+ customIds.add(route.getId());
}
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/2aab7c39/camel-core/src/test/java/org/apache/camel/issues/RouteIdAutoAssignClashIssue.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/issues/RouteIdAutoAssignClashIssue.java b/camel-core/src/test/java/org/apache/camel/issues/RouteIdAutoAssignClashIssue.java
new file mode 100644
index 0000000..6cfdeb7
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/issues/RouteIdAutoAssignClashIssue.java
@@ -0,0 +1,63 @@
+/**
+ * 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.issues;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class RouteIdAutoAssignClashIssue extends ContextTestSupport {
+
+ public void testRouteIdAutoAssignClash() throws Exception {
+ getMockEndpoint("mock:start1").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:start2").expectedBodiesReceived("Bye World");
+ getMockEndpoint("mock:start").expectedBodiesReceived("Hi Camel");
+
+ template.sendBody("direct:start1", "Hello World");
+ template.sendBody("direct:start2", "Bye World");
+ template.sendBody("direct:start", "Hi Camel");
+
+ assertMockEndpointsSatisfied();
+
+ assertEquals(3, context.getRouteDefinitions().size());
+ assertEquals(3, context.getRoutes().size());
+
+ assertNotNull(context.getRoute("route1"));
+ assertNotNull(context.getRoute("route2"));
+ // we will auto assign the route as route3
+ assertNotNull(context.getRoute("route3"));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // use route1, route2 to clash with Camel's auto assigning
+ from("direct:start1").routeId("route1")
+ .to("mock:start1");
+
+ // use route1, route2 to clash with Camel's auto assigning
+ from("direct:start2").routeId("route2")
+ .to("mock:start2");
+
+ // no assigned route id which should be auto assigned
+ from("direct:start")
+ .to("mock:start");
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/2aab7c39/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringRouteIdAutoAssignClashIssue.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringRouteIdAutoAssignClashIssue.java b/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringRouteIdAutoAssignClashIssue.java
new file mode 100644
index 0000000..5092f23
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/issues/SpringRouteIdAutoAssignClashIssue.java
@@ -0,0 +1,33 @@
+/**
+ * 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.issues;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.issues.RouteIdAutoAssignClashIssue;
+
+import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
+
+/**
+ *
+ */
+public class SpringRouteIdAutoAssignClashIssue extends RouteIdAutoAssignClashIssue {
+
+ protected CamelContext createCamelContext() throws Exception {
+ return createSpringCamelContext(this, "org/apache/camel/spring/issues/RouteIdAutoAssignClashIssue.xml");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/2aab7c39/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RouteIdAutoAssignClashIssue.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RouteIdAutoAssignClashIssue.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RouteIdAutoAssignClashIssue.xml
new file mode 100644
index 0000000..313bb59
--- /dev/null
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/RouteIdAutoAssignClashIssue.xml
@@ -0,0 +1,40 @@
+<?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 xmlns="http://camel.apache.org/schema/spring">
+ <route id="route1">
+ <from uri="direct:start1"/>
+ <to uri="mock:start1"/>
+ </route>
+ <route id="route2">
+ <from uri="direct:start2"/>
+ <to uri="mock:start2"/>
+ </route>
+ <route>
+ <from uri="direct:start"/>
+ <to uri="mock:start"/>
+ </route>
+ </camelContext>
+
+</beans>