You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/07/09 12:41:07 UTC

[sling-org-apache-sling-feature] branch features-service updated: Add 'from-feature' to api-regions extions

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

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


The following commit(s) were added to refs/heads/features-service by this push:
     new 85c4e93  Add 'from-feature' to api-regions extions
85c4e93 is described below

commit 85c4e93647a4b765527d421e9066c0c319c066ba
Author: David Bosschaert <bo...@adobe.com>
AuthorDate: Mon Jul 9 13:40:40 2018 +0100

    Add 'from-feature' to api-regions extions
---
 pom.xml                                            |  4 +-
 .../sling/feature/builder/ApplicationBuilder.java  | 43 ++++++++++++++++++++++
 2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index 336ee78..2d8129a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -82,8 +82,8 @@
         </dependency>
         <dependency>
             <groupId>org.apache.geronimo.specs</groupId>
-            <artifactId>geronimo-json_1.1_spec</artifactId>
-            <version>1.0</version>
+            <artifactId>geronimo-json_1.0_spec</artifactId>
+            <version>1.0-alpha-1</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
diff --git a/src/main/java/org/apache/sling/feature/builder/ApplicationBuilder.java b/src/main/java/org/apache/sling/feature/builder/ApplicationBuilder.java
index 80f7865..ece9070 100644
--- a/src/main/java/org/apache/sling/feature/builder/ApplicationBuilder.java
+++ b/src/main/java/org/apache/sling/feature/builder/ApplicationBuilder.java
@@ -18,8 +18,20 @@ package org.apache.sling.feature.builder;
 
 import org.apache.sling.feature.Application;
 import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.Feature;
 
+import java.io.StringReader;
+import java.util.Map;
+
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonStructure;
+import javax.json.JsonValue;
+
 /**
  * Build an application based on features.
  */
@@ -64,12 +76,43 @@ public class ApplicationBuilder {
                 a.getMetadata().put("from-feature", "" + i);
             }
 
+            for (Extension e : assembled.getExtensions()) {
+                // TODO this is really extension specific. Can we find a more generic solution?
+                if ("api-regions".equals(e.getName())) {
+                    JsonStructure json = Json.createReader(new StringReader(e.getJSON())).read();
+                    if (json instanceof JsonArray) {
+                        JsonArray newJson = addToJSONMaps((JsonArray) json, "from-feature", "" + i);
+                        e.setJSON(newJson.toString());
+                    }
+                }
+            }
+
             merge(app, assembled);
         }
 
         return app;
     }
 
+    private static JsonArray addToJSONMaps(JsonArray ja, String key, String value) {
+        JsonArrayBuilder ab = Json.createArrayBuilder();
+
+        for (JsonValue jv : ja) {
+            if (jv instanceof JsonObject) {
+                JsonObject jo = (JsonObject) jv;
+                JsonObjectBuilder ob = Json.createObjectBuilder();
+                for (Map.Entry<String, JsonValue> entry : jo.entrySet()) {
+                    ob.add(entry.getKey(), entry.getValue());
+                }
+                ob.add(key, value);
+                ab.add(ob.build());
+            } else {
+                ab.add(jv);
+            }
+        }
+
+        return ab.build();
+    }
+
     private static void merge(final Application target, final Feature source) {
         BuilderUtil.mergeVariables(target.getVariables(), source.getVariables());
         BuilderUtil.mergeBundles(target.getBundles(), source.getBundles(), BuilderUtil.ArtifactMerge.HIGHEST);