ServiceTrackerImpl.java
8.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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* 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.osgi.framework.ServiceReference
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.notification.impl;
import com.day.cq.wcm.notification.Channel;
import com.day.cq.wcm.notification.Subscription;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
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.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, componentAbstract=1)
@References(value={@Reference(name="channel", referenceInterface=Channel.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC), @Reference(name="subscription", referenceInterface=Subscription.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)})
public abstract class ServiceTrackerImpl {
protected final Logger logger;
private static final String PROPERTY_TYPE = "type";
private final Map<String, Channel> channels;
private final Map<String, String> channelsByName;
private final Map<String, Subscription> subscriptions;
private final Map<String, String> subscriptionsByName;
protected ComponentContext context;
protected final Map<String, ServiceDesc> unhandledServices;
public ServiceTrackerImpl() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.channels = new HashMap<String, Channel>();
this.channelsByName = new HashMap<String, String>();
this.subscriptions = new HashMap<String, Subscription>();
this.subscriptionsByName = new HashMap<String, String>();
this.unhandledServices = new HashMap<String, ServiceDesc>();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void activate(ComponentContext context) {
ArrayList<ServiceDesc> refs;
Map<String, ServiceDesc> map = this.unhandledServices;
synchronized (map) {
this.context = context;
refs = new ArrayList<ServiceDesc>(this.unhandledServices.values());
this.unhandledServices.clear();
}
for (ServiceDesc ref : refs) {
this.register(ref.type, ref.ref);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void deactivate(ComponentContext context) {
Map<String, ServiceDesc> map = this.unhandledServices;
synchronized (map) {
this.context = null;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindChannel(ServiceReference ref) {
Map<String, ServiceDesc> map = this.unhandledServices;
synchronized (map) {
if (this.context == null) {
ServiceDesc desc = new ServiceDesc();
desc.type = ServiceDescType.CHANNEL;
desc.ref = ref;
this.unhandledServices.put((String)ref.getProperty("service.pid"), desc);
} else {
this.register(ServiceDescType.CHANNEL, ref);
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindSubscription(ServiceReference ref) {
Map<String, ServiceDesc> map = this.unhandledServices;
synchronized (map) {
if (this.context == null) {
ServiceDesc desc = new ServiceDesc();
desc.type = ServiceDescType.SUBSCRIPTION;
desc.ref = ref;
this.unhandledServices.put((String)ref.getProperty("service.pid"), desc);
} else {
this.register(ServiceDescType.SUBSCRIPTION, ref);
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindChannel(ServiceReference ref) {
Map<String, ServiceDesc> map = this.unhandledServices;
synchronized (map) {
this.unregister(ServiceDescType.CHANNEL, ref);
this.unhandledServices.remove(ref.getProperty("service.pid"));
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindSubscription(ServiceReference ref) {
Map<String, ServiceDesc> map = this.unhandledServices;
synchronized (map) {
this.unregister(ServiceDescType.SUBSCRIPTION, ref);
this.unhandledServices.remove(ref.getProperty("service.pid"));
}
}
protected void register(ServiceDescType type, ServiceReference ref) {
String key = (String)ref.getProperty("service.pid");
String name = (String)ref.getProperty("type");
if (name == null) {
this.logger.error("Service " + (Object)ref + " is missing required property " + "type");
return;
}
Object service = null;
switch (type) {
case CHANNEL: {
service = this.context.locateService(type.getName(), ref);
if (service == null) break;
this.channels.put(key, (Channel)service);
this.channelsByName.put(name, key);
break;
}
case SUBSCRIPTION: {
service = this.context.locateService(type.getName(), ref);
if (service == null) break;
this.subscriptions.put(key, (Subscription)service);
this.subscriptionsByName.put(name, key);
}
}
if (service != null) {
this.logger.debug("Registering service {} : {}", (Object)key, service);
} else {
this.logger.error("Unable to register service {} for {}.", (Object)key, (Object)ref);
}
}
protected void unregister(ServiceDescType type, ServiceReference ref) {
String key = (String)ref.getProperty("service.pid");
String name = (String)ref.getProperty("type");
if (name == null) {
return;
}
switch (type) {
case CHANNEL: {
this.channels.remove(key);
if (!key.equals(this.channelsByName.get(name))) break;
this.channelsByName.remove(name);
break;
}
case SUBSCRIPTION: {
this.subscriptions.remove(key);
if (!key.equals(this.subscriptionsByName.get(name))) break;
this.subscriptionsByName.remove(name);
}
}
this.logger.debug("Unregistering service {}.", (Object)key);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public Channel getChannel(String name) {
Map<String, ServiceDesc> map = this.unhandledServices;
synchronized (map) {
String serviceId = this.channelsByName.get(name);
if (serviceId != null) {
return this.channels.get(serviceId);
}
return null;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public Subscription getSubscription(String name) {
Map<String, ServiceDesc> map = this.unhandledServices;
synchronized (map) {
String serviceId = this.subscriptionsByName.get(name);
if (serviceId != null) {
return this.subscriptions.get(serviceId);
}
return null;
}
}
protected Set<String> getSubscriptionNames() {
return this.subscriptionsByName.keySet();
}
protected static enum ServiceDescType {
CHANNEL("channel"),
SUBSCRIPTION("subscription");
private final String name;
private ServiceDescType(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
protected static final class ServiceDesc {
public ServiceDescType type;
public ServiceReference ref;
protected ServiceDesc() {
}
}
}