Skip to content

Commit a9a1f23

Browse files
committed
Startup hook should implement SlingRepositoryInitializer to propagate
exceptions properly This closes #545 partially
1 parent 46d6457 commit a9a1f23

File tree

3 files changed

+41
-80
lines changed

3 files changed

+41
-80
lines changed

accesscontroltool-startuphook-bundle/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
<groupId>org.apache.sling</groupId>
5858
<artifactId>org.apache.sling.settings</artifactId>
5959
</dependency>
60+
<dependency>
61+
<groupId>org.apache.sling</groupId>
62+
<artifactId>org.apache.sling.jcr.api</artifactId>
63+
<version>2.4.0</version><!-- required for https://issues.apache.org/jira/browse/SLING-5456, only available in AEM 6.3+ -->
64+
<scope>provided</scope>
65+
</dependency>
6066

6167
<!-- START: Test Dependencies -->
6268
<dependency>
@@ -114,7 +120,6 @@ Bundle-DocURL: https://github.com/Netcentric/accesscontroltool
114120
Bundle-License: Eclipse Public License, Version 1.0; link="https://www.eclipse.org/legal/epl-v10.html"
115121
Bundle-SymbolicName: biz.netcentric.cq.tools.accesscontroltool.startuphook.bundle
116122
Bundle-Vendor: Netcentric
117-
Bundle-Activator: biz.netcentric.cq.tools.actool.startuphook.impl.StartupBundleActivator
118123
Bundle-Name: ${project.name}
119124
Conditional-Package: biz.netcentric.cq.tools.actool.helper.runtime
120125
]]></bnd>

accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImpl.java

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.jackrabbit.JcrConstants;
2222
import org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.AccessControlConstants;
2323
import org.apache.sling.jcr.api.SlingRepository;
24+
import org.apache.sling.jcr.api.SlingRepositoryInitializer;
2425
import org.osgi.framework.BundleContext;
2526
import org.osgi.service.component.annotations.Activate;
2627
import org.osgi.service.component.annotations.Component;
@@ -33,13 +34,13 @@
3334
import org.slf4j.LoggerFactory;
3435

3536
import biz.netcentric.cq.tools.actool.api.AcInstallationService;
36-
import biz.netcentric.cq.tools.actool.helper.Constants;
3737
import biz.netcentric.cq.tools.actool.helper.runtime.RuntimeHelper;
3838
import biz.netcentric.cq.tools.actool.history.impl.HistoryUtils;
3939

