This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch issue/SLING-12773 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-packageinit.git
commit 8ff560cfae45ef53c7f55324c3636616fd73499f Author: Robert Munteanu <[email protected]> AuthorDate: Fri May 2 22:42:36 2025 +0200 SLING-12773 - JCR Packageinit should always install SNAPSHOT packages Adjust the ExecutionPlanRepoInitializer to only store and check hashes for execution plans that do not reference SNAPSHOT packages. --- .../impl/ExecutionPlanRepoInitializer.java | 78 +++++++++++----------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/apache/sling/jcr/packageinit/impl/ExecutionPlanRepoInitializer.java b/src/main/java/org/apache/sling/jcr/packageinit/impl/ExecutionPlanRepoInitializer.java index a864a31..173074f 100644 --- a/src/main/java/org/apache/sling/jcr/packageinit/impl/ExecutionPlanRepoInitializer.java +++ b/src/main/java/org/apache/sling/jcr/packageinit/impl/ExecutionPlanRepoInitializer.java @@ -20,14 +20,11 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; @@ -60,7 +57,6 @@ import org.slf4j.LoggerFactory; public class ExecutionPlanRepoInitializer implements SlingRepositoryInitializer { private static final String EXECUTEDPLANS_FILE = "executedplans.file"; - private List<String> executionPlans = new ArrayList<>(); private File statusFile; @@ -81,38 +77,12 @@ public class ExecutionPlanRepoInitializer implements SlingRepositoryInitializer */ private final Logger logger = LoggerFactory.getLogger(getClass()); private BundleContext context; + private Config config; @Activate - private void activate(BundleContext context, Config config) throws FileNotFoundException, IOException { - List<String> epCandidates = Arrays.asList(config.executionplans()); - if (!epCandidates.isEmpty()) { - if (StringUtils.isEmpty(config.statusfilepath())) { - // if no path is configured lookup default file in bundledata - statusFile = context.getDataFile(EXECUTEDPLANS_FILE); - } else { - Path statusFilePath = Paths.get(config.statusfilepath()); - if (statusFilePath.isAbsolute()) { - // only absolute references are considered for lookup of - // external statusfile - statusFile = statusFilePath.toFile(); - } else { - throw new IllegalStateException("Only absolute paths supported"); - } - } - if (statusFile.exists()) { - // in case statusFile already exists read all hashes - Set<Integer> executedHashes = new HashSet<>(); - try (BufferedReader br = new BufferedReader(new FileReader(statusFile))) { - for (String line; (line = br.readLine()) != null;) { - executedHashes.add(Integer.parseInt(line)); - } - } - this.executionPlans.addAll(filterCandidates(epCandidates, executedHashes)); - } else { - this.executionPlans.addAll(epCandidates); - } - } + private void activate(BundleContext context, Config config) { this.context = context; + this.config = config; } private static List<String> filterCandidates(List<String> epCandidates, Set<Integer> executedHashes) { @@ -126,7 +96,32 @@ public class ExecutionPlanRepoInitializer implements SlingRepositoryInitializer @Override public void processRepository(SlingRepository slingRepository) throws Exception { - if (!executionPlans.isEmpty()) { + + List<String> epCandidates = Arrays.asList(config.executionplans()); + Set<Integer> executedHashes = new HashSet<>(); + if (StringUtils.isEmpty(config.statusfilepath())) { + // if no path is configured lookup default file in bundledata + statusFile = context.getDataFile(EXECUTEDPLANS_FILE); + } else { + Path statusFilePath = Paths.get(config.statusfilepath()); + if (statusFilePath.isAbsolute()) { + // only absolute references are considered for lookup of + // external statusfile + statusFile = statusFilePath.toFile(); + } else { + throw new IllegalStateException("Only absolute paths supported"); + } + } + if (statusFile.exists()) { + // in case statusFile already exists read all hashes + try (BufferedReader br = new BufferedReader(new FileReader(statusFile))) { + for (String line; (line = br.readLine()) != null;) { + executedHashes.add(Integer.parseInt(line)); + } + } + } + + if (!epCandidates.isEmpty()) { ServiceTracker<PackageRegistry, ?> st = new ServiceTracker<>(context, PackageRegistry.class, null); try { st.open(); @@ -138,9 +133,13 @@ public class ExecutionPlanRepoInitializer implements SlingRepositoryInitializer @SuppressWarnings("deprecation") Session session = slingRepository.loginAdministrative(null); try (BufferedWriter writer = new BufferedWriter(new FileWriter(statusFile))) { - for (String plan : executionPlans) { + for (String plan : epCandidates) { builder.load(new ByteArrayInputStream(plan.getBytes(StandardCharsets.UTF_8))); builder.with(session); + boolean hasSnapshot = builder.preview().stream().anyMatch( p -> p.getVersionString().endsWith("-SNAPSHOT")); + if ( !hasSnapshot && isCandidateProcessed(plan, executedHashes)) { + continue; + } ExecutionPlan xplan = builder.execute(); if (xplan.getTasks().size() > 0) { if (xplan.hasErrors()) { @@ -157,9 +156,12 @@ public class ExecutionPlanRepoInitializer implements SlingRepositoryInitializer logger.info("No tasks found in execution plan - no additional packages installed."); } - // save hashes to file for crosscheck on subsequent startup to avoid double processing - writer.write(String.valueOf(plan.hashCode())); - writer.newLine(); + if ( !hasSnapshot ) { + // save hashes to file for crosscheck on subsequent startup to avoid double processing + // but always process SNAPSHOT packages + writer.write(String.valueOf(plan.hashCode())); + writer.newLine(); + } } } finally {
