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 2015/03/22 16:10:51 UTC
[2/3] camel git commit: CAMEL-8527: Processor in routes should be
IdAware
http://git-wip-us.apache.org/repos/asf/camel/blob/eaf8a3c4/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/LoadBalancerSupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/LoadBalancerSupport.java b/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/LoadBalancerSupport.java
index 238457f..4d307c8 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/LoadBalancerSupport.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/LoadBalancerSupport.java
@@ -23,6 +23,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.camel.Exchange;
import org.apache.camel.Navigate;
import org.apache.camel.Processor;
+import org.apache.camel.spi.IdAware;
import org.apache.camel.support.ServiceSupport;
import org.apache.camel.util.AsyncProcessorHelper;
import org.apache.camel.util.ServiceHelper;
@@ -39,10 +40,11 @@ import org.slf4j.LoggerFactory;
*
* @version
*/
-public abstract class LoadBalancerSupport extends ServiceSupport implements LoadBalancer, Navigate<Processor> {
+public abstract class LoadBalancerSupport extends ServiceSupport implements LoadBalancer, Navigate<Processor>, IdAware {
protected final Logger log = LoggerFactory.getLogger(getClass());
private final List<Processor> processors = new CopyOnWriteArrayList<Processor>();
+ private String id;
public void addProcessor(Processor processor) {
processors.add(processor);
@@ -67,6 +69,14 @@ public abstract class LoadBalancerSupport extends ServiceSupport implements Load
return processors.size() > 0;
}
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
protected void doStart() throws Exception {
ServiceHelper.startServices(processors);
}
http://git-wip-us.apache.org/repos/asf/camel/blob/eaf8a3c4/camel-core/src/main/java/org/apache/camel/spi/IdAware.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/spi/IdAware.java b/camel-core/src/main/java/org/apache/camel/spi/IdAware.java
new file mode 100644
index 0000000..f8bf16d
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/spi/IdAware.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.spi;
+
+/**
+ * To allow objects to be injected with an id, such as EIP {@link org.apache.camel.Processor}s which has been defined from Camel routes.
+ * <p/>
+ * This allows access to the id of the processor at runtime, which makes it easier to map it to the corresponding model definition.
+ */
+public interface IdAware extends HasId {
+
+ /**
+ * Sets the id
+ *
+ * @param id the id
+ */
+ void setId(String id);
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/eaf8a3c4/camel-core/src/test/java/org/apache/camel/processor/SimpleProcessorIdAwareTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/SimpleProcessorIdAwareTest.java b/camel-core/src/test/java/org/apache/camel/processor/SimpleProcessorIdAwareTest.java
new file mode 100644
index 0000000..da118e0
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/SimpleProcessorIdAwareTest.java
@@ -0,0 +1,66 @@
+/**
+ * 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.processor;
+
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.IdAware;
+
+/**
+ * @version
+ */
+public class SimpleProcessorIdAwareTest extends ContextTestSupport {
+
+ public void testIdAware() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello World");
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+
+ List<Processor> matches = context.getRoute("foo").filter("b*");
+ assertEquals(2, matches.size());
+
+ Processor bar = matches.get(0);
+ Processor baz = matches.get(1);
+
+ assertEquals("bar", ((IdAware) bar).getId());
+ assertEquals("baz", ((IdAware) baz).getId());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").routeId("foo")
+ .choice()
+ .when(header("bar"))
+ .to("log:bar").id("bar")
+ .otherwise()
+ .to("mock:result").id("result")
+ .end()
+ .to("log:baz").id("baz");
+ }
+ };
+ }
+}