sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [sling-org-apache-sling-feature] branch master updated: Fix an UnsupportedOperation exception during extension json merge and make sure the result ends-up in the target.
Date Tue, 08 May 2018 14:00:34 GMT
This is an automated email from the ASF dual-hosted git repository.

pauls pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature.git


The following commit(s) were added to refs/heads/master by this push:
     new a50b1c0  Fix an UnsupportedOperation exception during extension json merge and make
sure the result ends-up in the target.
a50b1c0 is described below

commit a50b1c0418f6531ddf3063f3d1df88d71f3862ee
Author: Karl Pauls <karlpauls@gmail.com>
AuthorDate: Tue May 8 16:00:22 2018 +0200

    Fix an UnsupportedOperation exception during extension json merge and make sure the result
ends-up in the target.
---
 .../apache/sling/feature/builder/BuilderUtil.java  | 141 ++++++++++++---------
 .../sling/feature/builder/BuilderUtilTest.java     |  17 +++
 2 files changed, 95 insertions(+), 63 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/builder/BuilderUtil.java b/src/main/java/org/apache/sling/feature/builder/BuilderUtil.java
index ad512dd..0c1805e 100644
--- a/src/main/java/org/apache/sling/feature/builder/BuilderUtil.java
+++ b/src/main/java/org/apache/sling/feature/builder/BuilderUtil.java
@@ -17,17 +17,22 @@
 package org.apache.sling.feature.builder;
 
 import java.io.StringReader;
+import java.io.StringWriter;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Stream;
 
 import javax.json.Json;
 import javax.json.JsonArray;
+import javax.json.JsonArrayBuilder;
 import javax.json.JsonObject;
 import javax.json.JsonStructure;
 import javax.json.JsonValue;
 import javax.json.JsonValue.ValueType;
+import javax.json.JsonWriter;
 
+import jdk.nashorn.internal.ir.debug.JSONWriter;
 import org.apache.sling.feature.Application;
 import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.Bundles;
