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 2022/03/24 15:40:49 UTC

[camel] branch main updated: CAMEL-17820: camel-jbang - Additional files from CLI is automatic added to classpath to make it easy to use them by Camel.

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


The following commit(s) were added to refs/heads/main by this push:
     new ed842b6  CAMEL-17820: camel-jbang - Additional files from CLI is automatic added to classpath to make it easy to use them by Camel.
ed842b6 is described below

commit ed842b6a2e57ed0799d81efd53bda002c16f3187
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Mar 24 16:39:42 2022 +0100

    CAMEL-17820: camel-jbang - Additional files from CLI is automatic added to classpath to make it easy to use them by Camel.
---
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 13 +++++-
 .../apache/camel/main/ExtraFilesClassLoader.java   | 54 ++++++++++++++++++++++
 .../java/org/apache/camel/main/KameletMain.java    | 10 +++-
 3 files changed, 74 insertions(+), 3 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 57390a1..9e2e0ce 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -229,12 +229,18 @@ class Run implements Callable<Integer> {
 
         StringJoiner js = new StringJoiner(",");
         StringJoiner sjReload = new StringJoiner(",");
+        StringJoiner sjClasspathFiles = new StringJoiner(",");
+
         for (String file : files) {
 
-            if (!acceptFile(file)) {
+            if (!knownFile(file)) {
+                // non known files to be added on classpath
+                sjClasspathFiles.add(file);
                 continue;
             }
 
+            // process known files as its likely DSLs or configuration files
+
             // check for properties files
             if (file.endsWith(".properties")) {
                 if (!ResourceHelper.hasScheme(file) && !file.startsWith("github:")) {
@@ -303,6 +309,9 @@ class Run implements Callable<Integer> {
             }
         }
         main.addInitialProperty("camel.main.routesIncludePattern", js.toString());
+        if (sjClasspathFiles.length() > 0) {
+            main.addInitialProperty("camel.jbang.classpathFiles", sjClasspathFiles.toString());
+        }
 
         // we can only reload if file based
         if (reload && sjReload.length() > 0) {
@@ -454,7 +463,7 @@ class Run implements Callable<Integer> {
         }
     }
 
-    private boolean acceptFile(String file) {
+    private boolean knownFile(String file) {
         String ext = FileUtil.onlyExt(file, true);
         return Arrays.stream(ACCEPTED_FILE_EXT).anyMatch(e -> e.equalsIgnoreCase(ext));
     }
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/ExtraFilesClassLoader.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/ExtraFilesClassLoader.java
new file mode 100644
index 0000000..4005a09
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/ExtraFilesClassLoader.java
@@ -0,0 +1,54 @@
+/*
+ * 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 java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+/**
+ * Classloader used to load the extra files that were present in the CLI arguments
+ */
+final class ExtraFilesClassLoader extends ClassLoader {
+
+    final String[] files;
+
+    public ExtraFilesClassLoader(ClassLoader parent, String[] files) {
+        super(parent);
+        this.files = files;
+    }
+
+    @Override
+    protected URL findResource(String name) {
+        return getResource(name);
+    }
+
+    @Override
+    public URL getResource(String name) {
+        for (String n : files) {
+            if (name.equals(n)) {
+                try {
+                    return new File(name).toURI().toURL();
+                } catch (MalformedURLException e) {
+                    // ignore
+                }
+            }
+        }
+        return null;
+    }
+
+}
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 a5ef447..9e6c088 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
@@ -157,8 +157,16 @@ public class KameletMain extends MainCommandLineSupport {
     protected CamelContext createCamelContext() {
         // do not build/init camel context yet
         DefaultCamelContext answer = new DefaultCamelContext(false);
+
+        // any additional files to add to classpath
+        ClassLoader parentCL = KameletMain.class.getClassLoader();
+        String cpFiles = getInitialProperties().getProperty("camel.jbang.classpathFiles");
+        if (cpFiles != null) {
+            parentCL = new ExtraFilesClassLoader(parentCL, cpFiles.split(","));
+            LOG.info("Additional files added to classpath: {}", cpFiles);
+        }
         if (kameletClassLoader == null) {
-            kameletClassLoader = new GroovyClassLoader(KameletMain.class.getClassLoader());
+            kameletClassLoader = new GroovyClassLoader(parentCL);
         }
         answer.setApplicationContextClassLoader(kameletClassLoader);
         answer.setRegistry(registry);