You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/04/01 06:47:07 UTC

[camel] 02/03: CAMEL-15105: added support for lazy initialization of plugins

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

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

commit 0f6bcbed35ca6505081f8fd229734d97edb57a2f
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Fri Mar 31 17:29:46 2023 +0200

    CAMEL-15105: added support for lazy initialization of plugins
---
 .../src/main/java/org/apache/camel/ExtendedCamelContext.java  |  8 ++++++++
 .../src/main/java/org/apache/camel/spi/PluginManager.java     |  8 ++++++++
 .../camel/impl/engine/DefaultCamelContextExtension.java       | 11 +++++++++++
 .../camel/impl/lw/LightweightCamelContextExtension.java       |  5 +++++
 4 files changed, 32 insertions(+)

diff --git a/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java b/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
index 7e08346dd92..291a324b3bc 100644
--- a/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
@@ -751,4 +751,12 @@ public interface ExtendedCamelContext {
      * @param module the instance of the extension
      */
     <T> void addContextPlugin(Class<T> type, T module);
+
+    /**
+     * Allows lazy installation of custom plugins to the Camel context.
+     *
+     * @param type   the type of the extension
+     * @param module the instance of the extension
+     */
+    <T> void lazyAddContextPlugin(Class<T> type, Supplier<T> module);
 }
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/PluginManager.java b/core/camel-api/src/main/java/org/apache/camel/spi/PluginManager.java
index 0b21cee566b..11043f7092b 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/PluginManager.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PluginManager.java
@@ -37,4 +37,12 @@ public interface PluginManager {
      * @param module the instance of the extension
      */
     <T> void addContextPlugin(Class<T> type, T module);
+
+    /**
+     * Allows lazy installation of custom plugins to the Camel context.
+     *
+     * @param type   the type of the extension
+     * @param module the instance of the extension
+     */
+    <T> void lazyAddContextPlugin(Class<T> type, Supplier<T> module);
 }
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java
index 21c0d1f15cc..54715bbb700 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java
@@ -925,4 +925,15 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
         final T addedModule = camelContext.getInternalServiceManager().addService(module);
         pluginManager.addContextPlugin(type, addedModule);
     }
+
+    @Override
+    public <T> void lazyAddContextPlugin(Class<T> type, Supplier<T> module) {
+        pluginManager.lazyAddContextPlugin(type, () -> lazyInitAndAdd(module));
+    }
+
+    private <T> T lazyInitAndAdd(Supplier<T> supplier) {
+        T module = supplier.get();
+
+        return camelContext.getInternalServiceManager().addService(module);
+    }
 }
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightCamelContextExtension.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightCamelContextExtension.java
index 40717ac7f95..4975be42564 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightCamelContextExtension.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightCamelContextExtension.java
@@ -750,6 +750,11 @@ class LightweightCamelContextExtension implements ExtendedCamelContext {
         camelContext.getCamelContextExtension().addContextPlugin(type, module);
     }
 
+    @Override
+    public <T> void lazyAddContextPlugin(Class<T> type, Supplier<T> module) {
+        camelContext.getCamelContextExtension().lazyAddContextPlugin(type, module);
+    }
+
     Endpoint doGetEndpoint(String uri, boolean normalized, boolean prototype) {
         StringHelper.notEmpty(uri, "uri");
         // in case path has property placeholders then try to let property