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 c8ab2fc  CAMEL-17647: camel-core - Properties component should support 
pluggable functions
c8ab2fc is described below

commit c8ab2fca5b1450ab7a79b940339830e0bab2a411
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Feb 15 12:09:16 2022 +0100

    CAMEL-17647: camel-core - Properties component should support pluggable 
functions
---
 .../component/properties/PropertiesComponent.java  | 10 ++++----
 .../properties/PropertiesFunctionResolver.java     | 27 +++++++++++++++++++++-
 .../PropertiesComponentFunctionTest.java           | 16 +++++++++++--
 .../ROOT/pages/using-propertyplaceholder.adoc      |  3 +++
 4 files changed, 48 insertions(+), 8 deletions(-)

diff --git 
a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
 
b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
index bc11a82..0eb3bf4 100644
--- 
a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
+++ 
b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
@@ -688,27 +688,27 @@ public class PropertiesComponent extends ServiceSupport
         }
 
         sources.sort(OrderedComparator.get());
-        ServiceHelper.initService(sources);
+        ServiceHelper.initService(sources, functionResolver);
     }
 
     @Override
     protected void doBuild() throws Exception {
-        ServiceHelper.buildService(sources);
+        ServiceHelper.buildService(sources, functionResolver);
     }
 
     @Override
     protected void doStart() throws Exception {
-        ServiceHelper.startService(sources);
+        ServiceHelper.startService(sources, functionResolver);
     }
 
     @Override
     protected void doStop() throws Exception {
-        ServiceHelper.stopService(sources);
+        ServiceHelper.stopService(sources, functionResolver);
     }
 
     @Override
     protected void doShutdown() throws Exception {
-        ServiceHelper.stopAndShutdownServices(sources);
+        ServiceHelper.stopAndShutdownServices(sources, functionResolver);
     }
 
     private void addPropertiesLocationsAsPropertiesSource(PropertiesLocation 
location) {
diff --git 
a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java
 
b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java
index ec1d733..46155b9 100644
--- 
a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java
+++ 
b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java
@@ -22,15 +22,20 @@ import java.util.Map;
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.NonManagedService;
+import org.apache.camel.StaticService;
 import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.PropertiesFunction;
+import org.apache.camel.support.service.ServiceHelper;
+import org.apache.camel.support.service.ServiceSupport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Resolver for built-in and custom {@link PropertiesFunction}.
  */
-public final class PropertiesFunctionResolver implements CamelContextAware {
+public final class PropertiesFunctionResolver extends ServiceSupport
+        implements CamelContextAware, NonManagedService, StaticService {
 
     public static final String RESOURCE_PATH = 
"META-INF/services/org/apache/camel/properties-function/";
 
@@ -109,6 +114,7 @@ public final class PropertiesFunctionResolver implements 
CamelContextAware {
             if (PropertiesFunction.class.isAssignableFrom(type)) {
                 PropertiesFunction answer = (PropertiesFunction) 
context.getInjector().newInstance(type, false);
                 CamelContextAware.trySetCamelContext(answer, camelContext);
+                ServiceHelper.startService(answer);
                 return answer;
             } else {
                 throw new IllegalArgumentException("Type is not a 
PropertiesFunction implementation. Found: " + type.getName());
@@ -125,4 +131,23 @@ public final class PropertiesFunctionResolver implements 
CamelContextAware {
         return factoryFinder.findClass(name).orElse(null);
     }
 
+    @Override
+    protected void doInit() throws Exception {
+        ServiceHelper.initService(functions.values());
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        ServiceHelper.startService(functions.values());
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        ServiceHelper.stopService(functions.values());
+    }
+
+    @Override
+    protected void doShutdown() throws Exception {
+        ServiceHelper.stopAndShutdownService(functions.values());
+    }
 }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java
index 1fa9970..4dde8c5 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java
@@ -19,11 +19,13 @@ package org.apache.camel.component.properties;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.spi.PropertiesFunction;
+import org.apache.camel.support.service.ServiceSupport;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 public class PropertiesComponentFunctionTest extends ContextTestSupport {
 
-    public static final class MyFunction implements PropertiesFunction {
+    public static final class MyFunction extends ServiceSupport implements 
PropertiesFunction {
 
         @Override
         public String getName() {
@@ -34,6 +36,7 @@ public class PropertiesComponentFunctionTest extends 
ContextTestSupport {
         public String apply(String remainder) {
             return "mock:" + remainder.toLowerCase();
         }
+
     }
 
     @Override
@@ -43,8 +46,9 @@ public class PropertiesComponentFunctionTest extends 
ContextTestSupport {
 
     @Test
     public void testFunction() throws Exception {
+        MyFunction func = new MyFunction();
         PropertiesComponent pc = (PropertiesComponent) 
context.getPropertiesComponent();
-        pc.addPropertiesFunction(new MyFunction());
+        pc.addPropertiesFunction(func);
 
         context.addRoutes(new RouteBuilder() {
             @Override
@@ -54,12 +58,20 @@ public class PropertiesComponentFunctionTest extends 
ContextTestSupport {
         });
         context.start();
 
+        // function should be started by camel
+        Assertions.assertTrue(func.isStarted());
+
         getMockEndpoint("mock:foo").expectedMessageCount(1);
         getMockEndpoint("mock:bar").expectedMessageCount(1);
 
         template.sendBody("direct:start", "Hello World");
 
         assertMockEndpointsSatisfied();
+
+        context.stop();
+
+        // function should be stopped by camel also
+        Assertions.assertTrue(func.isStopped());
     }
 
 }
diff --git a/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc 
b/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc
index d7410ac..a533e98 100644
--- a/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc
+++ b/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc
@@ -541,6 +541,9 @@ You also need to have `camel-component-maven-plugin` as 
part of building the com
 then ensure that this custom properties function has necessary source code 
generated that makes Camel
 able to automatically discover the function.
 
+NOTE: If the custom properties function need logic to startup and shutdown, 
then the function can extend `ServiceSupport`
+and have this logic in `doStart` and `doStop` methods.
+
 TIP: For an example see the `camel-base64` component.
 
 == Using third party property sources

Reply via email to