You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2017/06/06 06:29:28 UTC

svn commit: r1797737 - in /sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json: ApplicationJSONReader.java ConfigurationJSONReader.java ConfigurationJSONWriter.java FeatureJSONReader.java JSONReaderBase.java JSONWriterBase.java

Author: cziegeler
Date: Tue Jun  6 06:29:28 2017
New Revision: 1797737

URL: http://svn.apache.org/viewvc?rev=1797737&view=rev
Log:
Add json reader/writer for configurations

Added:
    sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONReader.java   (with props)
    sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONWriter.java   (with props)
Modified:
    sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ApplicationJSONReader.java
    sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java
    sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/JSONReaderBase.java
    sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/JSONWriterBase.java

Modified: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ApplicationJSONReader.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ApplicationJSONReader.java?rev=1797737&r1=1797736&r2=1797737&view=diff
==============================================================================
--- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ApplicationJSONReader.java (original)
+++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ApplicationJSONReader.java Tue Jun  6 06:29:28 2017
@@ -19,14 +19,11 @@ package org.apache.sling.feature.json;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
-import java.io.StringWriter;
-import java.io.Writer;
 import java.util.Map;
 
 import javax.json.Json;
 import javax.json.JsonObject;
 
-import org.apache.felix.configurator.impl.json.JSMin;
 import org.apache.felix.configurator.impl.json.JSONUtil;
 import org.apache.sling.feature.Application;
 
