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 2019/10/17 15:51:09 UTC

[sling-org-apache-sling-feature-extension-apiregions] 02/02: SLING-8783 : Create API for api regions

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

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

commit e8b5b46e7ec205919a73b7fba515a226d7137dee
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu Oct 17 17:49:29 2019 +0200

    SLING-8783 : Create API for api regions
---
 .../apiregions/APIRegionMergeHandler.java          | 47 ++++++-----
 .../analyser/AbstractApiRegionsAnalyserTask.java   |  5 +-
 .../CheckApiRegionsBundleExportsImports.java       |  3 +-
 .../apiregions/analyser/CheckApiRegionsOrder.java  |  4 +-
 .../extension/apiregions/api/ApiRegions.java       | 90 ++++++++++++----------
 .../apiregions/APIRegionMergeHandlerTest.java      |  2 +-
 6 files changed, 81 insertions(+), 70 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/APIRegionMergeHandler.java b/src/main/java/org/apache/sling/feature/extension/apiregions/APIRegionMergeHandler.java
index ec28400..4b6141a 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/APIRegionMergeHandler.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/APIRegionMergeHandler.java
@@ -55,36 +55,41 @@ public class APIRegionMergeHandler implements MergeHandler {
 
         storeBundleOrigins(context, source, target);
 
-        final ApiRegions srcRegions = ApiRegions.parse((JsonArray) sourceEx.getJSONStructure());
+        try {
+            final ApiRegions srcRegions = ApiRegions.parse((JsonArray) sourceEx.getJSONStructure());
 
-        storeRegionOrigins(context, source, target, srcRegions);
+            storeRegionOrigins(context, source, target, srcRegions);
 
-        final ApiRegions targetRegions;
-        if (targetEx != null) {
-            targetRegions = ApiRegions.parse((JsonArray) targetEx.getJSONStructure());
-        } else {
-            targetEx = new Extension(sourceEx.getType(), sourceEx.getName(), sourceEx.getState());
-            target.getExtensions().add(targetEx);
+            final ApiRegions targetRegions;
+            if (targetEx != null) {
+                targetRegions = ApiRegions.parse((JsonArray) targetEx.getJSONStructure());
+            } else {
+                targetEx = new Extension(sourceEx.getType(), sourceEx.getName(), sourceEx.getState());
+                target.getExtensions().add(targetEx);
 
-            targetRegions = new ApiRegions();
-        }
+                targetRegions = new ApiRegions();
+            }
 
-        for (final ApiRegion targetRegion : targetRegions.getRegions()) {
-            final ApiRegion sourceRegion = srcRegions.getRegionByName(targetRegion.getName());
-            if (sourceRegion != null) {
-                srcRegions.getRegions().remove(sourceRegion);
-                for (final ApiExport srcExp : sourceRegion.getExports()) {
-                    if (targetRegion.getExportByName(srcExp.getName()) == null) {
-                        targetRegion.getExports().add(srcExp);
+            for (final ApiRegion targetRegion : targetRegions.getRegions()) {
+                final ApiRegion sourceRegion = srcRegions.getRegionByName(targetRegion.getName());
+                if (sourceRegion != null) {
+                    srcRegions.getRegions().remove(sourceRegion);
+                    for (final ApiExport srcExp : sourceRegion.getExports()) {
+                        if (targetRegion.getExportByName(srcExp.getName()) == null) {
+                            targetRegion.getExports().add(srcExp);
+                        }
                     }
                 }
             }
-        }
 
-        // If there are any remaining regions in the src extension, process them now
-        targetRegions.getRegions().addAll(srcRegions.getRegions());
+            // If there are any remaining regions in the src extension, process them now
+            targetRegions.getRegions().addAll(srcRegions.getRegions());
+
+            targetEx.setJSONStructure(targetRegions.toJSONArray());
 
-        targetEx.setJSONStructure(targetRegions.toJSONArray());
+        } catch (final IOException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     private void storeRegionOrigins(HandlerContext context, Feature source, Feature target, ApiRegions regions) {
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/AbstractApiRegionsAnalyserTask.java b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/AbstractApiRegionsAnalyserTask.java
index 4e8e594..916e865 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/AbstractApiRegionsAnalyserTask.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/AbstractApiRegionsAnalyserTask.java
@@ -16,8 +16,9 @@
  */
 package org.apache.sling.feature.extension.apiregions.analyser;
 
+import java.io.IOException;
+
 import javax.json.JsonArray;
-import javax.json.stream.JsonParsingException;
 
 import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.Extensions;
@@ -57,7 +58,7 @@ public abstract class AbstractApiRegionsAnalyserTask implements AnalyserTask {
         ApiRegions apiRegions;
         try {
             apiRegions = ApiRegions.parse((JsonArray) apiRegionsExtension.getJSONStructure());
-        } catch (IllegalStateException | IllegalArgumentException | JsonParsingException e) {
+        } catch (IOException e) {
             ctx.reportError("API Regions '"
                     + apiRegionsExtension.getJSON()
                     + "' does not represent a valid JSON 'api-regions': "
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleExportsImports.java b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleExportsImports.java
index cd2d34e..dc65ee5 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleExportsImports.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleExportsImports.java
@@ -38,7 +38,6 @@ import java.util.TreeMap;
 import java.util.stream.Collectors;
 
 import javax.json.JsonArray;
-import javax.json.stream.JsonParsingException;
 
 import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.Extensions;
@@ -167,7 +166,7 @@ public class CheckApiRegionsBundleExportsImports implements AnalyserTask {
             if (apiRegionsExtension != null && apiRegionsExtension.getJSONStructure() != null) {
                 try {
                     apiRegions = ApiRegions.parse((JsonArray) apiRegionsExtension.getJSONStructure());
-                } catch (IllegalStateException | IllegalArgumentException | JsonParsingException e) {
+                } catch (IOException e) {
                     ctx.reportError("API Regions '" + apiRegionsExtension.getJSON()
                             + "' does not represent a valid JSON 'api-regions': " + e.getMessage());
                     return;
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsOrder.java b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsOrder.java
index d9aa9f3..a922933 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsOrder.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsOrder.java
@@ -16,11 +16,11 @@
  */
 package org.apache.sling.feature.extension.apiregions.analyser;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
 import javax.json.JsonArray;
-import javax.json.stream.JsonParsingException;
 
 import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.Extensions;
@@ -100,7 +100,7 @@ public class CheckApiRegionsOrder implements AnalyserTask {
                     return;
                 }
             }
-        } catch (final IllegalStateException | IllegalArgumentException | JsonParsingException e) {
+        } catch (final IOException e) {
             ctx.reportError("Invalid api regions");
         }
     }
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/api/ApiRegions.java b/src/main/java/org/apache/sling/feature/extension/apiregions/api/ApiRegions.java
index 00d939e..2dcf0d7 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/api/ApiRegions.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/api/ApiRegions.java
@@ -27,6 +27,7 @@ import java.util.Map;
 import javax.json.Json;
 import javax.json.JsonArray;
 import javax.json.JsonArrayBuilder;
+import javax.json.JsonException;
 import javax.json.JsonObject;
 import javax.json.JsonObjectBuilder;
 import javax.json.JsonReader;
@@ -116,8 +117,9 @@ public class ApiRegions {
      * Convert regions into json
      *
      * @return The json array
+     * @throws IOException If generating the JSON fails
      */
-    public JsonArray toJSONArray() {
+    public JsonArray toJSONArray() throws IOException {
         final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
 
         for (final ApiRegion region : this.getRegions()) {
@@ -156,15 +158,14 @@ public class ApiRegions {
      * Convert regions into json
      *
      * @return The json array as a string
+     * @throws IOException If generating the JSON fails
      */
-    public String toJSON() {
+    public String toJSON() throws IOException {
         final JsonArray array = this.toJSONArray();
         try (final StringWriter stringWriter = new StringWriter();
                 final JsonWriter writer = Json.createWriter(stringWriter)) {
             writer.writeArray(array);
             return stringWriter.toString();
-        } catch (final IOException e) {
-            throw new IllegalStateException(e);
         }
     }
 
@@ -173,8 +174,9 @@ public class ApiRegions {
      *
      * @param json The json as a string
      * @return The api regions
+     * @throws IOException If the json could not be parsed
      */
-    public static ApiRegions parse(final String json) {
+    public static ApiRegions parse(final String json) throws IOException {
         try (final JsonReader reader = Json.createReader(new StringReader(json))) {
             return parse(reader.readArray());
         }
@@ -185,54 +187,58 @@ public class ApiRegions {
      *
      * @param json The json
      * @return The api regions
+     * @throws IOException If the json could not be parsed
      */
-    public static ApiRegions parse(final JsonArray json) {
-        final ApiRegions regions = new ApiRegions();
+    public static ApiRegions parse(final JsonArray json) throws IOException {
+        try {
+            final ApiRegions regions = new ApiRegions();
 
-        for (final JsonValue value : json) {
-            if (value.getValueType() != ValueType.OBJECT) {
-                throw new IllegalArgumentException("Illegal api regions json " + json);
-            }
-            final ApiRegion region = new ApiRegion();
-
-            final JsonObject obj = (JsonObject) value;
-            region.setName(obj.getString(NAME_KEY));
-
-            for(final Map.Entry<String, JsonValue> entry : obj.entrySet()) {
-                if ( NAME_KEY.equals(entry.getKey()) ) {
-                    region.setName(obj.getString(NAME_KEY));
-                } else if (entry.getKey().equals(EXPORTS_KEY)) {
-                    for (final JsonValue e : (JsonArray)entry.getValue()) {
-                        if (e.getValueType() == ValueType.STRING) {
-                            final String name = ((JsonString) e).getString();
-                            if (!name.startsWith("#")) {
+            for (final JsonValue value : json) {
+                if (value.getValueType() != ValueType.OBJECT) {
+                    throw new IOException("Illegal api regions json " + json);
+                }
+                final ApiRegion region = new ApiRegion();
+
+                final JsonObject obj = (JsonObject) value;
+                region.setName(obj.getString(NAME_KEY));
+
+                for (final Map.Entry<String, JsonValue> entry : obj.entrySet()) {
+                    if (NAME_KEY.equals(entry.getKey())) {
+                        region.setName(obj.getString(NAME_KEY));
+                    } else if (entry.getKey().equals(EXPORTS_KEY)) {
+                        for (final JsonValue e : (JsonArray) entry.getValue()) {
+                            if (e.getValueType() == ValueType.STRING) {
+                                final String name = ((JsonString) e).getString();
+                                if (!name.startsWith("#")) {
+                                    final ApiExport export = new ApiExport();
+                                    region.getExports().add(export);
+
+                                    export.setName(name);
+                                }
+                            } else if (e.getValueType() == ValueType.OBJECT) {
+                                final JsonObject expObj = (JsonObject) e;
                                 final ApiExport export = new ApiExport();
                                 region.getExports().add(export);
 
-                                export.setName(name);
-                            }
-                        } else if (e.getValueType() == ValueType.OBJECT) {
-                            final JsonObject expObj = (JsonObject) e;
-                            final ApiExport export = new ApiExport();
-                            region.getExports().add(export);
-
-                            export.setName(expObj.getString(NAME_KEY));
-                            export.setToggle(expObj.getString(TOGGLE_KEY, null));
-                            if (expObj.containsKey(PREVIOUS_KEY)) {
-                                export.setPrevious(ArtifactId.parse(expObj.getString(PREVIOUS_KEY)));
+                                export.setName(expObj.getString(NAME_KEY));
+                                export.setToggle(expObj.getString(TOGGLE_KEY, null));
+                                if (expObj.containsKey(PREVIOUS_KEY)) {
+                                    export.setPrevious(ArtifactId.parse(expObj.getString(PREVIOUS_KEY)));
+                                }
                             }
                         }
+                    } else {
+                        region.getProperties().put(entry.getKey(), ((JsonString) entry.getValue()).getString());
                     }
-                } else {
-                    region.getProperties().put(entry.getKey(), ((JsonString) entry.getValue()).getString());
+                }
+                if (!regions.addUniqueRegion(region)) {
+                    throw new IOException("Region " + region.getName() + " is defined twice");
                 }
             }
-            if (!regions.addUniqueRegion(region)) {
-                throw new IllegalArgumentException("Region " + region.getName() + " is defined twice");
-            }
-
+            return regions;
+        } catch (final JsonException | IllegalArgumentException e) {
+            throw new IOException(e);
         }
-        return regions;
     }
 
     @Override
diff --git a/src/test/java/org/apache/sling/feature/extension/apiregions/APIRegionMergeHandlerTest.java b/src/test/java/org/apache/sling/feature/extension/apiregions/APIRegionMergeHandlerTest.java
index aa15510..3eafb39 100644
--- a/src/test/java/org/apache/sling/feature/extension/apiregions/APIRegionMergeHandlerTest.java
+++ b/src/test/java/org/apache/sling/feature/extension/apiregions/APIRegionMergeHandlerTest.java
@@ -76,7 +76,7 @@ public class APIRegionMergeHandlerTest {
     }
 
     @Test
-    public void testAPIRegionMerging() {
+    public void testAPIRegionMerging() throws Exception {
         APIRegionMergeHandler armh = new APIRegionMergeHandler();
 
         Feature tf = new Feature(ArtifactId.fromMvnId("x:t:1"));