JobConsumerInventoryServiceImpl.java
4.63 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.References
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.PropertiesUtil
* org.apache.sling.event.jobs.consumer.JobConsumer
* org.apache.sling.event.jobs.consumer.JobExecutor
* org.osgi.framework.ServiceReference
*/
package com.adobe.granite.offloading.impl;
import com.adobe.granite.offloading.impl.JobConsumerInfo;
import com.adobe.granite.offloading.impl.JobConsumerInfoImpl;
import com.adobe.granite.offloading.impl.JobConsumerInventoryService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
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.References;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.apache.sling.event.jobs.consumer.JobExecutor;
import org.osgi.framework.ServiceReference;
@Component
@Service(value={JobConsumerInventoryService.class})
@References(value={@Reference(referenceInterface=JobConsumer.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC, name="jobConsumer"), @Reference(referenceInterface=JobExecutor.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC, name="jobExecutor")})
public class JobConsumerInventoryServiceImpl
implements JobConsumerInventoryService {
private Map<String, List<JobConsumerInfo>> topicToConsumersMap = new ConcurrentHashMap<String, List<JobConsumerInfo>>();
@Override
public Map<String, List<JobConsumerInfo>> getRegisteredConsumers() {
return this.topicToConsumersMap;
}
protected void bindJobExecutor(ServiceReference serviceReference) {
this.bindService(serviceReference, true);
}
protected void unbindJobExecutor(ServiceReference serviceReference) {
this.unbindService(serviceReference, true);
}
protected void bindJobConsumer(ServiceReference serviceReference) {
this.bindService(serviceReference, false);
}
protected void unbindJobConsumer(ServiceReference serviceReference) {
this.unbindService(serviceReference, false);
}
private void bindService(ServiceReference serviceReference, boolean isExecutor) {
String[] topics = isExecutor ? PropertiesUtil.toStringArray((Object)serviceReference.getProperty("job.topics")) : PropertiesUtil.toStringArray((Object)serviceReference.getProperty("job.topics"));
if (topics != null) {
for (String topic : topics) {
if (!StringUtils.isNotEmpty((String)topic)) continue;
List<JobConsumerInfo> consumers = this.topicToConsumersMap.get(topic = topic.trim());
if (consumers == null) {
consumers = new ArrayList<JobConsumerInfo>();
this.topicToConsumersMap.put(topic, consumers);
}
JobConsumerInfoImpl consumerInfo = new JobConsumerInfoImpl(serviceReference, isExecutor);
consumers.add(consumerInfo);
Collections.sort(consumers);
}
}
}
private void unbindService(ServiceReference serviceReference, boolean isExecutor) {
String[] topics = isExecutor ? PropertiesUtil.toStringArray((Object)serviceReference.getProperty("job.topics")) : PropertiesUtil.toStringArray((Object)serviceReference.getProperty("job.topics"));
if (topics != null) {
for (String topic : topics) {
if (!StringUtils.isNotEmpty((String)topic)) continue;
List<JobConsumerInfo> consumers = this.topicToConsumersMap.get(topic = topic.trim());
if (consumers != null) {
JobConsumerInfoImpl consumerInfo = new JobConsumerInfoImpl(serviceReference, isExecutor);
consumers.remove(consumerInfo);
Collections.sort(consumers);
}
if (consumers.size() != 0) continue;
this.topicToConsumersMap.remove(topic);
}
}
}
}