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/01/09 14:24:46 UTC

[sling-org-apache-sling-feature-modelconverter] branch master updated: SLING-8977 : Support run modes for repo init conversion

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-modelconverter.git


The following commit(s) were added to refs/heads/master by this push:
     new c89423c  SLING-8977 : Support run modes for repo init conversion
c89423c is described below

commit c89423c7d63633cbb0fa6a20c74bac74c0ef7a61
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu Jan 9 15:24:35 2020 +0100

    SLING-8977 : Support run modes for repo init conversion
---
 .../modelconverter/FeatureToProvisioning.java      | 87 +++++++++++++++-------
 .../feature/modelconverter/ModelConverterTest.java | 67 +++++++++--------
 src/test/resources/repoinit-runmode.json           | 58 +++++++++++++++
 src/test/resources/repoinit-runmode.txt            | 23 ++++++
 4 files changed, 176 insertions(+), 59 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/modelconverter/FeatureToProvisioning.java b/src/main/java/org/apache/sling/feature/modelconverter/FeatureToProvisioning.java
index 6c70a0c..1a7ed3b 100644
--- a/src/main/java/org/apache/sling/feature/modelconverter/FeatureToProvisioning.java
+++ b/src/main/java/org/apache/sling/feature/modelconverter/FeatureToProvisioning.java
@@ -41,11 +41,8 @@ import javax.json.JsonString;
 import javax.json.JsonValue;
 
 import org.apache.sling.feature.ArtifactId;
-import org.apache.sling.feature.Bundles;
-import org.apache.sling.feature.Configurations;
 import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.ExtensionType;
-import org.apache.sling.feature.Extensions;
 import org.apache.sling.feature.builder.BuilderContext;
 import org.apache.sling.feature.builder.FeatureBuilder;
 import org.apache.sling.feature.builder.FeatureProvider;
@@ -54,6 +51,7 @@ import org.apache.sling.provisioning.model.Artifact;
 import org.apache.sling.provisioning.model.Configuration;
 import org.apache.sling.provisioning.model.Feature;
 import org.apache.sling.provisioning.model.Model;
+import org.apache.sling.provisioning.model.RunMode;
 import org.apache.sling.provisioning.model.Section;
 import org.apache.sling.provisioning.model.io.ModelWriter;
 import org.slf4j.Logger;
@@ -80,11 +78,11 @@ public class FeatureToProvisioning {
         }
 
         Object featureNameVar = feature.getVariables().remove(PROVISIONING_MODEL_NAME_VARIABLE);
-        String featureName;
+        String provModelName;
         if (featureNameVar instanceof String) {
-            featureName = (String) featureNameVar;
+            provModelName = (String) featureNameVar;
         } else {
-            featureName = feature.getId().getArtifactId();
+            provModelName = feature.getId().getArtifactId();
         }
 
         String runMode = feature.getVariables().remove(PROVISIONING_RUNMODES);
@@ -93,9 +91,7 @@ public class FeatureToProvisioning {
             runModes = runMode.split(",");
         }
 
