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

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


The following commit(s) were added to refs/heads/main by this push:
     new eeb44b80ef KARAF-7708 Add example and test for JSON configuration 
(#1725)
eeb44b80ef is described below

commit eeb44b80efd1577fcd1fa55c3c17911f1f409e95
Author: paulsp <pau...@apache.org>
AuthorDate: Fri Jul 21 01:57:05 2023 -0400

    KARAF-7708 Add example and test for JSON configuration (#1725)
    
    * KARAF-7708 Add example and test for JSON configuration
    
    ---------
    
    Co-authored-by: Paul Spencer <paul@hummingbird.local>
---
 examples/karaf-config-example/README.md            | 30 +++++++++++++++++++-
 .../src/main/feature/feature.xml                   | 24 +++++++++++++++-
 .../karaf/example/config/scr/MyComponent.java      |  7 ++++-
 .../karaf/itests/examples/ConfigExampleTest.java   | 32 ++++++++++++++++++++++
 4 files changed, 90 insertions(+), 3 deletions(-)

diff --git a/examples/karaf-config-example/README.md 
b/examples/karaf-config-example/README.md
index bceb216456..bc8e6a3b99 100644
--- a/examples/karaf-config-example/README.md
+++ b/examples/karaf-config-example/README.md
@@ -40,6 +40,7 @@ The configuration uses 
`etc/org.apache.karaf.example.config.cfg` configuration f
 * **karaf-config-example-listener** listens for any change in any 
configuration.
 * **karaf-config-example-blueprint** uses configuration within a blueprint 
container.
 * **karaf-config-example-scr** uses configuration within a scr component.
+* **karaf-config-example-scr-json** uses JSON configuration within a scr 
component.
 * **karaf-config-example-features** contains a Apache Karaf features 
repository used for deployment.
 
 ## Build
@@ -209,4 +210,31 @@ felix.fileinstall.filename = 
file:/home/jbonofre/Workspace/karaf/assemblies/apac
 component.id = 1
 foo = bar
 test = other
-```
\ No newline at end of file
+```
+
+### SCR-JSON
+
+SCR natively supports json configuration.  This feature uses the same 
`karaf-config-example-scr` bundle to display the configuration, but the feature 
defines the configuration is in JSON format.
+
+It's what `karaf-config-example-scr-json` feature is using:
+
+```
+karaf@root()> karaf-config-example-scr-json
+```
+
+At installation time, we can see the configuration display:
+
+```
+service.pid = org.apache.karaf.example.config
+hello = world
+org.apache.karaf.features.configKey = org.apache.karaf.example.config
+component.name = my-component
+felix.fileinstall.filename = 
file:/home/jbonofre/Workspace/karaf/assemblies/apache-karaf/target/apache-karaf-4.2.3-SNAPSHOT/etc/org.apache.karaf.example.config.cfg
+component.id = 1
+foo = bar
+complex = {"a":1,"b":"two"}
+port = 300
+an_Integer_collection = [2, 3, 4]
+an_int_array = [2, 3, 4]
+```
+
diff --git 
a/examples/karaf-config-example/karaf-config-example-features/src/main/feature/feature.xml
 
b/examples/karaf-config-example/karaf-config-example-features/src/main/feature/feature.xml
index 1897474e89..e9fbf66306 100644
--- 
a/examples/karaf-config-example/karaf-config-example-features/src/main/feature/feature.xml
+++ 
b/examples/karaf-config-example/karaf-config-example-features/src/main/feature/feature.xml
@@ -16,7 +16,7 @@
     See the License for the specific language governing permissions and
     limitations under the License.
 -->
-<features name="karaf-config-example-${project.version}" 
xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.4.0";>
+<features name="karaf-config-example-${project.version}" 
xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.4.0 
http://karaf.apache.org/xmlns/features/v1.4.0";>
 
     <feature name="karaf-config-example-common" version="${project.version}">
         <config name="org.apache.karaf.example.config">
@@ -25,6 +25,22 @@
         </config>
     </feature>
 
+    <feature name="karaf-config-example-common-json" 
version="${project.version}">
+        <config name="org.apache.karaf.example.config" >
+            {
+             "hello": "world",
+             "foo": "bar",
+             "port:Integer" : 300,
+             "an_int_array:int[]" : [2, 3, 4],
+             "an_Integer_collection:Collection&lt;Integer&gt;" : [2, 3, 4],
+             "complex": {
+               "a" : 1,
+               "b" : "two"
+               }
+             }
+        </config>
+    </feature>
+
     <feature name="karaf-config-example-static" version="${project.version}">
         <feature version="${project.version}" 
prerequisite="true">karaf-config-example-common</feature>
         
<bundle>mvn:org.apache.karaf.examples/karaf-config-example-static/${project.version}</bundle>
@@ -54,4 +70,10 @@
         
<bundle>mvn:org.apache.karaf.examples/karaf-config-example-scr/${project.version}</bundle>
     </feature>
 
+    <feature name="karaf-config-example-scr-json" version="${project.version}">
+        <feature prerequisite="true">scr</feature>
+        <feature prerequisite="true" 
version="${project.version}">karaf-config-example-common-json</feature>
+        
<bundle>mvn:org.apache.karaf.examples/karaf-config-example-scr/${project.version}</bundle>
+    </feature>
+
 </features>
diff --git 
a/examples/karaf-config-example/karaf-config-example-scr/src/main/java/org/apache/karaf/example/config/scr/MyComponent.java
 
b/examples/karaf-config-example/karaf-config-example-scr/src/main/java/org/apache/karaf/example/config/scr/MyComponent.java
index 47b50c983c..9421f65c5b 100644
--- 
a/examples/karaf-config-example/karaf-config-example-scr/src/main/java/org/apache/karaf/example/config/scr/MyComponent.java
+++ 
b/examples/karaf-config-example/karaf-config-example-scr/src/main/java/org/apache/karaf/example/config/scr/MyComponent.java
@@ -20,6 +20,7 @@ import org.osgi.service.component.ComponentContext;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 
+import java.util.Arrays;
 import java.util.Dictionary;
 import java.util.Enumeration;
 
@@ -32,7 +33,11 @@ public class MyComponent {
         Enumeration<String> keys = properties.keys();
         while (keys.hasMoreElements()) {
             String key = keys.nextElement();
-            System.out.println(key + " = " + properties.get(key));
+            if (properties.get(key) instanceof int[]) {
+                System.out.println(key + " = " + 
Arrays.toString((int[])properties.get(key)));
+            } else {
+                System.out.println(key + " = " + properties.get(key));
+            }
         }
     }
 
diff --git 
a/itests/test/src/test/java/org/apache/karaf/itests/examples/ConfigExampleTest.java
 
b/itests/test/src/test/java/org/apache/karaf/itests/examples/ConfigExampleTest.java
index 460d9dd72a..69cb4224ce 100644
--- 
a/itests/test/src/test/java/org/apache/karaf/itests/examples/ConfigExampleTest.java
+++ 
b/itests/test/src/test/java/org/apache/karaf/itests/examples/ConfigExampleTest.java
@@ -149,4 +149,36 @@ public class ConfigExampleTest extends BaseTest {
         assertContains("hello = exam", byteArrayOutputStream.toString());
     }
 
+    @Test
+    public void testScrJson() throws Exception {
+        addFeaturesRepository();
+
+        installAndAssertFeature("karaf-config-example-scr-json");
+
+        System.out.flush();
+
+        String output = byteArrayOutputStream.toString();
+
+        System.out.println(output);
+
+        assertContains("hello = world", output);
+        assertContains("complex = {\"a\":1,\"b\":\"two\"}", output);
+        assertContains("port = 300", output);
+        assertContains("an_Integer_collection = [2, 3, 4]", output);
+               assertContains("an_int_array = [2, 3, 4]", output);
+
+        assertContainsNot("hello = exam", byteArrayOutputStream.toString());
+
+        Configuration configuration = 
configurationAdmin.getConfiguration("org.apache.karaf.example.config", null);
+        Dictionary<String, Object> properties = new Hashtable<>();
+        properties.put("hello", "exam");
+        configuration.update(properties);
+
+        Thread.sleep(500);
+
+        System.out.flush();
+
+        assertContains("hello = exam", byteArrayOutputStream.toString());
+    }
+
 }

Reply via email to