TemplateConfImpl.java
7.32 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* 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.core.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
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 TemplateConfImpl {
private static final Logger log = LoggerFactory.getLogger(TemplateConfImpl.class);
private final Resource originalResource;
private final ResourceResolver configResolver;
public TemplateConfImpl(Resource resource) {
this.originalResource = resource;
this.configResolver = resource != null ? resource.getResourceResolver() : null;
}
public ValueMap getItem(String itemName) {
Resource res = this.getItemResource(itemName);
Resource valueMapRes = this.handleJcrContent(res);
if (res == null) {
return ValueMap.EMPTY;
}
if (log.isTraceEnabled() && "jcr:content".equals(valueMapRes.getName())) {
log.trace("- using jcr:content child for ValueMap: {}", (Object)valueMapRes.getPath());
}
return valueMapRes.getValueMap();
}
public Resource getItemResource(String itemName) {
if (this.originalResource == null) {
return null;
}
if (log.isTraceEnabled()) {
log.trace("- searching for item '{}'", (Object)itemName);
}
int idx = 1;
for (String path : this.getPaths()) {
Resource item = this.getItem(path, itemName);
if (item != null) {
if (log.isTraceEnabled()) {
log.trace("+ resolved config item at [{}]: {}", (Object)idx, (Object)item.getPath());
}
return item;
}
if (log.isTraceEnabled()) {
log.trace("- no item '{}' under config '{}'", (Object)itemName, (Object)path);
}
++idx;
}
if (log.isTraceEnabled()) {
log.trace("- could not resolve any config item for '{}' (or no permissions to read it)", (Object)itemName);
}
return null;
}
public List<ValueMap> getList(String parentItemName) {
List<Resource> resources = this.getListResources(parentItemName);
ArrayList<ValueMap> maps = new ArrayList<ValueMap>(resources.size());
for (Resource res : resources) {
Resource valueMapRes = this.handleJcrContent(res);
if (log.isTraceEnabled() && "jcr:content".equals(valueMapRes.getName())) {
log.trace("- using jcr:content child for ValueMap: {}", (Object)valueMapRes.getPath());
}
maps.add(valueMapRes.getValueMap());
}
return maps;
}
public List<Resource> getListResources(String parentItemName) {
if (log.isTraceEnabled()) {
log.trace("- searching for list '{}'", (Object)parentItemName);
}
ArrayList<Resource> result = new ArrayList<Resource>();
TreeMap<String, Resource> resourceMap = new TreeMap<String, Resource>();
if (this.configResolver == null || this.originalResource == null) {
return result;
}
for (String path : this.getPaths()) {
Resource parentItem = this.getItem(path, parentItemName);
if (parentItem == null) continue;
for (Resource item : parentItem.getChildren()) {
String itemKey = parentItemName + "/" + item.getName();
if (resourceMap.containsKey(itemKey)) continue;
resourceMap.put(itemKey, item);
}
}
for (Map.Entry entry : resourceMap.entrySet()) {
Resource res = (Resource)entry.getValue();
result.add(res);
}
Collections.reverse(result);
return result;
}
public List<String> getPaths() {
ArrayList<String> paths = new ArrayList<String>();
Resource conf = this.originalResource;
while (conf != null) {
boolean hasSettingNode = conf.getChild("settings") != null;
boolean pathIsPartOfSettingNode = conf.getPath().indexOf("/settings/") > 0;
String confPath = null;
if (hasSettingNode) {
confPath = conf.getPath() + "/";
if (confPath != null && !confPath.equals("/conf/global")) {
if (log.isTraceEnabled()) {
log.trace("- [{}] parent config => {}", (Object)paths.size(), (Object)confPath);
}
paths.add(confPath + "/");
}
conf = conf.getParent();
continue;
}
if (pathIsPartOfSettingNode) {
confPath = conf.getPath().substring(0, conf.getPath().indexOf("/settings"));
if (confPath != null && !confPath.equals("/conf/global")) {
if (log.isTraceEnabled()) {
log.trace("- [{}] parent config => {}", (Object)paths.size(), (Object)confPath);
}
paths.add(confPath + "/");
String parentPath = confPath.substring(0, confPath.lastIndexOf("/"));
while (!parentPath.equals("/conf")) {
if (log.isTraceEnabled()) {
log.trace("- [{}] parent config => {}", (Object)paths.size(), (Object)parentPath);
}
paths.add(parentPath + "/");
parentPath = parentPath.substring(0, parentPath.lastIndexOf("/"));
}
}
conf = null;
continue;
}
conf = null;
}
paths.add("/conf/global/");
if (log.isTraceEnabled()) {
log.trace("- [{}] apps config => {}", (Object)paths.size(), (Object)"/conf/global");
}
paths.add("/apps/");
if (log.isTraceEnabled()) {
log.trace("- [{}] apps config => {}", (Object)paths.size(), (Object)"/apps");
}
paths.add("/libs/");
if (log.isTraceEnabled()) {
log.trace("- [{}] libs config => {}", (Object)paths.size(), (Object)"/libs");
}
return Collections.unmodifiableList(paths);
}
private Resource handleJcrContent(Resource resource) {
Resource jcrContent;
if (resource != null && (jcrContent = resource.getChild("jcr:content")) != null) {
resource = jcrContent;
}
return resource;
}
private Resource getItem(String path, String itemRelativePath) {
Resource confBase = this.configResolver.getResource(path);
Resource item = null;
item = confBase == null ? this.configResolver.getResource(path + "/" + "settings" + "/" + itemRelativePath) : confBase.getChild("settings/" + itemRelativePath);
if (item == null && log.isTraceEnabled()) {
log.trace("- missing config '{}' (or no permissions to read it)", (Object)path);
}
return item;
}
}