@@ -45,17 +42,9 @@ public class ApplicationJSONReader exten
      */
     public static Application read(final Reader reader)
     throws IOException {
-        // minify JSON first (remove comments)
-        final String contents;
-        try ( final Writer out = new StringWriter()) {
-            final JSMin min = new JSMin(reader, out);
-            min.jsmin();
-            contents = out.toString();
-        }
-
         try {
             final ApplicationJSONReader mr = new ApplicationJSONReader();
-            mr.readApplication(new StringReader(contents));
+            mr.readApplication(reader);
             return mr.app;
         } catch (final IllegalStateException | IllegalArgumentException e) {
             throw new IOException(e);
@@ -80,7 +69,7 @@ public class ApplicationJSONReader exten
      */
     private void readApplication(final Reader reader)
     throws IOException {
-        final JsonObject json = Json.createReader(reader).readObject();
+        final JsonObject json = Json.createReader(new StringReader(minify(reader))).readObject();
 
         @SuppressWarnings("unchecked")
         final Map<String, Object> map = (Map<String, Object>) JSONUtil.getValue(json);

Added: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONReader.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONReader.java?rev=1797737&view=auto
==============================================================================
--- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONReader.java (added)
+++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONReader.java Tue Jun  6 06:29:28 2017
@@ -0,0 +1,81 @@
+/*
+ * 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.sling.feature.json;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import javax.json.Json;
+import javax.json.JsonObject;
+
+import org.apache.felix.configurator.impl.json.JSONUtil;
+import org.apache.sling.feature.Configuration;
+
+/**
+ * JSON Reader for configurations.
+ */
+public class ConfigurationJSONReader extends JSONReaderBase {
+
+    /**
+     * Read a map of configurations from the reader
+     * The reader is not closed. It is up to the caller to close the reader.
+     *
+     * @param reader The reader for the configuration
+     * @param location Optional location
+     * @return The read configurations
+     * @throws IOException If an IO errors occurs or the JSON is invalid.
+     */
+    public static List<Configuration> read(final Reader reader, final String location)
+    throws IOException {
+        try {
+            final ConfigurationJSONReader mr = new ConfigurationJSONReader(location);
+            return mr.readConfigurations(reader);
+        } catch (final IllegalStateException | IllegalArgumentException e) {
+            throw new IOException(e);
+        }
+    }
+
+    /**
+     * Private constructor
+     * @param location Optional location
+     */
+    ConfigurationJSONReader(final String location) {
+        super(location);
+    }
+
+    List<Configuration> readConfigurations(final Reader reader) throws IOException {
+        final List<Configuration> result = new ArrayList<>();
+
+        final JsonObject json = Json.createReader(new StringReader(minify(reader))).readObject();
+
+        @SuppressWarnings("unchecked")
+        final Map<String, Object> map = (Map<String, Object>) JSONUtil.getValue(json);
+
+        final Map<String, Object> objMap = Collections.singletonMap(JSONConstants.FEATURE_CONFIGURATIONS, (Object)map);
+
+        readConfigurations(objMap, result);
+
+        return result;
+    }
+}
+
+

Propchange: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONReader.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONWriter.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONWriter.java?rev=1797737&view=auto
==============================================================================
--- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONWriter.java (added)
+++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONWriter.java Tue Jun  6 06:29:28 2017
@@ -0,0 +1,57 @@
+/*
+ * 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.sling.feature.json;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import javax.json.Json;
+import javax.json.stream.JsonGenerator;
+
+import org.apache.sling.feature.Configuration;
+
+
+/**
+ * JSON writer for configurations
+ */
+public class ConfigurationJSONWriter extends JSONWriterBase {
+
+    /**
+     * Writes the configurations to the writer.
+     * The writer is not closed.
+     * @param writer Writer
+     * @param configs List of configurations
+     * @throws IOException
+     */
+    public static void write(final Writer writer, final List<Configuration> configs)
+    throws IOException {
+        final ConfigurationJSONWriter w = new ConfigurationJSONWriter();
+        w.writeConfigurations(writer, configs);
+    }
+
+    private void writeConfigurations(final Writer writer, final List<Configuration> configs)
+    throws IOException {
+        final JsonGenerator w = Json.createGenerator(writer);
+        w.writeStartObject();
+
+        writeConfigurationsMap(w, configs);
+
+        w.writeEnd();
+        w.flush();
+    }
+}

Propchange: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/ConfigurationJSONWriter.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Modified: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java?rev=1797737&r1=1797736&r2=1797737&view=diff
==============================================================================
--- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java (original)
+++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java Tue Jun  6 06:29:28 2017
@@ -19,8 +19,6 @@ package org.apache.sling.feature.json;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
-import java.io.StringWriter;
-import java.io.Writer;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -28,7 +26,6 @@ import java.util.Map;
 import javax.json.Json;
 import javax.json.JsonObject;
 
-import org.apache.felix.configurator.impl.json.JSMin;
 import org.apache.felix.configurator.impl.json.JSONUtil;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Capability;
@@ -52,17 +49,9 @@ public class FeatureJSONReader extends J
      */
     public static Feature read(final Reader reader, final String location)
     throws IOException {
-        // minify JSON first (remove comments)
-        final String contents;
-        try ( final Writer out = new StringWriter()) {
-            final JSMin min = new JSMin(reader, out);
-            min.jsmin();
-            contents = out.toString();
-        }
-
         try {
             final FeatureJSONReader mr = new FeatureJSONReader(location);
-            return mr.readFeature(new StringReader(contents));
+            return mr.readFeature(reader);
         } catch (final IllegalStateException | IllegalArgumentException e) {
             throw new IOException(e);
         }
@@ -87,7 +76,7 @@ public class FeatureJSONReader extends J
      */
     private Feature readFeature(final Reader reader)
     throws IOException {
-        final JsonObject json = Json.createReader(reader).readObject();
+        final JsonObject json = Json.createReader(new StringReader(minify(reader))).readObject();
 
         @SuppressWarnings("unchecked")
         final Map<String, Object> map = (Map<String, Object>) JSONUtil.getValue(json);

Modified: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/JSONReaderBase.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/JSONReaderBase.java?rev=1797737&r1=1797736&r2=1797737&view=diff
==============================================================================
--- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/JSONReaderBase.java (original)
+++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/JSONReaderBase.java Tue Jun  6 06:29:28 2017
@@ -17,7 +17,9 @@
 package org.apache.sling.feature.json;
 
 import java.io.IOException;
+import java.io.Reader;
 import java.io.StringWriter;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Enumeration;
@@ -32,6 +34,7 @@ import javax.json.JsonObjectBuilder;
 import javax.json.JsonStructure;
 import javax.json.JsonWriter;
 
+import org.apache.felix.configurator.impl.json.JSMin;
 import org.apache.felix.configurator.impl.json.JSONUtil;
 import org.apache.felix.configurator.impl.json.TypeConverter;
 import org.apache.felix.configurator.impl.model.Config;
@@ -67,6 +70,17 @@ abstract class JSONReaderBase {
         }
     }
 
+    protected String minify(final Reader reader) throws IOException {
+       // minify JSON (remove comments)
+        final String contents;
+        try ( final Writer out = new StringWriter()) {
+            final JSMin min = new JSMin(reader, out);
+            min.jsmin();
+            contents = out.toString();
+        }
+        return contents;
+    }
+
     /**
      * Read the bundles / start levels section
      * @param map The map describing the feature

Modified: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/JSONWriterBase.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/JSONWriterBase.java?rev=1797737&r1=1797736&r2=1797737&view=diff
==============================================================================
--- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/JSONWriterBase.java (original)
+++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/JSONWriterBase.java Tue Jun  6 06:29:28 2017
@@ -87,84 +87,95 @@ abstract class JSONWriterBase {
     protected void writeConfigurations(final JsonGenerator w, final List<Configuration> cfgs) {
         if ( !cfgs.isEmpty() ) {
             w.writeStartObject(JSONConstants.FEATURE_CONFIGURATIONS);
-            for(final Configuration cfg : cfgs) {
-                final String key;
-                if ( cfg.isFactoryConfiguration() ) {
-                    key = cfg.getFactoryPid() + "~" + cfg.getName();
-                } else {
-                    key = cfg.getPid();
-                }
-                w.writeStartObject(key);
 
-                final Enumeration<String> e = cfg.getProperties().keys();
-                while ( e.hasMoreElements() ) {
-                    final String name = e.nextElement();
-                    if ( Configuration.PROP_ARTIFACT.equals(name) ) {
-                        continue;
-                    }
+            writeConfigurationsMap(w, cfgs);
 
-                    final Object val = cfg.getProperties().get(name);
+            w.writeEnd();
+        }
+    }
 
-                    String typePostFix = null;
-                    final Object typeCheck;
-                    if ( val.getClass().isArray() ) {
-                        if ( Array.getLength(val) > 0 ) {
-                            typeCheck = Array.get(val, 0);
-                        } else {
-                            typeCheck = null;
-                        }
+    /**
+     * Write the list of configurations into a "configurations" element
+     * @param w The json generator
+     * @param cfgs The list of configurations
+     */
+    protected void writeConfigurationsMap(final JsonGenerator w, final List<Configuration> cfgs) {
+        for(final Configuration cfg : cfgs) {
+            final String key;
+            if ( cfg.isFactoryConfiguration() ) {
+                key = cfg.getFactoryPid() + "~" + cfg.getName();
+            } else {
+                key = cfg.getPid();
+            }
+            w.writeStartObject(key);
+
+            final Enumeration<String> e = cfg.getProperties().keys();
+            while ( e.hasMoreElements() ) {
+                final String name = e.nextElement();
+                if ( Configuration.PROP_ARTIFACT.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 = val;
+                        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 ( 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() ) {
-                        w.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 ) {
-                                    w.write((String)obj);
-                                } else if ( obj instanceof Boolean ) {
-                                    w.write((Boolean)obj);
-                                } else if ( obj instanceof Long ) {
-                                    w.write((Long)obj);
-                                } else if ( obj instanceof Double ) {
-                                    w.write((Double)obj);
-                                }
-                            } else {
-                                w.write(obj.toString());
-                            }
-                        }
-                        w.writeEnd();
-                    } else {
+                if ( val.getClass().isArray() ) {
+                    w.writeStartArray(name);
+                    for(int i=0; i<Array.getLength(val);i++ ) {
+                        final Object obj = Array.get(val, i);
                         if ( typePostFix == null ) {
-                            if ( val instanceof String ) {
-                                w.write(name, (String)val);
-                            } else if ( val instanceof Boolean ) {
-                                w.write(name, (Boolean)val);
-                            } else if ( val instanceof Long ) {
-                                w.write(name, (Long)val);
-                            } else if ( val instanceof Double ) {
-                                w.write(name, (Double)val);
+                            if ( obj instanceof String ) {
+                                w.write((String)obj);
+                            } else if ( obj instanceof Boolean ) {
+                                w.write((Boolean)obj);
+                            } else if ( obj instanceof Long ) {
+                                w.write((Long)obj);
+                            } else if ( obj instanceof Double ) {
+                                w.write((Double)obj);
                             }
                         } else {
-                            w.write(name + typePostFix, val.toString());
+                            w.write(obj.toString());
                         }
                     }
+                    w.writeEnd();
+                } else {
+                    if ( typePostFix == null ) {
+                        if ( val instanceof String ) {
+                            w.write(name, (String)val);
+                        } else if ( val instanceof Boolean ) {
+                            w.write(name, (Boolean)val);
+                        } else if ( val instanceof Long ) {
+                            w.write(name, (Long)val);
+                        } else if ( val instanceof Double ) {
+                            w.write(name, (Double)val);
+                        }
+                    } else {
+                        w.write(name + typePostFix, val.toString());
+                    }
                 }
-
-                w.writeEnd();
             }
+
             w.writeEnd();
         }
     }