ConfigManagerImpl.java
13.1 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoSupport
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.observation.Event
* javax.jcr.observation.EventIterator
* javax.jcr.observation.EventListener
* javax.jcr.observation.ObservationManager
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.util.Text
* org.apache.sling.jcr.api.SlingRepository
* org.apache.sling.jcr.resource.JcrPropertyMap
* org.apache.sling.settings.SlingSettingsService
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.replication.impl;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.AgentConfigGroup;
import com.day.cq.replication.ConfigManager;
import com.day.cq.replication.impl.AgentConfigGroupImpl;
import com.day.cq.replication.impl.AgentConfigImpl;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.JcrPropertyMap;
import org.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, immediate=1)
@Service(value={ConfigManager.class})
public class ConfigManagerImpl
implements ConfigManager,
EventListener {
private static final Logger log = LoggerFactory.getLogger(ConfigManagerImpl.class);
private static final Set<String> IGNORED_PROPERTIES = new HashSet<String>();
private static final String AGENT_CONFIG_ROOT_PATH = "/etc/replication";
@Reference
private SlingRepository repository = null;
@Reference
private SlingSettingsService settingsService = null;
@Reference
private CryptoSupport crypto = null;
private Session session;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Map<String, AgentConfigImpl> configurations = new TreeMap<String, AgentConfigImpl>();
private final Map<String, AgentConfigGroupImpl> configGroups = new TreeMap<String, AgentConfigGroupImpl>();
private final List<ConfigManager.ConfigEventListener> listeners = new LinkedList<ConfigManager.ConfigEventListener>();
@Activate
protected void activate() {
this.lock.writeLock().lock();
try {
this.session = this.repository.loginService("replicationService", null);
if (this.session.nodeExists("/etc/replication")) {
this.scan(this.session.getNode("/etc/replication"));
} else {
log.info("Unable to read any configured agents. {} does not exist.", (Object)"/etc/replication");
}
this.session.getWorkspace().getObservationManager().addEventListener((EventListener)this, 31, "/etc/replication", true, null, null, true);
log.info("ConfigManager service activated");
}
catch (RepositoryException e) {
throw new IllegalStateException("Error while initializing config manager", (Throwable)e);
}
finally {
this.lock.writeLock().unlock();
}
}
private void scan(Node node) throws RepositoryException {
NodeIterator iter = node.getNodes();
while (iter.hasNext()) {
Node n = iter.nextNode();
if ("jcr:content".equals(n.getName())) continue;
this.update(n);
this.scan(n);
}
}
@Deactivate
protected void deactivate() {
block5 : {
this.lock.writeLock().lock();
this.listeners.clear();
this.configGroups.clear();
this.configurations.clear();
try {
if (this.session == null) break block5;
try {
this.session.getWorkspace().getObservationManager().removeEventListener((EventListener)this);
}
catch (RepositoryException var1_1) {
// empty catch block
}
this.session.logout();
this.session = null;
}
finally {
this.lock.writeLock().unlock();
}
}
}
private void notifyListeners(final String path, final ConfigManager.ConfigEvent.Type type) {
ConfigManager.ConfigEvent evt = new ConfigManager.ConfigEvent(){
@Override
public ConfigManager.ConfigEvent.Type getType() {
return type;
}
@Override
public String getId() {
return path;
}
};
for (ConfigManager.ConfigEventListener listener : this.listeners) {
listener.onConfigEvent(evt);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void onEvent(EventIterator events) {
HashSet<String> paths = new HashSet<String>();
while (events.hasNext()) {
Event evt = events.nextEvent();
try {
int idx;
String path = evt.getPath();
if (evt.getType() != 1 && evt.getType() != 2) {
String name = Text.getName((String)path);
if (IGNORED_PROPERTIES.contains(name)) continue;
path = Text.getRelativeParent((String)path, (int)1);
}
if ((idx = path.indexOf("/jcr:content")) > 0) {
paths.add(path.substring(0, idx));
continue;
}
paths.add(path);
}
catch (RepositoryException e) {
log.warn("Error while reading observation information: " + (Object)e);
}
}
try {
this.lock.writeLock().lock();
for (String path : paths) {
this.session.refresh(false);
if (this.session.nodeExists(path)) {
Node node = this.session.getNode(path);
this.update(node);
continue;
}
this.configGroups.remove(path);
AgentConfigImpl cfg = this.configurations.remove(path);
if (cfg == null) continue;
cfg.getConfigGroup().getConfigurations().remove(cfg.getId());
this.notifyListeners(cfg.getId(), ConfigManager.ConfigEvent.Type.REMOVED);
}
}
catch (RepositoryException e) {
log.error("error while updating agents.", (Throwable)e);
}
finally {
this.lock.writeLock().unlock();
}
}
private AgentConfig loadConfig(Node node) throws RepositoryException {
if (node.hasNode("jcr:content")) {
String path = node.getPath();
AgentConfigImpl cfg = this.configurations.remove(path);
if (cfg != null) {
cfg.getConfigGroup().getConfigurations().remove(path);
}
AgentConfigGroupImpl group = null;
Node parent = node.getParent();
while (group == null && parent.getDepth() > 0 && !parent.getPath().equals("/etc/replication")) {
group = this.configGroups.get(parent.getPath());
if (group == null && (group = AgentConfigGroupImpl.create(parent, this.settingsService)) != null) {
this.configGroups.put(parent.getPath(), group);
}
parent = parent.getParent();
}
if (group == null) {
log.info("Possible agent configured outside of any config group: {}", (Object)path);
return null;
}
Node content = node.getNode("jcr:content");
this.makeConfigCompatible(content);
JcrPropertyMap props = new JcrPropertyMap(content);
HashMap<String, Object> properties = new HashMap<String, Object>((Map<String, Object>)props);
cfg = new AgentConfigImpl(node.getPath(), properties, content.getPath(), group, this.crypto);
group.getConfigurations().put(cfg.getId(), cfg);
this.configurations.put(cfg.getId(), cfg);
log.info("Configuration for agent at {} is {}active.", (Object)path, (Object)(group.isActive() ? "" : "not "));
return cfg;
}
return null;
}
private void makeConfigCompatible(Node content) throws RepositoryException {
if (content.hasProperty("protocolHTTPSRelaxed")) {
Property isRelaxedSslProp = content.getProperty("protocolHTTPSRelaxed");
if (isRelaxedSslProp.getBoolean() && !content.hasProperty("ssl")) {
content.setProperty("ssl", "relaxed");
}
isRelaxedSslProp.remove();
content.getSession().save();
}
}
@Override
public Map<String, AgentConfig> getConfigurations() {
try {
this.lock.readLock().lock();
LinkedHashMap<String, AgentConfig> linkedHashMap = new LinkedHashMap<String, AgentConfig>(this.configurations);
return linkedHashMap;
}
finally {
this.lock.readLock().unlock();
}
}
@Override
public Map<String, AgentConfigGroup> getConfigGroups() {
try {
this.lock.readLock().lock();
LinkedHashMap<String, AgentConfigGroup> linkedHashMap = new LinkedHashMap<String, AgentConfigGroup>(this.configGroups);
return linkedHashMap;
}
finally {
this.lock.readLock().unlock();
}
}
@Override
public void registerListener(ConfigManager.ConfigEventListener listener) {
this.listeners.add(listener);
}
@Override
public void unregisterListener(ConfigManager.ConfigEventListener listener) {
this.listeners.remove(listener);
}
private void update(Node node) throws RepositoryException {
String path = node.getPath();
if (AgentConfigGroupImpl.getRunModes(node) != null) {
AgentConfigGroupImpl grp = this.configGroups.get(path);
if (grp == null) {
grp = AgentConfigGroupImpl.create(node, this.settingsService);
this.configGroups.put(path, grp);
} else {
grp.update(node);
}
} else {
boolean isUpdate = this.configurations.containsKey(path);
AgentConfig config = this.loadConfig(node);
if (config == null) {
this.notifyListeners(path, ConfigManager.ConfigEvent.Type.REMOVED);
} else if (isUpdate) {
this.notifyListeners(path, ConfigManager.ConfigEvent.Type.UPDATED);
} else {
this.notifyListeners(path, ConfigManager.ConfigEvent.Type.ADDED);
}
}
}
static {
IGNORED_PROPERTIES.add("cq:name");
IGNORED_PROPERTIES.add("cq:siblingOrder");
IGNORED_PROPERTIES.add("cq:parentPath");
IGNORED_PROPERTIES.add("cq:childrenOrder");
IGNORED_PROPERTIES.add("jcr:baseVersion");
IGNORED_PROPERTIES.add("jcr:isCheckedOut");
IGNORED_PROPERTIES.add("jcr:predecessors");
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindSettingsService(SlingSettingsService slingSettingsService) {
this.settingsService = slingSettingsService;
}
protected void unbindSettingsService(SlingSettingsService slingSettingsService) {
if (this.settingsService == slingSettingsService) {
this.settingsService = null;
}
}
protected void bindCrypto(CryptoSupport cryptoSupport) {
this.crypto = cryptoSupport;
}
protected void unbindCrypto(CryptoSupport cryptoSupport) {
if (this.crypto == cryptoSupport) {
this.crypto = null;
}
}
}