-        Feature newFeature = new Feature(featureName);
-        convert(newFeature, feature.getVariables(), feature.getBundles(), feature.getConfigurations(),
-                feature.getFrameworkProperties(), feature.getExtensions(), outputFile.getAbsolutePath(), runModes);
+        convert(provModelName, feature, inputFile.getName(), outputFile.getAbsolutePath(), runModes);
     }
 
     static org.apache.sling.feature.Feature getFeature(final File file) throws UncheckedIOException {
@@ -118,22 +114,33 @@ public class FeatureToProvisioning {
         return FeatureBuilder.assemble(feature, bc);
     }
 
-    private static void convert(Feature f, Map<String,String> variables, Bundles bundles, Configurations configurations, Map<String,String> frameworkProps,
-            Extensions extensions, String outputFile, String [] runModes) {
+    /**
+     * Convert a feature to a provisioning model
+     *
+     * @param provModelName The name of the prov model
+     * @param feature       The feature model to convert
+     * @param outputFile    The output file to write the prov model to
+     * @param runModes      The run modes of the feature model
+     */
+    private static void convert(String provModelName, org.apache.sling.feature.Feature feature, String featureFileName,
+            String outputFile,
+            String[] runModes) {
+        Feature provModel = new Feature(provModelName);
+
         final Map<String, Feature> additionalFeatures = new HashMap<>();
 
         if (runModes != null && runModes.length == 0) {
             runModes = null;
         }
-        org.apache.sling.provisioning.model.KeyValueMap<String> vars = f.getVariables();
-        for (Map.Entry<String, String> entry : variables.entrySet()) {
+        org.apache.sling.provisioning.model.KeyValueMap<String> vars = provModel.getVariables();
+        for (Map.Entry<String, String> entry : feature.getVariables().entrySet()) {
             vars.put(entry.getKey(), entry.getValue());
         }
 
         Map<org.apache.sling.feature.Configuration, org.apache.sling.feature.Artifact> configBundleMap = new HashMap<>();
 
         // bundles
-        for(final org.apache.sling.feature.Artifact bundle : bundles) {
+        for (final org.apache.sling.feature.Artifact bundle : feature.getBundles()) {
             final ArtifactId id = bundle.getId();
             final Artifact newBundle = new Artifact(id.getGroupId(), id.getArtifactId(), id.getVersion(), id.getClassifier(), id.getType());
 
@@ -168,7 +175,9 @@ public class FeatureToProvisioning {
             // special handling for :boot or :launchpad
             if ( sl != null && sl.startsWith(":") ) {
                 if ( bundleRunModes != null ) {
-                    LOGGER.error("Unable to convert feature {}. Run modes must not be defined for bundles with start-level {}", f.getName(), sl);
+                    LOGGER.error(
+                            "Unable to convert feature {}. Run modes must not be defined for bundles with start-level {}",
+                            provModel.getName(), sl);
                     System.exit(1);
                 }
 
@@ -187,12 +196,12 @@ public class FeatureToProvisioning {
                     }
                 }
 
-                f.getOrCreateRunMode(bundleRunModes).getOrCreateArtifactGroup(startLevel).add(newBundle);
+                provModel.getOrCreateRunMode(bundleRunModes).getOrCreateArtifactGroup(startLevel).add(newBundle);
             }
         }
 
         // configurations
-        for(final org.apache.sling.feature.Configuration cfg : configurations) {
+        for (final org.apache.sling.feature.Configuration cfg : feature.getConfigurations()) {
             final Configuration c;
 
             List<String> runModeList = new ArrayList<>();
@@ -219,11 +228,11 @@ public class FeatureToProvisioning {
                 if (cfgRunModes.length == 0)
                     cfgRunModes = null;
             }
-            f.getOrCreateRunMode(cfgRunModes).getConfigurations().add(c);
+            provModel.getOrCreateRunMode(cfgRunModes).getConfigurations().add(c);
         }
 
         // framework properties
-        for(final Map.Entry<String, String> prop : frameworkProps.entrySet()) {
+        for (final Map.Entry<String, String> prop : feature.getFrameworkProperties().entrySet()) {
             String key = prop.getKey();
             int idx = key.indexOf(".runmodes:");
 
@@ -231,14 +240,14 @@ public class FeatureToProvisioning {
                 String rm = key.substring(idx + ".runmodes:".length());
                 String[] runmodes = rm.split(",");
                 key = key.substring(0, idx);
-                f.getOrCreateRunMode(runmodes).getSettings().put(key, prop.getValue());
+                provModel.getOrCreateRunMode(runmodes).getSettings().put(key, prop.getValue());
             } else {
-                f.getOrCreateRunMode(null).getSettings().put(key, prop.getValue());
+                provModel.getOrCreateRunMode(null).getSettings().put(key, prop.getValue());
             }
         }
 
         // extensions: content packages and repoinit
-        for(final Extension ext : extensions) {
+        for (final Extension ext : feature.getExtensions()) {
             if (Extension.EXTENSION_NAME_CONTENT_PACKAGES.equals(ext.getName())) {
                 for(final org.apache.sling.feature.Artifact cp : ext.getArtifacts() ) {
                     String[] extRunModes = runModes;
@@ -253,13 +262,13 @@ public class FeatureToProvisioning {
                             newCP.getMetadata().put(prop.getKey(), prop.getValue());
                         }
                     }
-                    f.getOrCreateRunMode(extRunModes).getOrCreateArtifactGroup(20).add(newCP);
+                    provModel.getOrCreateRunMode(extRunModes).getOrCreateArtifactGroup(20).add(newCP);
                 }
 
             } else if (Extension.EXTENSION_NAME_REPOINIT.equals(ext.getName())) {
-                final Section section = new Section("repoinit");
+                final String repoinitContents;
                 if (ext.getType() == ExtensionType.TEXT) {
-                    section.setContents(ext.getText());
+                    repoinitContents = ext.getText();
                 } else if (ext.getType() == ExtensionType.JSON) {
                     JsonReader reader = Json.createReader(new StringReader(ext.getJSON()));
                     JsonArray arr = reader.readArray();
@@ -270,9 +279,31 @@ public class FeatureToProvisioning {
                             sb.append('\n');
                         }
                     }
-                    section.setContents(sb.toString());
+                    repoinitContents = sb.toString();
+                } else {
+                    repoinitContents = null;
+                    LOGGER.error("Unable to convert repoinit extension with artifacts");
+                    System.exit(1);
+                }
+
+                if (runModes == null) {
+                    final Section section = new Section("repoinit");
+                    section.setContents(repoinitContents);
+                    provModel.getAdditionalSections().add(section);
+
+                } else {
+                    // create a factory configuration with repoinit
+                    // create an name from the featureFileName
+                    int lastDot = featureFileName.lastIndexOf('.');
+                    String name = lastDot == -1 ? featureFileName : featureFileName.substring(0, lastDot);
+                    name = name.replace('-', '_');
+
+                    final RunMode runMode = provModel.getOrCreateRunMode(runModes);
+                    final Configuration repoinitCfg = new Configuration(name,
+                            "org.apache.sling.jcr.repoinit.RepositoryInitializer");
+                    repoinitCfg.getProperties().put("scripts", repoinitContents);
+                    runMode.getConfigurations().add(repoinitCfg);
                 }
-                f.getAdditionalSections().add(section);
             } else if ( ext.isRequired() ) {
                 LOGGER.error("Unable to convert required extension {}", ext.getName());
                 System.exit(1);
@@ -282,7 +313,7 @@ public class FeatureToProvisioning {
         final String out = outputFile;
         final File file = new File(out);
         final Model m = new Model();
-        m.getFeatures().add(f);
+        m.getFeatures().add(provModel);
         for(final Feature addFeat : additionalFeatures.values()) {
             m.getFeatures().add(addFeat);
         }
diff --git a/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java b/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java
index c16c2b5..233145a 100644
--- a/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java
+++ b/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java
@@ -16,6 +16,37 @@
  */
 package org.apache.sling.feature.modelconverter;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.UncheckedIOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
 import org.apache.sling.feature.Bundles;
 import org.apache.sling.feature.Configurations;
 import org.apache.sling.feature.Extension;
@@ -51,37 +82,6 @@ import org.mockito.stubbing.Answer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.UncheckedIOException;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.nio.file.StandardOpenOption;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Dictionary;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Random;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
 public class ModelConverterTest {
 
     private static Logger LOGGER = LoggerFactory.getLogger(ModelConverterTest.class);
@@ -201,6 +201,11 @@ public class ModelConverterTest {
     }
 
     @Test
+    public void testRepoinitWithRunmodeToProvModel() throws Exception {
+        testConvertToProvisioningModel("/repoinit-runmode.json", "/repoinit-runmode.txt");
+    }
+
+    @Test
     public void testRepoinitToFeature() throws Exception {
         testConvertToFeature("/repoinit.txt", "/repoinit.json");
     }
diff --git a/src/test/resources/repoinit-runmode.json b/src/test/resources/repoinit-runmode.json
new file mode 100644
index 0000000..13f825b
--- /dev/null
+++ b/src/test/resources/repoinit-runmode.json
@@ -0,0 +1,58 @@
+{
+    "variables": {
+        "provisioning.runmodes": "publish"
+    },
+
+    "id": "generated/repoinit/1.0.0",
+
+    "repoinit:TEXT|true": [
+        "# general",
+        "create path (sling:OrderedFolder) /content",
+        "set ACL for everyone",
+        "    allow   jcr:read	on /content",
+        "end",
+
+        "# sling-mapping",
+        "create service user sling-mapping",
+
+        "set ACL for sling-mapping",
+        "    allow   jcr:read    on /",
+        "end",
+
+        "# sling-readall",
+        "create service user sling-readall",
+
+        "set ACL for sling-readall",
+        "    allow   jcr:read    on /",
+        "end",
+
+        "# sling-xss",
+        "create service user sling-xss",
+
+        "create path (sling:Folder) /libs/sling/xss",
+        "create path (sling:Folder) /apps/sling/xss",
+
+        "set ACL for sling-xss",
+        "    deny    jcr:all     on /",
+        "    allow   jcr:read    on /libs/sling/xss,/apps/sling/xss",
+        "end",
+
+        "# sling-i18n",
+        "create service user sling-i18n",
+
+        "set ACL for sling-i18n",
+        "    allow   jcr:read    on /",
+        "end",
+
+        "# sling-jcr-install",
+        "create service user sling-jcr-install",
+
+        "# used for config OSGi writeback",
+        "create path (sling:Folder) /apps/sling/install",
+
+        "set ACL for sling-jcr-install",
+        "    allow	jcr:read	on	/",
+        "    allow	rep:write	on /apps/sling/install",
+        "end"
+    ]
+}
diff --git a/src/test/resources/repoinit-runmode.txt b/src/test/resources/repoinit-runmode.txt
new file mode 100644
index 0000000..e76252d
--- /dev/null
+++ b/src/test/resources/repoinit-runmode.txt
@@ -0,0 +1,23 @@
+#
+#  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.
+#
+[feature name=repoinit]
+
+[configurations runModes=publish]
+  org.apache.sling.jcr.repoinit.RepositoryInitializer-repoinit_runmode
+    scripts="#\ general\ncreate\ path\ (sling:OrderedFolder)\ /content\nset\ ACL\ for\ everyone\n\ \ \ \ allow\ \ \ jcr:read\ on\ /content\nend\n#\ sling-mapping\ncreate\ service\ user\ sling-mapping\nset\ ACL\ for\ sling-mapping\n\ \ \ \ allow\ \ \ jcr:read\ \ \ \ on\ /\nend\n#\ sling-readall\ncreate\ service\ user\ sling-readall\nset\ ACL\ for\ sling-readall\n\ \ \ \ allow\ \ \ jcr:read\ \ \ \ on\ /\nend\n#\ sling-xss\ncreate\ service\ user\ sling-xss\ncreate\ path\ (sling:Folder)\ /li [...]