MCMFacadeImpl.java
23 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* 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.mcm.impl;
import com.day.cq.mcm.api.Experience;
import com.day.cq.mcm.api.MCMFacade;
import com.day.cq.mcm.api.MCMPlugin;
import com.day.cq.mcm.api.MCMResourceType;
import com.day.cq.mcm.api.Touchpoint;
import com.day.cq.mcm.impl.DirectPluginProvider;
import com.day.cq.mcm.impl.IndirectPluginProvider;
import com.day.cq.mcm.impl.MCMConfiguration;
import com.day.cq.mcm.impl.PluginProvider;
import com.day.cq.mcm.util.NormalizedResource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
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.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
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(metatype=0)
@Service(value={MCMFacade.class})
@Reference(name="MCMPlugin", referenceInterface=MCMPlugin.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
@Properties(value={@Property(name="service.description", value={"MCM Facade. An extension point for MCM plugins and provider of methods working on multiple plugins."})})
public class MCMFacadeImpl
implements MCMFacade {
private static final Logger log = LoggerFactory.getLogger(MCMFacadeImpl.class);
private HashMap<String, PluginProvider> experienceComponentTypesToPlugins = new HashMap();
private HashMap<String, PluginProvider> touchpointComponentTypesToPlugins = new HashMap();
private Set<MCMPlugin> plugins = Collections.synchronizedSet(new HashSet());
private ServiceTracker configurationServiceTracker;
private BundleContext bundleContext;
private HashMap<Object, MCMConfiguration> implementedConfigurations = new HashMap();
@Override
public MCMPlugin getPlugin(Resource r) {
String typeString;
NormalizedResource nr = new NormalizedResource();
nr.setResource(r);
MCMPlugin retval = null;
if (nr.getResource() != null && (typeString = (String)nr.getContentVals().get("sling:resourceType", String.class)) != null) {
retval = this.getPluginForType(typeString);
}
return retval;
}
@Override
public Collection<MCMPlugin> getPlugins() {
return this.plugins;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public Collection<String> getRegisteredExperienceComponents() {
HashMap<String, PluginProvider> hashMap = this.experienceComponentTypesToPlugins;
synchronized (hashMap) {
return new ArrayList<String>(this.experienceComponentTypesToPlugins.keySet());
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public Collection<String> getRegisteredTouchpointComponents() {
HashMap<String, PluginProvider> hashMap = this.touchpointComponentTypesToPlugins;
synchronized (hashMap) {
return new ArrayList<String>(this.touchpointComponentTypesToPlugins.keySet());
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public MCMResourceType getMCMType(String resourceTypeToTest) {
MCMResourceType retval = null;
HashMap<String, PluginProvider> hashMap = this.experienceComponentTypesToPlugins;
synchronized (hashMap) {
if (this.experienceComponentTypesToPlugins.containsKey(resourceTypeToTest)) {
retval = MCMResourceType.Experience;
}
}
if (retval == null) {
hashMap = this.touchpointComponentTypesToPlugins;
synchronized (hashMap) {
if (this.touchpointComponentTypesToPlugins.containsKey(resourceTypeToTest)) {
retval = MCMResourceType.Touchpoint;
}
}
}
return retval;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public MCMPlugin getPluginForType(String typeString) {
PluginProvider pp = null;
HashMap<String, PluginProvider> hashMap = this.experienceComponentTypesToPlugins;
synchronized (hashMap) {
pp = this.experienceComponentTypesToPlugins.get(typeString);
}
if (pp != null) {
return pp.getPlugin();
}
hashMap = this.touchpointComponentTypesToPlugins;
synchronized (hashMap) {
pp = this.touchpointComponentTypesToPlugins.get(typeString);
}
if (pp != null) {
return pp.getPlugin();
}
return null;
}
@Override
public Iterator<Experience> findExperiences(ResourceResolver rr, Touchpoint t) {
final MCMPlugin plugin = t.getPlugin();
String additionalPropertyCondition = t.getXPathAdditionToMatchTouchpointExperience();
final Iterator<Resource> experiences = this.findResources(rr, plugin.getExperienceRootComponents(), "/content", additionalPropertyCondition);
return new Iterator<Experience>(){
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public Experience next() {
return plugin.makeExperience((Resource)experiences.next());
}
@Override
public boolean hasNext() {
return experiences.hasNext();
}
};
}
@Override
public Iterator<Resource> findResources(ResourceResolver rr, Collection<String> rootComponents, String path, String additionalPropertyCondition) {
if (rootComponents.size() == 0) {
return new ArrayList().iterator();
}
StringBuilder query = new StringBuilder("/jcr:root");
String cleanedPath = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
query.append(cleanedPath);
query.append("//*[");
boolean first = true;
for (String comp : rootComponents) {
if (first) {
query.append("(");
}
if (!first) {
query.append(" or ");
}
first = false;
query.append("@sling:resourceType = ");
query.append("'").append(comp).append("'");
}
if (!first) {
query.append(")");
}
if (additionalPropertyCondition != null && !"".equals(additionalPropertyCondition)) {
if (!first) {
query.append(" and ");
}
query.append("(");
query.append(additionalPropertyCondition);
query.append(")");
}
query.append("]");
String queryString = query.toString();
Iterator result = rr.findResources(queryString, "xpath");
return result;
}
@Activate
protected void activate(ComponentContext context) {
this.bundleContext = context.getBundleContext();
this.configurationServiceTracker = new ServiceTracker(context.getBundleContext(), MCMConfiguration.class.getName(), (ServiceTrackerCustomizer)new ConfigurationSTCustomizer());
this.configurationServiceTracker.open();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Deactivate
protected void deactivate(ComponentContext context) {
if (this.configurationServiceTracker != null) {
this.configurationServiceTracker.close();
this.configurationServiceTracker = null;
}
this.plugins.clear();
HashMap<String, PluginProvider> hashMap = this.experienceComponentTypesToPlugins;
synchronized (hashMap) {
this.experienceComponentTypesToPlugins.clear();
}
hashMap = this.touchpointComponentTypesToPlugins;
synchronized (hashMap) {
this.touchpointComponentTypesToPlugins.clear();
}
this.bundleContext = null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindMCMPlugin(MCMPlugin plugin) {
log.info("Binding plugin {} to MCMFacadeImpl", (Object)plugin);
try {
HashMap<String, PluginProvider> hashMap;
String m;
DirectPluginProvider dpp;
if (!this.plugins.add(plugin)) {
String m2 = "OSGi passed '" + plugin + "' twice... ";
throw new RuntimeException(m2);
}
for (String type2 : plugin.getExperienceRootComponents()) {
hashMap = this.experienceComponentTypesToPlugins;
synchronized (hashMap) {
if (this.experienceComponentTypesToPlugins.containsKey(type2)) {
m = "MCMPlugin '" + plugin + "' tried to register Experience type " + type2 + ", which is already registered. " + "Aborting bind!";
throw new RuntimeException(m);
}
dpp = new DirectPluginProvider();
dpp.setPlugin(plugin);
this.experienceComponentTypesToPlugins.put(type2, dpp);
continue;
}
}
for (String type2 : plugin.getTouchpointRootComponents()) {
hashMap = this.touchpointComponentTypesToPlugins;
synchronized (hashMap) {
if (this.touchpointComponentTypesToPlugins.containsKey(type2)) {
m = "MCMPlugin '" + plugin + "' tried to register Touchpoint type " + type2 + ", which is already registered. " + "Aborting bind!";
throw new RuntimeException(m);
}
dpp = new DirectPluginProvider();
dpp.setPlugin(plugin);
this.touchpointComponentTypesToPlugins.put(type2, dpp);
continue;
}
}
}
catch (Exception e) {
log.error("Failed to bind MCMPlugin " + plugin + ".", (Throwable)e);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void addExperienceIndirection(String fromCompType, String toCompType) {
IndirectPluginProvider ipp = new IndirectPluginProvider();
ipp.setComponentTypeToProvideFrom(toCompType);
ipp.setFac(this);
HashMap<String, PluginProvider> hashMap = this.experienceComponentTypesToPlugins;
synchronized (hashMap) {
if (this.experienceComponentTypesToPlugins.containsKey(fromCompType)) {
log.error("Cannot add indirection from " + fromCompType + " to " + toCompType + ", " + "component type " + fromCompType + " already registered.");
} else {
this.experienceComponentTypesToPlugins.put(fromCompType, ipp);
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void removeExperienceIndirection(String fromCompType) {
boolean notIndirectProvider = false;
HashMap<String, PluginProvider> hashMap = this.experienceComponentTypesToPlugins;
synchronized (hashMap) {
PluginProvider pp = this.experienceComponentTypesToPlugins.get(fromCompType);
if (pp != null && !(pp instanceof IndirectPluginProvider)) {
notIndirectProvider = true;
} else if (pp != null) {
this.experienceComponentTypesToPlugins.remove(fromCompType);
}
}
if (notIndirectProvider) {
String m = "Trying to remove indirection at '" + fromCompType + "' but is not an indirection! Nothing removed.";
log.error(m);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void removeTouchpointIndirection(String fromCompType) {
boolean notIndirectProvider = false;
HashMap<String, PluginProvider> hashMap = this.touchpointComponentTypesToPlugins;
synchronized (hashMap) {
PluginProvider pp = this.touchpointComponentTypesToPlugins.get(fromCompType);
if (pp != null && !(pp instanceof IndirectPluginProvider)) {
notIndirectProvider = true;
} else if (pp != null) {
this.touchpointComponentTypesToPlugins.remove(fromCompType);
}
}
if (notIndirectProvider) {
String m = "Trying to remove indirection at '" + fromCompType + "' but is not an indirection! Nothing removed.";
log.error(m);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void addTouchpointIndirection(String fromCompType, String toCompType) {
IndirectPluginProvider ipp = new IndirectPluginProvider();
ipp.setComponentTypeToProvideFrom(toCompType);
ipp.setFac(this);
HashMap<String, PluginProvider> hashMap = this.touchpointComponentTypesToPlugins;
synchronized (hashMap) {
if (this.touchpointComponentTypesToPlugins.containsKey(fromCompType)) {
log.error("Cannot add indirection from " + fromCompType + " to " + toCompType + ", " + "component type " + fromCompType + " already registered.");
} else {
this.touchpointComponentTypesToPlugins.put(fromCompType, ipp);
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindMCMPlugin(MCMPlugin plugin) {
log.info("Unbinding plugin {} from MCMFacadeImpl", (Object)plugin);
this.plugins.remove(plugin);
HashMap<String, PluginProvider> hashMap = this.experienceComponentTypesToPlugins;
synchronized (hashMap) {
for (String type : plugin.getExperienceRootComponents()) {
this.experienceComponentTypesToPlugins.remove(type);
}
}
hashMap = this.touchpointComponentTypesToPlugins;
synchronized (hashMap) {
for (String type : plugin.getTouchpointRootComponents()) {
this.touchpointComponentTypesToPlugins.remove(type);
}
}
}
public class ConfigurationSTCustomizer
implements ServiceTrackerCustomizer {
public Object addingService(ServiceReference arg0) {
Object service = MCMFacadeImpl.this.bundleContext.getService(arg0);
MCMConfiguration newConfig = this.checkConfig(service);
if (newConfig != null) {
this.addConfig(arg0, newConfig);
return service;
}
return null;
}
private MCMConfiguration checkConfig(Object service) {
MCMConfiguration retval = null;
if (service instanceof MCMConfiguration) {
retval = (MCMConfiguration)service;
} else {
String m = "Tracking misconfigured, got " + service.getClass() + ", " + "and not a MCMConfiguration.";
log.error(m);
}
return retval;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void logConfig() {
if (log.isInfoEnabled()) {
String icM;
HashMap hashMap = MCMFacadeImpl.this.implementedConfigurations;
synchronized (hashMap) {
icM = MCMFacadeImpl.this.implementedConfigurations.toString();
}
log.info("Configuration references: {}", (Object)icM);
String m = "Configuration now: ";
HashMap hashMap2 = MCMFacadeImpl.this.experienceComponentTypesToPlugins;
synchronized (hashMap2) {
m = m + "Experiences: " + MCMFacadeImpl.this.experienceComponentTypesToPlugins.toString();
}
hashMap2 = MCMFacadeImpl.this.touchpointComponentTypesToPlugins;
synchronized (hashMap2) {
m = m + " Touchpoints: " + MCMFacadeImpl.this.touchpointComponentTypesToPlugins.toString();
}
log.info(m);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void addConfig(ServiceReference arg0, MCMConfiguration newConfig) {
log.info("Adding configuration {}", arg0.getProperty("service.pid"));
try {
MCMConfiguration clone = (MCMConfiguration)newConfig.clone();
if (clone.getExperienceIndirectionConfig() != null) {
MCMFacadeImpl.this.addExperienceIndirection(clone.getExperienceIndirectionConfig()[0], clone.getExperienceIndirectionConfig()[1]);
}
if (clone.getTouchpointIndirectionConfig() != null) {
MCMFacadeImpl.this.addTouchpointIndirection(clone.getTouchpointIndirectionConfig()[0], clone.getTouchpointIndirectionConfig()[1]);
}
HashMap hashMap = MCMFacadeImpl.this.implementedConfigurations;
synchronized (hashMap) {
if (null != MCMFacadeImpl.this.implementedConfigurations.put(arg0.getProperty("service.pid"), clone)) {
log.error("Configuration added twice: {}", arg0.getProperty("service.pid"));
}
}
}
catch (CloneNotSupportedException e) {
log.error("Configuration " + arg0.getProperty("service.pid") + " doesn't support clone!");
}
this.logConfig();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void modifiedService(ServiceReference arg0, Object arg1) {
log.info("Modifying configuration {}", arg0.getProperty("service.pid"));
try {
MCMConfiguration modConfig = this.checkConfig(MCMFacadeImpl.this.bundleContext.getService(arg0));
if (modConfig == null) {
throw new RuntimeException("Service not a configuration, cannot modify: " + (Object)arg0);
}
boolean update = false;
MCMConfiguration oldConfig = null;
HashMap hashMap = MCMFacadeImpl.this.implementedConfigurations;
synchronized (hashMap) {
oldConfig = (MCMConfiguration)MCMFacadeImpl.this.implementedConfigurations.get(arg0.getProperty("service.pid"));
if (oldConfig == null) {
String m = "Got modification notice for unimplemented configuration: " + arg0.getProperty("service.pid");
throw new RuntimeException(m);
}
if (!oldConfig.equals(modConfig)) {
update = true;
try {
modConfig = (MCMConfiguration)modConfig.clone();
MCMFacadeImpl.this.implementedConfigurations.put(arg0.getProperty("service.pid"), modConfig);
}
catch (CloneNotSupportedException e) {
throw new RuntimeException("Configuration cannot be cloned: " + arg0.getProperty("service.pid"));
}
}
}
if (update) {
if (oldConfig != null && oldConfig.getExperienceIndirectionConfig() != null) {
MCMFacadeImpl.this.removeExperienceIndirection(oldConfig.getExperienceIndirectionConfig()[0]);
}
if (oldConfig != null && oldConfig.getTouchpointIndirectionConfig() != null) {
MCMFacadeImpl.this.removeTouchpointIndirection(oldConfig.getTouchpointIndirectionConfig()[0]);
}
if (modConfig.getExperienceIndirectionConfig() != null) {
MCMFacadeImpl.this.addExperienceIndirection(modConfig.getExperienceIndirectionConfig()[0], modConfig.getExperienceIndirectionConfig()[1]);
}
if (modConfig.getTouchpointIndirectionConfig() != null) {
MCMFacadeImpl.this.addTouchpointIndirection(modConfig.getTouchpointIndirectionConfig()[0], modConfig.getTouchpointIndirectionConfig()[1]);
}
}
}
catch (Exception e) {
log.error("Modification of config failed.", (Throwable)e);
}
this.logConfig();
}
public void removedService(ServiceReference arg0, Object arg1) {
log.info("Removing service {}", arg0.getProperty("service.pid"));
MCMConfiguration remConfig = (MCMConfiguration)MCMFacadeImpl.this.implementedConfigurations.remove(arg0.getProperty("service.pid"));
if (remConfig != null) {
if (remConfig.getExperienceIndirectionConfig() != null) {
MCMFacadeImpl.this.removeExperienceIndirection(remConfig.getExperienceIndirectionConfig()[0]);
}
if (remConfig.getTouchpointIndirectionConfig() != null) {
MCMFacadeImpl.this.removeTouchpointIndirection(remConfig.getTouchpointIndirectionConfig()[0]);
}
} else {
log.warn("Configuration to be removed not present: {}", arg0.getProperty("service.pid"));
}
this.logConfig();
}
}
}