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/11/12 16:16:58 UTC

[sling-org-apache-sling-feature-extension-apiregions] branch master updated: Allow configuration of a non-temporary location for generated files

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-extension-apiregions.git


The following commit(s) were added to refs/heads/master by this push:
     new b78ba53  Allow configuration of a non-temporary location for generated files
b78ba53 is described below

commit b78ba53937781c573bf4b369ec1c7696ebe10e99
Author: David Bosschaert <bo...@adobe.com>
AuthorDate: Mon Nov 12 16:15:29 2018 +0000

    Allow configuration of a non-temporary location for generated files
    
    The location can be specified by providing the 'fileStorage'
    configuration item to the handler.
---
 pom.xml                                            |  2 +-
 .../extension/apiregions/AbstractHandler.java      | 13 +++-
 .../apiregions/BundleArtifactFeatureHandler.java   | 14 ++--
 .../extension/apiregions/BundleMappingHandler.java |  2 +-
 .../BundleArtifactFeatureHandlerTest.java          | 22 ++++++-
 .../apiregions/BundleMappingHandlerTest.java       | 74 +++++++++++++++++++++-
 6 files changed, 113 insertions(+), 14 deletions(-)

diff --git a/pom.xml b/pom.xml
index 8a1a5e9..7c834fb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -57,7 +57,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.feature</artifactId>
-            <version>0.2.0</version>
+            <version>0.2.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/AbstractHandler.java b/src/main/java/org/apache/sling/feature/extension/apiregions/AbstractHandler.java
index bc19632..0589a67 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/AbstractHandler.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/AbstractHandler.java
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.feature.extension.apiregions;
 
