Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLING-11617 - Skip execution plan processing #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.0</version>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -29,6 +30,7 @@
import java.util.TreeMap;

import org.apache.jackrabbit.vault.fs.io.ImportOptions;
import org.apache.jackrabbit.vault.packaging.PackageException;
import org.apache.jackrabbit.vault.packaging.PackageExistsException;
import org.apache.jackrabbit.vault.packaging.PackageId;
import org.apache.jackrabbit.vault.packaging.registry.ExecutionPlanBuilder;
Expand All @@ -48,13 +50,15 @@
import org.apache.sling.feature.launcher.spi.extensions.ExtensionHandler;

public class ContentHandler implements ExtensionHandler {
static final String SKIP_EXECUTIONPLANS_MSG = "ContentHandler set up to skip building of executionplans - only configuring FSPackageRegistry";
public static final String PACKAGEREGISTRY_HOME = "packageregistry.home";
public static final String SKIP_EXECUTIONPLANS_PROP = "skipexecutionplans";

private static final String REPOSITORY_HOME = "repository.home";

private static final String REGISTRY_FOLDER = "packageregistry";

private static ExecutionPlanBuilder buildExecutionPlan(Collection<Artifact> artifacts, Set<PackageId> satisfiedPackages, LauncherPrepareContext prepareContext, File registryHome) throws Exception {
static ExecutionPlanBuilder buildExecutionPlan(Collection<Artifact> artifacts, Set<PackageId> satisfiedPackages, LauncherPrepareContext prepareContext, File registryHome) throws IOException, PackageException {

List<File> packageReferences = new ArrayList<>();

Expand Down Expand Up @@ -97,42 +101,61 @@ public boolean handle(ExtensionContext context, Extension extension) throws Exce
File registryHome = getRegistryHomeDir(context);
if (extension.getType() == ExtensionType.ARTIFACTS
&& extension.getName().equals(Extension.EXTENSION_NAME_CONTENT_PACKAGES)) {
Map<Integer, Collection<Artifact>> orderedArtifacts = new TreeMap<>();
for (final Artifact a : extension.getArtifacts()) {
int order;
// content-packages without explicit start-order to be installed last
if (a.getMetadata().get(Artifact.KEY_START_ORDER) != null) {
order = a.getStartOrder();
} else {
order = Integer.MAX_VALUE;
if(skipBuildExecutionPlans()){
context.getLogger().info(SKIP_EXECUTIONPLANS_MSG);
} else {
List<String> executionPlans = new ArrayList<>();
Map<Integer, Collection<Artifact>> orderedArtifacts = getOrderedArtifacts(extension);
Set<PackageId> satisfiedPackages = new HashSet<>();
for (Collection<Artifact> artifacts : orderedArtifacts.values()) {
ExecutionPlanBuilder builder = buildExecutionPlan(artifacts, satisfiedPackages, context, registryHome);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
builder.save(baos);
executionPlans.add(baos.toString("UTF-8"));
}
orderedArtifacts.computeIfAbsent(order, id -> new ArrayList<>()).add(a);
configurePackageInit(context, registryHome, executionPlans);
}
List<String> executionPlans = new ArrayList<>();
Set<PackageId> satisfiedPackages = new HashSet<>();
for (Object key : orderedArtifacts.keySet()) {
Collection<Artifact> artifacts = orderedArtifacts.get(key);
ExecutionPlanBuilder builder = buildExecutionPlan(artifacts, satisfiedPackages, context, registryHome);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
builder.save(baos);
executionPlans.add(baos.toString("UTF-8"));
}
// Workaround for too bold relocation mechanism - corresponding details at https://issues.apache.org/jira/browse/MSHADE-156
final Configuration initcfg = new Configuration("org.apache.sling.jcr.packageinit.impl.ExecutionPlanRepoInitializer");
initcfg.getProperties().put("executionplans", executionPlans.toArray(new String[executionPlans.size()]));
initcfg.getProperties().put("statusfilepath", registryHome.getAbsolutePath() + "/executedplans.file");
context.addConfiguration(initcfg.getPid(), null, initcfg.getProperties());
// Workaround for too bold relocation mechanism - corresponding details at https://issues.apache.org/jira/browse/MSHADE-156
final Configuration registrycfg = new Configuration("org.UNSHADE.apache.jackrabbit.vault.packaging.registry.impl.FSPackageRegistry");
registrycfg.getProperties().put("homePath", registryHome.getPath());
context.addConfiguration(registrycfg.getPid(), null, registrycfg.getProperties());

configureFSRegistry(context, registryHome);
return true;
} else {
return false;
}
}

private Map<Integer, Collection<Artifact>> getOrderedArtifacts(Extension extension) {
Map<Integer, Collection<Artifact>> orderedArtifacts = new TreeMap<>();
for (final Artifact a : extension.getArtifacts()) {
int order;
// content-packages without explicit start-order to be installed last
if (a.getMetadata().get(Artifact.KEY_START_ORDER) != null) {
order = a.getStartOrder();
} else {
order = Integer.MAX_VALUE;
}
orderedArtifacts.computeIfAbsent(order, id -> new ArrayList<>()).add(a);
}
return orderedArtifacts;
}

private void configureFSRegistry(ExtensionContext context, File registryHome) {
// Workaround for too bold relocation mechanism - corresponding details at https://issues.apache.org/jira/browse/MSHADE-156
final Configuration registrycfg = new Configuration("org.UNSHADE.apache.jackrabbit.vault.packaging.registry.impl.FSPackageRegistry");
registrycfg.getProperties().put("homePath", registryHome.getPath());
context.addConfiguration(registrycfg.getPid(), null, registrycfg.getProperties());
}

private void configurePackageInit(ExtensionContext context, File registryHome, List<String> executionPlans) {
// Workaround for too bold relocation mechanism - corresponding details at https://issues.apache.org/jira/browse/MSHADE-156
final Configuration initcfg = new Configuration("org.apache.sling.jcr.packageinit.impl.ExecutionPlanRepoInitializer");
initcfg.getProperties().put("executionplans", executionPlans.toArray(new String[executionPlans.size()]));
initcfg.getProperties().put("statusfilepath", registryHome.getAbsolutePath() + "/executedplans.file");
context.addConfiguration(initcfg.getPid(), null, initcfg.getProperties());
}

private boolean skipBuildExecutionPlans() {
return Boolean.getBoolean(SKIP_EXECUTIONPLANS_PROP);
}

private File getRegistryHomeDir(ExtensionContext context) {
//read repository- home from framework properties (throw exception if repo.home not set)
String registryPath = System.getProperty(PACKAGEREGISTRY_HOME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -34,6 +36,7 @@
import org.apache.sling.feature.ExtensionState;
import org.apache.sling.feature.ExtensionType;
import org.apache.sling.feature.launcher.spi.extensions.ExtensionContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -43,6 +46,8 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import org.slf4j.Logger;

@RunWith(MockitoJUnitRunner.class)
public class ContentHandlerTest {

Expand Down Expand Up @@ -148,4 +153,28 @@ public void testMultipleStartOrders() throws Exception {
assertEquals(expected_1, executionplans[1]);
assertFalse(dictIt.hasNext());
}

@Test
public void testSkipBuilding() throws Exception {
ContentHandler ch = spy(new ContentHandler());
System.setProperty(ContentHandler.PACKAGEREGISTRY_HOME, testFolder.getRoot().toString());
System.setProperty(ContentHandler.SKIP_EXECUTIONPLANS_PROP, "true");
Extension ext = new Extension(ExtensionType.ARTIFACTS, "content-packages", ExtensionState.OPTIONAL);

Artifact artifact_a = new Artifact(TEST_PACKAGE_AID_A_10);
Artifact artifact_b = new Artifact(TEST_PACKAGE_AID_B_10);
Artifact artifact_c = new Artifact(TEST_PACKAGE_AID_C_10);
ext.getArtifacts().add(artifact_a);
ext.getArtifacts().add(artifact_b);
ext.getArtifacts().add(artifact_c);

Logger log = mock(Logger.class);
when(extensionContext.getLogger()).thenReturn(log);

ch.handle(extensionContext, ext);
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
verify(log).info(argumentCaptor.capture());
Assert.assertEquals(ContentHandler.SKIP_EXECUTIONPLANS_MSG, argumentCaptor.getValue());
}

}