21
21
import org .apache .jackrabbit .JcrConstants ;
22
22
import org .apache .jackrabbit .oak .spi .security .authorization .accesscontrol .AccessControlConstants ;
23
23
import org .apache .sling .jcr .api .SlingRepository ;
24
+ import org .apache .sling .jcr .api .SlingRepositoryInitializer ;
24
25
import org .osgi .framework .BundleContext ;
25
26
import org .osgi .service .component .annotations .Activate ;
26
27
import org .osgi .service .component .annotations .Component ;
33
34
import org .slf4j .LoggerFactory ;
34
35
35
36
import biz .netcentric .cq .tools .actool .api .AcInstallationService ;
36
- import biz .netcentric .cq .tools .actool .helper .Constants ;
37
37
import biz .netcentric .cq .tools .actool .helper .runtime .RuntimeHelper ;
38
38
import biz .netcentric .cq .tools .actool .history .impl .HistoryUtils ;
39
39
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
41
42
@ Designate (ocd =AcToolStartupHookServiceImpl .Config .class )
42
- public class AcToolStartupHookServiceImpl {
43
+ public class AcToolStartupHookServiceImpl implements SlingRepositoryInitializer {
43
44
private static final Logger LOG = LoggerFactory .getLogger (AcToolStartupHookServiceImpl .class );
44
45
45
46
@ 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 {
55
56
@ Reference (policyOption = ReferencePolicyOption .GREEDY )
56
57
private AcInstallationService acInstallationService ;
57
58
58
- @ Reference (policyOption = ReferencePolicyOption .GREEDY )
59
- private SlingRepository repository ;
60
-
61
59
private boolean isCompositeNodeStore ;
60
+ private Config .StartupHookActivation activationMode ;
62
61
63
62
@ Activate
64
63
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 ();
96
65
}
97
66
98
- private List <String > getRelevantPathsForInstallation () throws RepositoryException {
67
+ private List <String > getRelevantPathsForInstallation (SlingRepository repository ) throws RepositoryException {
99
68
Session session = null ;
100
69
try {
101
70
session = repository .loginService (null , null );
102
71
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 );
105
74
106
75
if (!isCompositeNodeStore ) {
107
76
return Collections .emptyList ();
@@ -118,7 +87,7 @@ private List<String> getRelevantPathsForInstallation() throws RepositoryExceptio
118
87
AccessControlConstants .REP_REPO_POLICY ).contains (node .getName ())) {
119
88
continue ;
120
89
}
121
- if (isCompositeNodeStore && Arrays .asList ("apps" , "libs" ).contains (node .getName ())) {
90
+ if (Arrays .asList ("apps" , "libs" ).contains (node .getName ())) {
122
91
continue ;
123
92
}
124
93
relevantPathsForInstallation .add (node .getPath ());
@@ -139,7 +108,7 @@ private List<String> getRelevantPathsForInstallation() throws RepositoryExceptio
139
108
}
140
109
}
141
110
142
- private void copyAcHistoryToOrFromApps (boolean isCloudReady ) {
111
+ private void copyAcHistoryToOrFromApps (SlingRepository repository , boolean isCloudReady ) {
143
112
144
113
if (isCloudReady ) {
145
114
Session session = null ;
@@ -178,4 +147,28 @@ private void copyAcHistoryToOrFromApps(boolean isCloudReady) {
178
147
179
148
}
180
149
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
+
181
174
}
0 commit comments