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 2020/04/27 06:15:33 UTC

[sling-slingstart-maven-plugin] branch master updated: SLING-9362 : Use Apache Felix cm.json for JSON handling

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-slingstart-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new e971f7b  SLING-9362 : Use Apache Felix cm.json for JSON handling
e971f7b is described below

commit e971f7b5fccaa0f85275cfbbe1080e3c5f636670
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Mon Apr 27 08:15:22 2020 +0200

    SLING-9362 : Use Apache Felix cm.json for JSON handling
---
 pom.xml                                            | 19 +++---
 .../maven/slingstart/FeatureModelConverter.java    | 73 +++-------------------
 .../sling/maven/slingstart/JSONFeatures.java       | 69 ++++++++++++++++++++
 .../slingstart/FeatureModelConverterTest.java      |  3 +
 .../slingstart/GenerateResourcesMojoTest.java      |  2 +
 5 files changed, 89 insertions(+), 77 deletions(-)

diff --git a/pom.xml b/pom.xml
index 0d7decc..1b24a93 100644
--- a/pom.xml
+++ b/pom.xml
@@ -155,7 +155,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.feature</artifactId>
-            <version>1.1.8</version>
+            <version>1.2.1-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
@@ -168,11 +168,6 @@
             <version>1.0.14</version>
         </dependency>
         <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.feature.io</artifactId>
-            <version>1.3.0</version>
-        </dependency>
-        <dependency>
             <groupId>org.apache.maven</groupId>
             <artifactId>maven-core</artifactId>
             <version>${maven.version}</version>
@@ -219,17 +214,17 @@
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.converter</artifactId>
-            <version>1.0.12</version>
+            <version>1.0.14</version>
         </dependency>
         <dependency>
             <groupId>org.apache.felix</groupId>
-            <artifactId>org.apache.felix.configurator</artifactId>
-            <version>1.0.10</version>
+            <artifactId>org.apache.felix.cm.json</artifactId>
+            <version>1.0.2</version>
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.johnzon</artifactId>
-            <version>1.1.2</version>
+            <version>1.2.2</version>
         </dependency>
         <dependency>
             <groupId>org.codehaus.plexus</groupId>
@@ -274,8 +269,8 @@
         </dependency>
         <dependency>
             <groupId>org.mockito</groupId>
-            <artifactId>mockito-all</artifactId>
-            <version>1.10.19</version>
+            <artifactId>mockito-core</artifactId>
+            <version>3.3.0</version>
             <scope>test</scope>
         </dependency>
         <dependency>
diff --git a/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java b/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
index bb0c564..76a9174 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
@@ -31,13 +31,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import javax.json.Json;
-import javax.json.JsonObject;
-import javax.json.JsonReader;
-import javax.json.JsonValue;
-import javax.json.stream.JsonGenerator;
-
-import org.apache.felix.configurator.impl.json.JSMin;
 import org.apache.maven.MavenExecutionException;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
 import org.apache.maven.artifact.resolver.ArtifactResolver;
