You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by al...@apache.org on 2021/06/22 09:11:41 UTC

[camel-quarkus] branch main updated: Tested and documented the specification of custom beans in application.properties #2778

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

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


The following commit(s) were added to refs/heads/main by this push:
     new c5bdc10  Tested and documented the specification of custom beans in application.properties #2778
c5bdc10 is described below

commit c5bdc1058522cc0f54e22cca69afac740b90fc56
Author: aldettinger <al...@gmail.com>
AuthorDate: Mon Jun 21 15:19:00 2021 +0200

    Tested and documented the specification of custom beans in application.properties #2778
---
 .../ROOT/pages/reference/extensions/core.adoc      | 12 ++++++
 .../core/runtime/src/main/doc/configuration.adoc   | 14 +++++-
 .../apache/camel/quarkus/core/CoreResource.java    | 30 +++++++++++++
 .../core/PropertiesCustomBeanResolvedByType.java   | 44 +++++++++++++++++++
 ...ustomBeanWithConstructorParameterInjection.java | 50 ++++++++++++++++++++++
 .../PropertiesCustomBeanWithSetterInjection.java   | 38 ++++++++++++++++
 .../core/src/main/resources/application.properties | 14 ++++++
 .../org/apache/camel/quarkus/core/CoreTest.java    | 16 +++++++
 8 files changed, 217 insertions(+), 1 deletion(-)

diff --git a/docs/modules/ROOT/pages/reference/extensions/core.adoc b/docs/modules/ROOT/pages/reference/extensions/core.adoc
index 5ecead6..65d3b84 100644
--- a/docs/modules/ROOT/pages/reference/extensions/core.adoc
+++ b/docs/modules/ROOT/pages/reference/extensions/core.adoc
@@ -92,6 +92,18 @@ quarkus.native.resources.includes = mysimple.txt
 
 More information about selecting resources for inclusion in the native executable can be found at xref:user-guide/native-mode.adoc#embedding-resource-in-native-executable[Embedding resource in native executable].
 
+==== Configuring a custom bean via properties in native mode
+When specifying a custom bean via properties in native mode with configuration like `#class:*` or `#type:*`, it may be needed to register some classes for reflection manually.
+
+For instance, the custom bean definition below involves the use of reflection for bean instantiation and setter invocation:
+[source,properties]
+---
+camel.beans.customBeanWithSetterInjection = #class:org.example.PropertiesCustomBeanWithSetterInjection
+camel.beans.customBeanWithSetterInjection.counter = 123
+---
+
+As such, the class `PropertiesCustomBeanWithSetterInjection` needs to be link:https://quarkus.io/guides/writing-native-applications-tips#registering-for-reflection[registered for reflection], note that field access could be omitted in this case.
+
 
 [width="100%",cols="80,5,15",options="header"]
 |===
diff --git a/extensions-core/core/runtime/src/main/doc/configuration.adoc b/extensions-core/core/runtime/src/main/doc/configuration.adoc
index 0b040fd..d2fdac9 100644
--- a/extensions-core/core/runtime/src/main/doc/configuration.adoc
+++ b/extensions-core/core/runtime/src/main/doc/configuration.adoc
@@ -41,4 +41,16 @@ as demonstrated below:
 quarkus.native.resources.includes = mysimple.txt
 ----
 
-More information about selecting resources for inclusion in the native executable can be found at xref:user-guide/native-mode.adoc#embedding-resource-in-native-executable[Embedding resource in native executable].
\ No newline at end of file
+More information about selecting resources for inclusion in the native executable can be found at xref:user-guide/native-mode.adoc#embedding-resource-in-native-executable[Embedding resource in native executable].
+
+==== Configuring a custom bean via properties in native mode
+When specifying a custom bean via properties in native mode with configuration like `#class:*` or `#type:*`, it may be needed to register some classes for reflection manually.
+
+For instance, the custom bean definition below involves the use of reflection for bean instantiation and setter invocation:
+[source,properties]
+---
+camel.beans.customBeanWithSetterInjection = #class:org.example.PropertiesCustomBeanWithSetterInjection
+camel.beans.customBeanWithSetterInjection.counter = 123
+---
+
+As such, the class `PropertiesCustomBeanWithSetterInjection` needs to be link:https://quarkus.io/guides/writing-native-applications-tips#registering-for-reflection[registered for reflection], note that field access could be omitted in this case.
diff --git a/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java b/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java
index 1e0785b..e1c131f 100644
--- a/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java
+++ b/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java
@@ -26,6 +26,7 @@ import java.util.stream.Collectors;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
+import javax.inject.Named;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
@@ -224,4 +225,33 @@ public class CoreResource {
     public boolean startupStepRecorder() {
         return context.adapt(ExtendedCamelContext.class).getStartupStepRecorder() instanceof DefaultStartupStepRecorder;
     }
+
+    @Path("/custom-bean-with-constructor-parameter-injection")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String customBeanWithConstructorParameterInjection() {
+        PropertiesCustomBeanWithConstructorParameterInjection customBeanWithConstructorParameterInjection = context
+                .getRegistry().lookupByNameAndType("customBeanWithConstructorParameterInjection",
+                        PropertiesCustomBeanWithConstructorParameterInjection.class);
+        return customBeanWithConstructorParameterInjection.toString();
+    }
+
+    @Path("/custom-bean-with-setter-injection")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String customBeanWithSetterInjection() {
+        PropertiesCustomBeanWithSetterInjection customBeanWithSetterInjection = context.getRegistry()
+                .lookupByNameAndType("customBeanWithSetterInjection", PropertiesCustomBeanWithSetterInjection.class);
+        return customBeanWithSetterInjection.toString();
+    }
+
+    @Named("myPropertiesCustomBeanResolvedByType")
+    PropertiesCustomBeanResolvedByType myPropertiesCustomBeanResolvedByType;
+
+    @Path("/custom-bean-resolved-by-type")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String customBeanResolvedByType() {
+        return myPropertiesCustomBeanResolvedByType.toString();
+    }
 }
