From a10eb4516c356ed4e8e32c294c3201f7f3efdcc0 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Tue, 26 Jan 2021 11:54:48 +0100 Subject: [PATCH 1/6] Startup hook should implement SlingRepositoryInitializer to propagate exceptions properly This closes #545 partially --- accesscontroltool-startuphook-bundle/pom.xml | 7 +- .../impl/AcToolStartupHookServiceImpl.java | 77 +++++++++---------- .../impl/StartupBundleActivator.java | 37 --------- 3 files changed, 41 insertions(+), 80 deletions(-) delete mode 100644 accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/StartupBundleActivator.java diff --git a/accesscontroltool-startuphook-bundle/pom.xml b/accesscontroltool-startuphook-bundle/pom.xml index e3ecef211..5bca9d563 100644 --- a/accesscontroltool-startuphook-bundle/pom.xml +++ b/accesscontroltool-startuphook-bundle/pom.xml @@ -57,6 +57,12 @@ org.apache.sling org.apache.sling.settings + + org.apache.sling + org.apache.sling.jcr.api + 2.4.0 + provided + @@ -114,7 +120,6 @@ Bundle-DocURL: https://github.com/Netcentric/accesscontroltool Bundle-License: Eclipse Public License, Version 1.0; link="https://www.eclipse.org/legal/epl-v10.html" Bundle-SymbolicName: biz.netcentric.cq.tools.accesscontroltool.startuphook.bundle Bundle-Vendor: Netcentric -Bundle-Activator: biz.netcentric.cq.tools.actool.startuphook.impl.StartupBundleActivator Bundle-Name: ${project.name} Conditional-Package: biz.netcentric.cq.tools.actool.helper.runtime ]]> diff --git a/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImpl.java b/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImpl.java index d7df276ba..2a240d6b9 100644 --- a/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImpl.java +++ b/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImpl.java @@ -21,6 +21,7 @@ import org.apache.jackrabbit.JcrConstants; import org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.AccessControlConstants; import org.apache.sling.jcr.api.SlingRepository; +import org.apache.sling.jcr.api.SlingRepositoryInitializer; import org.osgi.framework.BundleContext; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; @@ -33,13 +34,13 @@ import org.slf4j.LoggerFactory; import biz.netcentric.cq.tools.actool.api.AcInstallationService; -import biz.netcentric.cq.tools.actool.helper.Constants; import biz.netcentric.cq.tools.actool.helper.runtime.RuntimeHelper; import biz.netcentric.cq.tools.actool.history.impl.HistoryUtils; -@Component +@Component( + property = {"service.ranking:Integer=400"}) // must be executed after https://github.com/apache/sling-org-apache-sling-jcr-packageinit/blob/master/src/main/java/org/apache/sling/jcr/packageinit/impl/ExecutionPlanRepoInitializer.java#L53 @Designate(ocd=AcToolStartupHookServiceImpl.Config.class) -public class AcToolStartupHookServiceImpl { +public class AcToolStartupHookServiceImpl implements SlingRepositoryInitializer { private static final Logger LOG = LoggerFactory.getLogger(AcToolStartupHookServiceImpl.class); @ObjectClassDefinition(name = "AC Tool Startup Hook", description = "Applies AC Tool config automatically upon startup (depending on configuration/runtime)") @@ -55,53 +56,21 @@ public enum StartupHookActivation { @Reference(policyOption = ReferencePolicyOption.GREEDY) private AcInstallationService acInstallationService; - @Reference(policyOption = ReferencePolicyOption.GREEDY) - private SlingRepository repository; - private boolean isCompositeNodeStore; + private Config.StartupHookActivation activationMode; @Activate public void activate(BundleContext bundleContext, Config config) { - - boolean isCloudReady = RuntimeHelper.isCloudReadyInstance(); - Config.StartupHookActivation activationMode = config.activationMode(); - LOG.info("AcTool Startup Hook (start level: {} isCloudReady: {} activationMode: {})", - RuntimeHelper.getCurrentStartLevel(bundleContext), - isCloudReady, - activationMode); - - boolean applyOnStartup = (activationMode == Config.StartupHookActivation.ALWAYS) - || (isCloudReady && activationMode == Config.StartupHookActivation.CLOUD_ONLY); - - if(applyOnStartup) { - - try { - - List relevantPathsForInstallation = getRelevantPathsForInstallation(); - LOG.info("Running AcTool with " - + (relevantPathsForInstallation.isEmpty() ? "all paths" : "paths " + relevantPathsForInstallation) + "..."); - acInstallationService.apply(null, relevantPathsForInstallation.toArray(new String[relevantPathsForInstallation.size()]), - true); - LOG.info("AC Tool Startup Hook done. (start level " + RuntimeHelper.getCurrentStartLevel(bundleContext) + ")"); - - copyAcHistoryToOrFromApps(isCloudReady); - - } catch (RepositoryException e) { - LOG.error("Exception while triggering AC Tool on startup: " + e, e); - } - } else { - LOG.debug("Skipping AcTool Startup Hook: activationMode: {} isCloudReady: {}", activationMode, isCloudReady); - } - + activationMode = config.activationMode(); } - private List getRelevantPathsForInstallation() throws RepositoryException { + private List getRelevantPathsForInstallation(SlingRepository repository) throws RepositoryException { Session session = null; try { session = repository.loginService(null, null); - isCompositeNodeStore = RuntimeHelper.isCompositeNodeStore(session); - LOG.info("Repo is running with Composite NodeStore: " + isCompositeNodeStore); + boolean isCompositeNodeStore = RuntimeHelper.isCompositeNodeStore(session); + LOG.info("Repo is running with Composite NodeStore: {}", isCompositeNodeStore); if(!isCompositeNodeStore) { return Collections.emptyList(); @@ -118,7 +87,7 @@ private List getRelevantPathsForInstallation() throws RepositoryExceptio AccessControlConstants.REP_REPO_POLICY).contains(node.getName())) { continue; } - if (isCompositeNodeStore && Arrays.asList("apps", "libs").contains(node.getName())) { + if (Arrays.asList("apps", "libs").contains(node.getName())) { continue; } relevantPathsForInstallation.add(node.getPath()); @@ -139,7 +108,7 @@ private List getRelevantPathsForInstallation() throws RepositoryExceptio } } - private void copyAcHistoryToOrFromApps(boolean isCloudReady) { + private void copyAcHistoryToOrFromApps(SlingRepository repository, boolean isCloudReady) { if(isCloudReady) { Session session = null; @@ -178,4 +147,28 @@ private void copyAcHistoryToOrFromApps(boolean isCloudReady) { } + @Override + public void processRepository(SlingRepository repo) throws Exception { + boolean isCloudReady = RuntimeHelper.isCloudReadyInstance(); + LOG.info("AcTool Startup Hook (isCloudReady: {} activationMode: {})", + isCloudReady, + activationMode); + + boolean applyOnStartup = (activationMode == Config.StartupHookActivation.ALWAYS) + || (isCloudReady && activationMode == Config.StartupHookActivation.CLOUD_ONLY); + + if(applyOnStartup) { + List relevantPathsForInstallation = getRelevantPathsForInstallation(repo); + LOG.info("Running AcTool with " + + (relevantPathsForInstallation.isEmpty() ? "all paths" : "paths " + relevantPathsForInstallation) + "..."); + acInstallationService.apply(null, relevantPathsForInstallation.toArray(new String[relevantPathsForInstallation.size()]), + true); + LOG.info("AC Tool Startup Hook done."); + + copyAcHistoryToOrFromApps(repo, isCloudReady); + } else { + LOG.debug("Skipping AcTool Startup Hook: activationMode: {} isCloudReady: {}", activationMode, isCloudReady); + } + } + } diff --git a/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/StartupBundleActivator.java b/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/StartupBundleActivator.java deleted file mode 100644 index f0b1f44d9..000000000 --- a/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/StartupBundleActivator.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * (C) Copyright 2015 Netcentric AG. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package biz.netcentric.cq.tools.actool.startuphook.impl; - -import org.apache.sling.jcr.api.SlingRepository; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.service.component.annotations.Activate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import biz.netcentric.cq.tools.actool.helper.runtime.RuntimeHelper; - -/** Logging bundle startup with start level/slingRepositoryIsAvailable */ -public class StartupBundleActivator implements BundleActivator { - private static final Logger LOG = LoggerFactory.getLogger(StartupBundleActivator.class); - - @Activate - public void start(BundleContext bundleContext) { - boolean slingRepositoryIsAvailable = bundleContext.getServiceReference(SlingRepository.class) !=null; - int currentStartLevel = RuntimeHelper.getCurrentStartLevel(bundleContext); - LOG.info("AC Tool Bundle accesscontroltool-startuphook-bundle started at start level {}, SlingRepository is available: {}", - currentStartLevel, slingRepositoryIsAvailable); - - } - - @Override - public void stop(BundleContext context) throws Exception { - LOG.trace("AC Tool Bundle accesscontroltool-startuphook-bundle stopped"); - } -} From a4a1d07e8f441c1f0a05140cfc7e90384f728714 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Tue, 26 Jan 2021 12:02:27 +0100 Subject: [PATCH 2/6] Startup hook should implement SlingRepositoryInitializer to propagate exceptions properly This closes #545 partially --- .../actool/startuphook/impl/AcToolStartupHookServiceImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImpl.java b/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImpl.java index 2a240d6b9..0ed42a51a 100644 --- a/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImpl.java +++ b/accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImpl.java @@ -56,7 +56,6 @@ public enum StartupHookActivation { @Reference(policyOption = ReferencePolicyOption.GREEDY) private AcInstallationService acInstallationService; - private boolean isCompositeNodeStore; private Config.StartupHookActivation activationMode; @Activate @@ -115,7 +114,7 @@ private void copyAcHistoryToOrFromApps(SlingRepository repository, boolean isClo try { session = repository.loginService(null, null); - if(isCompositeNodeStore) { + if(RuntimeHelper.isCompositeNodeStore(session)) { LOG.info("Restoring history from /apps to /var"); if(session.nodeExists(HistoryUtils.AC_HISTORY_PATH_IN_APPS)) { From 83f4a6e2a97027e23472a58d7d159957739740a5 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 8 Nov 2021 16:57:11 +0100 Subject: [PATCH 3/6] Update pom.xml --- accesscontroltool-startuphook-bundle/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/accesscontroltool-startuphook-bundle/pom.xml b/accesscontroltool-startuphook-bundle/pom.xml index 4809ad2e2..27b44a984 100644 --- a/accesscontroltool-startuphook-bundle/pom.xml +++ b/accesscontroltool-startuphook-bundle/pom.xml @@ -51,7 +51,6 @@ org.osgi.framework provided - org.apache.sling org.apache.sling.jcr.api From fea43f6e111cc3ecfa71863f64b7440b9f547d8c Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 8 Nov 2021 17:16:20 +0100 Subject: [PATCH 4/6] remove reference to no longer existing activator --- accesscontroltool-startuphook-bundle/bnd.bnd | 1 - 1 file changed, 1 deletion(-) diff --git a/accesscontroltool-startuphook-bundle/bnd.bnd b/accesscontroltool-startuphook-bundle/bnd.bnd index 00cae8e22..bb7a08aff 100644 --- a/accesscontroltool-startuphook-bundle/bnd.bnd +++ b/accesscontroltool-startuphook-bundle/bnd.bnd @@ -1,3 +1,2 @@ Bundle-SymbolicName: biz.netcentric.cq.tools.accesscontroltool.startuphook.bundle -Bundle-Activator: biz.netcentric.cq.tools.actool.startuphook.impl.StartupBundleActivator Conditional-Package: biz.netcentric.cq.tools.actool.helper.runtime \ No newline at end of file From d6b99de92d9561656bc532d6474821b1cac9e514 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 8 Nov 2021 17:18:12 +0100 Subject: [PATCH 5/6] rely on managed versions only --- accesscontroltool-startuphook-bundle/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accesscontroltool-startuphook-bundle/pom.xml b/accesscontroltool-startuphook-bundle/pom.xml index 27b44a984..09087b2b3 100644 --- a/accesscontroltool-startuphook-bundle/pom.xml +++ b/accesscontroltool-startuphook-bundle/pom.xml @@ -51,10 +51,10 @@ org.osgi.framework provided + org.apache.sling org.apache.sling.jcr.api - 2.4.0 provided From deaa08017f6dccddd88017600467434a8f0cc7ae Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 8 Nov 2021 17:19:49 +0100 Subject: [PATCH 6/6] remove obsolete dependency --- accesscontroltool-startuphook-bundle/pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/accesscontroltool-startuphook-bundle/pom.xml b/accesscontroltool-startuphook-bundle/pom.xml index 09087b2b3..b643000bb 100644 --- a/accesscontroltool-startuphook-bundle/pom.xml +++ b/accesscontroltool-startuphook-bundle/pom.xml @@ -51,12 +51,6 @@ org.osgi.framework provided - - - org.apache.sling - org.apache.sling.jcr.api - provided - javax.jcr jcr