Author: ningjiang Date: Wed Aug 8 03:16:32 2012 New Revision: 1370653 URL: http://svn.apache.org/viewvc?rev=1370653&view=rev Log: CAMEL-5489 Support for selection of direct-vm consumers
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmConsumerExpression.java camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmConsumerExpressionTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmComponent.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmComponent.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmComponent.java?rev=1370653&r1=1370652&r2=1370653&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmComponent.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmComponent.java Wed Aug 8 03:16:32 2012 @@ -16,6 +16,8 @@ */ package org.apache.camel.component.directvm; +import java.util.ArrayList; +import java.util.Collection; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -37,6 +39,19 @@ public class DirectVmComponent extends D // on DefaultCamelContext private static final ConcurrentMap<String, DirectVmConsumer> CONSUMERS = new ConcurrentHashMap<String, DirectVmConsumer>(); + /** + * Gets all the consumer endpoints. + * + * @return consumer endpoints + */ + public static Collection<Endpoint> getConsumerEndpoints() { + Collection<Endpoint> endpoints = new ArrayList<Endpoint>(CONSUMERS.size()); + for (DirectVmConsumer consumer : CONSUMERS.values()) { + endpoints.add(consumer.getEndpoint()); + } + return endpoints; + } + @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { DirectVmEndpoint answer = new DirectVmEndpoint(uri, this); Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmConsumerExpression.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmConsumerExpression.java?rev=1370653&view=auto ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmConsumerExpression.java (added) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/directvm/DirectVmConsumerExpression.java Wed Aug 8 03:16:32 2012 @@ -0,0 +1,55 @@ +/** + * 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.component.directvm; + +import java.util.ArrayList; +import java.util.Collection; + +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.support.ExpressionAdapter; +import org.apache.camel.util.AntPathMatcher; + +/** + * The expression to select direct-vm consumers based on ant-like path pattern matching. + */ +public class DirectVmConsumerExpression extends ExpressionAdapter { + + private final AntPathMatcher matcher; + private final String pattern; + + public DirectVmConsumerExpression(String pattern) { + this.matcher = new AntPathMatcher(); + this.pattern = pattern; + } + + @Override + public Object evaluate(Exchange exchange) { + Collection<Endpoint> endpoints = new ArrayList<Endpoint>(); + for (Endpoint endpoint : DirectVmComponent.getConsumerEndpoints()) { + if (matcher.match(pattern, endpoint.getEndpointKey())) { + endpoints.add(endpoint); + } + } + return endpoints; + } + + @Override + public String toString() { + return "DirectVmConsumerExpression[" + pattern + "]"; + } +} Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmConsumerExpressionTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmConsumerExpressionTest.java?rev=1370653&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmConsumerExpressionTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/directvm/DirectVmConsumerExpressionTest.java Wed Aug 8 03:16:32 2012 @@ -0,0 +1,110 @@ +/** + * 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.component.directvm; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.util.ServiceHelper; +import org.junit.After; +import org.junit.Before; + +/** + * + */ +public class DirectVmConsumerExpressionTest extends ContextTestSupport { + + private CamelContext context2; + private CamelContext context3; + + @Override + @Before + protected void setUp() throws Exception { + super.setUp(); + + context2 = new DefaultCamelContext(); + context3 = new DefaultCamelContext(); + + ServiceHelper.startServices(context2); + ServiceHelper.startServices(context3); + + // add routes after CamelContext has been started + RouteBuilder routeBuilder = createRouteBuilderCamelContext2(); + if (routeBuilder != null) { + context2.addRoutes(routeBuilder); + } + + routeBuilder = createRouteBuilderCamelContext3(); + if (routeBuilder != null) { + context3.addRoutes(routeBuilder); + } + } + + @Override + @After + protected void tearDown() throws Exception { + ServiceHelper.stopServices(context2, context3); + super.tearDown(); + } + + public void testSelectEndpoint() throws Exception { + MockEndpoint result2 = context2.getEndpoint("mock:result2", MockEndpoint.class); + result2.expectedBodiesReceived("Hello World"); + + MockEndpoint result3 = context3.getEndpoint("mock:result3", MockEndpoint.class); + result3.expectedBodiesReceived("Hello World"); + + template.sendBody("direct:start", "Hello World"); + + MockEndpoint.assertIsSatisfied(context2); + MockEndpoint.assertIsSatisfied(context3); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .recipientList(new DirectVmConsumerExpression("direct-vm://parent/**/context*")); + } + }; + } + + private RouteBuilder createRouteBuilderCamelContext2() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct-vm:parent/child/context2") + .to("mock:result2"); + } + }; + } + + private RouteBuilder createRouteBuilderCamelContext3() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct-vm:parent/child/grandchild/context3") + .to("mock:result3"); + } + }; + } + +}