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/08/15 11:22:01 UTC

git commit: CAMEL-7702: Added RoutePolicyFactory.

Repository: camel
Updated Branches:
  refs/heads/master c7d35c547 -> 95c6c773e


CAMEL-7702: Added RoutePolicyFactory.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/95c6c773
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/95c6c773
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/95c6c773

Branch: refs/heads/master
Commit: 95c6c773ed8e66140b5c3676a3d0a1bf7bc851e7
Parents: c7d35c5
Author: Claus Ibsen <da...@apache.org>
Authored: Fri Aug 15 11:21:45 2014 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Aug 15 11:21:45 2014 +0200

----------------------------------------------------------------------
 .../java/org/apache/camel/CamelContext.java     | 15 ++++
 .../apache/camel/impl/DefaultCamelContext.java  | 14 ++++
 .../org/apache/camel/model/RouteDefinition.java | 10 +++
 .../apache/camel/spi/RoutePolicyFactory.java    | 36 ++++++++
 .../camel/impl/RoutePolicyFactoryTest.java      | 87 ++++++++++++++++++++
 .../xml/AbstractCamelContextFactoryBean.java    | 10 +++
 .../impl/SpringRoutePolicyFactoryTest.java      | 30 +++++++
 .../impl/SpringRoutePolicyFactoryTest.xml       | 42 ++++++++++
 8 files changed, 244 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/95c6c773/camel-core/src/main/java/org/apache/camel/CamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/CamelContext.java b/camel-core/src/main/java/org/apache/camel/CamelContext.java
index 022ae36..ce59a5a 100644
--- a/camel-core/src/main/java/org/apache/camel/CamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/CamelContext.java
@@ -53,6 +53,7 @@ import org.apache.camel.spi.ProcessorFactory;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.spi.RestRegistry;
+import org.apache.camel.spi.RoutePolicyFactory;
 import org.apache.camel.spi.RouteStartupOrder;
 import org.apache.camel.spi.RuntimeEndpointRegistry;
 import org.apache.camel.spi.ServicePool;
@@ -1376,4 +1377,18 @@ public interface CamelContext extends SuspendableService, RuntimeConfiguration {
      */
     void setRestRegistry(RestRegistry restRegistry);
 
+    /**
+     * Adds the given route policy factory
+     *
+     * @param routePolicyFactory the factory
+     */
+    void addRoutePolicyFactory(RoutePolicyFactory routePolicyFactory);
+
+    /**
+     * Gets the route policy factories
+     *
+     * @return the list of current route policy factories
+     */
+    List<RoutePolicyFactory> getRoutePolicyFactories();
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/95c6c773/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 822ca9b..ea78f61 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
@@ -119,6 +119,7 @@ import org.apache.camel.spi.Registry;
 import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.spi.RestRegistry;
 import org.apache.camel.spi.RouteContext;
+import org.apache.camel.spi.RoutePolicyFactory;
 import org.apache.camel.spi.RouteStartupOrder;
 import org.apache.camel.spi.RuntimeEndpointRegistry;
 import org.apache.camel.spi.ServicePool;
@@ -179,6 +180,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
     private RestConfiguration restConfiguration = new RestConfiguration();
     private RestRegistry restRegistry = new DefaultRestRegistry();
     private List<InterceptStrategy> interceptStrategies = new ArrayList<InterceptStrategy>();
+    private List<RoutePolicyFactory> routePolicyFactories = new ArrayList<RoutePolicyFactory>();
 
     // special flags to control the first startup which can are special
     private volatile boolean firstStartDone;
@@ -1481,6 +1483,18 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
         }
     }
 
