You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2019/12/19 10:17:44 UTC

[sling-org-apache-sling-feature-io] branch feature/SLING-8419 created (now b292183)

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

kwin pushed a change to branch feature/SLING-8419
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-io.git.


      at b292183  SLING-8419 refactor method to serialize OSGi configs as JSON

This branch includes the following new commits:

     new b292183  SLING-8419 refactor method to serialize OSGi configs as JSON

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[sling-org-apache-sling-feature-io] 01/01: SLING-8419 refactor method to serialize OSGi configs as JSON

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch feature/SLING-8419
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-io.git

commit b292183cd2e9f3987acccc83eec5b4504c2cdc3e
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu Dec 19 11:17:30 2019 +0100

    SLING-8419 refactor method to serialize OSGi configs as JSON
    
    This allows reuse in the OSGi installer
---
 .../feature/io/json/ConfigurationJSONWriter.java   | 87 ++++++++++++++++++++--
 .../sling/feature/io/json/JSONWriterBase.java      | 72 +-----------------
 2 files changed, 83 insertions(+), 76 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/io/json/ConfigurationJSONWriter.java b/src/main/java/org/apache/sling/feature/io/json/ConfigurationJSONWriter.java
index 3732a5e..5b1ab1b 100644
--- a/src/main/java/org/apache/sling/feature/io/json/ConfigurationJSONWriter.java
+++ b/src/main/java/org/apache/sling/feature/io/json/ConfigurationJSONWriter.java
@@ -18,9 +18,13 @@ package org.apache.sling.feature.io.json;
 
 import java.io.IOException;
 import java.io.Writer;
+import java.lang.reflect.Array;
+import java.util.Dictionary;
+import java.util.Enumeration;
 
 import javax.json.stream.JsonGenerator;
 
+import org.apache.sling.feature.Configuration;
 import org.apache.sling.feature.Configurations;
 
 
@@ -45,12 +49,85 @@ public class ConfigurationJSONWriter extends JSONWriterBase {
     private void writeConfigurations(final Writer writer, final Configurations configs)
     throws IOException {
         JsonGenerator generator = newGenerator(writer);
-
-        // TODO is this correct?
-        generator.writeStartObject(JSONConstants.FEATURE_CONFIGURATIONS);
         writeConfigurations(generator, configs);
-        generator.writeEnd();
-
         generator.close();
     }
+
+    /**
+     * Write the OSGi configuration in to a JSON structure as defined in 
+     * @param generator The json generator
+     * @param props The configuration properties to write
+     */
+    public static void writeConfiguration(final JsonGenerator generator, final Dictionary<String, Object> props) {
+
+        final Enumeration<String> e = props.keys();
+        while ( e.hasMoreElements() ) {
+            final String name = e.nextElement();
+            if ( Configuration.PROP_ARTIFACT_ID.equals(name) ) {
+                continue;
+            }
+            final Object val = props.get(name);
+            writeConfigurationProperty(generator, name, val);
+        }
+    }
+
+    public static void writeConfigurationProperty(JsonGenerator generator, String name, Object val) {
+        String typePostFix = null;
+        final Object typeCheck;
+        if ( val.getClass().isArray() ) {
+            if ( Array.getLength(val) > 0 ) {
+                typeCheck = Array.get(val, 0);
+            } else {
+                typeCheck = null;
+            }
+        } else {
+            typeCheck = val;
+        }
+
+        if ( typeCheck instanceof Integer ) {
+            typePostFix = ":Integer";
+        } else if ( typeCheck instanceof Byte ) {
+            typePostFix = ":Byte";
+        } else if ( typeCheck instanceof Character ) {
+            typePostFix = ":Character";
+        } else if ( typeCheck instanceof Float ) {
+            typePostFix = ":Float";
+        }
+
+        if ( val.getClass().isArray() ) {
+            generator.writeStartArray(name);
+            for(int i=0; i<Array.getLength(val);i++ ) {
+                final Object obj = Array.get(val, i);
+                if ( typePostFix == null ) {
+                    if ( obj instanceof String ) {
+                        generator.write((String)obj);
+                    } else if ( obj instanceof Boolean ) {
+                        generator.write((Boolean)obj);
+                    } else if ( obj instanceof Long ) {
+                        generator.write((Long)obj);
+                    } else if ( obj instanceof Double ) {
+                        generator.write((Double)obj);
+                    }
+                } else {
+                    generator.write(obj.toString());
+                }
+            }
+
+            generator.writeEnd();
+        } else {
+            if ( typePostFix == null ) {
+                if ( val instanceof String ) {
+                    generator.write(name, (String)val);
+                } else if ( val instanceof Boolean ) {
+                    generator.write(name, (Boolean)val);
+                } else if ( val instanceof Long ) {
+                    generator.write(name, (Long)val);
+                } else if ( val instanceof Double ) {
+                    generator.write(name, (Double)val);
+                }
+            } else {
+                generator.write(name + typePostFix, val.toString());
+            }
+        }
+    }
 }