diff --git a/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/PropertiesCustomBeanResolvedByType.java b/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/PropertiesCustomBeanResolvedByType.java
new file mode 100644
index 0000000..3c90725
--- /dev/null
+++ b/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/PropertiesCustomBeanResolvedByType.java
@@ -0,0 +1,44 @@
+/*
+ * 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.quarkus.core;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Named;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+@ApplicationScoped
+@Named("myPropertiesCustomBeanResolvedByType")
+@RegisterForReflection(fields = false)
+public class PropertiesCustomBeanResolvedByType {
+
+    private String name;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String toString() {
+        return name;
+    }
+
+}
diff --git a/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/PropertiesCustomBeanWithConstructorParameterInjection.java b/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/PropertiesCustomBeanWithConstructorParameterInjection.java
new file mode 100644
index 0000000..795dcee
--- /dev/null
+++ b/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/PropertiesCustomBeanWithConstructorParameterInjection.java
@@ -0,0 +1,50 @@
+/*
+ * 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.quarkus.core;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+@RegisterForReflection(fields = false, methods = false)
+public class PropertiesCustomBeanWithConstructorParameterInjection {
+
+    private String url;
+    private String username;
+    private String password;
+
+    public PropertiesCustomBeanWithConstructorParameterInjection(String url, String username, String password) {
+        this.url = url;
+        this.username = username;
+        this.password = password;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    @Override
+    public String toString() {
+        return url + "|" + username + "|" + password;
+    }
+}
diff --git a/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/PropertiesCustomBeanWithSetterInjection.java b/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/PropertiesCustomBeanWithSetterInjection.java
new file mode 100644
index 0000000..3a341e3
--- /dev/null
+++ b/integration-test-groups/foundation/core/src/main/java/org/apache/camel/quarkus/core/PropertiesCustomBeanWithSetterInjection.java
@@ -0,0 +1,38 @@
+/*
+ * 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.quarkus.core;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+@RegisterForReflection(fields = false)
+public class PropertiesCustomBeanWithSetterInjection {
+
+    private int counter;
+
+    public int getCounter() {
+        return counter;
+    }
+
+    public void setCounter(int counter) {
+        this.counter = counter;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("%d", counter);
+    }
+}
diff --git a/integration-test-groups/foundation/core/src/main/resources/application.properties b/integration-test-groups/foundation/core/src/main/resources/application.properties
index 819426a..df8de76 100644
--- a/integration-test-groups/foundation/core/src/main/resources/application.properties
+++ b/integration-test-groups/foundation/core/src/main/resources/application.properties
@@ -29,3 +29,17 @@ quarkus.camel.native.reflection.exclude-patterns = org.apache.commons.lang3.tupl
 quarkus.camel.runtime-catalog.languages = false
 quarkus.native.resources.includes = mysimple.txt,include-pattern-folder/*
 quarkus.native.resources.excludes = exclude-pattern-folder/*,include-pattern-folder/excluded.txt
+
+#
+# Camel
+#
+myUrl = "localhost:2121"
+myUsername = "scott"
+myPassword = "tiger"
+camel.beans.customBeanWithConstructorParameterInjection = #class:org.apache.camel.quarkus.core.PropertiesCustomBeanWithConstructorParameterInjection('{{myUrl}}', '{{myUsername}}', '{{myPassword}}')
+
+camel.beans.customBeanWithSetterInjection = #class:org.apache.camel.quarkus.core.PropertiesCustomBeanWithSetterInjection
+camel.beans.customBeanWithSetterInjection.counter = 123
+
+camel.beans.customBeanResolvedByType = #type:org.apache.camel.quarkus.core.PropertiesCustomBeanResolvedByType
+camel.beans.customBeanResolvedByType.name = Donkey
diff --git a/integration-test-groups/foundation/core/src/test/java/org/apache/camel/quarkus/core/CoreTest.java b/integration-test-groups/foundation/core/src/test/java/org/apache/camel/quarkus/core/CoreTest.java
index 3b429d9..21f4f5a 100644
--- a/integration-test-groups/foundation/core/src/test/java/org/apache/camel/quarkus/core/CoreTest.java
+++ b/integration-test-groups/foundation/core/src/test/java/org/apache/camel/quarkus/core/CoreTest.java
@@ -103,4 +103,20 @@ public class CoreTest {
     void testStartupStepRecorder() {
         RestAssured.when().get("/core/startup-step-recorder").then().body(is("true"));
     }
+
+    @Test
+    void testCustomBeanWithConstructorParameterInjection() {
+        RestAssured.when().get("/core/custom-bean-with-constructor-parameter-injection").then()
+                .body(is("localhost:2121|scott|tiger"));
+    }
+
+    @Test
+    void testCustomBeanWithSetterInjection() {
+        RestAssured.when().get("/core/custom-bean-with-setter-injection").then().body(is("123"));
+    }
+
+    @Test
+    void testCustomBeanResolvedByType() {
+        RestAssured.when().get("/core/custom-bean-resolved-by-type").then().body(is("Donkey"));
+    }
 }