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 09549a6719a5d3fc4054e5d09e000003c62fcbac
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Wed Aug 3 11:31:14 2022 +0200

    CAMEL-18339: Java DSL (joor) - Allow compiled classes to be loadable from 
anywhere in Camel
---
 .../java/org/apache/camel/spi/ClassResolver.java   |  7 ++++
 .../camel/impl/engine/DefaultClassResolver.java    | 30 +++++++++++----
 .../camel/dsl/java/joor/JavaJoorClassLoader.java   | 45 ++++++++++++++++++++++
 .../dsl/java/joor/JavaRoutesBuilderLoader.java     | 14 +++++++
 4 files changed, 89 insertions(+), 7 deletions(-)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/ClassResolver.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/ClassResolver.java
index c00ac92ebf6..f7748d7eb3e 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/ClassResolver.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/ClassResolver.java
@@ -26,6 +26,13 @@ import java.util.Enumeration;
  */
 public interface ClassResolver {
 
+    /**
+     * Adds a custom class loader to use.
+     *
+     * @param classLoader a custom class loader
+     */
+    void addClassLoader(ClassLoader classLoader);
+
     /**
      * Resolves the given class by its name
      *
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultClassResolver.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultClassResolver.java
index eb7a032fdbc..14f4cc89ff5 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultClassResolver.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultClassResolver.java
@@ -19,6 +19,8 @@ package org.apache.camel.impl.engine;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.Enumeration;
+import java.util.LinkedHashSet;
+import java.util.Set;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
@@ -31,6 +33,7 @@ import org.apache.camel.util.ObjectHelper;
  */
 public class DefaultClassResolver implements ClassResolver, CamelContextAware {
 
+    private Set<ClassLoader> classLoaders;
     private CamelContext camelContext;
 
     public DefaultClassResolver() {
@@ -50,9 +53,27 @@ public class DefaultClassResolver implements ClassResolver, 
CamelContextAware {
         return camelContext;
     }
 
+    @Override
+    public void addClassLoader(ClassLoader classLoader) {
+        if (classLoaders == null) {
+            classLoaders = new LinkedHashSet<>();
+        }
+        classLoaders.add(classLoader);
+    }
+
     @Override
     public Class<?> resolveClass(String name) {
-        Class<?> answer = loadClass(name, 
DefaultClassResolver.class.getClassLoader());
+        Class<?> answer;
+        if (classLoaders != null) {
+            for (ClassLoader loader : classLoaders) {
+                answer = loadClass(name, loader);
+                if (answer != null) {
+                    return answer;
+                }
+            }
+        }
+
+        answer = loadClass(name, DefaultClassResolver.class.getClassLoader());
         if (answer == null && getApplicationContextClassLoader() != null) {
             // fallback and use application context class loader
             answer = loadClass(name, getApplicationContextClassLoader());
@@ -62,12 +83,7 @@ public class DefaultClassResolver implements ClassResolver, 
CamelContextAware {
 
     @Override
     public <T> Class<T> resolveClass(String name, Class<T> type) {
-        Class<T> answer = CastUtils.cast(loadClass(name, 
DefaultClassResolver.class.getClassLoader()));
-        if (answer == null && getApplicationContextClassLoader() != null) {
-            // fallback and use application context class loader
-            answer = CastUtils.cast(loadClass(name, 
getApplicationContextClassLoader()));
-        }
-        return answer;
+        return CastUtils.cast(resolveClass(name), type);
     }
 
     @Override
diff --git 
a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaJoorClassLoader.java
 
b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaJoorClassLoader.java
new file mode 100644
index 00000000000..d9b8f630778
--- /dev/null
+++ 
b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaJoorClassLoader.java
@@ -0,0 +1,45 @@
+/*
+ * 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.dsl.java.joor;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.CompilePostProcessor;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class JavaJoorClassLoader extends ClassLoader implements 
CompilePostProcessor {
+
+    private final Map<String, Class<?>> classes = new HashMap<>();
+
+    public JavaJoorClassLoader() {
+        super(JavaJoorClassLoader.class.getClassLoader());
+    }
+
+    @Override
+    protected Class<?> findClass(String name) throws ClassNotFoundException {
+        return classes.get(name);
+    }
+
+    @Override
+    public void postCompile(CamelContext camelContext, String name, Class<?> 
clazz, byte[] byteCode, Object instance) throws Exception {
+        if (name != null && clazz != null) {
+            classes.put(name, clazz);
+        }
+    }
+
+}
diff --git 
a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
 
b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
index 413f7ec1759..970612f5fb0 100644
--- 
a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
+++ 
b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
@@ -30,6 +30,7 @@ import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.RoutesBuilder;
@@ -64,6 +65,19 @@ public class JavaRoutesBuilderLoader extends 
ExtendedRouteBuilderLoaderSupport {
         super(EXTENSION);
     }
 
+    @Override
+    protected void doBuild() throws Exception {
+        super.doBuild();
+
+        // register joor classloader to camel so we are able to load classes 
we have compiled
+        CamelContext context = getCamelContext();
+        if (context != null) {
+            JavaJoorClassLoader cl = new JavaJoorClassLoader();
+            context.getClassResolver().addClassLoader(cl);
+            addCompilePostProcessor(cl);
+        }
+    }
+
     @Override
     protected RouteBuilder doLoadRouteBuilder(Resource resource) throws 
Exception {
         Collection<RoutesBuilder> answer = 
doLoadRoutesBuilders(List.of(resource));

Reply via email to