Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Startup hook should implement SlingRepositoryInitializer to propagate #548

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion accesscontroltool-startuphook-bundle/bnd.bnd
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion accesscontroltool-startuphook-bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<artifactId>org.osgi.framework</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<dependency>
<groupId>javax.jcr</groupId>
<artifactId>jcr</artifactId>
<scope>provided</scope>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)")
Expand All @@ -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<String> 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<String> getRelevantPathsForInstallation() throws RepositoryException {
private List<String> 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();
Expand All @@ -117,7 +86,7 @@ private List<String> 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());
Expand All @@ -138,14 +107,14 @@ private List<String> 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)) {
Expand Down Expand Up @@ -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<String> relevantPathsForInstallation = getRelevantPathsForInstallation(repo);
LOG.info("Running AcTool with "
+ (relevantPathsForInstallation.isEmpty() ? "all paths" : "paths " + relevantPathsForInstallation) + "...");
acInstallationService.apply(null, relevantPathsForInstallation.toArray(new String[relevantPathsForInstallation.size()]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kwin during the repo initialisation process this method participates, IIRC the SlingRepository will not be available as service yet? (see

)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, this probably requires some refactoring of AcInstallationServiceImpl()

true);
LOG.info("AC Tool Startup Hook done.");

copyAcHistoryToOrFromApps(repo, isCloudReady);
} else {
LOG.debug("Skipping AcTool Startup Hook: activationMode: {} isCloudReady: {}", activationMode, isCloudReady);
}
}

}

This file was deleted.