sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1802586 - in /sling/trunk/contrib/extensions/distribution/core/src: main/java/org/apache/sling/distribution/serialization/impl/vlt/ test/java/org/apache/sling/distribution/packaging/impl/importer/ test/java/org/apache/sling/distribution/se...
Date Fri, 21 Jul 2017 12:38:55 GMT
Author: simonetripodi
Date: Fri Jul 21 12:38:55 2017
New Revision: 1802586

URL: http://svn.apache.org/viewvc?rev=1802586&view=rev
Log:
SLING-7017 - Make it possible to mount packages at different paths

Added:
    sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMapping.java
  (with props)
    sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMappingTest.java
  (with props)
Modified:
    sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializer.java
    sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VaultDistributionPackageBuilderFactory.java
    sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VltUtils.java
    sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/packaging/impl/importer/LocalDistributionPackageImporterTest.java
    sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializerTest.java

Modified: sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializer.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializer.java?rev=1802586&r1=1802585&r2=1802586&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializer.java
(original)
+++ sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializer.java
Fri Jul 21 12:38:55 2017
@@ -18,16 +18,21 @@
  */
 package org.apache.sling.distribution.serialization.impl.vlt;
 
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.StringTokenizer;
 import java.util.TreeMap;
 import java.util.UUID;
 
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
 import org.apache.jackrabbit.vault.fs.api.ImportMode;
 import org.apache.jackrabbit.vault.fs.api.WorkspaceFilter;
+import org.apache.jackrabbit.vault.fs.config.MetaInf;
 import org.apache.jackrabbit.vault.fs.io.AccessControlHandling;
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.ImportOptions;
@@ -55,6 +60,15 @@ public class FileVaultContentSerializer
     private static final String VERSION = "0.0.1";
     private static final String PACKAGE_GROUP = "sling/distribution";
 
+    /**
+     * The custom <code>Path-Mapping</code> property.
+     */
+    private static final String PATH_MAPPING_PROPERTY = "Path-Mapping";
+
+    private static final String MAPPING_SEPARATOR = "=";
+
+    private static final String MAPPING_DELIMITER = ";";
+
     private final Packaging packaging;
     private final ImportMode importMode;
     private final AccessControlHandling aclHandling;
@@ -64,9 +78,11 @@ public class FileVaultContentSerializer
     private final TreeMap<String, List<String>> propertyFilters;
     private final boolean useBinaryReferences;
     private final String name;
+    private final Map<String, String> exportPathMapping;
 
     public FileVaultContentSerializer(String name, Packaging packaging, ImportMode importMode,
AccessControlHandling aclHandling, String[] packageRoots,
-                                      String[] nodeFilters, String[] propertyFilters, boolean
useBinaryReferences, int autosaveThreshold) {
+                                      String[] nodeFilters, String[] propertyFilters, boolean
useBinaryReferences, int autosaveThreshold,
+                                      Map<String, String> exportPathMapping) {
         this.name = name;
         this.packaging = packaging;
         this.importMode = importMode;
@@ -76,6 +92,7 @@ public class FileVaultContentSerializer
         this.nodeFilters = VltUtils.parseFilters(nodeFilters);
         this.propertyFilters = VltUtils.parseFilters(propertyFilters);
         this.useBinaryReferences = useBinaryReferences;
+        this.exportPathMapping = exportPathMapping;
     }
 
     @Override
@@ -87,7 +104,7 @@ public class FileVaultContentSerializer
             String packageName = TYPE + "_" + System.currentTimeMillis() + "_" + UUID.randomUUID();
 
             WorkspaceFilter filter = VltUtils.createFilter(exportOptions.getRequest(), nodeFilters,
propertyFilters);
-            ExportOptions opts = VltUtils.getExportOptions(filter, packageRoots, packageGroup,
packageName, VERSION, useBinaryReferences);
+            ExportOptions opts = VltUtils.getExportOptions(filter, packageRoots, packageGroup,
packageName, VERSION, useBinaryReferences, exportPathMapping);
 
             log.debug("assembling package {} user {}", packageGroup + '/' + packageName +
"-" + VERSION, resourceResolver.getUserID());
 
@@ -110,6 +127,29 @@ public class FileVaultContentSerializer
             Importer importer = new Importer(importOptions);
             archive = new ZipStreamArchive(inputStream);
             archive.open(false);
