Skip to content

Commit

Permalink
Merge pull request #14 from DominikSuess/issue/SLING-8623
Browse files Browse the repository at this point in the history
Issue/sling 8623
  • Loading branch information
karlpauls committed Aug 9, 2019
2 parents ff7dcda + 6f90f5b commit a33300e
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 48 deletions.
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.sling.feature.cpconverter;

import static org.apache.sling.feature.cpconverter.vltpkg.VaultPackageUtils.detectPackageType;
import static java.util.Objects.requireNonNull;

import java.io.File;
Expand All @@ -36,6 +37,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.PackageType;
import org.apache.jackrabbit.vault.packaging.VaultPackage;
import org.apache.sling.feature.ArtifactId;
import org.apache.sling.feature.cpconverter.acl.AclManager;
Expand Down Expand Up @@ -79,6 +81,8 @@ public class ContentPackage2FeatureModelConverter extends BaseVaultPackageScanne

private PackagesEventsEmitter emitter;

private boolean dropContent = false;

public ContentPackage2FeatureModelConverter() {
this(false);
}
Expand Down Expand Up @@ -133,6 +137,12 @@ public ContentPackage2FeatureModelConverter setEmitter(PackagesEventsEmitter emi
this.emitter = emitter;
return this;
}

public ContentPackage2FeatureModelConverter setDropContent(boolean dropContent) {
this.dropContent = dropContent;
return this;
}


public void convert(File...contentPackages) throws Exception {
requireNonNull(contentPackages , "Null content-package(s) can not be converted.");
Expand All @@ -156,7 +166,7 @@ protected Collection<VaultPackage> firstPass(File...contentPackages) throws Exce
idPackageMapping.put(pack.getId(), pack);

// analyze sub-content packages in order to filter out
// possible outdated conflictring packages
// possible outdated conflicting packages
recollectorVaultPackageScanner.traverse(pack);

logger.info("content-package '{}' successfully read!", contentPackage);
Expand Down Expand Up @@ -196,9 +206,7 @@ protected void secondPass(Collection<VaultPackage> orderedContentPackages) throw

// deploy the new zip content-package to the local mvn bundles dir

artifactsDeployer.deploy(new FileArtifactWriter(contentPackageArchive), packageId);

featuresManager.addArtifact(null, packageId);
processContentPackageArchive(contentPackageArchive, packageId);

// finally serialize the Feature Model(s) file(s)

Expand Down Expand Up @@ -270,17 +278,28 @@ public void processSubPackage(String path, VaultPackage vaultPackage) throws Exc
File contentPackageArchive = clonedPackage.createPackage();

// deploy the new content-package to the local mvn bundles dir and attach it to the feature

artifactsDeployer.deploy(new FileArtifactWriter(contentPackageArchive), packageId);

featuresManager.addArtifact(null, packageId);
processContentPackageArchive(contentPackageArchive, packageId);

// restore the previous assembler
mainPackageAssembler = handler;

emitter.endSubPackage();
}

private void processContentPackageArchive(File contentPackageArchive, ArtifactId packageId) throws Exception {
try (VaultPackage vaultPackage = open(contentPackageArchive)) {
PackageType packageType = detectPackageType(vaultPackage);
// don't deploy & add content-packages of type content to featuremodel if dropContent is set
if (PackageType.CONTENT != packageType || !dropContent) {
// deploy the new content-package to the local mvn bundles dir and attach it to the feature
artifactsDeployer.deploy(new FileArtifactWriter(contentPackageArchive), packageId);
featuresManager.addArtifact(null, packageId);
} else {
logger.info("Dropping package of PackageType.CONTENT {}", packageId.getArtifactId());
}
}
}

protected boolean isSubContentPackageIncluded(String path) {
return subContentPackages.containsValue(path);
}
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.apache.sling.feature.cpconverter.vltpkg;