@@ -51,8 +56,8 @@ class BuilderUtil {
 
     // bundles
     static void mergeBundles(final Bundles target,
-            final Bundles source,
-            final ArtifactMerge artifactMergeAlg) {
+        final Bundles source,
+        final ArtifactMerge artifactMergeAlg) {
         for(final Map.Entry<Integer, List<Artifact>> entry : source.getBundlesByStartOrder().entrySet())
{
             for(final Artifact a : entry.getValue()) {
                 // version handling - use provided algorithm
@@ -118,71 +123,79 @@ class BuilderUtil {
 
     // default merge for extensions
     static void mergeExtensions(final Extension target,
-            final Extension source,
-            final ArtifactMerge artifactMergeAlg) {
+        final Extension source,
+        final ArtifactMerge artifactMergeAlg) {
         switch ( target.getType() ) {
             case TEXT : // simply append
-                        target.setText(target.getText() + "\n" + source.getText());
-                        break;
-            case JSON : final JsonStructure struct1;
-                        try ( final StringReader reader = new StringReader(target.getJSON())
) {
-                            struct1 = Json.createReader(reader).read();
-                        }
-                        final JsonStructure struct2;
-                        try ( final StringReader reader = new StringReader(source.getJSON())
) {
-                            struct2 = Json.createReader(reader).read();
-                        }
+                target.setText(target.getText() + "\n" + source.getText());
+                break;
+            case JSON : JsonStructure struct1;
+                try ( final StringReader reader = new StringReader(target.getJSON()) ) {
+                    struct1 = Json.createReader(reader).read();
+                }
+                JsonStructure struct2;
+                try ( final StringReader reader = new StringReader(source.getJSON()) ) {
+                    struct2 = Json.createReader(reader).read();
+                }
 
-                        if ( struct1.getValueType() != struct2.getValueType() ) {
-                            throw new IllegalStateException("Found different JSON types for
extension " + target.getName()
-                                + " : " + struct1.getValueType() + " and " + struct2.getValueType());
-                        }
-                        if ( struct1.getValueType() == ValueType.ARRAY ) {
-                            // array is append
-                            final JsonArray a1 = (JsonArray)struct1;
-                            final JsonArray a2 = (JsonArray)struct2;
-                            for(final JsonValue val : a2) {
-                                a1.add(val);
-                            }
-                        } else {
-                            // object is merge
-                            merge((JsonObject)struct1, (JsonObject)struct2);
-                        }
-                        break;
+                if ( struct1.getValueType() != struct2.getValueType() ) {
+                    throw new IllegalStateException("Found different JSON types for extension
" + target.getName()
+                        + " : " + struct1.getValueType() + " and " + struct2.getValueType());
+                }
+                if ( struct1.getValueType() == ValueType.ARRAY ) {
+                    final JsonArrayBuilder builder = Json.createArrayBuilder();
+
+                    Stream.concat(
+                        ((JsonArray) struct1).stream(),
+                        ((JsonArray) struct2).stream()
+                    ).forEachOrdered(builder::add);
+
+                    struct1 = builder.build();
+                } else {
+                    // object is merge
+                    merge((JsonObject)struct1, (JsonObject)struct2);
+                }
+                StringWriter buffer = new StringWriter();
+                try (JsonWriter writer = Json.createWriter(buffer))
+                {
+                    writer.write(struct1);
+                }
+                target.setJSON(buffer.toString());
+                break;
 
             case ARTIFACTS : for(final Artifact a : source.getArtifacts()) {
-                                 // use artifactMergeAlg
-                                 boolean add = true;
-                                 for(final Artifact targetArtifact : target.getArtifacts())
{
-                                     if ( targetArtifact.getId().isSame(a.getId()) ) {
-                                         if ( artifactMergeAlg == ArtifactMerge.HIGHEST )
{
-                                             if ( targetArtifact.getId().getOSGiVersion().compareTo(a.getId().getOSGiVersion())
> 0 ) {
-                                                 add = false;
-                                             } else {
-                                                 target.getArtifacts().remove(targetArtifact);
-                                             }
-                                         } else { // latest
+                // use artifactMergeAlg
+                boolean add = true;
+                for(final Artifact targetArtifact : target.getArtifacts()) {
+                    if ( targetArtifact.getId().isSame(a.getId()) ) {
+                        if ( artifactMergeAlg == ArtifactMerge.HIGHEST ) {
+                            if ( targetArtifact.getId().getOSGiVersion().compareTo(a.getId().getOSGiVersion())
> 0 ) {
+                                add = false;
+                            } else {
+                                target.getArtifacts().remove(targetArtifact);
+                            }
+                        } else { // latest
 
-                                             target.getArtifacts().remove(targetArtifact);
-                                         }
-                                         break;
-                                     }
-                                 }
+                            target.getArtifacts().remove(targetArtifact);
+                        }
+                        break;
+                    }
+                }
 
-                                 if ( add ) {
-                                     target.getArtifacts().add(a);
-                                 }
+                if ( add ) {
+                    target.getArtifacts().add(a);
+                }
 
-                             }
-                             break;
+            }
+                break;
         }
     }
 
     // extensions (add/merge)
     static void mergeExtensions(final Feature target,
-            final Feature source,
-            final ArtifactMerge artifactMergeAlg,
-            final BuilderContext context) {
+        final Feature source,
+        final ArtifactMerge artifactMergeAlg,
+        final BuilderContext context) {
         for(final Extension ext : source.getExtensions()) {
             boolean found = false;
             for(final Extension current : target.getExtensions()) {
@@ -190,7 +203,7 @@ class BuilderUtil {
                     found = true;
                     if ( current.getType() != ext.getType() ) {
                         throw new IllegalStateException("Found different types for extension
" + current.getName()
-                        + " : " + current.getType() + " and " + ext.getType());
+                            + " : " + current.getType() + " and " + ext.getType());
                     }
                     boolean handled = false;
                     for(final FeatureExtensionHandler fem : context.getFeatureExtensionHandlers())
{
@@ -219,8 +232,8 @@ class BuilderUtil {
     }
 
     static void mergeExtensions(final Application target,
-            final Feature source,
-            final ArtifactMerge artifactMergeAlg) {
+        final Feature source,
+        final ArtifactMerge artifactMergeAlg) {
         for(final Extension ext : source.getExtensions()) {
             boolean found = false;
             for(final Extension current : target.getExtensions()) {
@@ -228,7 +241,7 @@ class BuilderUtil {
                     found = true;
                     if ( current.getType() != ext.getType() ) {
                         throw new IllegalStateException("Found different types for extension
" + current.getName()
-                        + " : " + current.getType() + " and " + ext.getType());
+                            + " : " + current.getType() + " and " + ext.getType());
                     }
                     // default merge
                     mergeExtensions(current, ext, artifactMergeAlg);
@@ -250,12 +263,14 @@ class BuilderUtil {
                     // new type wins
                     obj1.put(entry.getKey(), entry.getValue());
                 } else if ( oldValue.getValueType() == ValueType.ARRAY ) {
-                    final JsonArray a1 = (JsonArray)oldValue;
-                    final JsonArray a2 = (JsonArray)entry.getValue();
-                    for(final JsonValue val : a2) {
-                        a1.add(val);
-                    }
+                    final JsonArrayBuilder builder = Json.createArrayBuilder();
+
+                    Stream.concat(
+                        ((JsonArray) oldValue).stream(),
+                        ((JsonArray)entry.getValue()).stream()
+                    ).forEachOrdered(builder::add);
 
+                    obj1.put(entry.getKey(), builder.build());
                 } else if ( oldValue.getValueType() == ValueType.OBJECT ) {
                     merge((JsonObject)oldValue, (JsonObject)entry.getValue());
                 } else {
diff --git a/src/test/java/org/apache/sling/feature/builder/BuilderUtilTest.java b/src/test/java/org/apache/sling/feature/builder/BuilderUtilTest.java
index b5d3570..465da4b 100644
--- a/src/test/java/org/apache/sling/feature/builder/BuilderUtilTest.java
+++ b/src/test/java/org/apache/sling/feature/builder/BuilderUtilTest.java
@@ -19,6 +19,8 @@ package org.apache.sling.feature.builder;
 import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Bundles;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.builder.BuilderUtil;
 import org.apache.sling.feature.builder.BuilderUtil.ArtifactMerge;
 import org.junit.Test;
@@ -151,6 +153,21 @@ public class BuilderUtilTest {
         assertContains(result, 3, ArtifactId.parse("g/f/2.5"));
     }
 
+    @Test public void testMergeExtensions() {
+        Extension target = new Extension(ExtensionType.JSON, "target", true);
+
+        target.setJSON("[\"target1\", \"target2\"]");
+
+        Extension source = new Extension(ExtensionType.JSON, "source", true);
+
+        source.setJSON("[\"source1\",\"source2\"]");
+
+        BuilderUtil.mergeExtensions(target, source, ArtifactMerge.HIGHEST);
+
+        assertEquals(target.getJSON(), "[\"target1\",\"target2\",\"source1\",\"source2\"]");
+
+    }
+
     public static Artifact createBundle(final String id, final int startOrder) {
         final Artifact a = new Artifact(ArtifactId.parse(id));
         a.getMetadata().put(Artifact.KEY_START_ORDER, String.valueOf(startOrder));

-- 
To stop receiving notification emails like this one, please contact
pauls@apache.org.

Mime
View raw message