+
+            // retrieve the mapping
+            MetaInf metaInf = archive.getMetaInf();
+            if (metaInf != null) {
+                Properties metaInfProperties = metaInf.getProperties();
+                if (metaInfProperties != null) {
+                    String pathsMappingProperty = metaInfProperties.getProperty(PATH_MAPPING_PROPERTY);
+
+                    if (pathsMappingProperty != null && !pathsMappingProperty.isEmpty())
{
+                        RegexpPathMapping pathMapping = new RegexpPathMapping();
+
+                        StringTokenizer pathsMappingTokenizer = new StringTokenizer(pathsMappingProperty,
MAPPING_DELIMITER);
+                        while (pathsMappingTokenizer.hasMoreTokens()) {
+                            String[] pathMappingHeader = pathsMappingTokenizer.nextToken().split(MAPPING_SEPARATOR);
+                            pathMapping.addMapping(pathMappingHeader[0], pathMappingHeader[1]);
+                        }
+
+                        importOptions.setPathMapping(pathMapping);
+                    }
+                }
+            }
+
+            // now import the content
             importer.run(archive, session.getRootNode());
             if (importer.hasErrors() && importOptions.isStrict()) {
                 throw new PackageException("Errors during import.");

Added: sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMapping.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMapping.java?rev=1802586&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMapping.java
(added)
+++ sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMapping.java
Fri Jul 21 12:38:55 2017
@@ -0,0 +1,60 @@
+/*
+ * 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.distribution.serialization.impl.vlt;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.jackrabbit.vault.fs.api.PathMapping;
+
+final class RegexpPathMapping implements PathMapping {
+
+    private final Map<Pattern, String> pathsMapping = new HashMap<Pattern, String>();
+
+    public <K, V> void addAllMappings(Map<K, V> pathsMappingMap) {
+        for (Entry<K, V> entry : pathsMappingMap.entrySet()) {
+            addMapping(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
+        }
+    }
+
+    public void addMapping(String fromPattern, String toPattern) {
+        pathsMapping.put(Pattern.compile(fromPattern), toPattern);
+    }
+
+    @Override
+    public String map(String path) {
+        for (Entry<Pattern, String> pathMapping : pathsMapping.entrySet()) {
+            Matcher matcher = pathMapping.getKey().matcher(path);
+            if (matcher.matches()) {
+                return matcher.replaceAll(pathMapping.getValue());
+            }
+        }
+        return path;
+    }
+
+    // `reverse` is not taken in consideration at all in this version
+    @Override
+    public String map(String path, boolean reverse) {
+        return map(path);
+    }
+
+}

Propchange: sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMapping.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMapping.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMapping.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VaultDistributionPackageBuilderFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VaultDistributionPackageBuilderFactory.java?rev=1802586&r1=1802585&r2=1802586&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VaultDistributionPackageBuilderFactory.java
(original)
+++ sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VaultDistributionPackageBuilderFactory.java
Fri Jul 21 12:38:55 2017
@@ -198,6 +198,12 @@ public class VaultDistributionPackageBui
     )
     private static final String MONITORING_QUEUE_SIZE = "monitoringQueueSize";
 
+    @Property(cardinality = 100,
+              label = "Paths mapping",
+              description = "List of paths that require be mapped." +
+              "The format is {sourcePattern}={destinationPattern}, e.g. /etc/(.*)=/var/$1/some
or simply /data=/bak")
+    private static final String PATHS_MAPPING = "pathsMapping";
+
     @Reference
     private Packaging packaging;
 
@@ -242,8 +248,12 @@ public class VaultDistributionPackageBui
             aclHandling = AccessControlHandling.valueOf(aclHandlingString.trim());
         }
 
+        // check the mount path patterns, if any
+        Map<String, String> pathsMapping = PropertiesUtil.toMap(config.get(PATHS_MAPPING),
new String[0]);
+        pathsMapping = SettingsUtils.removeEmptyEntries(pathsMapping);
+
         DistributionContentSerializer contentSerializer = new FileVaultContentSerializer(name,
packaging, importMode, aclHandling,
-                packageRoots, packageNodeFilters, packagePropertyFilters, useBinaryReferences,
autosaveThreshold);
+                packageRoots, packageNodeFilters, packagePropertyFilters, useBinaryReferences,
autosaveThreshold, pathsMapping);
 
         DistributionPackageBuilder wrapped;
         if ("filevlt".equals(type)) {

Modified: sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VltUtils.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VltUtils.java?rev=1802586&r1=1802585&r2=1802586&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VltUtils.java
(original)
+++ sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VltUtils.java
Fri Jul 21 12:38:55 2017
@@ -31,6 +31,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.NavigableMap;
 import java.util.Properties;
 import java.util.Set;
@@ -67,6 +68,15 @@ public class VltUtils {
 
     private final static Logger log = LoggerFactory.getLogger(VltUtils.class);
 
+    /**
+     * The custom <code>Path-Mapping</code> property.
+     */
+    private static final String PATH_MAPPING_PROPERTY = "Path-Mapping";
+
+    private static final String MAPPING_SEPARATOR = "=";
+
+    private static final String MAPPING_DELIMITER = ";";
+
     public static WorkspaceFilter createFilter(DistributionRequest distributionRequest, NavigableMap<String,
List<String>> nodeFilters,
                                                NavigableMap<String, List<String>>
propertyFilters) {
         DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter();
@@ -138,7 +148,8 @@ public class VltUtils {
                                                  String packageGroup,
                                                  String packageName,
                                                  String packageVersion,
-                                                 boolean useBinaryReferences) {
+                                                 boolean useBinaryReferences,
+                                                 Map<String, String> exportPathMapping)
{
         DefaultMetaInf inf = new DefaultMetaInf();
         ExportOptions opts = new ExportOptions();
         inf.setFilter(filter);
@@ -148,6 +159,23 @@ public class VltUtils {
         props.setProperty(VaultPackage.NAME_NAME, packageName);
         props.setProperty(VaultPackage.NAME_VERSION, packageVersion);
         props.setProperty(PackageProperties.NAME_USE_BINARY_REFERENCES, String.valueOf(useBinaryReferences));
+
+        if (exportPathMapping != null && !exportPathMapping.isEmpty()) {
+            StringBuilder builder = new StringBuilder();
+
+            for (Entry<String, String> entry : exportPathMapping.entrySet()) {
+                if (builder.length() > 0) {
+                    builder.append(MAPPING_DELIMITER);
+                }
+
+                builder.append(entry.getKey())
+                       .append(MAPPING_SEPARATOR)
+                       .append(entry.getValue());
+            }
+
+            props.setProperty(PATH_MAPPING_PROPERTY, builder.toString());
+        }
+
         inf.setProperties(props);
 
         opts.setMetaInf(inf);

Modified: sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/packaging/impl/importer/LocalDistributionPackageImporterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/packaging/impl/importer/LocalDistributionPackageImporterTest.java?rev=1802586&r1=1802585&r2=1802586&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/packaging/impl/importer/LocalDistributionPackageImporterTest.java
(original)
+++ sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/packaging/impl/importer/LocalDistributionPackageImporterTest.java
Fri Jul 21 12:38:55 2017
@@ -80,7 +80,8 @@ public class LocalDistributionPackageImp
                 new String[0],
                 new String[0],
                 false,
-                -1
+                -1,
+                null
         );
 
         DistributionPackageBuilder builder =

Modified: sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializerTest.java?rev=1802586&r1=1802585&r2=1802586&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializerTest.java
(original)
+++ sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializerTest.java
Fri Jul 21 12:38:55 2017
@@ -25,6 +25,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.OutputStream;
+import java.util.HashMap;
 
 import org.apache.jackrabbit.vault.fs.api.ImportMode;
 import org.apache.jackrabbit.vault.fs.io.AccessControlHandling;
@@ -81,7 +82,7 @@ public class FileVaultContentSerializerT
         boolean useReferences = false;
         int threshold = 1024;
         FileVaultContentSerializer fileVaultContentSerializer = new FileVaultContentSerializer("vlt",
packaging, importMode,
-                aclHandling, packageRoots, nodeFilters, propertyFilters, useReferences, threshold);
+                aclHandling, packageRoots, nodeFilters, propertyFilters, useReferences, threshold,
new HashMap<String, String>());
 
         ResourceResolver sessionResolver = mock(ResourceResolver.class);
         Session session = mock(Session.class);
@@ -123,7 +124,7 @@ public class FileVaultContentSerializerT
         boolean useReferences = false;
         int thershold = 1024;
         FileVaultContentSerializer fileVaultContentSerializer = new FileVaultContentSerializer("vlt",
packaging, importMode,
-                aclHandling, packageRoots, nodeFilters, propertyFilters, useReferences, thershold);
+                aclHandling, packageRoots, nodeFilters, propertyFilters, useReferences, thershold,
new HashMap<String, String>());
 
         File file = new File(getClass().getResource("/vlt/dp.vlt").getFile());
 

Added: sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMappingTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMappingTest.java?rev=1802586&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMappingTest.java
(added)
+++ sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMappingTest.java
Fri Jul 21 12:38:55 2017
@@ -0,0 +1,57 @@
+/*
+ * 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.distribution.serialization.impl.vlt;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.sling.distribution.serialization.impl.vlt.RegexpPathMapping;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Testcase for {@link RegexpPathMappingTest}
+ */
+public class RegexpPathMappingTest {
+
+    private RegexpPathMapping pathMapping;
+
+    @Before
+    public void setUp() {
+        pathMapping = new RegexpPathMapping();
+    }
+
+    @After
+    public void tearDown() {
+        pathMapping = null;
+    }
+
+    @Test
+    public void testIdentityMapping() {
+        assertEquals("/etc/my/fake/data", pathMapping.map("/etc/my/fake/data"));
+    }
+
+    @Test
+    public void testCorrectMapping() {
+        pathMapping.addMapping("/etc/(.*)", "/dummy/$1/custom");
+
+        assertEquals("/dummy/my/fake/data/custom", pathMapping.map("/etc/my/fake/data"));
+    }
+
+}

Propchange: sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMappingTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMappingTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/RegexpPathMappingTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message