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 ()
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}
0 commit comments