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/04/27 10:01:30 UTC

[sling-org-apache-sling-feature-modelconverter] 12/40: Support writing variables when converting from provisioning model

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

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

commit 91f48ed50625802d6ddf219f9a77e5f522b14fb0
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Tue Mar 20 12:02:58 2018 +0000

    Support writing variables when converting from provisioning model
---
 .../modelconverter/impl/FeatureToProvisioning.java |  2 +-
 .../modelconverter/impl/ProvisioningToFeature.java | 31 +++++++++++++---
 .../modelconverter/impl/ModelConverterTest.java    | 41 +++++++++++++++++-----
 src/test/resources/boot.json                       | 36 +++++++++----------
 src/test/resources/boot.txt                        |  2 +-
 5 files changed, 80 insertions(+), 32 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/modelconverter/impl/FeatureToProvisioning.java b/src/main/java/org/apache/sling/feature/modelconverter/impl/FeatureToProvisioning.java
index ed0bd38..28cdff9 100644
--- a/src/main/java/org/apache/sling/feature/modelconverter/impl/FeatureToProvisioning.java
+++ b/src/main/java/org/apache/sling/feature/modelconverter/impl/FeatureToProvisioning.java
@@ -50,7 +50,7 @@ import java.util.Map;
  */
 public class FeatureToProvisioning {
     private static final Logger LOGGER = LoggerFactory.getLogger(FeatureToProvisioning.class);
-    private static final String PROVISIONING_MODEL_NAME_VARIABLE = "provisioning.model.name";
+    static final String PROVISIONING_MODEL_NAME_VARIABLE = "provisioning.model.name";
 
     public static void convert(File file, String output, ArtifactManager am) throws IOException {
         org.apache.sling.feature.Feature feature = FeatureUtil.getFeature(file.getAbsolutePath(), am, SubstituteVariables.NONE);
diff --git a/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java b/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java
index 860de6b..0df66db 100644
--- a/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java
+++ b/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java
@@ -52,11 +52,13 @@ import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 
 /** Converter that converts the provisioning model to the feature model.
@@ -64,6 +66,15 @@ import java.util.Set;
 public class ProvisioningToFeature {
     private static Logger LOGGER = LoggerFactory.getLogger(ProvisioningToFeature.class);
 
+    public static void convert(File file, String output) {
+        Model model = createModel(Collections.singletonList(file), null, false);
+        final List<org.apache.sling.feature.Feature> features = buildFeatures(model);
+        if (features.size() != 1)
+            throw new IllegalStateException("TODO");
+
+        writeFeature(features.get(0), output, 0);
+    }
+
     public static void convert(List<File> files,  String outputFile, String runModes, boolean createApp,
             boolean includeModelInfo, String propsFile) {
         final Model model = createModel(files, runModes, includeModelInfo);
@@ -199,7 +210,7 @@ public class ProvisioningToFeature {
     /**
      * Read the provisioning model
      */
-    static Model readProvisioningModel(final File file)
+    public static Model readProvisioningModel(final File file)
     throws IOException {
         try (final FileReader is = new FileReader(file)) {
             final Model m = ModelReader.read(is, file.getAbsolutePath());
@@ -331,7 +342,7 @@ public class ProvisioningToFeature {
         final Application app = new Application();
 
         for(final Feature feature : model.getFeatures() ) {
-            buildFromFeature(feature, app.getBundles(), app.getConfigurations(), app.getExtensions(), app.getFrameworkProperties());
+            buildFromFeature(feature, app.getVariables(), app.getBundles(), app.getConfigurations(), app.getExtensions(), app.getFrameworkProperties());
         }
 
         // hard coded dependency to launchpad api
@@ -349,10 +360,19 @@ public class ProvisioningToFeature {
     }
 
     private static void buildFromFeature(final Feature feature,
+            final KeyValueMap variables,
             final Bundles bundles,
             final Configurations configurations,
             final Extensions extensions,
             final KeyValueMap properties) {
+        for (Iterator<Map.Entry<String, String>> it = feature.getVariables().iterator(); it.hasNext(); ) {
+            Entry<String, String> entry = it.next();
+            variables.put(entry.getKey(), entry.getValue());
+        }
+        if (feature.getName().startsWith(":")) {
+            variables.put(FeatureToProvisioning.PROVISIONING_MODEL_NAME_VARIABLE, feature.getName());
+        }
+
         Extension cpExtension = extensions.getByName(Extension.NAME_CONTENT_PACKAGES);
         for(final RunMode runMode : feature.getRunModes() ) {
             if ( !ModelConstants.FEATURE_LAUNCHPAD.equals(feature.getName()) ) {
@@ -378,7 +398,10 @@ public class ProvisioningToFeature {
                             } else if ( startLevel == 0 ) {
                                 startLevel = 20;
                             }
-                            newArtifact.getMetadata().put(org.apache.sling.feature.Artifact.KEY_START_ORDER, String.valueOf(startLevel));
+
+                            // TODO this is probably not correct
+                            // newArtifact.getMetadata().put(org.apache.sling.feature.Artifact.KEY_START_ORDER, String.valueOf(startLevel));
+
                             bundles.add(newArtifact);
                         }
                     }
@@ -436,7 +459,7 @@ public class ProvisioningToFeature {
             final org.apache.sling.feature.Feature f = new org.apache.sling.feature.Feature(ArtifactId.parse(idString));
             features.add(f);
 
-            buildFromFeature(feature, f.getBundles(), f.getConfigurations(), f.getExtensions(), f.getFrameworkProperties());
+            buildFromFeature(feature, f.getVariables(), f.getBundles(), f.getConfigurations(), f.getExtensions(), f.getFrameworkProperties());
         }
 
         return features;
diff --git a/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java b/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java
index 46dc48b..1572e9a 100644
--- a/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java
+++ b/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java
@@ -76,12 +76,24 @@ public class ModelConverterTest {
         testConvertToProvisioningModel("/boot.json", "/boot.txt");
     }
 
+    /* @Test
+    public void testBootToFeature() throws Exception {
+        testConvertToFeature("/boot.txt", "/boot.json");
+    } */
+
     @Test
     public void testOak() throws Exception {
         testConvertToProvisioningModel("/oak.json", "/oak.txt");
     }
 
-    private void testConvertToProvisioningModel(String originalJSON, String expectedProvModel) throws URISyntaxException, IOException {
+    public void testConvertToFeature(String originalProvModel, String expectedJSON) throws Exception {
+        File inFile = new File(getClass().getResource(originalProvModel).toURI());
+        File outFile = new File(tempDir.toFile(), expectedJSON + ".generated");
+
+        ProvisioningToFeature.convert(inFile, outFile.getAbsolutePath());
+    }
+
+    public void testConvertToProvisioningModel(String originalJSON, String expectedProvModel) throws URISyntaxException, IOException {
         File inFile = new File(getClass().getResource(originalJSON).toURI());
         File outFile = new File(tempDir.toFile(), expectedProvModel + ".generated");
 
@@ -106,17 +118,17 @@ public class ModelConverterTest {
         assertEquals(expected.getName(), actual.getName());
         assertEquals(expected.getVersion(), actual.getVersion());
         assertEquals(expected.getType(), actual.getType());
-        assertRunModesEqual(expected.getRunModes(), actual.getRunModes());
+        assertRunModesEqual(expected.getName(), expected.getRunModes(), actual.getRunModes());
         assertKVMapEquals(expected.getVariables(), actual.getVariables());
         assertSectionsEqual(expected.getAdditionalSections(), actual.getAdditionalSections());
     }
 
-    private void assertRunModesEqual(List<RunMode> expected, List<RunMode> actual) {
+    private void assertRunModesEqual(String featureName, List<RunMode> expected, List<RunMode> actual) {
         assertEquals(expected.size(), actual.size());
         for (RunMode rm : expected) {
             boolean found = false;
             for (RunMode arm : actual) {
-                if (runModesEqual(rm, arm)) {
+                if (runModesEqual(featureName, rm, arm)) {
                     found = true;
                     break;
                 }
@@ -128,7 +140,7 @@ public class ModelConverterTest {
         }
     }
 
-    private boolean runModesEqual(RunMode rm1, RunMode rm2) {
+    private boolean runModesEqual(String featureName, RunMode rm1, RunMode rm2) {
         if (rm1.getNames() == null) {
             if (rm2.getNames() != null)
                 return false;
@@ -151,7 +163,7 @@ public class ModelConverterTest {
         for (ArtifactGroup g1 : ag1) {
             boolean found = false;
             for (ArtifactGroup g2 : ag2) {
-                if (artifactGroupsEquals(g1, g2)) {
+                if (artifactGroupsEquals(featureName, g1, g2)) {
                     found = true;
                     break;
                 }
@@ -228,8 +240,10 @@ public class ModelConverterTest {
         return m;
     }
 
-    private boolean artifactGroupsEquals(ArtifactGroup g1, ArtifactGroup g2) {
-        if (g1.getStartLevel() != g2.getStartLevel())
+    private boolean artifactGroupsEquals(String featureName, ArtifactGroup g1, ArtifactGroup g2) {
+        int sl1 = effectiveStartLevel(featureName, g1.getStartLevel());
+        int sl2 = effectiveStartLevel(featureName, g2.getStartLevel());
+        if (sl1 != sl2)
             return false;
 
         List<Artifact> al1 = new ArrayList<>();
@@ -247,6 +261,17 @@ public class ModelConverterTest {
         return true;
     }
 
+    private int effectiveStartLevel(String featureName, int startLevel) {
+        if (startLevel != 0)
+            return startLevel;
+
+        if (ModelConstants.FEATURE_BOOT.equals(featureName)) {
+            return 1;
+        } else {
+            return 20;
+        }
+    }
+
     private void assertKVMapEquals(KeyValueMap<String> expected, KeyValueMap<String> actual) {
         assertEquals(kvToMap(expected), kvToMap(actual));
     }
diff --git a/src/test/resources/boot.json b/src/test/resources/boot.json
index 8d0634a..c267b67 100644
--- a/src/test/resources/boot.json
+++ b/src/test/resources/boot.json
@@ -12,75 +12,75 @@
     "bundles": [
         {
             "id": "org.slf4j/slf4j-api/${slf4j.version}",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.commons.log/5.1.0",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.commons.logservice/1.0.6",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.slf4j/jcl-over-slf4j/${slf4j.version}",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.slf4j/log4j-over-slf4j/${slf4j.version}",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.settings/1.3.8",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.fragment.xml/1.0.2",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.fragment.transaction/1.0.0",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.javax.activation/0.1.0",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.fragment.ws/1.0.2",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.launchpad.installer/1.2.2",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.installer.core/3.8.12",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.installer.provider.file/1.1.0",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.sling/org.apache.sling.installer.factory.configuration/1.1.2",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.felix/org.apache.felix.configadmin/1.8.16",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.felix/org.apache.felix.eventadmin/1.4.10",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.aries/org.apache.aries.util/1.1.3",
-            "start-level": 20
+            "start-level": 1
         },
         {
             "id": "org.apache.geronimo.specs/geronimo-atinject_1.0_spec/1.0",
-            "start-level": 20
+            "start-level": 1
         }],
         
     "framework-properties": {
diff --git a/src/test/resources/boot.txt b/src/test/resources/boot.txt
index 7e22e39..df5a947 100644
--- a/src/test/resources/boot.txt
+++ b/src/test/resources/boot.txt
@@ -32,7 +32,7 @@
 [variables]
     slf4j.version=1.7.25
 
-[artifacts startLevel=20]
+[artifacts]
     org.slf4j/slf4j-api/${slf4j.version}
     org.apache.sling/org.apache.sling.commons.log/5.1.0
     org.apache.sling/org.apache.sling.commons.logservice/1.0.6

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