OffloadingPropertyProvider.java
7.71 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.PropertiesUtil
* org.apache.sling.discovery.PropertyProvider
* org.osgi.framework.BundleContext
* org.osgi.service.cm.Configuration
* org.osgi.service.cm.ConfigurationAdmin
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.offloading.impl;
import com.adobe.granite.offloading.impl.JobConsumerInfo;
import com.adobe.granite.offloading.impl.JobConsumerInventoryService;
import java.util.Collection;
import java.util.Dictionary;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.discovery.PropertyProvider;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={PropertyProvider.class})
@Properties(value={@Property(name="instance.properties", value={"com.adobe.granite.offloading.infrastructure.osgiconsole.path", "com.adobe.granite.offloading.job.registeredtopics", "job.consumermanager.whitelist", "job.consumermanager.blacklist"})})
public class OffloadingPropertyProvider
implements PropertyProvider {
private static final Logger LOG = LoggerFactory.getLogger(OffloadingPropertyProvider.class);
private ComponentContext componentContext;
@Reference
private ConfigurationAdmin configurationAdmin;
@Reference
private JobConsumerInventoryService jobConsumerInventoryService;
public String getProperty(String name) {
if ("com.adobe.granite.offloading.infrastructure.osgiconsole.path".equals(name)) {
return this.getOSGIConsolePath();
}
if ("com.adobe.granite.offloading.job.registeredtopics".equals(name)) {
return this.getRegisteredTopics();
}
if ("job.consumermanager.whitelist".equals(name)) {
return this.getWhitelistTopics();
}
if ("job.consumermanager.blacklist".equals(name)) {
return this.getBlacklistTopics();
}
return null;
}
private String getOSGIConsolePath() {
String consolePath = null;
try {
String filter = "(service.pid=org.apache.felix.webconsole.internal.servlet.OsgiManager)";
Configuration[] configurations = this.configurationAdmin.listConfigurations(filter);
if (configurations != null) {
for (Configuration c : configurations) {
Dictionary properties = c.getProperties();
consolePath = (String)properties.get("manager.root");
}
}
}
catch (Exception e) {
LOG.error("Unable to retrieve Felix's web console path.", (Throwable)e);
}
if (StringUtils.isEmpty((String)consolePath)) {
consolePath = this.componentContext.getBundleContext().getProperty("felix.webconsole.manager.root");
}
if (StringUtils.isEmpty((String)consolePath)) {
consolePath = "/system/console";
}
return consolePath;
}
private String getRegisteredTopics() {
TreeSet<String> topics;
Map<String, List<JobConsumerInfo>> topicToConsumersMap = this.jobConsumerInventoryService.getRegisteredConsumers();
if (topicToConsumersMap != null && (topics = new TreeSet<String>(topicToConsumersMap.keySet())).size() > 0) {
StringBuilder sb = new StringBuilder();
for (String topic : topics) {
sb.append(topic).append(",");
}
if (sb.length() > 0) {
return sb.substring(0, sb.length() - 1);
}
}
return null;
}
private String getWhitelistTopics() {
String whitelistTopics = null;
try {
String filter = "(service.pid=org.apache.sling.event.impl.jobs.JobConsumerManager)";
Configuration[] configurations = this.configurationAdmin.listConfigurations(filter);
if (configurations != null) {
for (Configuration c : configurations) {
Dictionary properties = c.getProperties();
String[] topicsArray = PropertiesUtil.toStringArray(properties.get("job.consumermanager.whitelist"));
if (topicsArray == null) continue;
StringBuilder sb = new StringBuilder();
for (String topic : topicsArray) {
sb.append(topic).append(",");
}
if (sb.length() <= 0) continue;
whitelistTopics = sb.substring(0, sb.length() - 1);
}
}
}
catch (Exception e) {
LOG.error("Unable to retrieve whitelist topics.", (Throwable)e);
}
return whitelistTopics;
}
private String getBlacklistTopics() {
String blacklistTopics = null;
try {
String filter = "(service.pid=org.apache.sling.event.impl.jobs.JobConsumerManager)";
Configuration[] configurations = this.configurationAdmin.listConfigurations(filter);
if (configurations != null) {
for (Configuration c : configurations) {
Dictionary properties = c.getProperties();
String[] topicsArray = PropertiesUtil.toStringArray(properties.get("job.consumermanager.blacklist"));
if (topicsArray == null) continue;
StringBuilder sb = new StringBuilder();
for (String topic : topicsArray) {
sb.append(topic).append(",");
}
if (sb.length() <= 0) continue;
blacklistTopics = sb.substring(0, sb.length() - 1);
}
}
}
catch (Exception e) {
LOG.error("Unable to retrieve blacklist topics.", (Throwable)e);
}
return blacklistTopics;
}
protected void activate(ComponentContext componentContext) {
this.componentContext = componentContext;
}
protected void deactivate(ComponentContext componentContext) {
if (this.componentContext != null) {
this.componentContext = null;
}
}
protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
this.configurationAdmin = configurationAdmin;
}
protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
if (this.configurationAdmin == configurationAdmin) {
this.configurationAdmin = null;
}
}
protected void bindJobConsumerInventoryService(JobConsumerInventoryService jobConsumerInventoryService) {
this.jobConsumerInventoryService = jobConsumerInventoryService;
}
protected void unbindJobConsumerInventoryService(JobConsumerInventoryService jobConsumerInventoryService) {
if (this.jobConsumerInventoryService == jobConsumerInventoryService) {
this.jobConsumerInventoryService = null;
}
}
}