camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [camel-k-runtime] 04/08: kamelets: create a camel-kamelet component #375
Date Thu, 30 Jul 2020 09:15:04 GMT
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch kamelets-claus
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git

commit 164b741fe46808cbabdfdfda67f6d06975339403
Author: Claus Ibsen <claus.ibsen@gmail.com>
AuthorDate: Wed Jul 29 17:54:25 2020 +0200

    kamelets: create a camel-kamelet component #375
---
 camel-kamelet/pom.xml                              |  2 +-
 .../camel/component/kamelet/KameletComponent.java  |  2 +-
 .../camel/component/kamelet/KameletEndpoint.java   | 39 +++++++++++++++++-----
 .../camel/component/kamelet/KameletTest.java       |  2 +-
 4 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/camel-kamelet/pom.xml b/camel-kamelet/pom.xml
index 7083b87..33e09c3 100644
--- a/camel-kamelet/pom.xml
+++ b/camel-kamelet/pom.xml
@@ -43,7 +43,7 @@
 
         <dependency>
             <groupId>org.apache.camel</groupId>
-            <artifactId>camel-support</artifactId>
+            <artifactId>camel-core-engine</artifactId>
         </dependency>
         <dependency>
             <groupId>org.apache.camel</groupId>
diff --git a/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java
b/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java
index f5c6f6e..05a8abc 100644
--- a/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java
+++ b/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java
@@ -42,7 +42,7 @@ public class KameletComponent extends DefaultComponent {
 
         //
         // The properties for the kamelets are determined by global properties
-        // and local endpoint parametes,
+        // and local endpoint parameters,
         //
         // Global parameters are loaded in the following order:
         //
diff --git a/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletEndpoint.java
b/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletEndpoint.java
index d3f0eac..4300ea6 100644
--- a/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletEndpoint.java
+++ b/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletEndpoint.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.kamelet;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.camel.AsyncCallback;
@@ -25,12 +27,18 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.impl.event.CamelContextInitializedEvent;
+import org.apache.camel.model.ModelCamelContext;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.spi.CamelEvent;
+import org.apache.camel.spi.EventNotifier;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriPath;
 import org.apache.camel.support.DefaultAsyncProducer;
 import org.apache.camel.support.DefaultConsumer;
 import org.apache.camel.support.DefaultEndpoint;
+import org.apache.camel.support.EventNotifierSupport;
 import org.apache.camel.support.service.ServiceHelper;
 
 @UriEndpoint(
@@ -87,16 +95,29 @@ public class KameletEndpoint extends DefaultEndpoint {
     }
 
     @Override
-    protected void doStart() throws Exception {
-        try {
-            // Add a route to the camel context from the given template
-            // TODO: add validation (requires: https://issues.apache.org/jira/browse/CAMEL-15312)
-            getCamelContext().addRouteFromTemplate(routeId, templateId, kameletProperties);
-        } catch (Exception e) {
-            throw new IllegalArgumentException(e);
-        }
+    protected void doInit() throws Exception {
+        super.doInit();
+
+        // TODO: lets find a nicer way to do this
+        EventNotifier notifier = new EventNotifierSupport() {
+            @Override
+            public void notify(CamelEvent event) throws Exception {
+                String id = getCamelContext().addRouteFromTemplate(routeId, templateId, kameletProperties);
+                List<RouteDefinition> list = new ArrayList<>(1);
+                list.add(getCamelContext().adapt(ModelCamelContext.class).getRouteDefinition(id));
+                getCamelContext().adapt(ModelCamelContext.class).startRouteDefinitions(list);
+                // no longer needed so we can remove ourselves
+                getCamelContext().getManagementStrategy().removeEventNotifier(this);
+            }
+
+            @Override
+            public boolean isEnabled(CamelEvent event) {
+                return event instanceof CamelContextInitializedEvent;
+            }
+        };
 
-        super.doStart();
+        ServiceHelper.startService(notifier);
+        getCamelContext().getManagementStrategy().addEventNotifier(notifier);
     }
 
     // *********************************
diff --git a/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletTest.java
b/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletTest.java
index 8f95084..32634a0 100644
--- a/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletTest.java
+++ b/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletTest.java
@@ -58,7 +58,7 @@ public class KameletTest {
             public void configure() throws Exception {
                 // routes
                 from("direct:template")
-                    .to("kamelet:setBody/test?bodyValue=bv")
+                    .toF("kamelet:setBody/test?bodyValue=%s", body)
                     .to("log:1");
             }
         });


Mime
View raw message