RolloutConfigManagerFactoryImpl.java
19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.LabeledResource
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.msm.api.ActionConfig
* com.day.cq.wcm.msm.api.LiveAction
* com.day.cq.wcm.msm.api.LiveActionFactory
* com.day.cq.wcm.msm.api.RolloutConfig
* com.day.cq.wcm.msm.api.RolloutConfigManager
* com.day.cq.wcm.msm.api.RolloutManager
* com.day.cq.wcm.msm.api.RolloutManager$Trigger
* com.day.text.Text
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.observation.EventIterator
* javax.jcr.observation.EventListener
* javax.jcr.observation.ObservationManager
* org.apache.commons.collections.map.ListOrderedMap
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.adapter.AdapterFactory
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceReference
* org.osgi.service.component.ComponentContext
* org.osgi.util.tracker.ServiceTracker
* org.osgi.util.tracker.ServiceTrackerCustomizer
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl;
import com.day.cq.commons.LabeledResource;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.ActionConfig;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveActionFactory;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutConfigManager;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.RolloutConfigManagerFactory;
import com.day.cq.wcm.msm.impl.actions.ActionConfigImpl;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(name="com.day.cq.wcm.msm.impl.RolloutConfigManagerFactoryImpl", label="%rolloutconfigmgr.name", description="%rolloutconfigmgr.description", metatype=0, immediate=1)
@Service(value={AdapterFactory.class, RolloutConfigManagerFactory.class})
public class RolloutConfigManagerFactoryImpl
implements AdapterFactory,
RolloutConfigManagerFactory,
EventListener {
@Reference
private ResourceResolverFactory resolverFactory = null;
@Property(name="adapters")
public static final String[] ADAPTER_CLASSES = new String[]{RolloutConfigManager.class.getName()};
@Property(name="adaptables")
public static final String[] ADAPTABLE_CLASSES = new String[]{ResourceResolver.class.getName()};
private final Logger log = LoggerFactory.getLogger(RolloutConfigManagerFactoryImpl.class);
static final String CONFIG_ROOT = "/etc/msm/rolloutconfigs";
static final String CONFIG_RESOURCE_TYPE = "wcm/msm/components/rolloutconfig";
static final String PN_ACTION_FACTORY = "cq:liveActionFactory";
static final String PN_TRIGGER = "cq:trigger";
static final String NT_LIVE_SYNC_ACTION = "cq:LiveSyncAction";
private Lock lock = new ReentrantLock();
private ListOrderedMap guardedConfigs;
private ResourceResolver serviceResolver;
private ServiceTracker liveActionFactoryTracker;
private int modCount = 0;
LiveActionFactoryIndex index;
@Activate
protected void activate(ComponentContext context) throws Exception {
this.serviceResolver = this.resolverFactory.getServiceResourceResolver(Collections.emptyMap());
Session adminSession = (Session)this.serviceResolver.adaptTo(Session.class);
if (adminSession == null || adminSession.getWorkspace().getObservationManager() == null) {
throw new IllegalArgumentException("Need a Repository with Observation to run");
}
adminSession.getWorkspace().getObservationManager().addEventListener((EventListener)this, 63, "/etc/msm/rolloutconfigs", true, null, null, false);
BundleContext bundleContext = context.getBundleContext();
this.index = new LiveActionFactoryIndex(bundleContext);
this.liveActionFactoryTracker = new ServiceTracker(bundleContext, LiveActionFactory.class.getName(), (ServiceTrackerCustomizer)this.index);
this.liveActionFactoryTracker.open();
}
@Deactivate
protected void deactivate(ComponentContext context) throws RepositoryException {
this.liveActionFactoryTracker.close();
if (this.serviceResolver != null && this.serviceResolver.isLive()) {
ObservationManager om;
Session adminSession = (Session)this.serviceResolver.adaptTo(Session.class);
if (adminSession != null && (om = adminSession.getWorkspace().getObservationManager()) != null) {
om.removeEventListener((EventListener)this);
}
this.serviceResolver.close();
}
this.flushGuardedConfigs();
}
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (RolloutConfigManager.class.isAssignableFrom(type) && ResourceResolver.class.isInstance(adaptable)) {
return (AdapterType)this.create((ResourceResolver)adaptable);
}
return null;
}
public void onEvent(EventIterator events) {
if (events.hasNext()) {
this.flushGuardedConfigs();
}
}
@Override
public RolloutConfigManager create(ResourceResolver resolver) {
return new RolloutConfigManagerImpl(resolver);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private List<String> getIndex() {
this.lock.lock();
ArrayList<String> ret = new ArrayList<String>();
try {
if (this.guardedConfigs == null) {
this.serviceResolver.refresh();
this.guardedConfigs = new ListOrderedMap();
Resource root = this.serviceResolver.getResource("/etc/msm/rolloutconfigs");
if (root != null) {
this.guardedConfigs = this.loadChildren(root.listChildren(), this.guardedConfigs);
}
}
ret.addAll(this.guardedConfigs.keySet());
}
finally {
this.lock.unlock();
}
return ret;
}
private ListOrderedMap loadChildren(Iterator<Resource> children, ListOrderedMap ret) {
while (children.hasNext()) {
Resource child = children.next();
if (child.isResourceType("wcm/msm/components/rolloutconfig")) {
try {
RolloutConfigImpl cfg = this.load(child, ret.size());
if (cfg != null) {
ret.put((Object)cfg.getPath(), (Object)cfg);
}
}
catch (WCMException e) {
this.log.error("Failed to load RolloutConfig at {}: {}", (Object)child.getPath(), (Object)e);
}
}
ret = this.loadChildren(child.listChildren(), ret);
}
return ret;
}
private RolloutConfigImpl load(Resource config, int priority) throws WCMException {
String prop;
Iterator children = config.listChildren();
if (children == null || !children.hasNext()) {
this.log.warn("Found invalid config at {}, config does not have an LiveAction configured", (Object)config.getPath());
return null;
}
RolloutManager.Trigger trigger = null;
ValueMap map = (ValueMap)config.adaptTo(ValueMap.class);
if (map != null && (prop = (String)map.get("cq:trigger", null)) != null) {
try {
trigger = RolloutManager.Trigger.valueOf((String)prop);
}
catch (IllegalArgumentException e) {
try {
trigger = RolloutManager.Trigger.fromName((String)prop);
}
catch (IllegalArgumentException e2) {
this.log.warn("Config contains illegal Name for Trigger {} : ignore", (Object)prop);
}
}
}
if (trigger == null) {
this.log.warn("Found invalid config at {}, config does not have a Trigger", (Object)config.getPath());
return null;
}
ArrayList<LiveAction> actions = new ArrayList<LiveAction>();
while (children.hasNext()) {
Resource resource = (Resource)children.next();
String factId = (String)((ValueMap)resource.adaptTo(ValueMap.class)).get("cq:liveActionFactory", (Object)resource.getName());
LiveActionFactory factory = this.index.getFactory(factId);
if (factory == null) {
this.log.error("RolloutConfig {} is invalid: Failed to resolve Factory with id {}, maybe Service is uninstalled", (Object)config.getPath(), (Object)factId);
return null;
}
try {
actions.add(factory.createAction(resource));
continue;
}
catch (WCMException e) {
this.log.error("RolloutConfig {} is invalid: Failed to create Action {}", (Object)config.getPath(), (Object)e.getMessage());
return null;
}
}
return new RolloutConfigImpl(config, trigger, actions.toArray((T[])new LiveAction[actions.size()]), priority);
}
private int getModCount() {
return this.modCount;
}
private void flushGuardedConfigs() {
this.lock.lock();
this.guardedConfigs = null;
++this.modCount;
this.lock.unlock();
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
class LiveActionFactoryIndex
implements ServiceTrackerCustomizer {
private final ConcurrentHashMap<String, LiveActionFactory> factories;
private final BundleContext bundleContext;
private LiveActionFactoryIndex(BundleContext bundleContext) {
this.factories = new ConcurrentHashMap();
this.bundleContext = bundleContext;
}
public Object addingService(ServiceReference serviceReference) {
String[] names = PropertiesUtil.toStringArray((Object)serviceReference.getProperty("liveActionName"));
if (names != null) {
LiveActionFactory fact = (LiveActionFactory)this.bundleContext.getService(serviceReference);
if (fact != null) {
for (String name : names) {
this.factories.put(name, fact);
}
}
return fact;
}
return null;
}
public void modifiedService(ServiceReference serviceReference, Object o) {
this.removedService(serviceReference, o);
this.addingService(serviceReference);
}
public void removedService(ServiceReference serviceReference, Object o) {
String[] names = PropertiesUtil.toStringArray((Object)serviceReference.getProperty("liveActionName"));
if (names != null) {
for (String name : names) {
this.factories.remove(name);
}
RolloutConfigManagerFactoryImpl.this.flushGuardedConfigs();
}
}
public LiveActionFactory getFactory(String name) {
return this.factories.get(name);
}
public int size() {
return this.factories.size();
}
void addAll(HashMap<String, LiveActionFactory> factories) {
factories.putAll(factories);
}
}
private static final class RolloutConfigImpl
implements RolloutConfig,
Comparable<RolloutConfigImpl> {
private final RolloutManager.Trigger trigger;
private final int priority;
private final String path;
private final Resource resource;
private final List<LiveAction> actions;
private LabeledResource delegatee;
RolloutConfigImpl(Resource config, RolloutManager.Trigger trigger, LiveAction[] liveActions, int priority) {
this.resource = config;
this.trigger = trigger;
this.priority = priority;
this.path = config.getName().endsWith("jcr:content") ? Text.getRelativeParent((String)config.getPath(), (int)1) : config.getPath();
this.actions = Collections.unmodifiableList(Arrays.asList(liveActions));
}
public RolloutManager.Trigger getTrigger() {
return this.trigger;
}
public List<LiveAction> getActions() {
return this.actions;
}
public Set<ActionConfig> getActionsConfig() {
TreeSet<ActionConfig> ret = new TreeSet<ActionConfig>();
int rank = 0;
Iterator children = this.resource.listChildren();
int i = 0;
while (children.hasNext()) {
LiveAction action;
Resource child = (Resource)children.next();
if ((action = this.actions.get(i++)) == null) continue;
HashMap<String, String> conf = new HashMap<String, String>();
ValueMap map = (ValueMap)child.adaptTo(ValueMap.class);
for (String key : map.keySet()) {
conf.put(key, (String)map.get(key, String.class));
}
ret.add(new ActionConfigImpl(action.getName(), action.getParameterName(), conf, rank++));
}
return ret;
}
public void write(JSONWriter jsonWriter) throws JSONException {
jsonWriter.object();
jsonWriter.key("path").value((Object)this.getPath());
jsonWriter.key("name").value((Object)this.getName());
jsonWriter.key("title").value((Object)this.getTitle());
jsonWriter.key("trigger").value((Object)this.getTrigger());
jsonWriter.key("actions");
jsonWriter.array();
for (LiveAction ac : this.getActions()) {
jsonWriter.object();
jsonWriter.key("name").value((Object)ac.getName());
jsonWriter.key("title").value((Object)ac.getTitle());
jsonWriter.endObject();
}
jsonWriter.endArray();
jsonWriter.endObject();
}
public String getPath() {
return this.path;
}
public String getName() {
return this.getDelegatee() != null ? this.getDelegatee().getName() : null;
}
public String getTitle() {
return this.getDelegatee() != null ? this.getDelegatee().getTitle() : null;
}
public String getDescription() {
return this.getDelegatee() != null ? this.getDelegatee().getDescription() : null;
}
@Override
public int compareTo(RolloutConfigImpl o) {
return this.priority - o.priority;
}
public int hashCode() {
return this.priority;
}
public boolean equals(Object obj) {
return obj instanceof RolloutConfigImpl && ((RolloutConfigImpl)obj).path.equals(this.path);
}
private LabeledResource getDelegatee() {
if (this.delegatee == null) {
this.delegatee = (LabeledResource)this.resource.adaptTo(LabeledResource.class);
}
return this.delegatee;
}
}
private final class RolloutConfigManagerImpl
implements RolloutConfigManager {
private final ResourceResolver resolver;
private int modCount;
private List<String> index;
private Map<String, RolloutConfigImpl> readConfigs;
private RolloutConfigManagerImpl(ResourceResolver resolver) {
this.modCount = -1;
this.readConfigs = new HashMap<String, RolloutConfigImpl>();
this.resolver = resolver;
}
public RolloutConfig getRolloutConfig(String path) throws WCMException {
Resource resource;
int idx;
if (!Text.isDescendant((String)"/etc/msm/rolloutconfigs", (String)path)) {
return null;
}
List<String> all = this.getPathIndex();
RolloutConfigImpl config = this.readConfigs.get(path);
if (config == null && (idx = all.indexOf(path)) > -1 && (resource = this.resolver.getResource(path)) != null) {
if (resource.getChild("jcr:content") != null) {
resource = resource.getChild("jcr:content");
}
config = RolloutConfigManagerFactoryImpl.this.load(resource, idx);
this.readConfigs.put(path, config);
}
return config;
}
public Set<String> getRolloutConfigs() throws WCMException {
LinkedHashSet<String> ret = new LinkedHashSet<String>();
for (String path : this.getPathIndex()) {
if (this.getRolloutConfig(path) == null) continue;
ret.add(path);
}
return ret;
}
private List<String> getPathIndex() {
if (RolloutConfigManagerFactoryImpl.this.getModCount() != this.modCount) {
this.readConfigs.clear();
this.index = RolloutConfigManagerFactoryImpl.this.getIndex();
this.modCount = RolloutConfigManagerFactoryImpl.this.getModCount();
}
return this.index;
}
}
}