Skip to content

Commit fd03f45

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

File tree

3 files changed

+40
-80
lines changed

3 files changed

+40
-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: 34 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,12 @@
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()
4141
@Designate(ocd=AcToolStartupHookServiceImpl.Config.class)
42-
public class AcToolStartupHookServiceImpl {
42+
public class AcToolStartupHookServiceImpl implements SlingRepositoryInitializer {
4343
private static final Logger LOG = LoggerFactory.getLogger(AcToolStartupHookServiceImpl.class);
4444

4545
@ObjectClassDefinition(name = "AC Tool Startup Hook", description = "Applies AC Tool config automatically upon startup (depending on configuration/runtime)")
@@ -55,53 +55,21 @@ public enum StartupHookActivation {
5555
@Reference(policyOption = ReferencePolicyOption.GREEDY)
5656
private AcInstallationService acInstallationService;
5757

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

6361
@Activate
6462
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-
63+
activationMode = config.activationMode();
9664
}
9765

98-
private List<String> getRelevantPathsForInstallation() throws RepositoryException {
66+
private List<String> getRelevantPathsForInstallation(SlingRepository repository) throws RepositoryException {
9967
Session session = null;
10068
try {
10169
session = repository.loginService(null, null);
10270

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

10674
if(!isCompositeNodeStore) {
10775
return Collections.emptyList();
@@ -118,7 +86,7 @@ private List<String> getRelevantPathsForInstallation() throws RepositoryExceptio
11886
AccessControlConstants.REP_REPO_POLICY).contains(node.getName())) {
11987
continue;
12088
}
121-
if (isCompositeNodeStore && Arrays.asList("apps", "libs").contains(node.getName())) {
89+
if (Arrays.asList("apps", "libs").contains(node.getName())) {
12290
continue;
12391
}
12492
relevantPathsForInstallation.add(node.getPath());
@@ -139,7 +107,7 @@ private List<String> getRelevantPathsForInstallation() throws RepositoryExceptio
139107
}
140108
}
141109

142-
private void copyAcHistoryToOrFromApps(boolean isCloudReady) {
110+
private void copyAcHistoryToOrFromApps(SlingRepository repository, boolean isCloudReady) {
143111

144112
if(isCloudReady) {
145113
Session session = null;
@@ -178,4 +146,28 @@ private void copyAcHistoryToOrFromApps(boolean isCloudReady) {
178146

179147
}
180148

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

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)