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

kwin pushed a commit to branch feature/keep-all-package-metadata
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git

commit d9a83828e9d3a038f049c9c5702d339bf0a03528
Author: Konrad Windszus <[email protected]>
AuthorDate: Fri Mar 12 20:58:25 2021 +0100

    SLING-10205 keep custom properties in converted packages
    
    removed irrelevant metadata below jcr_root
    all metadata is being taken over from source, except for
    properties.xml and filter.xml
---
 .../ContentPackage2FeatureModelConverter.java      | 11 ++-
 ...ntentPackage2FeatureModelConverterLauncher.java |  3 +
 .../cpconverter/vltpkg/VaultPackageAssembler.java  | 63 ++++++--------
 .../sling/feature/cpconverter/vltpkg/config.xml    | 95 ----------------------
 .../cpconverter/vltpkg/definition/.content.xml     | 19 -----
 .../sling/feature/cpconverter/vltpkg/settings.xml  | 22 -----
 .../ContentPackage2FeatureModelConverterTest.java  | 24 +-----
 .../vltpkg/VaultPackageAssemblerTest.java          |  4 +-
 8 files changed, 44 insertions(+), 197 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 090865e..5edf45a 100644
--- 
a/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
+++ 
b/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
@@ -94,6 +94,8 @@ public class ContentPackage2FeatureModelConverter extends 
BaseVaultPackageScanne
 
     private boolean dropContent = false;
 
+    private boolean removeInstallHook = false;
+
     private final File tmpDirectory;
 
     public ContentPackage2FeatureModelConverter() {
@@ -166,6 +168,11 @@ public class ContentPackage2FeatureModelConverter extends 
BaseVaultPackageScanne
         return this;
     }
 
+    public @NotNull ContentPackage2FeatureModelConverter 
setRemoveInstallHooks(boolean removeInstallHook) {
+        this.removeInstallHook = removeInstallHook;
+        return this;
+    }
+
     public File getTempDirectory() {
         return this.tmpDirectory;
     }
