2121import org .apache .jackrabbit .JcrConstants ;
2222import org .apache .jackrabbit .oak .spi .security .authorization .accesscontrol .AccessControlConstants ;
2323import org .apache .sling .jcr .api .SlingRepository ;
24+ import org .apache .sling .jcr .api .SlingRepositoryInitializer ;
2425import org .osgi .framework .BundleContext ;
2526import org .osgi .service .component .annotations .Activate ;
2627import org .osgi .service .component .annotations .Component ;
3334import org .slf4j .LoggerFactory ;
3435
3536import biz .netcentric .cq .tools .actool .api .AcInstallationService ;
36- import biz .netcentric .cq .tools .actool .helper .Constants ;
3737import biz .netcentric .cq .tools .actool .helper .runtime .RuntimeHelper ;
3838import 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}
0 commit comments