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 2021/11/21 09:32:03 UTC

[camel] 01/02: CAMEL-17217: camel-kamelet-main - Auto download DSL when loading routes in different DSLs

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

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

commit 29b9e643e347c19c08b57a8d7846f99917ab2363
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Nov 21 10:30:16 2021 +0100

    CAMEL-17217: camel-kamelet-main - Auto download DSL when loading routes in different DSLs
---
 .../camel/impl/engine/DefaultRoutesLoader.java     |  6 +-
 .../src/main/docs/kamelet-main.adoc                |  6 +-
 .../main/DependencyDownloaderRoutesLoader.java     | 85 ++++++++++++++++++++++
 .../java/org/apache/camel/main/KameletMain.java    |  2 +
 .../apache/camel/main/KameletMainGithubTest.java   | 10 +++
 5 files changed, 105 insertions(+), 4 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java
index e060d8f..9795917 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoutesLoader.java
@@ -110,7 +110,7 @@ public class DefaultRoutesLoader extends ServiceSupport implements RoutesLoader
      * @return                          a {@link RoutesBuilderLoader}
      * @throws IllegalArgumentException if no {@link RoutesBuilderLoader} can be found for the given file extension
      */
-    private RoutesBuilderLoader getRoutesLoader(String extension) throws Exception {
+    protected RoutesBuilderLoader getRoutesLoader(String extension) throws Exception {
         RoutesBuilderLoader answer = getCamelContext().getRegistry().lookupByNameAndType(
                 ROUTES_LOADER_KEY_PREFIX + extension,
                 RoutesBuilderLoader.class);
@@ -125,10 +125,10 @@ public class DefaultRoutesLoader extends ServiceSupport implements RoutesLoader
     /**
      * Looks up a {@link RoutesBuilderLoader} for the given extension with factory finder.
      *
-     * @param  extension the file extension for which a loader should be find.
+     * @param  extension the file extension for which a loader should be found.
      * @return           a {@link RoutesBuilderLoader} or null if none found
      */
-    private RoutesBuilderLoader resolveService(String extension) {
+    protected RoutesBuilderLoader resolveService(String extension) {
         final ExtendedCamelContext ecc = getCamelContext().adapt(ExtendedCamelContext.class);
         final FactoryFinder finder = ecc.getBootstrapFactoryFinder(RoutesBuilderLoader.FACTORY_PATH);
 
diff --git a/dsl/camel-kamelet-main/src/main/docs/kamelet-main.adoc b/dsl/camel-kamelet-main/src/main/docs/kamelet-main.adoc
index 94f5654..9515d46 100644
--- a/dsl/camel-kamelet-main/src/main/docs/kamelet-main.adoc
+++ b/dsl/camel-kamelet-main/src/main/docs/kamelet-main.adoc
@@ -23,7 +23,11 @@ The `KameletMain` is pre-configured with the following properties:
 ----
 camel.component.kamelet.location = classpath:/kamelets,github:apache:camel-kamelets
 camel.main.routes-include-pattern = classpath:camel/*
-camel.main.lightweight = true
+camel.main.routes-reload-enabled = true
+camel.main.routes-reload-directory = src/main/resources
+camel.main.routes-reload-pattern = camel/*.yaml
+camel.main.shutdown-timeout = 5
+camel.main.lightweight = false
 ----
 
 These settings can be overridden by configuration in `application.properties`.
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DependencyDownloaderRoutesLoader.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DependencyDownloaderRoutesLoader.java
new file mode 100644
index 0000000..e7e3b3a
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DependencyDownloaderRoutesLoader.java
@@ -0,0 +1,85 @@
+/*
+ * 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.CamelContextAware;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.impl.engine.DefaultRoutesLoader;
+import org.apache.camel.spi.FactoryFinder;
+import org.apache.camel.spi.RoutesBuilderLoader;
+import org.apache.camel.support.ResolverHelper;
+import org.apache.camel.support.service.ServiceHelper;
+
+/**
+ * Auto downloaded needed DSL JARs.
+ */
+public class DependencyDownloaderRoutesLoader extends DefaultRoutesLoader {
+
+    @Override
+    protected RoutesBuilderLoader getRoutesLoader(String extension) throws Exception {
+        RoutesBuilderLoader loader = super.getRoutesLoader(extension);
+        if (loader == null) {
+            loader = resolveService(extension);
+        }
+        return loader;
+    }
+
+    @Override
+    protected RoutesBuilderLoader resolveService(String extension) {
+        RoutesBuilderLoader loader = super.resolveService(extension);
+
+        if (loader == null) {
+            if ("groovy".equals(extension)) {
+                downloadLoader("camel-groovy-dsl");
+            } else if ("java".equals(extension)) {
+                downloadLoader("camel-java-joor-dsl");
+            } else if ("js".equals(extension)) {
+                downloadLoader("camel-js-dsl");
+            } else if ("kts".equals(extension)) {
+                downloadLoader("camel-kotlin-dsl");
+            } else if ("xml".equals(extension)) {
+                downloadLoader("camel-xml-io-dsl");
+            } else if ("yaml".equals(extension)) {
+                downloadLoader("camel-yaml-dsl");
+            }
+
+            // need to use regular factory finder as bootstrap has already marked the loader as a miss
+            final ExtendedCamelContext ecc = getCamelContext().adapt(ExtendedCamelContext.class);
+            final FactoryFinder finder = ecc.getFactoryFinder(RoutesBuilderLoader.FACTORY_PATH);
+
+            RoutesBuilderLoader answer
+                    = ResolverHelper.resolveService(ecc, finder, extension, RoutesBuilderLoader.class).orElse(null);
+
+            if (answer != null) {
+                CamelContextAware.trySetCamelContext(answer, getCamelContext());
+                ServiceHelper.startService(answer);
+            }
+
+            return answer;
+        }
+
+        return loader;
+    }
+
+    private void downloadLoader(String artifactId) {
+        if (!DownloaderHelper.alreadyOnClasspath(getCamelContext(), artifactId)) {
+            DownloaderHelper.downloadDependency(getCamelContext(), "org.apache.camel", artifactId,
+                    getCamelContext().getVersion());
+        }
+    }
+
+}
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
index 3274e80..471c4db 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
@@ -155,6 +155,7 @@ public class KameletMain extends MainCommandLineSupport {
                 answer.setComponentResolver(new DependencyDownloaderComponentResolver(answer));
                 answer.setDataFormatResolver(new DependencyDownloaderDataFormatResolver(answer));
                 answer.setLanguageResolver(new DependencyDownloaderLanguageResolver(answer));
+                answer.setRoutesLoader(new DependencyDownloaderRoutesLoader());
                 answer.addService(new DependencyDownloaderKamelet());
             } catch (Exception e) {
                 throw RuntimeCamelException.wrapRuntimeException(e);
@@ -169,6 +170,7 @@ public class KameletMain extends MainCommandLineSupport {
      */
     protected void configureInitialProperties() {
         addInitialProperty("camel.component.kamelet.location", "classpath:/kamelets,github:apache:camel-kamelets");
+        addInitialProperty("camel.main.routes-include-pattern", "classpath:camel/*");
         addInitialProperty("camel.main.routes-reload-enabled", "true");
         addInitialProperty("camel.main.routes-reload-directory", "src/main/resources");
         addInitialProperty("camel.main.routes-reload-pattern", "camel/*.yaml");
diff --git a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainGithubTest.java b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainGithubTest.java
index a91ba5e..397c80a 100644
--- a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainGithubTest.java
+++ b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainGithubTest.java
@@ -31,4 +31,14 @@ public class KameletMainGithubTest {
 
         main.run();
     }
+
+    @Test
+    public void testKameletMainGroovy() throws Exception {
+        KameletMain main = new KameletMain();
+        main.setDownload(true);
+        main.configure().withDurationMaxSeconds(10);
+        main.configure().withRoutesIncludePattern("github:apache:camel-k:examples/languages/simple.groovy");
+
+        main.run();
+    }
 }