+    public List<RoutePolicyFactory> getRoutePolicyFactories() {
+        return routePolicyFactories;
+    }
+
+    public void setRoutePolicyFactories(List<RoutePolicyFactory> routePolicyFactories) {
+        this.routePolicyFactories = routePolicyFactories;
+    }
+
+    public void addRoutePolicyFactory(RoutePolicyFactory routePolicyFactory) {
+        getRoutePolicyFactories().add(routePolicyFactory);
+    }
+
     public void setStreamCaching(Boolean cache) {
         this.streamCache = cache;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/95c6c773/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java b/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
index 5ecd5a4..723c953 100644
--- a/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
@@ -49,6 +49,7 @@ import org.apache.camel.processor.interceptor.HandleFault;
 import org.apache.camel.spi.LifecycleStrategy;
 import org.apache.camel.spi.RouteContext;
 import org.apache.camel.spi.RoutePolicy;
+import org.apache.camel.spi.RoutePolicyFactory;
 import org.apache.camel.util.CamelContextHelper;
 import org.apache.camel.util.ObjectHelper;
 
@@ -889,6 +890,15 @@ public class RouteDefinition extends ProcessorDefinition<RouteDefinition> {
                 routeContext.getRoutePolicyList().add(policy);
             }
         }
+        if (camelContext.getRoutePolicyFactories() != null) {
+            for (RoutePolicyFactory factory : camelContext.getRoutePolicyFactories()) {
+                RoutePolicy policy = factory.createRoutePolicy(camelContext, getId(), this);
+                if (policy != null) {
+                    log.debug("RoutePolicy is enabled: {} on route: {}", policy, getId());
+                    routeContext.getRoutePolicyList().add(policy);
+                }
+            }
+        }
 
         // configure auto startup
         Boolean isAutoStartup = CamelContextHelper.parseBoolean(camelContext, getAutoStartup());

http://git-wip-us.apache.org/repos/asf/camel/blob/95c6c773/camel-core/src/main/java/org/apache/camel/spi/RoutePolicyFactory.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/spi/RoutePolicyFactory.java b/camel-core/src/main/java/org/apache/camel/spi/RoutePolicyFactory.java
new file mode 100644
index 0000000..2632388
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/spi/RoutePolicyFactory.java
@@ -0,0 +1,36 @@
+/**
+ * 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;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.model.RouteDefinition;
+
+/**
+ * A factory to create {@link org.apache.camel.spi.RoutePolicy} and assign to routes automatic.
+ */
+public interface RoutePolicyFactory {
+
+    /**
+     * Creates a new {@link org.apache.camel.spi.RoutePolicy} which will be assigned to the given route.
+     *
+     * @param camelContext the camel context
+     * @param routeId      the route id
+     * @param route        the route definition
+     * @return the created {@link org.apache.camel.spi.RoutePolicy}, or <tt>null</tt> to not use a policy for this route
+     */
+    RoutePolicy createRoutePolicy(CamelContext camelContext, String routeId, RouteDefinition route);
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/95c6c773/camel-core/src/test/java/org/apache/camel/impl/RoutePolicyFactoryTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/RoutePolicyFactoryTest.java b/camel-core/src/test/java/org/apache/camel/impl/RoutePolicyFactoryTest.java
new file mode 100644
index 0000000..f3b3e16
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/RoutePolicyFactoryTest.java
@@ -0,0 +1,87 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Route;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.spi.RoutePolicy;
+import org.apache.camel.spi.RoutePolicyFactory;
+
+public class RoutePolicyFactoryTest extends ContextTestSupport {
+
+    public void testRoutePolicyFactory() throws Exception {
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+        getMockEndpoint("mock:foo").expectedHeaderReceived("RoutePolicy", "foo-route");
+        getMockEndpoint("mock:bar").expectedHeaderReceived("RoutePolicy", "bar-route");
+
+        template.sendBody("direct:foo", "Hello Foo");
+        template.sendBody("direct:bar", "Hello Bar");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public static final class MyRoutePolicyFactory implements RoutePolicyFactory {
+
+        public MyRoutePolicyFactory() {
+        }
+
+        @Override
+        public RoutePolicy createRoutePolicy(CamelContext camelContext, String routeId, RouteDefinition route) {
+            return new MyRoutePolicy(routeId);
+        }
+    }
+
+    private static final class MyRoutePolicy extends RoutePolicySupport {
+
+        private final String routeId;
+
+        private MyRoutePolicy(String routeId) {
+            this.routeId = routeId;
+        }
+
+        public String getRouteId() {
+            return routeId;
+        }
+
+        @Override
+        public void onExchangeBegin(Route route, Exchange exchange) {
+            exchange.getIn().setHeader("RoutePolicy", routeId);
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                context.addRoutePolicyFactory(new MyRoutePolicyFactory());
+
+                from("direct:foo").routeId("foo-route")
+                    .to("mock:foo");
+
+                from("direct:bar").routeId("bar-route")
+                    .to("mock:bar");
+            }
+        };
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/95c6c773/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
----------------------------------------------------------------------
diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
index 7ca86df..51afc9c 100644
--- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
+++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
@@ -85,6 +85,7 @@ import org.apache.camel.spi.NodeIdFactory;
 import org.apache.camel.spi.PackageScanClassResolver;
 import org.apache.camel.spi.PackageScanFilter;
 import org.apache.camel.spi.ProcessorFactory;
+import org.apache.camel.spi.RoutePolicyFactory;
 import org.apache.camel.spi.RuntimeEndpointRegistry;
 import org.apache.camel.spi.ShutdownStrategy;
 import org.apache.camel.spi.StreamCachingStrategy;
@@ -277,6 +278,15 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex
                 }
             }
         }