@@ -227,7 +234,7 @@ public class ContentPackage2FeatureModelConverter extends 
BaseVaultPackageScanne
         for (VaultPackage vaultPackage : orderedContentPackages) {
             try {
                 emitter.startPackage(vaultPackage);
-                mainPackageAssembler = 
VaultPackageAssembler.create(this.getTempDirectory(), vaultPackage);
+                mainPackageAssembler = 
VaultPackageAssembler.create(this.getTempDirectory(), vaultPackage, 
removeInstallHook);
                 assemblers.add(mainPackageAssembler);
 
                 ArtifactId mvnPackageId = toArtifactId(vaultPackage);
@@ -309,7 +316,7 @@ public class ContentPackage2FeatureModelConverter extends 
BaseVaultPackageScanne
 
         PackageId originalPackageId = vaultPackage.getId();
         ArtifactId mvnPackageId = toArtifactId(vaultPackage);
-        VaultPackageAssembler clonedPackage = 
VaultPackageAssembler.create(this.getTempDirectory(), vaultPackage);
+        VaultPackageAssembler clonedPackage = 
VaultPackageAssembler.create(this.getTempDirectory(), vaultPackage, 
removeInstallHook);
 
         // Please note: THIS IS A HACK to meet the new requirement without 
drastically change the original design
         // temporary swap the main handler to collect stuff
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 61dd3c5..8211022 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
@@ -104,6 +104,9 @@ public final class 
ContentPackage2FeatureModelConverterLauncher implements Runna
     @Option(names = { "--entry-handler-config" }, description = "Config for 
entry handlers that support it (classname:<config-string>", required = false)
     private List<String> entryHandlerConfigs = null;
 
+    @Option(names = { "--remove-install-hooks" }, description = "", required = 
false)
+    private boolean removeInstallHooks = false;
+
     @Override
     public void run() {
         if (quiet) {
diff --git 
a/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
 
b/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
index 4991088..68c4ef3 100644
--- 
a/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
+++ 
b/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
@@ -35,7 +35,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.*;
+import java.util.function.Predicate;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
@@ -48,6 +50,7 @@ import org.apache.jackrabbit.vault.packaging.Dependency;
 import org.apache.jackrabbit.vault.packaging.PackageId;
 import org.apache.jackrabbit.vault.packaging.PackageProperties;
 import org.apache.jackrabbit.vault.packaging.VaultPackage;
+import org.apache.jackrabbit.vault.util.Constants;
 import 
org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
 import org.apache.sling.feature.cpconverter.handlers.EntryHandler;
 import org.codehaus.plexus.archiver.Archiver;
@@ -58,19 +61,22 @@ import org.jetbrains.annotations.Nullable;
 
 public class VaultPackageAssembler implements EntryHandler, FileFilter {
 
-    private static final String NAME_PATH = "path";
-
-    private static final String JCR_ROOT_DIR = "jcr_root";
-
-    private static final String[] INCLUDE_RESOURCES = { 
PACKAGE_DEFINITION_XML, CONFIG_XML, SETTINGS_XML };
-
     private static final Pattern OSGI_BUNDLE_PATTERN = 
Pattern.compile("(jcr_root)?/apps/[^/]+/install(\\.([^/]+))?/.+\\.jar");
-    
-    public static @NotNull VaultPackageAssembler create(@NotNull File tempDir, 
@NotNull VaultPackage vaultPackage) {
-        return create(tempDir, vaultPackage, 
Objects.requireNonNull(vaultPackage.getMetaInf().getFilter()));
+
+    private final static class RemoveInstallHooksPredicate implements 
Predicate<Map.Entry<Object, Object>> {
+        
+        @Override
+        public boolean test(java.util.Map.Entry<Object, Object> entry) {
+            String key = (String)entry.getKey();
+            return !key.startsWith(PackageProperties.PREFIX_INSTALL_HOOK);
+        }
     }
 
-    private static @NotNull VaultPackageAssembler create(@NotNull File 
baseTempDir, @NotNull VaultPackage vaultPackage, @NotNull WorkspaceFilter 
filter) {
+    public static @NotNull VaultPackageAssembler create(@NotNull File tempDir, 
@NotNull VaultPackage vaultPackage, boolean removeInstallHooks) {
+        return create(tempDir, vaultPackage, 
Objects.requireNonNull(vaultPackage.getMetaInf().getFilter()), 
removeInstallHooks);
+    }
+    
+    private static @NotNull VaultPackageAssembler create(@NotNull File 
baseTempDir, @NotNull VaultPackage vaultPackage, @NotNull WorkspaceFilter 
filter, boolean removeInstallHooks) {
         final File tempDir = new File(baseTempDir, 
"synthetic-content-packages_" + System.currentTimeMillis());
         PackageId packageId = vaultPackage.getId();
         String fileName = packageId.toString().replaceAll("/", 
"-").replaceAll(":", "-") + "-" + vaultPackage.getFile().getName();
@@ -88,30 +94,21 @@ public class VaultPackageAssembler implements EntryHandler, 
FileFilter {
             throw new IllegalStateException("Unable to create jcr root dir: " 
+ jcrRootDirectory);
         }
 
-        PackageProperties packageProperties = vaultPackage.getProperties();
 
         Properties properties = new Properties();
+        Map<Object, Object> originalPackageProperties = 
vaultPackage.getMetaInf().getProperties();
+        if (originalPackageProperties == null) {
+            throw new IllegalArgumentException("No package properties found in 
" + vaultPackage.getId());
+        }
+        if (removeInstallHooks) {
+            originalPackageProperties = 
originalPackageProperties.entrySet().stream().filter(new 
RemoveInstallHooksPredicate()).collect(Collectors.toMap(Map.Entry::getKey, 
Map.Entry::getValue));
+        }
+        properties.putAll(originalPackageProperties);
         properties.setProperty(PackageProperties.NAME_VERSION,
-                               
packageProperties.getProperty(PackageProperties.NAME_VERSION)
+                               vaultPackage.getId().getVersion().toString()
                                                              + '-'
                                                              + 
PACKAGE_CLASSIFIER);
 
-        for (String key : new String[] {
-                PackageProperties.NAME_GROUP,
-                PackageProperties.NAME_NAME,
-                PackageProperties.NAME_CREATED_BY,
-                PackageProperties.NAME_CREATED,
-                PackageProperties.NAME_REQUIRES_ROOT,
-                PackageProperties.NAME_PACKAGE_TYPE,
-                PackageProperties.NAME_AC_HANDLING,
-                NAME_PATH
-        }) {
-            String value = packageProperties.getProperty(key);
-            if (value != null && !value.isEmpty()) {
-                properties.setProperty(key, value);
-            }
-        }
-
         Set<Dependency> dependencies = getDependencies(vaultPackage);
 
         VaultPackageAssembler assembler = new VaultPackageAssembler(tempDir, 
storingDirectory, properties, dependencies);
@@ -248,14 +245,6 @@ public class VaultPackageAssembler implements 
EntryHandler, FileFilter {
             IOUtils.copy(input, output);
         }
 
-        // copy the required resources
-
-        for (String resource : INCLUDE_RESOURCES) {
-            try (InputStream input = getClass().getResourceAsStream(resource)) 
{
-                addEntry(ROOT_DIR + '/' + resource, input);
-            }
-        }
-
         // create the target archiver
 
         Archiver archiver = new ZipArchiver();
@@ -272,7 +261,7 @@ public class VaultPackageAssembler implements EntryHandler, 
FileFilter {
     }
 
     private void computeFilters(@NotNull File outputDirectory) {
-        File jcrRootDir = new File(outputDirectory, JCR_ROOT_DIR);
+        File jcrRootDir = new File(outputDirectory, ROOT_DIR);
 
         if (jcrRootDir.exists() && jcrRootDir.isDirectory()) {
             for (File child : jcrRootDir.listFiles(this)) {
diff --git 
a/src/main/resources/org/apache/sling/feature/cpconverter/vltpkg/config.xml 
b/src/main/resources/org/apache/sling/feature/cpconverter/vltpkg/config.xml
deleted file mode 100644
index 54a0a38..0000000
--- a/src/main/resources/org/apache/sling/feature/cpconverter/vltpkg/config.xml
+++ /dev/null
@@ -1,95 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- 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.
--->
-<vaultfs version="1.1">
-  <!--
-    Defines the content aggregation. The order of the defined aggregates
-    is important for finding the correct aggregator.
-  -->
-  <aggregates>
-    <!--
-      Defines an aggregate that handles nt:file and nt:resource nodes.
-    -->
-    <aggregate type="file" title="File Aggregate"/>
-
-    <!--
-      Defines an aggregate that handles file/folder like nodes. It matches
-      all nt:hierarchyNode nodes that have or define a jcr:content
-      child node and excludes child nodes that are nt:hierarchyNodes.
-    -->
-    <aggregate type="filefolder" title="File/Folder Aggregate"/>
-
-    <!--
-      Defines an aggregate that handles nt:nodeType nodes and serializes
-      them into .cnd notation.
-    -->
-    <aggregate type="nodetype" title="Node Type Aggregate" />
-
-    <!--
-      Defines an aggregate that defines full coverage for certain node
-      types that cannot be covered by the default aggregator.
-    -->
-    <aggregate type="full" title="Full Coverage Aggregate">
-      <matches>
-        <include nodeType="rep:AccessControl" respectSupertype="true" />
-        <include nodeType="cq:Widget" respectSupertype="true" />
-        <include nodeType="cq:WidgetCollection" respectSupertype="true" />
-        <include nodeType="cq:EditConfig" respectSupertype="true" />
-        <include nodeType="cq:WorkflowModel" respectSupertype="true" />
-        <include nodeType="vlt:FullCoverage" respectSupertype="true" />
-        <include nodeType="mix:language" respectSupertype="true" />
-        <include nodeType="sling:OsgiConfig" respectSupertype="true" />
-      </matches>
-    </aggregate>
-
-    <!--
-      Defines an aggregate that handles nt:folder like nodes.
-    -->
-    <aggregate type="generic" title="Folder Aggregate">
-      <matches>
-        <include nodeType="nt:folder" respectSupertype="true" />
-      </matches>
-      <contains>
-        <exclude isNode="true" />
-      </contains>
-    </aggregate>
-
-    <!--
-      Defines the default aggregate
-    -->
-    <aggregate type="generic" title="Default Aggregator" isDefault="true">
-      <contains>
-        <exclude nodeType="nt:hierarchyNode" respectSupertype="true" />
-      </contains>
-      <matches>
-        <!-- all -->
-      </matches>
-    </aggregate>
-
-  </aggregates>
-
-  <!--
-    defines the input handlers
-  -->
-  <handlers>
-    <handler type="folder"/>
-    <handler type="file"/>
-    <handler type="nodetype"/>
-    <handler type="generic"/>
-  </handlers>
-
-</vaultfs>
diff --git 
a/src/main/resources/org/apache/sling/feature/cpconverter/vltpkg/definition/.content.xml
 
b/src/main/resources/org/apache/sling/feature/cpconverter/vltpkg/definition/.content.xml
deleted file mode 100644
index abf67f1..0000000
--- 
a/src/main/resources/org/apache/sling/feature/cpconverter/vltpkg/definition/.content.xml
+++ /dev/null
@@ -1,19 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- 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.
--->
-<jcr:root xmlns:vlt="http://www.day.com/jcr/vault/1.0"; 
xmlns:jcr="http://www.jcp.org/jcr/1.0";
-  jcr:primaryType="vlt:PackageDefinition" />
diff --git 
a/src/main/resources/org/apache/sling/feature/cpconverter/vltpkg/settings.xml 
b/src/main/resources/org/apache/sling/feature/cpconverter/vltpkg/settings.xml
deleted file mode 100644
index 679e6b1..0000000
--- 
a/src/main/resources/org/apache/sling/feature/cpconverter/vltpkg/settings.xml
+++ /dev/null
@@ -1,22 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  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.
-  -->
-<vault version="1.0">
-  <ignore name=".git" />
-  <ignore name=".svn" />
-  <ignore name=".DS_Store" />
-</vault>
\ No newline at end of file
diff --git 
a/src/test/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverterTest.java
 
b/src/test/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverterTest.java
index 4fa6911..fafef04 100644
--- 
a/src/test/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverterTest.java
+++ 
b/src/test/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverterTest.java
@@ -190,9 +190,6 @@ public class ContentPackage2FeatureModelConverterTest {
                                 "META-INF/vault/properties.xml",
                                 "META-INF/vault/config.xml",
                                 "META-INF/vault/filter.xml",
-                                "jcr_root/settings.xml",
-                                "jcr_root/config.xml",
-                                "jcr_root/definition/.content.xml",
                                 "jcr_root/apps/.content.xml");
             verifyContentPackage(new File(outputDirectory, 
"asd/sample/Asd.Retail.ui.content/0.0.1/Asd.Retail.ui.content-0.0.1-cp2fm-converted.zip"),
                                 "META-INF/vault/settings.xml",
@@ -200,28 +197,21 @@ public class ContentPackage2FeatureModelConverterTest {
                                 "META-INF/vault/config.xml",
                                 "META-INF/vault/filter.xml",
                                 "META-INF/vault/filter-plugin-generated.xml",
-                                "jcr_root/settings.xml",
                                 "jcr_root/content/asd/.content.xml",
-                                "jcr_root/content/asd/resources.xml",
-                                "jcr_root/config.xml",
-                                "jcr_root/definition/.content.xml");
+                                "jcr_root/content/asd/resources.xml");
             verifyContentPackage(new File(outputDirectory, 
"asd/sample/asd.retail.apps/0.0.1/asd.retail.apps-0.0.1-cp2fm-converted.zip"),
                                 "META-INF/vault/settings.xml",
                                 "META-INF/vault/properties.xml",
                                 "META-INF/vault/config.xml",
                                 "META-INF/vault/filter.xml",
                                 "META-INF/vault/filter-plugin-generated.xml",
-                                "jcr_root/settings.xml",
-                                "jcr_root/config.xml",
-                                "jcr_root/definition/.content.xml");
+                                "jcr_root");
             verifyContentPackage(new File(outputDirectory, 
"asd/sample/asd.retail.all/0.0.1/asd.retail.all-0.0.1-cp2fm-converted.zip"),
                                 "META-INF/vault/settings.xml",
                                 "META-INF/vault/properties.xml",
                                 "META-INF/vault/config.xml",
                                 "META-INF/vault/filter.xml",
-                                "jcr_root/settings.xml",
-                                "jcr_root/config.xml",
-                                "jcr_root/definition/.content.xml");
+                                "jcr_root");
         } finally {
             deleteDirTree(outputDirectory);
         }
@@ -281,19 +271,13 @@ public class ContentPackage2FeatureModelConverterTest {
                                 "META-INF/vault/properties.xml",
                                 "META-INF/vault/config.xml",
                                 "META-INF/vault/filter.xml",
-                                "jcr_root/settings.xml",
-                                "jcr_root/config.xml",
-                                "jcr_root/definition/.content.xml",
                                 "jcr_root/apps/.content.xml");
             verifyContentPackage(new File(outputDirectory, 
"asd/sample/asd.retail.apps/0.0.1/asd.retail.apps-0.0.1-cp2fm-converted.zip"),
                                 "META-INF/vault/settings.xml",
                                 "META-INF/vault/properties.xml",
                                 "META-INF/vault/config.xml",
                                 "META-INF/vault/filter.xml",
-                                "META-INF/vault/filter-plugin-generated.xml",
-                                "jcr_root/settings.xml",
-                                "jcr_root/config.xml",
-                                "jcr_root/definition/.content.xml");
+                                "META-INF/vault/filter-plugin-generated.xml");
             // in contrast to previous test when dropping content packages the 
cases below would be filtered out and files wouldn'T be in cache
             assertFalse(new File(outputDirectory, 
"asd/sample/Asd.Retail.ui.content/0.0.1/Asd.Retail.ui.content-0.0.1-cp2fm-converted.zip").exists());
             assertFalse(new File(outputDirectory, 
"asd/sample/asd.retail.all/0.0.1/asd.retail.all-0.0.1-cp2fm-converted.zip").exists());
diff --git 
a/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java
 
b/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java
index a757177..c1726cf 100644
--- 
a/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java
+++ 
b/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java
@@ -59,7 +59,7 @@ public class VaultPackageAssemblerTest {
         VaultPackage vaultPackage = new PackageManagerImpl().open(file);
 
         this.testDirectory = new File(System.getProperty("java.io.tmpdir"), 
getClass().getName() + '_' + System.currentTimeMillis());
-        this.assembler = VaultPackageAssembler.create(testDirectory, 
vaultPackage);
+        this.assembler = VaultPackageAssembler.create(testDirectory, 
vaultPackage, false);
     }
 
     @After
@@ -98,7 +98,7 @@ public class VaultPackageAssemblerTest {
         File file = FileUtils.toFile(resource);
         VaultPackage vaultPackage = new PackageManagerImpl().open(file);
 
-        VaultPackageAssembler assembler = 
VaultPackageAssembler.create(testDirectory, vaultPackage);
+        VaultPackageAssembler assembler = 
VaultPackageAssembler.create(testDirectory, vaultPackage, false);
         PackageId packageId = vaultPackage.getId();
         String fileName = packageId.toString().replaceAll("/", 
"-").replaceAll(":", "-") + "-" + vaultPackage.getFile().getName();
         File storingDirectory = new File(assembler.getTempDir(), fileName + 
"-deflated");

Reply via email to