40-
@Component
40+
@Component(
41+
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
4142
@Designate(ocd=AcToolStartupHookServiceImpl.Config.class)
42-
public class AcToolStartupHookServiceImpl {
43+
public class AcToolStartupHookServiceImpl implements SlingRepositoryInitializer {
4344
private static final Logger LOG = LoggerFactory.getLogger(AcToolStartupHookServiceImpl.class);
4445

4546
@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 {
5556
@Reference(policyOption = ReferencePolicyOption.GREEDY)
5657
private AcInstallationService acInstallationService;
5758

58-
@Reference(policyOption = ReferencePolicyOption.GREEDY)
59-
private SlingRepository repository;
60-
6159
private boolean isCompositeNodeStore;
60+
private Config.StartupHookActivation activationMode;
6261

6362
@Activate
6463
public void activate(BundleContext bundleContext, Config config) {
65-
66-
boolean isCloudReady = RuntimeHelper.isCloudReadyInstance();
67-
Config.StartupHookActivation activationMode = config.activationMode();
68-
LOG.info("AcTool Startup Hook (start level: {} isCloudReady: {} activationMode: {})",
69-
RuntimeHelper.getCurrentStartLevel(bundleContext),
70-
isCloudReady,
71-
activationMode);
72-
73-
boolean applyOnStartup = (activationMode == Config.StartupHookActivation.ALWAYS)
74-
|| (isCloudReady && activationMode == Config.StartupHookActivation.CLOUD_ONLY);
75-
76-
if(applyOnStartup) {
77-
78-
try {
79-
80-
List<String> relevantPathsForInstallation = getRelevantPathsForInstallation();
81-
LOG.info("Running AcTool with "
82-
+ (relevantPathsForInstallation.isEmpty() ? "all paths" : "paths " + relevantPathsForInstallation) + "...");
83-
acInstallationService.apply(null, relevantPathsForInstallation.toArray(new String[relevantPathsForInstallation.size()]),
84-
true);
85-
LOG.info("AC Tool Startup Hook done. (start level " + RuntimeHelper.getCurrentStartLevel(bundleContext) + ")");
86-
87-
copyAcHistoryToOrFromApps(isCloudReady);
88-
89-
} catch (RepositoryException e) {
90-
LOG.error("Exception while triggering AC Tool on startup: " + e, e);
91-
}
92-
} else {
93-
LOG.debug("Skipping AcTool Startup Hook: activationMode: {} isCloudReady: {}", activationMode, isCloudReady);
94-
}
95-
64+
activationMode = config.activationMode();
9665
}
9766

98-
private List<String> getRelevantPathsForInstallation() throws RepositoryException {
67+
private List<String> getRelevantPathsForInstallation(SlingRepository repository) throws RepositoryException {
9968
Session session = null;
10069
try {
10170
session = repository.loginService(null, null);
10271

103-
isCompositeNodeStore = RuntimeHelper.isCompositeNodeStore(session);
104-
LOG.info("Repo is running with Composite NodeStore: " + isCompositeNodeStore);
72+
boolean isCompositeNodeStore = RuntimeHelper.isCompositeNodeStore(session);
73+
LOG.info("Repo is running with Composite NodeStore: {}", isCompositeNodeStore);
10574

10675
if(!isCompositeNodeStore) {
10776
return Collections.emptyList();
@@ -118,7 +87,7 @@ private List<String> getRelevantPathsForInstallation() throws RepositoryExceptio
11887
AccessControlConstants.REP_REPO_POLICY).contains(node.getName())) {
11988
continue;
12089
}
121-
if (isCompositeNodeStore && Arrays.asList("apps", "libs").contains(node.getName())) {
90+
if (Arrays.asList("apps", "libs").contains(node.getName())) {
12291
continue;
12392
}
12493
relevantPathsForInstallation.add(node.getPath());
@@ -139,7 +108,7 @@ private List<String> getRelevantPathsForInstallation() throws RepositoryExceptio
139108
}
140109
}
141110

142-
private void copyAcHistoryToOrFromApps(boolean isCloudReady) {
111+
private void copyAcHistoryToOrFromApps(SlingRepository repository, boolean isCloudReady) {
143112

144113
if(isCloudReady) {
145114
Session session = null;
@@ -178,4 +147,28 @@ private void copyAcHistoryToOrFromApps(boolean isCloudReady) {
178147

179148
}
180149

150+
@Override
151+
public void processRepository(SlingRepository repo) throws Exception {
152+
boolean isCloudReady = RuntimeHelper.isCloudReadyInstance();
153+
LOG.info("AcTool Startup Hook (isCloudReady: {} activationMode: {})",
154+
isCloudReady,
155+
activationMode);
156+
157+
boolean applyOnStartup = (activationMode == Config.StartupHookActivation.ALWAYS)
158+
|| (isCloudReady && activationMode == Config.StartupHookActivation.CLOUD_ONLY);
159+
160+
if(applyOnStartup) {
161+
List<String> relevantPathsForInstallation = getRelevantPathsForInstallation(repo);
162+
LOG.info("Running AcTool with "
163+
+ (relevantPathsForInstallation.isEmpty() ? "all paths" : "paths " + relevantPathsForInstallation) + "...");
164+
acInstallationService.apply(null, relevantPathsForInstallation.toArray(new String[relevantPathsForInstallation.size()]),
165+
true);
166+
LOG.info("AC Tool Startup Hook done.");
167+
168+
copyAcHistoryToOrFromApps(repo, isCloudReady);
169+
} else {
170+
LOG.debug("Skipping AcTool Startup Hook: activationMode: {} isCloudReady: {}", activationMode, isCloudReady);
171+
}
172+
}
173+
181174
}

accesscontroltool-startuphook-bundle/src/main/java/biz/netcentric/cq/tools/actool/startuphook/impl/StartupBundleActivator.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)