ExtendedFactoryCache.java
10.5 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Component
* org.apache.sling.rewriter.GeneratorFactory
* org.apache.sling.rewriter.ProcessorFactory
* org.apache.sling.rewriter.SerializerFactory
* org.apache.sling.rewriter.TransformerFactory
* org.osgi.framework.BundleContext
* org.osgi.framework.Filter
* org.osgi.framework.InvalidSyntaxException
* org.osgi.framework.ServiceReference
* org.osgi.framework.ServiceRegistration
* org.osgi.service.component.ComponentContext
* org.osgi.service.component.ComponentFactory
* org.osgi.util.tracker.ServiceTracker
* org.osgi.util.tracker.ServiceTrackerCustomizer
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.rewriter.processor.impl;
import com.day.cq.rewriter.pipeline.Generator;
import com.day.cq.rewriter.pipeline.RewriterTransformerFactory;
import com.day.cq.rewriter.pipeline.Serializer;
import com.day.cq.rewriter.pipeline.Transformer;
import com.day.cq.rewriter.processor.Processor;
import com.day.cq.rewriter.processor.impl.GeneratorFactoryWrapper;
import com.day.cq.rewriter.processor.impl.ProcessorFactoryWrapper;
import com.day.cq.rewriter.processor.impl.RewriterTransformerFactoryWrapper;
import com.day.cq.rewriter.processor.impl.SerializerFactoryWrapper;
import com.day.cq.rewriter.processor.impl.TransformerFactoryWrapper;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.felix.scr.annotations.Component;
import org.apache.sling.rewriter.GeneratorFactory;
import org.apache.sling.rewriter.ProcessorFactory;
import org.apache.sling.rewriter.SerializerFactory;
import org.apache.sling.rewriter.TransformerFactory;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.ComponentFactory;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1)
public class ExtendedFactoryCache {
private static final Logger LOGGER = LoggerFactory.getLogger((String)ExtendedFactoryCache.class.getName());
private HashingServiceTrackerCustomizer oldGeneratorTracker;
private HashingServiceTrackerCustomizer oldSerializerTracker;
private HashingServiceTrackerCustomizer oldTransformerTracker;
private RewriterTransformerFactoryServiceTracker oldRewritingTransformerTracker;
private HashingServiceTrackerCustomizer oldProcessorTracker;
protected void activate(ComponentContext ctx) throws InvalidSyntaxException {
BundleContext context = ctx.getBundleContext();
this.oldGeneratorTracker = new HashingServiceTrackerCustomizer(context, ComponentType.GENERATOR, context.createFilter("(component.factory=" + Generator.class.getName() + "/*)"));
this.oldSerializerTracker = new HashingServiceTrackerCustomizer(context, ComponentType.SERIALIZER, context.createFilter("(component.factory=" + Serializer.class.getName() + "/*)"));
this.oldTransformerTracker = new HashingServiceTrackerCustomizer(context, ComponentType.TRANSFORMER, context.createFilter("(component.factory=" + Transformer.class.getName() + "/*)"));
this.oldProcessorTracker = new HashingServiceTrackerCustomizer(context, ComponentType.PROCESSOR, context.createFilter("(component.factory=" + Processor.class.getName() + "/*)"));
this.oldRewritingTransformerTracker = new RewriterTransformerFactoryServiceTracker(context);
this.oldGeneratorTracker.open();
this.oldSerializerTracker.open();
this.oldTransformerTracker.open();
this.oldRewritingTransformerTracker.open();
this.oldProcessorTracker.open();
}
protected void deactivate(ComponentContext ctx) {
this.oldGeneratorTracker.close();
this.oldSerializerTracker.close();
this.oldTransformerTracker.close();
this.oldRewritingTransformerTracker.close();
this.oldProcessorTracker.close();
this.oldGeneratorTracker = null;
this.oldProcessorTracker = null;
this.oldRewritingTransformerTracker = null;
this.oldTransformerTracker = null;
this.oldSerializerTracker = null;
}
private static final class RewriterTransformerFactoryServiceTracker
extends ServiceTracker {
private final Map<ServiceReference, ServiceRegistration> services = new ConcurrentHashMap<ServiceReference, ServiceRegistration>();
private final BundleContext context;
public RewriterTransformerFactoryServiceTracker(BundleContext bc) {
super(bc, RewriterTransformerFactory.class.getName(), null);
this.context = bc;
}
public Object addingService(ServiceReference reference) {
RewriterTransformerFactory rtf = (RewriterTransformerFactory)this.context.getService(reference);
if (rtf != null) {
String[] keys;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Found rewriter transformer factory {}.", (Object)rtf);
}
Hashtable<String, Object> props = new Hashtable<String, Object>();
for (String key : keys = reference.getPropertyKeys()) {
if (key.equals("service.id") || key.equals("objectClass") || key.equals("component.factory") || key.equals("component.name")) continue;
props.put(key, reference.getProperty(key));
}
props.put("pipeline.mode", "global");
ServiceRegistration reg = this.context.registerService(TransformerFactory.class.getName(), (Object)new RewriterTransformerFactoryWrapper(rtf), props);
this.services.put(reference, reg);
}
return rtf;
}
public void removedService(ServiceReference reference, Object service) {
ServiceRegistration reg = this.services.remove((Object)reference);
if (reg != null) {
reg.unregister();
}
this.context.ungetService(reference);
}
}
private static final class HashingServiceTrackerCustomizer
extends ServiceTracker {
private final Map<String, ServiceRegistration> services = new ConcurrentHashMap<String, ServiceRegistration>();
private final BundleContext context;
private final ComponentType componentType;
public HashingServiceTrackerCustomizer(BundleContext bc, ComponentType componentType, Filter filter) {
super(bc, filter, null);
this.componentType = componentType;
this.context = bc;
}
private String getType(ServiceReference ref) {
String factory = (String)ref.getProperty("component.factory");
int pos = factory.lastIndexOf(47);
if (pos != -1) {
String type = factory.substring(pos + 1);
return type;
}
return null;
}
public Object addingService(ServiceReference reference) {
ComponentFactory factory;
String type = this.getType(reference);
ComponentFactory componentFactory = factory = type == null ? null : (ComponentFactory)this.context.getService(reference);
if (factory != null) {
boolean skip = false;
if ("htmlparser".equals(type) && this.componentType == ComponentType.GENERATOR) {
LOGGER.info("Skipping service {}, type={}.", (Object)factory, (Object)type);
skip = true;
}
if ("htmlwriter".equals(type) && this.componentType == ComponentType.SERIALIZER) {
LOGGER.info("Skipping service {}, type={}.", (Object)factory, (Object)type);
skip = true;
}
if (!skip) {
String[] keys;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Found service {}, type={}.", (Object)factory, (Object)type);
}
Hashtable<String, Object> props = new Hashtable<String, Object>();
for (String key : keys = reference.getPropertyKeys()) {
if (key.equals("service.id") || key.equals("objectClass") || key.equals("component.factory") || key.equals("component.name")) continue;
props.put(key, reference.getProperty(key));
}
props.put("pipeline.type", type);
ServiceRegistration reg = null;
switch (this.componentType) {
case GENERATOR: {
reg = this.context.registerService(GeneratorFactory.class.getName(), (Object)new GeneratorFactoryWrapper(type, factory), props);
break;
}
case TRANSFORMER: {
reg = this.context.registerService(TransformerFactory.class.getName(), (Object)new TransformerFactoryWrapper(type, factory), props);
break;
}
case SERIALIZER: {
reg = this.context.registerService(SerializerFactory.class.getName(), (Object)new SerializerFactoryWrapper(type, factory), props);
break;
}
case PROCESSOR: {
reg = this.context.registerService(ProcessorFactory.class.getName(), (Object)new ProcessorFactoryWrapper(type, factory), props);
}
}
if (reg != null) {
this.services.put(type, reg);
}
}
}
return factory;
}
public void removedService(ServiceReference reference, Object service) {
ServiceRegistration reg;
String type = this.getType(reference);
if (type != null && (reg = this.services.remove(type)) != null) {
reg.unregister();
}
this.context.ungetService(reference);
}
}
static enum ComponentType {
GENERATOR,
TRANSFORMER,
SERIALIZER,
PROCESSOR;
private ComponentType() {
}
}
}