ServiceTrackerImpl.java
9.56 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Session
* 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.commons.osgi.ServiceUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.replication.impl;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.ContentBuilder;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationContent;
import com.day.cq.replication.ReplicationContentFactory;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.TransportHandler;
import com.day.cq.replication.impl.ServiceTracker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.Session;
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.commons.osgi.ServiceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@References(value={@Reference(name="contentBuilder", referenceInterface=ContentBuilder.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC), @Reference(name="transportHandler", referenceInterface=TransportHandler.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)})
@Service(value={ServiceTracker.class})
public class ServiceTrackerImpl
implements ServiceTracker {
private final Logger logger;
private final Map<String, List<ContentBuilderDesc>> contentBuildersByName;
private final List<TransportHandlerDesc> transportHandlers;
public ServiceTrackerImpl() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.contentBuildersByName = new HashMap<String, List<ContentBuilderDesc>>();
this.transportHandlers = new ArrayList<TransportHandlerDesc>();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindContentBuilder(ContentBuilder builder, Map<String, Object> props) {
String name = PropertiesUtil.toString((Object)props.get("name"), (String)null);
if (name != null) {
Map<String, List<ContentBuilderDesc>> map = this.contentBuildersByName;
synchronized (map) {
List<ContentBuilderDesc> list = this.contentBuildersByName.get(name);
if (list == null) {
list = new ArrayList<ContentBuilderDesc>();
this.contentBuildersByName.put(name, list);
}
ContentBuilderDesc desc = new ContentBuilderDesc();
desc.contentBuilder = new ContentBuilderWrapper(builder);
desc.properties = props;
list.add(desc);
Collections.sort(list);
}
this.logger.debug("Registering content builder {} : {}", (Object)name, (Object)builder);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindTransportHandler(TransportHandler handler, Map<String, Object> props) {
List<TransportHandlerDesc> list = this.transportHandlers;
synchronized (list) {
TransportHandlerDesc desc = new TransportHandlerDesc();
desc.transportHandler = handler;
desc.properties = props;
this.transportHandlers.add(desc);
Collections.sort(this.transportHandlers);
}
this.logger.debug("Registering transport handler: {}", (Object)handler);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindContentBuilder(ContentBuilder builder, Map<String, Object> props) {
String name = PropertiesUtil.toString((Object)props.get("name"), (String)null);
if (name != null) {
Map<String, List<ContentBuilderDesc>> map = this.contentBuildersByName;
synchronized (map) {
Long key = (Long)props.get("service.id");
List<ContentBuilderDesc> list = this.contentBuildersByName.get(name);
if (list != null) {
Iterator<ContentBuilderDesc> i = list.iterator();
while (i.hasNext()) {
ContentBuilderDesc desc = i.next();
if (key != (Long)desc.properties.get("service.id")) continue;
i.remove();
break;
}
if (list.size() == 0) {
this.contentBuildersByName.remove(name);
}
}
}
}
this.logger.debug("Unregistering content builder {} : {}", (Object)name, (Object)builder);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindTransportHandler(TransportHandler handler, Map<String, Object> props) {
Long key = (Long)props.get("service.id");
List<TransportHandlerDesc> list = this.transportHandlers;
synchronized (list) {
Iterator<TransportHandlerDesc> i = this.transportHandlers.iterator();
while (i.hasNext()) {
TransportHandlerDesc desc = i.next();
if (key != (Long)desc.properties.get("service.id")) continue;
i.remove();
break;
}
this.transportHandlers.remove(key);
}
this.logger.debug("Unregistering transport handler: {}", (Object)handler);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public ContentBuilder getContentBuilder(String name) {
Map<String, List<ContentBuilderDesc>> map = this.contentBuildersByName;
synchronized (map) {
List<ContentBuilderDesc> list = this.contentBuildersByName.get(name);
if (list != null && list.size() > 0) {
return list.get((int)0).contentBuilder;
}
return null;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public TransportHandler getTransportHandler(AgentConfig config) {
List<TransportHandlerDesc> list = this.transportHandlers;
synchronized (list) {
for (TransportHandlerDesc desc : this.transportHandlers) {
if (!desc.transportHandler.canHandle(config)) continue;
return desc.transportHandler;
}
return null;
}
}
public class ContentBuilderWrapper
implements ContentBuilder {
private final ContentBuilder delegatee;
private final boolean hasNewMethod;
public ContentBuilderWrapper(ContentBuilder cb) {
this.delegatee = cb;
boolean hasMethod = false;
try {
this.delegatee.create(null, null, null, null);
hasMethod = true;
}
catch (AbstractMethodError var4_4) {
}
catch (Throwable t) {
hasMethod = true;
}
this.hasNewMethod = hasMethod;
}
@Override
public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory contentFactory) throws ReplicationException {
return this.delegatee.create(session, action, contentFactory);
}
@Override
public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory contentFactory, Map<String, Object> parameters) throws ReplicationException {
if (this.hasNewMethod) {
return this.delegatee.create(session, action, contentFactory, parameters);
}
return this.delegatee.create(session, action, contentFactory);
}
@Override
public String getName() {
return this.delegatee.getName();
}
@Override
public String getTitle() {
return this.delegatee.getTitle();
}
}
protected static final class TransportHandlerDesc
implements Comparable<TransportHandlerDesc> {
public TransportHandler transportHandler;
public Map<String, Object> properties;
protected TransportHandlerDesc() {
}
@Override
public int compareTo(TransportHandlerDesc o) {
Comparable tc = ServiceUtil.getComparableForServiceRanking(this.properties);
return - tc.compareTo(o.properties);
}
}
protected static final class ContentBuilderDesc
implements Comparable<ContentBuilderDesc> {
public ContentBuilderWrapper contentBuilder;
public Map<String, Object> properties;
protected ContentBuilderDesc() {
}
@Override
public int compareTo(ContentBuilderDesc o) {
Comparable tc = ServiceUtil.getComparableForServiceRanking(this.properties);
return - tc.compareTo(o.properties);
}
}
}