This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git
The following commit(s) were added to refs/heads/master by this push:
new 76a9f4a SLING-10234 recalculate package type for converted packages
(#66)
76a9f4a is described below
commit 76a9f4afb4856c425d3b0135da0000266d707a3a
Author: Konrad Windszus <[email protected]>
AuthorDate: Mon Mar 22 21:47:02 2021 +0100
SLING-10234 recalculate package type for converted packages (#66)
Co-authored-by: Karl Pauls <[email protected]>
---
.../cpconverter/vltpkg/VaultPackageAssembler.java | 72 +++++++++++++++------
.../ContentPackage2FeatureModelConverterTest.java | 20 ++----
.../vltpkg/VaultPackageAssemblerTest.java | 2 +-
.../VaultPackageAssemblerUnparameterizedTest.java | 53 +++++++++++++++
.../cpconverter/immutable/jcr_root/apps/test.txt | 16 +++++
.../cpconverter/mixed/jcr_root/apps/test.txt | 16 +++++
.../cpconverter/mixed/jcr_root/content/test.txt | 16 +++++
.../cpconverter/mutable/jcr_root/content/test.txt | 16 +++++
.../sling/feature/cpconverter/test_dep_b-1.0.zip | Bin 5783 -> 8335 bytes
9 files changed, 176 insertions(+), 35 deletions(-)
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 a8ea4f8..58a1aa7 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
@@ -18,10 +18,7 @@ package org.apache.sling.feature.cpconverter.vltpkg;
import static org.apache.jackrabbit.vault.util.Constants.FILTER_XML;
import static org.apache.jackrabbit.vault.util.Constants.META_DIR;
-import static
org.apache.jackrabbit.vault.util.Constants.PACKAGE_DEFINITION_XML;
-import static org.apache.jackrabbit.vault.util.Constants.CONFIG_XML;
import static org.apache.jackrabbit.vault.util.Constants.PROPERTIES_XML;
-import static org.apache.jackrabbit.vault.util.Constants.SETTINGS_XML;
import static org.apache.jackrabbit.vault.util.Constants.ROOT_DIR;
import static
org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter.PACKAGE_CLASSIFIER;
import static
org.apache.sling.feature.cpconverter.vltpkg.VaultPackageUtils.getDependencies;
@@ -39,12 +36,15 @@ import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
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;
+import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.jackrabbit.vault.fs.api.PathFilterSet;
import org.apache.jackrabbit.vault.fs.api.WorkspaceFilter;
import org.apache.jackrabbit.vault.fs.config.DefaultWorkspaceFilter;
@@ -53,6 +53,7 @@ import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
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.PackageType;
import org.apache.jackrabbit.vault.packaging.VaultPackage;
import org.apache.jackrabbit.vault.util.Constants;
import
org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
@@ -65,7 +66,7 @@ import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class VaultPackageAssembler implements EntryHandler, FileFilter {
+public class VaultPackageAssembler implements EntryHandler {
private static final Pattern OSGI_BUNDLE_PATTERN =
Pattern.compile("(jcr_root)?/apps/[^/]+/install(\\.([^/]+))?/.+\\.jar");
@@ -233,10 +234,6 @@ public class VaultPackageAssembler implements
EntryHandler, FileFilter {
}
public @NotNull File createPackage() throws IOException {
- return createPackage(this.tmpDir);
- }
-
- public @NotNull File createPackage(@NotNull File outputDirectory) throws
IOException {
// generate the Vault properties XML file
File metaDir = new File(storingDirectory, META_DIR);
@@ -244,6 +241,18 @@ public class VaultPackageAssembler implements
EntryHandler, FileFilter {
throw new IOException("Could not create meta Dir: " + metaDir);
}
+ final PackageType sourcePackageType;
+ final String sourcePackageTypeValue =
(String)properties.get(PackageProperties.NAME_PACKAGE_TYPE);
+ if (sourcePackageTypeValue != null) {
+ sourcePackageType =
PackageType.valueOf(sourcePackageTypeValue.toUpperCase());
+ } else {
+ sourcePackageType = null;
+ }
+ PackageType newPackageType = recalculatePackageType(sourcePackageType,
storingDirectory);
+ if (newPackageType != null) {
+ properties.setProperty(PackageProperties.NAME_PACKAGE_TYPE,
newPackageType.name().toLowerCase());
+ }
+
setDependencies(dependencies, properties);
File xmlProperties = new File(metaDir, PROPERTIES_XML);
@@ -252,8 +261,8 @@ public class VaultPackageAssembler implements EntryHandler,
FileFilter {
properties.storeToXML(fos, null);
}
- // generate the Vault filter XML file
- computeFilters(outputDirectory);
+ // generate the Vault filter XML file based on new contents of the
package
+ computeFilters(storingDirectory);
File xmlFilter = new File(metaDir, FILTER_XML);
try (InputStream input = filter.getSource();
FileOutputStream output = new FileOutputStream(xmlFilter)) {
@@ -275,11 +284,31 @@ public class VaultPackageAssembler implements
EntryHandler, FileFilter {
return destFile;
}
- private void computeFilters(@NotNull File outputDirectory) {
- File jcrRootDir = new File(outputDirectory, ROOT_DIR);
+ static @Nullable PackageType recalculatePackageType(PackageType
sourcePackageType, @NotNull File outputDirectory) {
+ if (sourcePackageType != null && sourcePackageType !=
PackageType.MIXED) {
+ return null;
+ }
+ AtomicBoolean foundMutableFiles = new AtomicBoolean();
+ AtomicBoolean foundImmutableFiles = new AtomicBoolean();
+ forEachDirectoryBelowJcrRoot(outputDirectory, child -> {
+ if (child.getName().equals("apps") ||
child.getName().equals("libs")) {
+ foundImmutableFiles.weakCompareAndSet(false, true);
+ } else {
+ foundMutableFiles.weakCompareAndSet(false, true);
+ }
+ });
+ if (foundImmutableFiles.get() && !foundMutableFiles.get()) {
+ return PackageType.APPLICATION;
+ } else if (!foundImmutableFiles.get() && foundMutableFiles.get()) {
+ return PackageType.CONTENT;
+ } else {
+ return PackageType.MIXED;
+ }
+
+ }
- if (jcrRootDir.exists() && jcrRootDir.isDirectory()) {
- for (File child : jcrRootDir.listFiles(this)) {
+ private void computeFilters(@NotNull File outputDirectory) {
+ forEachDirectoryBelowJcrRoot(outputDirectory, child -> {
TreeNode node = lowestCommonAncestor(new TreeNode(child));
File lowestCommonAncestor = node != null ? node.val : null;
if (lowestCommonAncestor != null) {
@@ -287,21 +316,24 @@ public class VaultPackageAssembler implements
EntryHandler, FileFilter {
filter.add(new PathFilterSet(root));
}
+ });
+ }
+
+ private static void forEachDirectoryBelowJcrRoot(File outputDirectory,
Consumer<File> consumer) {
+ File jcrRootDir = new File(outputDirectory, ROOT_DIR);
+ if (jcrRootDir.exists() && jcrRootDir.isDirectory()) {
+ for (File child :
jcrRootDir.listFiles((FileFilter)DirectoryFileFilter.INSTANCE)) {
+ consumer.accept(child);
}
}
}
- @Override
- public boolean accept(@NotNull File pathname) {
- return pathname.isDirectory();
- }
-
private @Nullable TreeNode lowestCommonAncestor(@NotNull TreeNode root) {
int currMaxDepth = 0;//curr tree's deepest leaf depth
int countMaxDepth = 0;//num of deepest leaves
TreeNode node = null;
- for (File child : root.val.listFiles(this)) {
+ for (File child :
root.val.listFiles((FileFilter)DirectoryFileFilter.INSTANCE)) {
TreeNode temp = lowestCommonAncestor(new TreeNode(child));
if (temp == null) {
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 9ea256c..8df90bc 100644
---
a/src/test/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverterTest.java
+++
b/src/test/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverterTest.java
@@ -33,17 +33,7 @@ import java.io.StringReader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.InvalidPropertiesFormatException;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Properties;
-import java.util.StringTokenizer;
+import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -796,7 +786,7 @@ public class ContentPackage2FeatureModelConverterTest {
// see SLING-8649
@Test
public void filteredOutContentPackagesAreExcludedDependencies() throws
Exception {
- File[] contentPackages = load("test_dep_a-1.0.zip",
"test_dep_b-1.0.zip", "test_dep_b-1.0.zip");
+ File[] contentPackages = load("test_dep_a-1.0.zip",
"test_dep_b-1.0.zip");
// input: c <- a <- b
// expected output: c <- a
@@ -804,7 +794,7 @@ public class ContentPackage2FeatureModelConverterTest {
File outputDirectory = new File(System.getProperty("java.io.tmpdir"),
getClass().getName() + '_' + System.currentTimeMillis());
try {
- converter.setFeaturesManager(new DefaultFeaturesManager(true, 5,
outputDirectory, null, null, null))
+ converter.setFeaturesManager(new DefaultFeaturesManager(true, 5,
outputDirectory, null, null, new HashMap<>()))
.setDropContent(true)
.setBundlesDeployer(new
DefaultArtifactsDeployer(outputDirectory))
.setEmitter(DefaultPackagesEventsEmitter.open(outputDirectory))
@@ -814,6 +804,7 @@ public class ContentPackage2FeatureModelConverterTest {
assertNull(a.getExtensions().getByName("content-packages"));
Feature b = getFeature(outputDirectory, "test_b.json");
+ assertNotNull(b.getExtensions().getByName("content-packages"));
Artifacts artifacts =
b.getExtensions().getByName("content-packages").getArtifacts();
assertFalse(artifacts.isEmpty());
assertEquals("my_packages:test_b:zip:cp2fm-converted:1.0",
artifacts.iterator().next().getId().toString());
@@ -822,7 +813,8 @@ public class ContentPackage2FeatureModelConverterTest {
VaultPackage vaultPackage = new
PackageManagerImpl().open(contentPackage);
String dependencies =
vaultPackage.getProperties().getProperty(PackageProperties.NAME_DEPENDENCIES);
assertEquals("my_packages:test_c", dependencies);
- } finally {
+ }
+ finally {
deleteDirTree(outputDirectory);
}
}
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 c1726cf..1528fee 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
@@ -77,7 +77,7 @@ public class VaultPackageAssemblerTest {
if (resourceLocation != null) {
assembler.addEntry(resourceLocation,
getClass().getResourceAsStream("../handlers" + resourceLocation));
}
- File contentPackage = assembler.createPackage(testDirectory);
+ File contentPackage = assembler.createPackage();
ZipFile zipFile = new ZipFile(contentPackage);
ZipEntry resourceEntry;
diff --git
a/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerUnparameterizedTest.java
b/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerUnparameterizedTest.java
new file mode 100644
index 0000000..268a933
--- /dev/null
+++
b/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerUnparameterizedTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.vltpkg;
+
+import java.io.File;
+import java.net.URL;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.vault.packaging.PackageType;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class VaultPackageAssemblerUnparameterizedTest {
+
+ @Test
+ public void testRecalculatePackageType() {
+ URL resource =
VaultPackageAssemblerTest.class.getResource("../immutable");
+ File immutableInput = FileUtils.toFile(resource);
+ resource = VaultPackageAssemblerTest.class.getResource("../mutable");
+ File mutableInput = FileUtils.toFile(resource);
+ resource = VaultPackageAssemblerTest.class.getResource("../mixed");
+ File mixedInput = FileUtils.toFile(resource);
+
+ Assert.assertEquals(null,
VaultPackageAssembler.recalculatePackageType(PackageType.APPLICATION,
immutableInput));
+ Assert.assertEquals(null,
VaultPackageAssembler.recalculatePackageType(PackageType.CONTENT,
immutableInput));
+ Assert.assertEquals(PackageType.APPLICATION,
VaultPackageAssembler.recalculatePackageType(PackageType.MIXED,
immutableInput));
+ Assert.assertEquals(PackageType.APPLICATION,
VaultPackageAssembler.recalculatePackageType(null, immutableInput));
+
+ Assert.assertEquals(null,
VaultPackageAssembler.recalculatePackageType(PackageType.APPLICATION,
mutableInput));
+ Assert.assertEquals(null,
VaultPackageAssembler.recalculatePackageType(PackageType.CONTENT,
mutableInput));
+ Assert.assertEquals(PackageType.CONTENT,
VaultPackageAssembler.recalculatePackageType(PackageType.MIXED, mutableInput));
+ Assert.assertEquals(PackageType.CONTENT,
VaultPackageAssembler.recalculatePackageType(null, mutableInput));
+
+ Assert.assertEquals(null,
VaultPackageAssembler.recalculatePackageType(PackageType.APPLICATION,
mixedInput));
+ Assert.assertEquals(null,
VaultPackageAssembler.recalculatePackageType(PackageType.CONTENT, mixedInput));
+ Assert.assertEquals(PackageType.MIXED,
VaultPackageAssembler.recalculatePackageType(PackageType.MIXED, mixedInput));
+ Assert.assertEquals(PackageType.MIXED,
VaultPackageAssembler.recalculatePackageType(null, mixedInput));
+ }
+}
diff --git
a/src/test/resources/org/apache/sling/feature/cpconverter/immutable/jcr_root/apps/test.txt
b/src/test/resources/org/apache/sling/feature/cpconverter/immutable/jcr_root/apps/test.txt
new file mode 100644
index 0000000..520eaaa
--- /dev/null
+++
b/src/test/resources/org/apache/sling/feature/cpconverter/immutable/jcr_root/apps/test.txt
@@ -0,0 +1,16 @@
+# 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.
+
+Some file
\ No newline at end of file
diff --git
a/src/test/resources/org/apache/sling/feature/cpconverter/mixed/jcr_root/apps/test.txt
b/src/test/resources/org/apache/sling/feature/cpconverter/mixed/jcr_root/apps/test.txt
new file mode 100644
index 0000000..520eaaa
--- /dev/null
+++
b/src/test/resources/org/apache/sling/feature/cpconverter/mixed/jcr_root/apps/test.txt
@@ -0,0 +1,16 @@
+# 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.
+
+Some file
\ No newline at end of file
diff --git
a/src/test/resources/org/apache/sling/feature/cpconverter/mixed/jcr_root/content/test.txt
b/src/test/resources/org/apache/sling/feature/cpconverter/mixed/jcr_root/content/test.txt
new file mode 100644
index 0000000..520eaaa
--- /dev/null
+++
b/src/test/resources/org/apache/sling/feature/cpconverter/mixed/jcr_root/content/test.txt
@@ -0,0 +1,16 @@
+# 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.
+
+Some file
\ No newline at end of file
diff --git
a/src/test/resources/org/apache/sling/feature/cpconverter/mutable/jcr_root/content/test.txt
b/src/test/resources/org/apache/sling/feature/cpconverter/mutable/jcr_root/content/test.txt
new file mode 100644
index 0000000..520eaaa
--- /dev/null
+++
b/src/test/resources/org/apache/sling/feature/cpconverter/mutable/jcr_root/content/test.txt
@@ -0,0 +1,16 @@
+# 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.
+
+Some file
\ No newline at end of file
diff --git
a/src/test/resources/org/apache/sling/feature/cpconverter/test_dep_b-1.0.zip
b/src/test/resources/org/apache/sling/feature/cpconverter/test_dep_b-1.0.zip
index db449d9..6832f9b 100644
Binary files
a/src/test/resources/org/apache/sling/feature/cpconverter/test_dep_b-1.0.zip
and
b/src/test/resources/org/apache/sling/feature/cpconverter/test_dep_b-1.0.zip
differ