@@ -224,66 +217,16 @@ public class FeatureModelConverter {
      */
     public static String readFeatureFile(final MavenProject project, final File file,
             final String suggestedClassifier) {
-        final StringBuilder sb = new StringBuilder();
-        try (final Reader reader = new FileReader(file)) {
-            final char[] buf = new char[4096];
-            int l = 0;
-
-            while ((l = reader.read(buf)) > 0) {
-                sb.append(buf, 0, l);
-            }
-        } catch (final IOException io) {
-            throw new RuntimeException("Unable to read feature " + file.getAbsolutePath(), io);
-        }
-        final String readJson = sb.toString();
-
-        // minify JSON (remove comments)
-        String json;
-        try (final Writer out = new StringWriter(); final Reader in = new StringReader(readJson)) {
-            final JSMin min = new JSMin(in, out);
-            min.jsmin();
-            json = out.toString();
+        final ArtifactId fileId = new ArtifactId(project.getGroupId(),
+                project.getArtifactId(),
+                project.getVersion(),
+                suggestedClassifier,
+                "slingosgifeature");
+        try ( final Reader reader = new FileReader(file) ) {
+            return Substitution.replaceMavenVars(project, JSONFeatures.read(reader, fileId, file.getAbsolutePath()));
         } catch (final IOException e) {
-            throw new RuntimeException("Unable to read feature file " + file.getAbsolutePath(), e);
+            throw new RuntimeException("Unable to read feature file " + file.getAbsolutePath() + " : " + e.getMessage(), e);
         }
-
-        // check if "id" is set
-        try (final JsonReader reader = Json.createReader(new StringReader(json))) {
-            final JsonObject obj = reader.readObject();
-            if (!obj.containsKey("id")) {
-                final StringBuilder isb = new StringBuilder();
-                isb.append(project.getGroupId());
-                isb.append(':');
-                isb.append(project.getArtifactId());
-                isb.append(':');
-                isb.append("slingosgifeature");
-
-                if (suggestedClassifier != null) {
-                    isb.append(':');
-                    isb.append(suggestedClassifier);
-                }
-                isb.append(':');
-                isb.append(project.getVersion());
-
-                final StringWriter writer = new StringWriter();
-
-                try (final JsonGenerator generator = Json.createGenerator(writer)) {
-                    generator.writeStartObject();
-
-                    generator.write("id", isb.toString());
-
-                    for (final Map.Entry<String, JsonValue> entry : obj.entrySet()) {
-                        generator.write(entry.getKey(), entry.getValue());
-                    }
-                    generator.writeEnd();
-                }
-
-                json = writer.toString();
-            }
-        }
-
-        // replace variables
-        return Substitution.replaceMavenVars(project, json);
     }
 
     /**
diff --git a/src/main/java/org/apache/sling/maven/slingstart/JSONFeatures.java b/src/main/java/org/apache/sling/maven/slingstart/JSONFeatures.java
new file mode 100644
index 0000000..c3624b1
--- /dev/null
+++ b/src/main/java/org/apache/sling/maven/slingstart/JSONFeatures.java
@@ -0,0 +1,69 @@
+/*
+ * 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.maven.slingstart;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.util.Map;
+
+import javax.json.Json;
+import javax.json.JsonException;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonReader;
+import javax.json.JsonValue;
+import javax.json.JsonWriter;
+
+import org.apache.felix.cm.json.Configurations;
+import org.apache.sling.feature.ArtifactId;
+
+public class JSONFeatures {
+
+    /**
+     * Read the feature and add the {@code id} attribute if missing
+     * @param reader The reader
+     * @param optionalId The artifact id to use if the {@code id} attribute is missing
+     * @param location The location
+     * @return The feature as a string
+     * @throws IOException If reading fails
+     */
+    public static String read(final Reader reader, final ArtifactId optionalId, final String location)
+    throws IOException {
+        JsonObject featureObj;
+        try (JsonReader jsonReader = Json.createReader(Configurations.jsonCommentAwareReader(reader))) {
+            featureObj = jsonReader.readObject();
+            if ( !featureObj.containsKey("id") ) {
+                final JsonObjectBuilder job = Json.createObjectBuilder();
+                job.add("id", optionalId.toMvnId());
+                for(final Map.Entry<String, JsonValue> prop : featureObj.entrySet() ) {
+                    job.add(prop.getKey(), prop.getValue());
+                }
+                featureObj = job.build();
+            }
+        } catch ( final JsonException je) {
+            throw new IOException(location.concat(" : " ).concat(je.getMessage()), je);
+        }
+
+        try ( final StringWriter writer = new StringWriter()) {
+            try ( final JsonWriter jsonWriter = Json.createWriter(writer)) {
+                jsonWriter.writeObject(featureObj);
+            }
+            return writer.toString();
+        }
+    }
+}
diff --git a/src/test/java/org/apache/sling/maven/slingstart/FeatureModelConverterTest.java b/src/test/java/org/apache/sling/maven/slingstart/FeatureModelConverterTest.java
index 451aad2..0a7e028 100644
--- a/src/test/java/org/apache/sling/maven/slingstart/FeatureModelConverterTest.java
+++ b/src/test/java/org/apache/sling/maven/slingstart/FeatureModelConverterTest.java
@@ -84,6 +84,8 @@ public class FeatureModelConverterTest {
         MavenProject proj = Mockito.mock(MavenProject.class);
         Mockito.when(proj.getBasedir()).thenReturn(projBaseDir);
         Mockito.when(proj.getBuild()).thenReturn(build);
+        Mockito.when(proj.getGroupId()).thenReturn("g");
+        Mockito.when(proj.getArtifactId()).thenReturn("a");
         Mockito.when(proj.getVersion()).thenReturn("1.0");
         final Properties projProps = new Properties();
         Mockito.when(proj.getProperties()).thenReturn(projProps);
@@ -122,6 +124,7 @@ public class FeatureModelConverterTest {
         Mockito.when(proj.getBasedir()).thenReturn(projBaseDir);
         Mockito.when(proj.getBuild()).thenReturn(build);
         Mockito.when(proj.getGroupId()).thenReturn("generated");
+        Mockito.when(proj.getArtifactId()).thenReturn("a");
         Mockito.when(proj.getVersion()).thenReturn("0.0.1-SNAPSHOT");
         final Properties projProps = new Properties();
         Mockito.when(proj.getProperties()).thenReturn(projProps);
diff --git a/src/test/java/org/apache/sling/maven/slingstart/GenerateResourcesMojoTest.java b/src/test/java/org/apache/sling/maven/slingstart/GenerateResourcesMojoTest.java
index c708e45..16713f5 100644
--- a/src/test/java/org/apache/sling/maven/slingstart/GenerateResourcesMojoTest.java
+++ b/src/test/java/org/apache/sling/maven/slingstart/GenerateResourcesMojoTest.java
@@ -71,6 +71,8 @@ public class GenerateResourcesMojoTest {
 
         MavenProject proj = Mockito.mock(MavenProject.class);
         Mockito.when(proj.getBuild()).thenReturn(build);
+        Mockito.when(proj.getGroupId()).thenReturn("g");
+        Mockito.when(proj.getArtifactId()).thenReturn("a");
         Mockito.when(proj.getVersion()).thenReturn("1");
         final Properties projProps = new Properties();
         Mockito.when(proj.getProperties()).thenReturn(projProps);