+        // add route policy factories
+        Map<String, RoutePolicyFactory> routePolicyFactories = getContext().getRegistry().findByTypeWithName(RoutePolicyFactory.class);
+        if (routePolicyFactories != null && !routePolicyFactories.isEmpty()) {
+            for (Entry<String, RoutePolicyFactory> entry : routePolicyFactories.entrySet()) {
+                RoutePolicyFactory factory = entry.getValue();
+                LOG.info("Using custom RoutePolicyFactory with id: {} and implementation: {}", entry.getKey(), factory);
+                getContext().addRoutePolicyFactory(factory);
+            }
+        }
 
         // set the default thread pool profile if defined
         initThreadPoolProfiles(getContext());

http://git-wip-us.apache.org/repos/asf/camel/blob/95c6c773/components/camel-spring/src/test/java/org/apache/camel/spring/impl/SpringRoutePolicyFactoryTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/impl/SpringRoutePolicyFactoryTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/impl/SpringRoutePolicyFactoryTest.java
new file mode 100644
index 0000000..6cca092
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/impl/SpringRoutePolicyFactoryTest.java
@@ -0,0 +1,30 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.RoutePolicyFactoryTest;
+
+import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
+
+public class SpringRoutePolicyFactoryTest extends RoutePolicyFactoryTest {
+
+    protected CamelContext createCamelContext() throws Exception {
+        return createSpringCamelContext(this, "org/apache/camel/spring/impl/SpringRoutePolicyFactoryTest.xml");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/95c6c773/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/SpringRoutePolicyFactoryTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/SpringRoutePolicyFactoryTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/SpringRoutePolicyFactoryTest.xml
new file mode 100644
index 0000000..b3a0876
--- /dev/null
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/SpringRoutePolicyFactoryTest.xml
@@ -0,0 +1,42 @@
+<?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
+    ">
+
+  <!-- START SNIPPET: e1 -->
+  <!-- this is our route policy factory implementation, which creates a route policy for all the routes -->
+  <bean id="myRoutePolicyFactory" class="org.apache.camel.impl.RoutePolicyFactoryTest$MyRoutePolicyFactory"/>
+
+  <camelContext xmlns="http://camel.apache.org/schema/spring">
+    <route id="foo-route">
+      <from uri="direct:foo"/>
+      <to uri="mock:foo"/>
+    </route>
+
+    <route id="bar-route">
+      <from uri="direct:bar"/>
+      <to uri="mock:bar"/>
+    </route>
+  </camelContext>
+  <!-- END SNIPPET: e1 -->
+
+</beans>