import static java.util.stream.Collectors.joining;
import static org.apache.sling.feature.cpconverter.vltpkg.VaultPackageUtils.detectPackageType;

import java.io.File;
import java.io.FileWriter;
Expand All @@ -26,10 +27,7 @@
import java.util.Date;
import java.util.Stack;

import org.apache.jackrabbit.vault.fs.api.PathFilterSet;
import org.apache.jackrabbit.vault.fs.api.WorkspaceFilter;
import org.apache.jackrabbit.vault.packaging.PackageId;
import org.apache.jackrabbit.vault.packaging.PackageType;
import org.apache.jackrabbit.vault.packaging.VaultPackage;

/**
Expand Down Expand Up @@ -114,37 +112,4 @@ public void endSubPackage() {
endPackage();
}

private static PackageType detectPackageType(VaultPackage vaultPackage) {
PackageType packageType = vaultPackage.getPackageType();
if (packageType != null) {
return packageType;
}

// borrowed from org.apache.jackrabbit.vault.fs.io.AbstractExporter
WorkspaceFilter filter = vaultPackage.getMetaInf().getFilter();

boolean hasApps = false;
boolean hasOther = false;
for (PathFilterSet p : filter.getFilterSets()) {
if ("cleanup".equals(p.getType())) {
continue;
}
String root = p.getRoot();
if ("/apps".equals(root)
|| root.startsWith("/apps/")
|| "/libs".equals(root)
|| root.startsWith("/libs/")) {
hasApps = true;
} else {
hasOther = true;
}
}
if (hasApps && !hasOther) {
return PackageType.APPLICATION;
} else if (hasOther && !hasApps) {
return PackageType.CONTENT;
}
return PackageType.MIXED;
}

}
@@ -0,0 +1,65 @@
/*
* 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 org.apache.jackrabbit.vault.fs.api.PathFilterSet;
import org.apache.jackrabbit.vault.fs.api.WorkspaceFilter;
import org.apache.jackrabbit.vault.packaging.PackageType;
import org.apache.jackrabbit.vault.packaging.VaultPackage;

public class VaultPackageUtils {

private VaultPackageUtils() {
// this class must not be instantiated from outside
}

public static PackageType detectPackageType(VaultPackage vaultPackage) {
PackageType packageType = vaultPackage.getPackageType();
if (packageType != null) {
return packageType;
}

// borrowed from org.apache.jackrabbit.vault.fs.io.AbstractExporter
WorkspaceFilter filter = vaultPackage.getMetaInf().getFilter();

boolean hasApps = false;
boolean hasOther = false;
if (filter != null) {
for (PathFilterSet p : filter.getFilterSets()) {
if ("cleanup".equals(p.getType())) {
continue;
}
String root = p.getRoot();
if ("/apps".equals(root)
|| root.startsWith("/apps/")
|| "/libs".equals(root)
|| root.startsWith("/libs/")) {
hasApps = true;
} else {
hasOther = true;
}
}
if (hasApps && !hasOther) {
return PackageType.APPLICATION;
} else if (hasOther && !hasApps) {
return PackageType.CONTENT;
}
}
return PackageType.MIXED;
}

}
Expand Up @@ -40,6 +40,7 @@
import org.apache.jackrabbit.vault.packaging.CyclicDependencyException;
import org.apache.jackrabbit.vault.packaging.VaultPackage;
import org.apache.sling.feature.ArtifactId;
import org.apache.sling.feature.Artifacts;
import org.apache.sling.feature.Extension;
import org.apache.sling.feature.Feature;
import org.apache.sling.feature.cpconverter.acl.DefaultAclManager;
Expand Down Expand Up @@ -195,6 +196,76 @@ public void convertContentPackage() throws Exception {
"jcr_root/config.xml",
"jcr_root/definition/.content.xml");
}

@Test
public void convertContentPackageDropContent() throws Exception {
URL packageUrl = getClass().getResource("test-content-package.zip");
File packageFile = FileUtils.toFile(packageUrl);

File outputDirectory = new File(System.getProperty("java.io.tmpdir"), getClass().getName() + '_' + System.currentTimeMillis());

converter.setFeaturesManager(new DefaultFeaturesManager(true, 5, outputDirectory, null, null))
.setBundlesDeployer(new DefaultArtifactsDeployer(outputDirectory))
.setEmitter(DefaultPackagesEventsEmitter.open(outputDirectory))
.setDropContent(true)
.convert(packageFile);

verifyFeatureFile(outputDirectory,
"asd.retail.all.json",
"asd.sample:asd.retail.all:slingosgifeature:0.0.1",
Arrays.asList("org.apache.felix:org.apache.felix.framework:6.0.1"),
Arrays.asList("org.apache.sling.commons.log.LogManager.factory.config~asd-retail"),
Arrays.asList("asd.sample:asd.retail.apps:zip:cp2fm-converted:0.0.1",
"asd:Asd.Retail.config:zip:cp2fm-converted:0.0.1"));
verifyFeatureFile(outputDirectory,
"asd.retail.all-author.json",
"asd.sample:asd.retail.all:slingosgifeature:author:0.0.1",
Arrays.asList("org.apache.sling:org.apache.sling.api:2.20.0"),
Collections.emptyList(),
Collections.emptyList());
verifyFeatureFile(outputDirectory,
"asd.retail.all-publish.json",
"asd.sample:asd.retail.all:slingosgifeature:publish:0.0.1",
Arrays.asList("org.apache.sling:org.apache.sling.models.api:1.3.8"),
Arrays.asList("org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended~asd-retail"),
Collections.emptyList());

// verify the runmode.mapper integrity
File runmodeMapperFile = new File(outputDirectory, "runmode.mapping");
assertTrue(runmodeMapperFile.exists());
assertTrue(runmodeMapperFile.isFile());
Properties runModes = new Properties();
try (FileInputStream input = new FileInputStream(runmodeMapperFile)) {
runModes.load(input);
}
assertFalse(runModes.isEmpty());
assertTrue(runModes.containsKey("(default)"));
assertEquals("asd.retail.all.json", runModes.getProperty("(default)"));
assertEquals("asd.retail.all-author.json", runModes.getProperty("author"));
assertEquals("asd.retail.all-publish.json", runModes.getProperty("publish"));

verifyContentPackage(new File(outputDirectory, "asd/Asd.Retail.config/0.0.1/Asd.Retail.config-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/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");
// 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());
}

private void verifyFeatureFile(File outputDirectory,
String name,
Expand All @@ -219,10 +290,15 @@ private void verifyFeatureFile(File outputDirectory,
assertNotNull(expectedConfiguration + " not found in Feature " + expectedArtifactId, feature.getConfigurations().getConfiguration(expectedConfiguration));
}

for (String expectedContentPackagesExtension : expectedContentPackagesExtensions) {
assertTrue(expectedContentPackagesExtension + " not found in Feature " + expectedArtifactId,
feature.getExtensions().getByName("content-packages").getArtifacts().containsExact(ArtifactId.fromMvnId(expectedContentPackagesExtension)));
verifyInstalledArtifact(outputDirectory, expectedContentPackagesExtension);
if (expectedContentPackagesExtensions.size() > 0) {
Artifacts contentPackages = feature.getExtensions().getByName("content-packages").getArtifacts();
assertEquals(expectedContentPackagesExtensions.size(), contentPackages.size());

for (String expectedContentPackagesExtension : expectedContentPackagesExtensions) {
assertTrue(expectedContentPackagesExtension + " not found in Feature " + expectedArtifactId,
contentPackages.containsExact(ArtifactId.fromMvnId(expectedContentPackagesExtension)));
verifyInstalledArtifact(outputDirectory, expectedContentPackagesExtension);
}
}
}
}
Expand Down

0 comments on commit a33300e

Please sign in to comment.