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

simonetripodi pushed a commit to branch multiple-packages-conversion
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git


The following commit(s) were added to refs/heads/multiple-packages-conversion 
by this push:
     new ccf9a85  SLING-8390 - Converter not handling serviceusers and acls 
spread across multiple packages
ccf9a85 is described below

commit ccf9a851acf3ec08a8e344fb2cfc38dc29f0fd6a
Author: stripodi <stripodi@simos-mbp>
AuthorDate: Mon May 6 11:46:42 2019 +0200

    SLING-8390 - Converter not handling serviceusers and acls spread across
    multiple packages
    
    added simple vartiable interplator
---
 .../ContentPackage2FeatureModelConverter.java      | 16 ++++-
 .../cpconverter/SimpleVariablesInterpolator.java   | 52 ++++++++++++++
 ...ntentPackage2FeatureModelConverterLauncher.java |  6 +-
 .../SimpleVariablesInterpolatorTest.java           | 80 ++++++++++++++++++++++
 4 files changed, 152 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
 
b/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
index 6ef0071..609e080 100644
--- 
a/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
+++ 
b/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
@@ -77,6 +77,8 @@ public class ContentPackage2FeatureModelConverter {
 
     private final RegexBasedResourceFilter filter = new 
RegexBasedResourceFilter();
 
+    private final SimpleVariablesInterpolator interpolator = new 
SimpleVariablesInterpolator();
+
     private BundlesDeployer artifactDeployer;
 
     private boolean strictValidation = false;
@@ -97,6 +99,8 @@ public class ContentPackage2FeatureModelConverter {
 
     private String idOverride;
 
+    private Map<String, String> properties;
+
     public ContentPackage2FeatureModelConverter setStrictValidation(boolean 
strictValidation) {
         this.strictValidation = strictValidation;
         return this;
@@ -157,6 +161,11 @@ public class ContentPackage2FeatureModelConverter {
         return this;
     }
 
+    public ContentPackage2FeatureModelConverter setProperties(Map<String, 
String> properties) {
+        this.properties = properties;
+        return this;
+    }
+
     public AclManager getAclManager() {
         return aclManager;
     }
@@ -356,6 +365,10 @@ public class ContentPackage2FeatureModelConverter {
             fileNameBuilder.append('-').append(classifier);
         }
 
+        if (properties != null) {
+            properties.put("filename", fileNameBuilder.toString());
+        }
+
         fileNameBuilder.append(JSON_FILE_EXTENSION);
 
         String fileName = fileNameBuilder.toString();
@@ -365,7 +378,8 @@ public class ContentPackage2FeatureModelConverter {
         logger.info("Writing resulting Feature Model '{}' to file '{}'...", 
feature.getId(), targetFile);
 
         if (idOverride != null && !idOverride.isEmpty()) {
-            ArtifactId idOverrride = 
appendRunmode(ArtifactId.parse(idOverride), runMode);
+            String interpolatedIdOverride = 
interpolator.interpolate(idOverride, properties);
+            ArtifactId idOverrride = 
appendRunmode(ArtifactId.parse(interpolatedIdOverride), runMode);
             feature = feature.copy(idOverrride);
         }
 
diff --git 
a/src/main/java/org/apache/sling/feature/cpconverter/SimpleVariablesInterpolator.java
 
b/src/main/java/org/apache/sling/feature/cpconverter/SimpleVariablesInterpolator.java
new file mode 100644
index 0000000..bc76cf4
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/feature/cpconverter/SimpleVariablesInterpolator.java
@@ -0,0 +1,52 @@
+/*
+ * 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.feature.cpconverter;
+
+import static java.util.Objects.requireNonNull;
+
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+final class SimpleVariablesInterpolator {
+
+    private final Pattern replacementPattern = 
Pattern.compile("\\$\\{\\{(.+?)\\}\\}");
+
+    public String interpolate(String format, Map<String, String> properties) {
+        requireNonNull(format, "Input string format must be not null");
+
+        if (properties == null || properties.isEmpty()) {
+            return format;
+        }
+
+        Matcher matcher = replacementPattern.matcher(format);
+        StringBuffer result = new StringBuffer();
+        while (matcher.find()) {
+            String variable = matcher.group(1);
+            String resolved = properties.get(variable);
+
+            if (resolved == null) {
+                resolved = String.format("\\$\\{\\{%s\\}\\}", variable);
+            }
+
+            matcher.appendReplacement(result, resolved);
+        }
+        matcher.appendTail(result);
+        return result.toString();
+    }
+
+}
diff --git 
a/src/main/java/org/apache/sling/feature/cpconverter/cli/ContentPackage2FeatureModelConverterLauncher.java
 
b/src/main/java/org/apache/sling/feature/cpconverter/cli/ContentPackage2FeatureModelConverterLauncher.java
index faff807..5393a29 100644
--- 
a/src/main/java/org/apache/sling/feature/cpconverter/cli/ContentPackage2FeatureModelConverterLauncher.java
+++ 
b/src/main/java/org/apache/sling/feature/cpconverter/cli/ContentPackage2FeatureModelConverterLauncher.java
@@ -80,6 +80,9 @@ public final class 
ContentPackage2FeatureModelConverterLauncher implements Runna
     @Option(names = { "-i", "--artifact-id" }, description = "The optional 
Artifact Id the Feature File will have, once generated; it will be derived, if 
not specified.", required = false)
     private String artifactId;
 
+    @Option(names = {"-D", "--define"}, description = "Define a system 
property", required = false)
+    private Map<String, String> properties = new HashMap<>();
+
     @Parameters(arity = "1..*", paramLabel = "content-packages", description = 
"The content-package input file(s).")
     private List<File> contentPackages;
 
@@ -117,7 +120,8 @@ public final class 
ContentPackage2FeatureModelConverterLauncher implements Runna
                                                              
.setBundlesStartOrder(bundlesStartOrder)
                                                              
.setArtifactsOutputDirectory(artifactsOutputDirectory)
                                                              
.setFeatureModelsOutputDirectory(featureModelsOutputDirectory)
-                                                             
.setIdOverride(artifactId);
+                                                             
.setIdOverride(artifactId)
+                                                             
.setProperties(properties);
 
             if (filteringPatterns != null && filteringPatterns.length > 0) {
                 for (String filteringPattern : filteringPatterns) {
diff --git 
a/src/test/java/org/apache/sling/feature/cpconverter/SimpleVariablesInterpolatorTest.java
 
b/src/test/java/org/apache/sling/feature/cpconverter/SimpleVariablesInterpolatorTest.java
new file mode 100644
index 0000000..f1bd636
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/feature/cpconverter/SimpleVariablesInterpolatorTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.feature.cpconverter;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SimpleVariablesInterpolatorTest {
+
+    private SimpleVariablesInterpolator interpolator;
+
+    @Before
+    public void setUp() {
+        interpolator = new SimpleVariablesInterpolator();
+    }
+
+    @After
+    public void tearDown() {
+        interpolator = null;
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void doesNotAcceptNullInpoutString() {
+        interpolator.interpolate(null, null);
+    }
+
+    @Test
+    public void nullPropertiesReturnOriginalString() {
+        doesNotIntepolate(null);
+    }
+
+    @Test
+    public void emptyPropertiesReturnOriginalString() {
+        doesNotIntepolate(new HashMap<String, String>());
+    }
+
+    @Test
+    public void variableNotFoundWillNotBeInterpolated() {
+        Map<String, String> properties = new HashMap<>();
+        properties.put("that.variable", "just a sample value");
+        doesNotIntepolate(properties);
+    }
+
+    private void doesNotIntepolate(Map<String, String> properties) {
+        String expected = "${{this}}${{will}}${{not}}${{be}}${{interpolated}}";
+        String actual = interpolator.interpolate(expected, properties);
+        assertEquals(expected, actual);
+    }
+
+    @Test
+    public void correctInterpolation() {
+        Map<String, String> properties = new HashMap<>();
+        properties.put("filename", "asd.test.dev");
+        String format = 
"${project.groupId}:${project.artifactId}:slingosgifeature:${{filename}}:${project.version}";
+        String expected = 
"${project.groupId}:${project.artifactId}:slingosgifeature:asd.test.dev:${project.version}";
+        String actual = interpolator.interpolate(format, properties);
+        assertEquals(expected, actual);
+    }
+
+}

Reply via email to