+import org.apache.sling.feature.builder.HandlerContext;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -37,9 +39,16 @@ class AbstractHandler {
     static final String ORG_FEATURE_KEY = "org-feature";
 
     private static final String FILE_PREFIX = "apiregions.";
+    private static final String FILE_STORAGE_DIR_KEY = "fileStorage";
 
-    protected File getDataFile(String name) throws IOException {
-        Path p = Files.createTempFile(FILE_PREFIX, name);
+    protected File getDataFile(HandlerContext context, String name) throws IOException {
+        String stg = context.getConfiguration().get(FILE_STORAGE_DIR_KEY);
+        Path p;
+        if (stg != null) {
+            p = new File(stg, name).toPath();
+        } else {
+            p = Files.createTempFile(FILE_PREFIX, name);
+        }
         File f = p.toFile();
         f.deleteOnExit();
 
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandler.java b/src/main/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandler.java
index 014315c..b963d9f 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandler.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandler.java
@@ -45,15 +45,15 @@ public class BundleArtifactFeatureHandler extends AbstractHandler implements Pos
             return;
 
         try {
-            writeBundleToFeatureMap(feature);
-            writeFeatureToRegionAndPackageMap(feature, extension);
+            writeBundleToFeatureMap(context, feature);
+            writeFeatureToRegionAndPackageMap(context, feature, extension);
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
     }
 
-    private void writeBundleToFeatureMap(Feature feature) throws IOException {
-        File bundlesFile = getDataFile("bundles.properties");
+    private void writeBundleToFeatureMap(HandlerContext context, Feature feature) throws IOException {
+        File bundlesFile = getDataFile(context, "bundles.properties");
         Properties map = loadProperties(bundlesFile);
 
         for (Artifact b : feature.getBundles()) {
@@ -77,12 +77,12 @@ public class BundleArtifactFeatureHandler extends AbstractHandler implements Pos
         storeProperties(map, bundlesFile);
     }
 
-    private void writeFeatureToRegionAndPackageMap(Feature feature, Extension extension) throws IOException {
+    private void writeFeatureToRegionAndPackageMap(HandlerContext context, Feature feature, Extension extension) throws IOException {
         JsonReader jr = Json.createReader(new StringReader(extension.getJSON()));
         JsonArray ja = jr.readArray();
 
-        File featuresFile = getDataFile("features.properties");
-        File regionsFile = getDataFile("regions.properties");
+        File featuresFile = getDataFile(context, "features.properties");
+        File regionsFile = getDataFile(context, "regions.properties");
         Properties frMap = loadProperties(featuresFile);
         Properties rpMap = loadProperties(regionsFile);
 
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/BundleMappingHandler.java b/src/main/java/org/apache/sling/feature/extension/apiregions/BundleMappingHandler.java
index 00a9c0a..72ba5c1 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/BundleMappingHandler.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/BundleMappingHandler.java
@@ -35,7 +35,7 @@ public class BundleMappingHandler extends AbstractHandler implements PostProcess
             return;
 
         try {
-            File idBSNFile = getDataFile("idbsnver.properties");
+            File idBSNFile = getDataFile(context, "idbsnver.properties");
             Properties map = loadProperties(idBSNFile);
 
             for (Artifact b : feature.getBundles()) {
diff --git a/src/test/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandlerTest.java b/src/test/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandlerTest.java
index e8b5ca9..03f8fd6 100644
--- a/src/test/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandlerTest.java
+++ b/src/test/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandlerTest.java
@@ -21,11 +21,15 @@ import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.builder.ArtifactProvider;
+import org.apache.sling.feature.builder.HandlerContext;
 import org.junit.Test;
 
 import java.io.FileReader;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Properties;
 
 import static org.junit.Assert.assertEquals;
@@ -45,7 +49,7 @@ public class BundleArtifactFeatureHandlerTest {
 
         Extension ex = new Extension(ExtensionType.JSON, "api-regions", false);
         ex.setJSON("[]");
-        bafh.postProcess(null, f, ex);
+        bafh.postProcess(new TestHandlerContextImpl(), f, ex);
 
         String p = System.getProperty("apiregions.bundles.properties");
         Properties actual = new Properties();
@@ -72,7 +76,7 @@ public class BundleArtifactFeatureHandlerTest {
                 + "\"exports\":[\"test\"],"
                 + "\"org-feature\":\"an.other:feature:123\"}]");
 
-        bafh.postProcess(null, f, ex);
+        bafh.postProcess(new TestHandlerContextImpl(), f, ex);
 
         String p = System.getProperty("apiregions.features.properties");
         Properties actual = new Properties();
@@ -108,4 +112,18 @@ public class BundleArtifactFeatureHandlerTest {
         bafh.postProcess(null, null, ex);
         // Should not do anything and definitely not throw an exception
     }
+
+    private static class TestHandlerContextImpl implements HandlerContext {
+        private final Map<String, String> cfg = new HashMap<>();
+
+        @Override
+        public ArtifactProvider getArtifactProvider() {
+            return null;
+        }
+
+        @Override
+        public Map<String, String> getConfiguration() {
+            return cfg;
+        }
+    }
 }
diff --git a/src/test/java/org/apache/sling/feature/extension/apiregions/BundleMappingHandlerTest.java b/src/test/java/org/apache/sling/feature/extension/apiregions/BundleMappingHandlerTest.java
index 899302a..a07b703 100644
--- a/src/test/java/org/apache/sling/feature/extension/apiregions/BundleMappingHandlerTest.java
+++ b/src/test/java/org/apache/sling/feature/extension/apiregions/BundleMappingHandlerTest.java
@@ -22,14 +22,21 @@ import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.builder.ArtifactProvider;
+import org.apache.sling.feature.builder.HandlerContext;
 import org.junit.Test;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.Map;
 import java.util.Properties;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 public class BundleMappingHandlerTest {
     @Test
@@ -59,7 +66,7 @@ public class BundleMappingHandlerTest {
         f.getBundles().add(b2);
         Artifact b3 = new Artifact(ArtifactId.fromMvnId("g:b3:0"));
         f.getBundles().add(b3);
-        bmh.postProcess(() -> ap, f, ex);
+        bmh.postProcess(new TestHandlerContext(ap), f, ex);
 
         String p = System.getProperty("apiregions.idbsnver.properties");
         Properties actual = new Properties();
@@ -72,6 +79,47 @@ public class BundleMappingHandlerTest {
     }
 
     @Test
+    public void testSpecificDirectory() throws Exception {
+        Path tempDir = Files.createTempDirectory(getClass().getSimpleName());
+
+        try {
+            ArtifactProvider ap = new ArtifactProvider() {
+                @Override
+                public File provide(ArtifactId id) {
+                    switch(id.toMvnId()) {
+                    case "g:b1:1":
+                        return getResourceFile("b1/b1.jar");
+                    default: return null;
+                    }
+                }
+            };
+
+            Extension ex = new Extension(ExtensionType.JSON, "api-regions", false);
+            Feature f = new Feature(ArtifactId.fromMvnId("foo:bar:123"));
+            Artifact b1 = new Artifact(ArtifactId.fromMvnId("g:b1:1"));
+            f.getBundles().add(b1);
+
+            BundleMappingHandler bmh = new BundleMappingHandler();
+            bmh.postProcess(new TestHandlerContext(ap,
+                    Collections.singletonMap("fileStorage", tempDir.toString())), f, ex);
+
+            File expectedFile = new File(tempDir.toFile(), "idbsnver.properties");
+            assertTrue(expectedFile.exists());
+            Properties p = new Properties();
+            p.load(new FileInputStream(expectedFile));
+
+            Properties ep = new Properties();
+            ep.put("g:b1:1", "b1~1.0.0");
+            assertEquals(ep, p);
+        } finally {
+            for (File f : tempDir.toFile().listFiles()) {
+                f.delete();
+            }
+            tempDir.toFile().delete();
+        }
+    }
+
+    @Test
     public void testUnrelatedExtension() {
         BundleMappingHandler bmh = new BundleMappingHandler();
         Extension ex = new Extension(ExtensionType.JSON, "foobar", false);
@@ -82,4 +130,28 @@ public class BundleMappingHandlerTest {
     private File getResourceFile(String filename) {
         return new File(getClass().getClassLoader().getResource(filename).getFile());
     }
+
+    private class TestHandlerContext implements HandlerContext {
+        private final ArtifactProvider artifactProvider;
+        private final Map<String, String> config;
+
+        private TestHandlerContext(ArtifactProvider ap, Map<String, String> cfg) {
+            artifactProvider = ap;
+            config = cfg;
+        }
+
+        public TestHandlerContext(ArtifactProvider ap) {
+            this(ap, Collections.emptyMap());
+        }
+
+        @Override
+        public ArtifactProvider getArtifactProvider() {
+            return artifactProvider;
+        }
+
+        @Override
+        public Map<String, String> getConfiguration() {
+            return config;
+        }
+    }
 }