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 diff --git a/accesscontroltool-startuphook-bundle/pom.xml b/accesscontroltool-startuphook-bundle/pom.xml index 11ce805cb..b643000bb 100644 --- a/accesscontroltool-startuphook-bundle/pom.xml +++ b/accesscontroltool-startuphook-bundle/pom.xml @@ -51,7 +51,7 @@ org.osgi.framework provided - + javax.jcr jcr provided 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 0b6426d36..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 @@ -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; @@ -36,9 +37,10 @@ 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)") @@ -54,53 +56,20 @@ 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(); @@ -117,7 +86,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()); @@ -138,14 +107,14 @@ private List getRelevantPathsForInstallation() throws RepositoryExceptio } } - private void copyAcHistoryToOrFromApps(boolean isCloudReady) { + private void copyAcHistoryToOrFromApps(SlingRepository repository, boolean isCloudReady) { if(isCloudReady) { Session session = null; 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)) { @@ -177,4 +146,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"); - } -}