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 2019/02/28 11:59:16 UTC

[camel] branch master updated: CAMEL-13243: Main supports light-weight ioc for route builder classes.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 222849f  CAMEL-13243: Main supports light-weight ioc for route builder classes.
222849f is described below

commit 222849fe507875a9d17fe2115c694a6c211eaef5
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Feb 28 12:59:02 2019 +0100

    CAMEL-13243: Main supports light-weight ioc for route builder classes.
---
 .../java/org/apache/camel/main/MainSupport.java    | 20 +++++--
 .../java/org/apache/camel/main/MainIoCTest.java    | 62 ++++++++++++++++++++++
 2 files changed, 79 insertions(+), 3 deletions(-)

diff --git a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
index 63c8be1..8e1af19 100644
--- a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
+++ b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
@@ -568,12 +568,16 @@ public abstract class MainSupport extends ServiceSupport {
 
     protected void loadRouteBuilders(CamelContext camelContext) throws Exception {
         if (routeBuilderClasses != null) {
-            // get the list of route builder classes
             String[] routeClasses = routeBuilderClasses.split(",");
             for (String routeClass : routeClasses) {
                 Class<?> routeClazz = camelContext.getClassResolver().resolveClass(routeClass);
-                RouteBuilder builder = (RouteBuilder) routeClazz.newInstance();
-                getRouteBuilders().add(builder);
+                // lets use Camel's injector so the class has some support for dependency injection
+                Object builder = camelContext.getInjector().newInstance(routeClazz);
+                if (builder instanceof RouteBuilder) {
+                    getRouteBuilders().add((RouteBuilder) builder);
+                } else {
+                    LOG.warn("Class {} is not a RouteBuilder class", routeClazz);
+                }
             }
         }
     }
@@ -645,6 +649,16 @@ public abstract class MainSupport extends ServiceSupport {
         getRouteBuilders().add(routeBuilder);
     }
 
+    public void addRouteBuilder(Class routeBuilder) {
+        String existing = routeBuilderClasses;
+        if (existing != null) {
+            existing = existing + "," + routeBuilder.getName();
+        } else {
+            existing = routeBuilder.getName();
+        }
+        setRouteBuilderClasses(existing);
+    }
+
     public abstract class Option {
         private String abbreviation;
         private String fullName;
diff --git a/core/camel-core/src/test/java/org/apache/camel/main/MainIoCTest.java b/core/camel-core/src/test/java/org/apache/camel/main/MainIoCTest.java
new file mode 100644
index 0000000..cc67037
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/main/MainIoCTest.java
@@ -0,0 +1,62 @@
+/**
+ * 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.main;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.PropertyInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MainIoCTest extends Assert {
+
+    @Test
+    public void testMainIoC() throws Exception {
+        Main main = new Main();
+        // add as class so we get IoC
+        main.addRouteBuilder(MyRouteBuilder.class);
+        main.start();
+
+        CamelContext camelContext = main.getCamelContext();
+
+        assertNotNull(camelContext);
+
+        MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class);
+        endpoint.expectedBodiesReceived("World");
+
+        main.getCamelTemplate().sendBody("direct:start", "<message>1</message>");
+
+        endpoint.assertIsSatisfied();
+
+        main.stop();
+    }
+
+    public static class MyRouteBuilder extends RouteBuilder {
+
+        // properties is automatic loaded from classpath:application.properties
+        // so we should be able to inject this field
+
+        @PropertyInject(value = "hello")
+        private String hello;
+
+        @Override
+        public void configure() throws Exception {
+            from("direct:start").transform().constant(hello).to("mock:results");
+        }
+    }
+}