diff --git a/src/main/java/org/apache/sling/feature/io/json/JSONWriterBase.java b/src/main/java/org/apache/sling/feature/io/json/JSONWriterBase.java
index 66b6ed2..cd8deb8 100644
--- a/src/main/java/org/apache/sling/feature/io/json/JSONWriterBase.java
+++ b/src/main/java/org/apache/sling/feature/io/json/JSONWriterBase.java
@@ -17,10 +17,8 @@
 package org.apache.sling.feature.io.json;
 
 import java.io.Writer;
-import java.lang.reflect.Array;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
 
@@ -104,75 +102,7 @@ abstract class JSONWriterBase {
 
         for(final Configuration cfg : cfgs) {
             generator.writeStartObject(cfg.getPid());
-
-            final Enumeration<String> e = cfg.getProperties().keys();
-            while ( e.hasMoreElements() ) {
-                final String name = e.nextElement();
-                if ( Configuration.PROP_ARTIFACT_ID.equals(name) ) {
-                    continue;
-                }
-
-                final Object val = cfg.getProperties().get(name);
-
-                String typePostFix = null;
-                final Object typeCheck;
-                if ( val.getClass().isArray() ) {
-                    if ( Array.getLength(val) > 0 ) {
-                        typeCheck = Array.get(val, 0);
-                    } else {
-                        typeCheck = null;
-                    }
-                } else {
-                    typeCheck = val;
-                }
-
-                if ( typeCheck instanceof Integer ) {
-                    typePostFix = ":Integer";
-                } else if ( typeCheck instanceof Byte ) {
-                    typePostFix = ":Byte";
-                } else if ( typeCheck instanceof Character ) {
-                    typePostFix = ":Character";
-                } else if ( typeCheck instanceof Float ) {
-                    typePostFix = ":Float";
-                }
-
-                if ( val.getClass().isArray() ) {
-                    generator.writeStartArray(name);
-                    for(int i=0; i<Array.getLength(val);i++ ) {
-                        final Object obj = Array.get(val, i);
-                        if ( typePostFix == null ) {
-                            if ( obj instanceof String ) {
-                                generator.write((String)obj);
-                            } else if ( obj instanceof Boolean ) {
-                                generator.write((Boolean)obj);
-                            } else if ( obj instanceof Long ) {
-                                generator.write((Long)obj);
-                            } else if ( obj instanceof Double ) {
-                                generator.write((Double)obj);
-                            }
-                        } else {
-                            generator.write(obj.toString());
-                        }
-                    }
-
-                    generator.writeEnd();
-                } else {
-                    if ( typePostFix == null ) {
-                        if ( val instanceof String ) {
-                            generator.write(name, (String)val);
-                        } else if ( val instanceof Boolean ) {
-                            generator.write(name, (Boolean)val);
-                        } else if ( val instanceof Long ) {
-                            generator.write(name, (Long)val);
-                        } else if ( val instanceof Double ) {
-                            generator.write(name, (Double)val);
-                        }
-                    } else {
-                        generator.write(name + typePostFix, val.toString());
-                    }
-                }
-            }
-
+            ConfigurationJSONWriter.writeConfiguration(generator, cfg.getConfigurationProperties());
             generator.writeEnd();
         }