This is an automated email from the ASF dual-hosted git repository.
fmariani pushed a commit to branch camel-4.14.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.14.x by this push:
new 4187d177c7d ContextServicePlugin SPI - provides a plugin system for
Apache Camel that allows automatic discovery and initialization of third-party
components during CamelContext startup.
4187d177c7d is described below
commit 4187d177c7d729e8f584bee794200937c27289e2
Author: Croway <[email protected]>
AuthorDate: Tue Sep 16 17:41:14 2025 +0200
ContextServicePlugin SPI - provides a plugin system for Apache Camel that
allows automatic discovery and initialization of third-party components during
CamelContext startup.
CAMEL-22423: Start the ContextServiceLoaderPluginResolver
CAMEL-22423: Start the ContextServiceLoaderPluginResolver
---
.../spi/ContextServiceLoaderPluginResolver.java | 41 ++++++++++++
.../org/apache/camel/spi/ContextServicePlugin.java | 69 +++++++++++++++++++
.../camel/impl/engine/AbstractCamelContext.java | 13 ++++
.../engine/DefaultContextServiceLoaderPlugin.java | 77 ++++++++++++++++++++++
.../camel/impl/engine/SimpleCamelContext.java | 6 ++
.../java/org/apache/camel/main/KameletMain.java | 11 ++++
6 files changed, 217 insertions(+)
diff --git
a/core/camel-api/src/main/java/org/apache/camel/spi/ContextServiceLoaderPluginResolver.java
b/core/camel-api/src/main/java/org/apache/camel/spi/ContextServiceLoaderPluginResolver.java
new file mode 100644
index 00000000000..4e49e02cc92
--- /dev/null
+++
b/core/camel-api/src/main/java/org/apache/camel/spi/ContextServiceLoaderPluginResolver.java
@@ -0,0 +1,41 @@
+/*
+ * 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.spi;
+
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.StatefulService;
+
+/**
+ * Service Provider Interface (SPI) for discovering and loading {@link
ContextServicePlugin} implementations during
+ * CamelContext initialization.
+ * <p>
+ * This resolver is responsible for automatically discovering and loading
plugins that extend Camel's functionality
+ * through the Java ServiceLoader mechanism. Implementations of this interface
participate in the Camel service
+ * lifecycle and are typically invoked during CamelContext startup to
initialize third-party components and extensions.
+ * <p>
+ * The plugin resolution process typically occurs after the CamelContext has
been created and configured but before
+ * routes are started, ensuring that all plugins have the opportunity to
modify or extend the context as needed.
+ * <p>
+ * Implementations must be stateful services that can be started and stopped
as part of the normal Camel lifecycle, and
+ * they must be aware of the CamelContext they are operating on.
+ *
+ * @see ContextServicePlugin
+ * @see CamelContextAware
+ * @see StatefulService
+ */
+public interface ContextServiceLoaderPluginResolver extends CamelContextAware,
StatefulService {
+}
diff --git
a/core/camel-api/src/main/java/org/apache/camel/spi/ContextServicePlugin.java
b/core/camel-api/src/main/java/org/apache/camel/spi/ContextServicePlugin.java
new file mode 100644
index 00000000000..22e6553f6c2
--- /dev/null
+++
b/core/camel-api/src/main/java/org/apache/camel/spi/ContextServicePlugin.java
@@ -0,0 +1,69 @@
+/*
+ * 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.spi;
+
+import org.apache.camel.CamelContext;
+
+/**
+ * A plugin interface that allows third-party components to perform
initialization tasks when a CamelContext is being
+ * configured and started.
+ * <p>
+ * Implementations of this interface are automatically discovered and loaded
via the Java ServiceLoader mechanism. To
+ * register a plugin, create a service provider configuration file at
+ * {@code META-INF/services/org.apache.camel.spi.ContextServicePlugin}
containing the fully qualified class name of your
+ * implementation.
+ * <p>
+ * Common use cases include:
+ * <ul>
+ * <li>Registering beans in the Camel registry</li>
+ * <li>Adding event notifiers for monitoring</li>
+ * <li>Configuring global interceptors</li>
+ * <li>Setting up custom type converters</li>
+ * </ul>
+ *
+ * <h3>Example Usage:</h3>
+ *
+ * <pre>
+ * <code>
+ * public class MyContextServicePlugin implements ContextServicePlugin {
+ * @Override
+ * public void load(CamelContext camelContext) {
+ * // Register a bean in the registry
+ * camelContext.getRegistry().bind("myBean", new MyBean());
+ *
+ * // Add an event notifier
+ * camelContext.getManagementStrategy().addEventNotifier(new
MyEventNotifier());
+ * }
+ * }
+ * </code>
+ * </pre>
+ *
+ * @see org.apache.camel.impl.engine.DefaultContextServiceLoaderPlugin
+ */
+public interface ContextServicePlugin {
+
+ /**
+ * Called during CamelContext initialization to allow the plugin to
configure or customize the context.
+ * <p>
+ * This method is invoked after the CamelContext has been created but
before routes are started. Implementations
+ * should perform any necessary setup operations such as registering
beans, adding event notifiers, or configuring
+ * global settings.
+ *
+ * @param camelContext the CamelContext being initialized, never {@code
null}
+ */
+ void load(CamelContext camelContext);
+}
diff --git
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 89c8ae73411..bfc4026effe 100644
---
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -104,6 +104,7 @@ import org.apache.camel.spi.CliConnectorFactory;
import org.apache.camel.spi.ComponentNameResolver;
import org.apache.camel.spi.ComponentResolver;
import org.apache.camel.spi.ConfigurerResolver;
+import org.apache.camel.spi.ContextServiceLoaderPluginResolver;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.spi.DataFormatResolver;
import org.apache.camel.spi.DataType;
@@ -347,6 +348,7 @@ public abstract class AbstractCamelContext extends
BaseService
* Called during object construction to initialize context plugins
*/
protected void initPlugins() {
+
camelContextExtension.addContextPlugin(ContextServiceLoaderPluginResolver.class,
createContextServiceLoaderPlugin());
camelContextExtension.addContextPlugin(StartupConditionStrategy.class,
createStartupConditionStrategy());
camelContextExtension.addContextPlugin(CamelBeanPostProcessor.class,
createBeanPostProcessor());
camelContextExtension.addContextPlugin(CamelDependencyInjectionAnnotationFactory.class,
@@ -2341,6 +2343,15 @@ public abstract class AbstractCamelContext extends
BaseService
startupStepRecorder.endStep(step5);
}
+ // Start context service loader plugin to discover and load
third-party plugins early in build phase
+ ContextServiceLoaderPluginResolver contextServicePlugin
+ =
camelContextExtension.getContextPlugin(ContextServiceLoaderPluginResolver.class);
+ if (contextServicePlugin != null) {
+ StartupStep step6 =
startupStepRecorder.beginStep(CamelContext.class, null, "Start
ContextServiceLoaderPlugin");
+ ServiceHelper.startService(contextServicePlugin);
+ startupStepRecorder.endStep(step6);
+ }
+
// Call all registered trackers with this context
// Note, this may use a partially constructed object
CamelContextTracker.notifyContextCreated(this);
@@ -4431,6 +4442,8 @@ public abstract class AbstractCamelContext extends
BaseService
protected abstract StartupConditionStrategy
createStartupConditionStrategy();
+ protected abstract ContextServiceLoaderPluginResolver
createContextServiceLoaderPlugin();
+
protected abstract BackOffTimerFactory createBackOffTimerFactory();
protected abstract TaskManagerRegistry createTaskManagerRegistry();
diff --git
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextServiceLoaderPlugin.java
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextServiceLoaderPlugin.java
new file mode 100644
index 00000000000..41840bd68eb
--- /dev/null
+++
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextServiceLoaderPlugin.java
@@ -0,0 +1,77 @@
+/*
+ * 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.impl.engine;
+
+import java.util.ServiceLoader;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.spi.ContextServiceLoaderPluginResolver;
+import org.apache.camel.spi.ContextServicePlugin;
+import org.apache.camel.support.service.ServiceSupport;
+
+/**
+ * Default implementation that automatically discovers and loads {@link
ContextServicePlugin} implementations using the
+ * Java ServiceLoader mechanism.
+ * <p>
+ * This service is responsible for scanning the classpath for implementations
of {@code ContextServicePlugin} and
+ * invoking their {@code load} method during CamelContext startup. Plugin
implementations are discovered through service
+ * provider configuration files located at: {@code
META-INF/services/org.apache.camel.spi.ContextServicePlugin}
+ * <p>
+ * The loading process occurs during the {@link #doStart()} phase, ensuring
that all plugins are initialized before
+ * routes are started but after the CamelContext has been created and
configured.
+ * <p>
+ * This class extends {@link ServiceSupport} to participate in the Camel
service lifecycle and implements
+ * {@link CamelContextAware} to receive the CamelContext instance that plugins
will operate on.
+ *
+ * @see ContextServicePlugin
+ * @see ServiceLoader
+ */
+public class DefaultContextServiceLoaderPlugin extends ServiceSupport
implements ContextServiceLoaderPluginResolver {
+ private CamelContext camelContext;
+
+ /**
+ * Discovers and loads all {@link ContextServicePlugin} implementations
found on the classpath.
+ * <p>
+ * This method is called during service startup and uses {@link
ServiceLoader} to automatically discover plugin
+ * implementations. Each discovered plugin's {@code load} method is
invoked with the current CamelContext, allowing
+ * plugins to perform their initialization logic.
+ * <p>
+ * The plugins are loaded in the order they are discovered by the
ServiceLoader, which may vary between JVM
+ * implementations and is generally not guaranteed to be deterministic.
+ *
+ * @throws Exception if any plugin fails to load or throws an exception
during initialization
+ */
+ @Override
+ protected void doStart() throws Exception {
+ ServiceLoader<ContextServicePlugin> contextServicePlugins =
ServiceLoader.load(ContextServicePlugin.class,
+ camelContext.getApplicationContextClassLoader());
+ for (ContextServicePlugin plugin : contextServicePlugins) {
+ plugin.load(camelContext);
+ }
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ @Override
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+}
diff --git
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
index 941ce38253e..0430cfdc7fe 100644
---
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
+++
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
@@ -46,6 +46,7 @@ import org.apache.camel.spi.CliConnectorFactory;
import org.apache.camel.spi.ComponentNameResolver;
import org.apache.camel.spi.ComponentResolver;
import org.apache.camel.spi.ConfigurerResolver;
+import org.apache.camel.spi.ContextServiceLoaderPluginResolver;
import org.apache.camel.spi.DataFormatResolver;
import org.apache.camel.spi.DeferServiceFactory;
import org.apache.camel.spi.DumpRoutesStrategy;
@@ -755,6 +756,11 @@ public class SimpleCamelContext extends
AbstractCamelContext {
return new DefaultStartupConditionStrategy();
}
+ @Override
+ protected ContextServiceLoaderPluginResolver
createContextServiceLoaderPlugin() {
+ return new DefaultContextServiceLoaderPlugin();
+ }
+
@Override
protected BackOffTimerFactory createBackOffTimerFactory() {
return new DefaultBackOffTimerFactory(this);
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 95a48ebb021..65fdc91bbae 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
@@ -89,6 +89,7 @@ import org.apache.camel.spi.CliConnector;
import org.apache.camel.spi.CliConnectorFactory;
import org.apache.camel.spi.CompileStrategy;
import org.apache.camel.spi.ComponentResolver;
+import org.apache.camel.spi.ContextServiceLoaderPluginResolver;
import org.apache.camel.spi.DataFormatResolver;
import org.apache.camel.spi.FactoryFinder;
import org.apache.camel.spi.FactoryFinderResolver;
@@ -534,6 +535,7 @@ public class KameletMain extends MainCommandLineSupport {
}
}
}
+
configure().withProfile(profile);
// embed HTTP server if port is specified
@@ -732,6 +734,15 @@ public class KameletMain extends MainCommandLineSupport {
throw RuntimeCamelException.wrapRuntimeException(e);
}
+ // Start the context service loader plugin after all dependencies and
downloaders are set up
+ // This ensures the classpath is enhanced and ServiceLoader can
discover third-party plugins
+ ContextServiceLoaderPluginResolver contextServicePlugin = answer
+
.getCamelContextExtension().getContextPlugin(ContextServiceLoaderPluginResolver.class);
+ if (contextServicePlugin != null) {
+ // force start context service loader plugin to discover and load
third-party plugins
+ ServiceHelper.startService(contextServicePlugin);
+ }
+
return answer;
}