ConfigurationManagerImpl.java
10.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.Template
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Value
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.webservicesupport.impl;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.Service;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConfigurationManagerImpl
implements ConfigurationManager {
private static final Logger log = LoggerFactory.getLogger(ConfigurationManagerImpl.class);
private ResourceResolver resolver;
public ConfigurationManagerImpl() {
}
public ConfigurationManagerImpl(ResourceResolver resolver) {
if (resolver == null) {
throw new IllegalArgumentException("resolver may not be null");
}
this.resolver = resolver;
}
@Override
public Iterator<Service> getServices() {
LinkedList<Service> entries = new LinkedList<Service>();
Resource baseResource = this.resolver.getResource("/etc/cloudservices");
if (baseResource != null) {
Iterator iter = baseResource.listChildren();
while (iter.hasNext()) {
Resource resource = (Resource)iter.next();
Service srvc = (Service)resource.adaptTo(Service.class);
if (srvc != null) {
entries.add(srvc);
continue;
}
log.debug("Resource at {} is not adaptable to com.day.cq.configuration.Service", (Object)resource.getPath());
}
}
return entries.iterator();
}
@Override
public Iterator<Service> getServices(String[] paths, Comparator<Service> comparator) {
LinkedList<Service> entries = new LinkedList<Service>();
for (String path : paths) {
if (path.length() <= 0) continue;
String servicename = this.getServiceName(path);
Service service = this.getService(servicename);
if (service == null) {
log.warn("Cloud service {} not found", (Object)path);
continue;
}
entries.add(service);
}
Collections.sort(entries, comparator);
return entries.iterator();
}
@Override
public Service getService(String name) {
Service service = null;
if (name != null) {
String path = "/etc/cloudservices/" + name;
Resource serviceResource = this.resolver.getResource(path);
if (serviceResource != null) {
service = (Service)serviceResource.adaptTo(Service.class);
} else {
log.debug("Resource at {} is not adaptable to com.day.cq.configuration.Service", (Object)path);
}
}
return service;
}
@Override
public Service getService(Configuration configuration) {
Resource tplContent;
Resource tplRes;
Service service = null;
Template tpl = configuration.getTemplate();
if (tpl != null && (tplContent = (tplRes = (Resource)tpl.adaptTo(Resource.class)).getChild("jcr:content")) != null) {
ValueMap props = (ValueMap)tplContent.adaptTo(ValueMap.class);
return this.getService((String)props.get("cq:cloudservicename", String.class));
}
return service;
}
@Override
public String getServiceName(String path) {
if (path != null && path.startsWith("/etc/cloudservices")) {
int idx = (path = path.replace("/etc/cloudservices/", "")).indexOf("/");
if (idx > -1) {
path = path.substring(0, idx);
}
return path;
}
return null;
}
@Override
public Configuration getConfiguration(String path) {
Resource cfg = this.resolver.getResource(path);
return this.getConfigurationFromResource(path, cfg);
}
private Configuration getConfigurationFromResource(String path, Resource cfg) {
String publicChildPath = path + "/" + "jcr:content" + "/" + "public";
Configuration config = null;
if (cfg != null) {
config = (Configuration)cfg.adaptTo(Configuration.class);
} else {
cfg = this.resolver.getResource(publicChildPath);
if (cfg != null) {
config = (Configuration)cfg.adaptTo(Configuration.class);
}
}
if (config == null) {
log.debug("Resource at {} is not readable or adaptable to com.day.cq.configuration.Configuration ( also tried looking at {} )", (Object)path, (Object)publicChildPath);
}
return config;
}
@Override
public Configuration getConfiguration(String servicename, String[] configurationPaths) {
for (String path2 : configurationPaths) {
if (path2.indexOf(servicename) <= -1) continue;
return this.getConfiguration(path2);
}
for (String path2 : configurationPaths) {
Resource cfgPage = this.resolver.getResource(path2);
if (cfgPage != null) {
Iterator childs = cfgPage.listChildren();
Configuration cfg = this.searchConfiguration(servicename, childs);
if (cfg == null) continue;
return cfg;
}
log.warn("Resource with path {} does not exist!", (Object)path2);
}
return null;
}
private Configuration searchConfiguration(String servicename, Iterator<Resource> childs) {
Resource child;
Configuration cfg = null;
while (childs.hasNext() && (cfg = this.searchConfiguration(servicename, (child = childs.next()).listChildren())) == null) {
Service srv;
Configuration adpcfg = (Configuration)child.adaptTo(Configuration.class);
if (adpcfg == null || (srv = this.getService(adpcfg)) == null || !srv.getName().equals(servicename)) continue;
return adpcfg;
}
return cfg;
}
@Override
public Iterator<Configuration> getConfigurations(String rootPath) {
LinkedList<Configuration> configurations = new LinkedList<Configuration>();
Resource rootResource = this.resolver.getResource(rootPath);
List<Resource> configResources = this.getChildren(rootResource);
for (Resource resource : configResources) {
Configuration cfg = (Configuration)resource.adaptTo(Configuration.class);
if (cfg != null) {
configurations.add(cfg);
continue;
}
log.debug("Resource at {} is not adaptable to com.day.cq.configuration.Configuration", (Object)resource.getPath());
}
return configurations.iterator();
}
@Override
public Iterator<Configuration> getConfigurations(Resource resource) {
LinkedList<Configuration> configurations = new LinkedList<Configuration>();
try {
Page cfgPage;
Node contentNode;
Resource cfgRes = this.getConfigurationResource(resource);
if (cfgRes != null && (contentNode = (Node)(cfgPage = (Page)cfgRes.adaptTo(Page.class)).getContentResource().adaptTo(Node.class)) != null && contentNode.hasProperty("cq:cloudserviceconfigs")) {
Property prop = contentNode.getProperty("cq:cloudserviceconfigs");
if (prop.isMultiple()) {
for (Value v : prop.getValues()) {
Configuration c = this.getConfiguration(v.getString());
if (c == null) continue;
configurations.add(c);
}
} else {
Configuration c = this.getConfiguration(prop.getValue().getString());
if (c != null) {
configurations.add(c);
}
}
}
}
catch (RepositoryException cfgRes) {
// empty catch block
}
return configurations.iterator();
}
@Override
public Resource getConfigurationResource(Resource resource) {
ResourceResolver resolver = resource.getResourceResolver();
try {
String resourcePath = resource.getPath();
while (resourcePath.lastIndexOf("/") > 0) {
Node jcrContent;
Page page;
Resource contentResource;
resource = resolver.getResource(resourcePath);
if (resource != null && (page = (Page)resource.adaptTo(Page.class)) != null && (contentResource = page.getContentResource()) != null && (jcrContent = (Node)contentResource.adaptTo(Node.class)).hasProperty("cq:cloudserviceconfigs")) {
return resource;
}
resourcePath = resourcePath.substring(0, resourcePath.lastIndexOf("/"));
}
}
catch (Exception ignored) {
log.warn("Exception while searching for cloudservice resource", (Throwable)ignored);
}
return null;
}
private List<Resource> getChildren(Resource resource) {
LinkedList<Resource> newResource = new LinkedList<Resource>();
if (resource != null) {
Iterator iter = resource.listChildren();
while (iter.hasNext()) {
Resource child = (Resource)iter.next();
newResource.add(child);
if (!child.listChildren().hasNext()) continue;
newResource.addAll(this.getChildren(child));
}
}
return newResource;
}
}