Scene7EndpointsManagerImpl.java
12.2 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.PageEvent
* com.day.cq.wcm.api.PageModification
* com.day.cq.wcm.api.PageModification$ModificationType
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.threads.ThreadPool
* org.apache.sling.commons.threads.ThreadPoolManager
* org.apache.sling.jcr.api.SlingRepository
* org.apache.sling.jcr.resource.JcrResourceResolverFactory
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceRegistration
* org.osgi.service.component.ComponentContext
* org.osgi.service.event.Event
* org.osgi.service.event.EventHandler
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.scene7.impl;
import com.day.cq.dam.scene7.api.Scene7Endpoint;
import com.day.cq.dam.scene7.api.Scene7EndpointsManager;
import com.day.cq.wcm.api.PageEvent;
import com.day.cq.wcm.api.PageModification;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
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.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.threads.ThreadPool;
import org.apache.sling.commons.threads.ThreadPoolManager;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1)
@Service(value={Scene7EndpointsManager.class})
public class Scene7EndpointsManagerImpl
implements Scene7EndpointsManager,
EventHandler {
private static final Logger LOG = LoggerFactory.getLogger(Scene7EndpointsManagerImpl.class);
@Reference
private JcrResourceResolverFactory resourceResolverFactory;
@Reference
private ThreadPoolManager threadPoolManager;
@Reference
private SlingRepository slingRepository;
private ResourceResolver rr;
private ServiceRegistration eventHandlerRegistration;
private ConcurrentHashMap<String, Scene7Endpoint> endpoints;
private ThreadPool threadPool;
@Override
public URL getAPIServer(String region) {
Scene7Endpoint endpoint = this.endpoints.get(region);
return endpoint == null ? null : endpoint.getApiURL();
}
@Override
public URL getIPSServer(String region) {
Scene7Endpoint endpoint = this.endpoints.get(region);
return endpoint == null ? null : endpoint.getIpsURL();
}
@Override
public URL getSPSServer(String region) {
Scene7Endpoint endpoint = this.endpoints.get(region);
return endpoint == null ? null : endpoint.getSpsURL();
}
@Override
public URL getViewerSdkServer(String region) {
Scene7Endpoint endpoint = this.endpoints.get(region);
return endpoint == null ? null : endpoint.getViewerSdkURL();
}
@Override
public String getAPIVersion(String region) {
Scene7Endpoint endpoint = this.endpoints.get(region);
return endpoint == null ? null : endpoint.getApiVersion();
}
public void handleEvent(Event event) {
this.processEvent(event);
}
@Override
public List<Scene7Endpoint> getScene7Endpoints() {
ArrayList<Scene7Endpoint> endpointsList = new ArrayList<Scene7Endpoint>(this.endpoints.values());
Collections.sort(endpointsList);
return endpointsList;
}
private void processEvent(Event event) {
final PageEvent pageEvent = PageEvent.fromEvent((Event)event);
if (pageEvent != null) {
Runnable eventTask = new Runnable(){
@Override
public void run() {
Iterator pageModifications = pageEvent.getModifications();
while (pageModifications.hasNext()) {
PageModification modification = (PageModification)pageModifications.next();
String path = modification.getPath();
if (!path.startsWith("/etc/cloudservices/scene7/endpoints")) continue;
if (modification.getType().equals((Object)PageModification.ModificationType.CREATED) || modification.getType().equals((Object)PageModification.ModificationType.MODIFIED)) {
Resource endpointResource = Scene7EndpointsManagerImpl.this.rr.getResource(path + "/" + "jcr:content");
if (endpointResource == null) continue;
Scene7EndpointsManagerImpl.this.extractEndpointFromResource(endpointResource);
continue;
}
if (!modification.getType().equals((Object)PageModification.ModificationType.DELETED)) continue;
Scene7EndpointsManagerImpl.this.endpoints.remove(ResourceUtil.getName((String)path));
LOG.info("Removed Scene7 endpoint from {}", (Object)path);
}
}
};
if (this.threadPool != null) {
this.threadPool.execute(eventTask);
} else {
LOG.error("'threadPool' is null.");
}
}
}
private void extractEndpointFromResource(Resource endpointResource) {
String region = endpointResource.getParent().getName();
ValueMap endpointProperties = (ValueMap)endpointResource.adaptTo(ValueMap.class);
URL viewersdkURL = null;
if (endpointProperties != null) {
String apiVersion;
URL apiURL;
URL ipsURL;
URL spsURL;
String apiURLValue = (String)endpointProperties.get("apiURL", String.class);
try {
apiURL = new URL(apiURLValue);
}
catch (MalformedURLException e) {
LOG.warn("Scene7 endpoint {}'s API URL (={}) is malformed; skipping", (Object)endpointResource.getParent().getPath(), (Object)apiURLValue);
return;
}
String ipsURLValue = (String)endpointProperties.get("ipsURL", String.class);
try {
ipsURL = new URL(ipsURLValue);
}
catch (MalformedURLException e) {
LOG.warn("Scene7 endpoint {}'s IPS URL (={}) is malformed; skipping", (Object)endpointResource.getParent().getPath(), (Object)ipsURLValue);
return;
}
String spsURLValue = (String)endpointProperties.get("spsURL", String.class);
try {
spsURL = new URL(spsURLValue);
}
catch (MalformedURLException e) {
LOG.warn("Scene7 endpoint {}'s SPS URL (={}) is malformed; skipping", (Object)endpointResource.getParent().getPath(), (Object)spsURLValue);
return;
}
String viewersdkURLValue = (String)endpointProperties.get("viewersdkURL", String.class);
if (viewersdkURLValue != null) {
try {
viewersdkURL = new URL(viewersdkURLValue);
}
catch (MalformedURLException e) {
LOG.warn("Scene7 endpoint {}'s Viewer SDK URL (={}) is malformed; skipping", (Object)endpointResource.getParent().getPath(), (Object)viewersdkURLValue);
return;
}
}
if ((apiVersion = (String)endpointProperties.get("apiVersion", String.class)) == null) {
LOG.warn("Scene7 endpoint {}'s API version is missing; skipping", (Object)endpointResource.getParent().getPath());
return;
}
String description = (String)endpointProperties.get("jcr:title", String.class);
Scene7Endpoint endpoint = new Scene7Endpoint(apiURL, ipsURL, spsURL, viewersdkURL, apiVersion, region, description);
this.endpoints.put(region, endpoint);
LOG.info("Found Scene7 endpoint for region {}: {}", (Object)region, (Object)endpoint.toString());
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void activate(ComponentContext context) {
Hashtable<String, String> listenEventsDictionary = new Hashtable<String, String>();
listenEventsDictionary.put("event.topics", "com/day/cq/wcm/core/page");
this.eventHandlerRegistration = context.getBundleContext().registerService(EventHandler.class.getName(), (Object)this, listenEventsDictionary);
LOG.info("Registered for OSGi events: topics={}", (Object)"com/day/cq/wcm/core/page");
this.endpoints = new ConcurrentHashMap();
Session configSession = null;
try {
configSession = this.slingRepository.loginService("scene7configservice", null);
this.rr = this.resourceResolverFactory.getResourceResolver(configSession);
Resource endpointsRoot = this.rr.getResource("/etc/cloudservices/scene7/endpoints");
if (endpointsRoot != null) {
Iterator iterator = endpointsRoot.listChildren();
while (iterator.hasNext()) {
Resource jcrContent;
Resource endpointResource = (Resource)iterator.next();
if (!endpointResource.getResourceType().equals("cq:Page") || (jcrContent = endpointResource.getChild("jcr:content")) == null) continue;
this.extractEndpointFromResource(jcrContent);
}
this.threadPool = this.threadPoolManager.get("Event processing pool");
}
}
catch (RepositoryException e) {
LOG.error("Unable to obtain a resource resolver", (Throwable)e);
}
finally {
if (this.rr != null) {
this.rr.close();
}
if (configSession != null) {
configSession.logout();
}
}
}
protected void deactivate() {
if (this.eventHandlerRegistration != null) {
this.eventHandlerRegistration.unregister();
this.eventHandlerRegistration = null;
}
if (this.rr != null) {
this.rr.close();
}
if (this.threadPool != null) {
this.threadPoolManager.release(this.threadPool);
}
}
protected void bindResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
this.resourceResolverFactory = jcrResourceResolverFactory;
}
protected void unbindResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
if (this.resourceResolverFactory == jcrResourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
protected void bindThreadPoolManager(ThreadPoolManager threadPoolManager) {
this.threadPoolManager = threadPoolManager;
}
protected void unbindThreadPoolManager(ThreadPoolManager threadPoolManager) {
if (this.threadPoolManager == threadPoolManager) {
this.threadPoolManager = null;
}
}
protected void bindSlingRepository(SlingRepository slingRepository) {
this.slingRepository = slingRepository;
}
protected void unbindSlingRepository(SlingRepository slingRepository) {
if (this.slingRepository == slingRepository) {
this.slingRepository = null;
}
}
}