MobilePlatformInternalAdapterFactory.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.contentsync.handler.util.RequestResponseFactory
* org.apache.felix.scr.annotations.Component
* 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.engine.SlingRequestProcessor
* org.osgi.framework.ServiceReference
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl;
import com.adobe.cq.mobile.platform.impl.FrameworkContentExporter;
import com.adobe.cq.mobile.platform.impl.MobileAppProvider;
import com.adobe.cq.mobile.platform.impl.MobileAppProviderFactory;
import com.day.cq.contentsync.handler.util.RequestResponseFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.felix.scr.annotations.Component;
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.engine.SlingRequestProcessor;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={MobilePlatformInternalAdapterFactory.class})
@Property(name="service.description", value={"AEM Mobile Platform Adapter Factory"})
public class MobilePlatformInternalAdapterFactory {
private static Logger log = LoggerFactory.getLogger(MobilePlatformInternalAdapterFactory.class);
private static final Class<FrameworkContentExporter> FRAMEWORK_CONTENT_EXPORTER_CLASS = FrameworkContentExporter.class;
private static final Class<MobileAppProvider> MOBILE_APP_PROVIDER_CLASS = MobileAppProvider.class;
private ComponentContext context;
@Property(name="adapters", propertyPrivate=1)
private static final String[] ADAPTER_CLASSES = new String[]{FRAMEWORK_CONTENT_EXPORTER_CLASS.getName(), MOBILE_APP_PROVIDER_CLASS.getName()};
@Property(name="adaptables", propertyPrivate=1)
private static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName(), ResourceResolver.class.getName()};
@Reference
RequestResponseFactory requestResponseFactory;
@Reference
SlingRequestProcessor requestProcessor;
@Reference(name="Factory", referenceInterface=MobileAppProviderFactory.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
private List<ServiceReference> unhandledFactories = new ArrayList<ServiceReference>();
private SortedMap<ServiceReference, MobileAppProviderFactory> factories;
public MobilePlatformInternalAdapterFactory() {
this.factories = new TreeMap<ServiceReference, MobileAppProviderFactory>(new Comparator<ServiceReference>(){
@Override
public int compare(ServiceReference ref1, ServiceReference ref2) {
int rank2;
long id2;
long id1 = MobilePlatformInternalAdapterFactory.this.getServiceID(ref1);
if (id1 == (id2 = MobilePlatformInternalAdapterFactory.this.getServiceID(ref2))) {
return 0;
}
int rank1 = MobilePlatformInternalAdapterFactory.this.getServiceRanking(ref1);
if (rank1 == (rank2 = MobilePlatformInternalAdapterFactory.this.getServiceRanking(ref2))) {
return (int)(id1 - id2);
}
return rank2 - rank1;
}
});
}
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (adaptable instanceof Resource) {
return this.getAdapter((Resource)adaptable, type);
}
if (adaptable instanceof ResourceResolver) {
return this.getAdapter((ResourceResolver)adaptable, type);
}
log.info("Can't adapt {} to {}", (Object)(adaptable == null ? "null" : adaptable.getClass().getName()), type);
return null;
}
private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
if (MOBILE_APP_PROVIDER_CLASS == type) {
return (AdapterType)this.findAppProvider(resource);
}
return null;
}
private <AdapterType> AdapterType getAdapter(ResourceResolver resolver, Class<AdapterType> type) {
if (FRAMEWORK_CONTENT_EXPORTER_CLASS == type) {
return (AdapterType)new FrameworkContentExporter(this.requestResponseFactory, this.requestProcessor, resolver);
}
return null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected MobileAppProvider findAppProvider(Resource resource) {
SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
synchronized (sortedMap) {
for (MobileAppProviderFactory factory : this.factories.values()) {
MobileAppProvider appProvider = factory.getMobileAppProvider(resource);
if (appProvider == null) continue;
return appProvider;
}
}
return null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void activate(ComponentContext context) {
SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
synchronized (sortedMap) {
this.context = context;
for (ServiceReference reference : this.unhandledFactories) {
this.registerAppFactory(reference);
}
this.unhandledFactories.clear();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void deactivate(ComponentContext context) {
SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
synchronized (sortedMap) {
this.context = null;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindFactory(ServiceReference reference) {
SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
synchronized (sortedMap) {
if (this.context == null) {
this.unhandledFactories.add(reference);
} else {
this.registerAppFactory(reference);
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindFactory(ServiceReference reference) {
SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
synchronized (sortedMap) {
this.unregisterAppFactory(reference);
this.unhandledFactories.remove((Object)reference);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void registerAppFactory(ServiceReference reference) {
MobileAppProviderFactory factory = (MobileAppProviderFactory)this.context.locateService("Factory", reference);
if (factory != null) {
SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
synchronized (sortedMap) {
this.factories.put(reference, factory);
log.info("App Provider Factories in order: " + this.factories.values().toString());
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void unregisterAppFactory(ServiceReference reference) {
SortedMap<ServiceReference, MobileAppProviderFactory> sortedMap = this.factories;
synchronized (sortedMap) {
this.factories.remove((Object)reference);
}
}
private long getServiceID(ServiceReference reference) {
return (Long)reference.getProperty("service.id");
}
private int getServiceRanking(ServiceReference reference) {
Object obj = reference.getProperty("service.ranking");
if (obj == null) {
return 0;
}
if (obj instanceof Integer) {
return (Integer)obj;
}
log.warn("Component " + reference.getProperty("component.name") + " has a non-Integer '" + "service.ranking" + "' property");
return 0;
}
protected void bindRequestResponseFactory(RequestResponseFactory requestResponseFactory) {
this.requestResponseFactory = requestResponseFactory;
}
protected void unbindRequestResponseFactory(RequestResponseFactory requestResponseFactory) {
if (this.requestResponseFactory == requestResponseFactory) {
this.requestResponseFactory = null;
}
}
protected void bindRequestProcessor(SlingRequestProcessor slingRequestProcessor) {
this.requestProcessor = slingRequestProcessor;
}
protected void unbindRequestProcessor(SlingRequestProcessor slingRequestProcessor) {
if (this.requestProcessor == slingRequestProcessor) {
this.requestProcessor = null;
}
}
}