1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.config.json;
18
19 import com.fasterxml.jackson.core.JsonParser;
20 import com.fasterxml.jackson.databind.JsonNode;
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import org.apache.logging.log4j.core.config.AbstractConfiguration;
23 import org.apache.logging.log4j.core.config.Configuration;
24 import org.apache.logging.log4j.core.config.ConfigurationSource;
25 import org.apache.logging.log4j.core.config.FileConfigurationMonitor;
26 import org.apache.logging.log4j.core.config.Node;
27 import org.apache.logging.log4j.core.config.Reconfigurable;
28 import org.apache.logging.log4j.core.config.plugins.util.PluginType;
29 import org.apache.logging.log4j.core.config.plugins.util.ResolverUtil;
30 import org.apache.logging.log4j.core.config.status.StatusConfiguration;
31 import org.apache.logging.log4j.core.util.Patterns;
32
33 import java.io.ByteArrayInputStream;
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Map;
42
43
44
45
46 public class JsonConfiguration extends AbstractConfiguration implements Reconfigurable {
47
48 private static final long serialVersionUID = 1L;
49 private static final String[] VERBOSE_CLASSES = new String[] { ResolverUtil.class.getName() };
50 private final List<Status> status = new ArrayList<>();
51 private JsonNode root;
52
53 public JsonConfiguration(final ConfigurationSource configSource) {
54 super(configSource);
55 final File configFile = configSource.getFile();
56 byte[] buffer;
57 try {
58 try (final InputStream configStream = configSource.getInputStream()) {
59 buffer = toByteArray(configStream);
60 }
61 final InputStream is = new ByteArrayInputStream(buffer);
62 root = getObjectMapper().readTree(is);
63 if (root.size() == 1) {
64 for (final JsonNode node : root) {
65 root = node;
66 }
67 }
68 processAttributes(rootNode, root);
69 final StatusConfiguration statusConfig = new StatusConfiguration().withVerboseClasses(VERBOSE_CLASSES)
70 .withStatus(getDefaultStatus());
71 for (final Map.Entry<String, String> entry : rootNode.getAttributes().entrySet()) {
72 final String key = entry.getKey();
73 final String value = getStrSubstitutor().replace(entry.getValue());
74
75 if ("status".equalsIgnoreCase(key)) {
76 statusConfig.withStatus(value);
77 } else if ("dest".equalsIgnoreCase(key)) {
78 statusConfig.withDestination(value);
79 } else if ("shutdownHook".equalsIgnoreCase(key)) {
80 isShutdownHookEnabled = !"disable".equalsIgnoreCase(value);
81 } else if ("verbose".equalsIgnoreCase(entry.getKey())) {
82 statusConfig.withVerbosity(value);
83 } else if ("packages".equalsIgnoreCase(key)) {
84 pluginPackages.addAll(Arrays.asList(value.split(Patterns.COMMA_SEPARATOR)));
85 } else if ("name".equalsIgnoreCase(key)) {
86 setName(value);
87 } else if ("monitorInterval".equalsIgnoreCase(key)) {
88 final int intervalSeconds = Integer.parseInt(value);
89 if (intervalSeconds > 0 && configFile != null) {
90 monitor = new FileConfigurationMonitor(this, configFile, listeners, intervalSeconds);
91 }
92 } else if ("advertiser".equalsIgnoreCase(key)) {
93 createAdvertiser(value, configSource, buffer, "application/json");
94 }
95 }
96 statusConfig.initialize();
97 if (getName() == null) {
98 setName(configSource.getLocation());
99 }
100 } catch (final Exception ex) {
101 LOGGER.error("Error parsing {}", configSource.getLocation(), ex);
102 }
103 }
104
105 protected ObjectMapper getObjectMapper() {
106 return new ObjectMapper().configure(JsonParser.Feature.ALLOW_COMMENTS, true);
107 }
108
109 @Override
110 public void setup() {
111 final Iterator<Map.Entry<String, JsonNode>> iter = root.fields();
112 final List<Node> children = rootNode.getChildren();
113 while (iter.hasNext()) {
114 final Map.Entry<String, JsonNode> entry = iter.next();
115 final JsonNode n = entry.getValue();
116 if (n.isObject()) {
117 LOGGER.debug("Processing node for object {}", entry.getKey());
118 children.add(constructNode(entry.getKey(), rootNode, n));
119 } else if (n.isArray()) {
120 LOGGER.error("Arrays are not supported at the root configuration.");
121 }
122 }
123 LOGGER.debug("Completed parsing configuration");
124 if (status.size() > 0) {
125 for (final Status s : status) {
126 LOGGER.error("Error processing element " + s.name + ": " + s.errorType);
127 }
128 }
129 }
130
131 @Override
132 public Configuration reconfigure() {
133 try {
134 final ConfigurationSource source = getConfigurationSource().resetInputStream();
135 if (source == null) {
136 return null;
137 }
138 return new JsonConfiguration(source);
139 } catch (final IOException ex) {
140 LOGGER.error("Cannot locate file {}", getConfigurationSource(), ex);
141 }
142 return null;
143 }
144
145 private Node constructNode(final String name, final Node parent, final JsonNode jsonNode) {
146 final PluginType<?> type = pluginManager.getPluginType(name);
147 final Node node = new Node(parent, name, type);
148 processAttributes(node, jsonNode);
149 final Iterator<Map.Entry<String, JsonNode>> iter = jsonNode.fields();
150 final List<Node> children = node.getChildren();
151 while (iter.hasNext()) {
152 final Map.Entry<String, JsonNode> entry = iter.next();
153 final JsonNode n = entry.getValue();
154 if (n.isArray() || n.isObject()) {
155 if (type == null) {
156 status.add(new Status(name, n, ErrorType.CLASS_NOT_FOUND));
157 }
158 if (n.isArray()) {
159 LOGGER.debug("Processing node for array {}", entry.getKey());
160 for (int i = 0; i < n.size(); ++i) {
161 final String pluginType = getType(n.get(i), entry.getKey());
162 final PluginType<?> entryType = pluginManager.getPluginType(pluginType);
163 final Node item = new Node(node, entry.getKey(), entryType);
164 processAttributes(item, n.get(i));
165 if (pluginType.equals(entry.getKey())) {
166 LOGGER.debug("Processing {}[{}]", entry.getKey(), i);
167 } else {
168 LOGGER.debug("Processing {} {}[{}]", pluginType, entry.getKey(), i);
169 }
170 final Iterator<Map.Entry<String, JsonNode>> itemIter = n.get(i).fields();
171 final List<Node> itemChildren = item.getChildren();
172 while (itemIter.hasNext()) {
173 final Map.Entry<String, JsonNode> itemEntry = itemIter.next();
174 if (itemEntry.getValue().isObject()) {
175 LOGGER.debug("Processing node for object {}", itemEntry.getKey());
176 itemChildren.add(constructNode(itemEntry.getKey(), item, itemEntry.getValue()));
177 } else if (itemEntry.getValue().isArray()) {
178 final JsonNode array = itemEntry.getValue();
179 final String entryName = itemEntry.getKey();
180 LOGGER.debug("Processing array for object {}", entryName);
181 for (int j = 0; j < array.size(); ++j) {
182 itemChildren.add(constructNode(entryName, item, array.get(j)));
183 }
184 }
185
186 }
187 children.add(item);
188 }
189 } else {
190 LOGGER.debug("Processing node for object {}", entry.getKey());
191 children.add(constructNode(entry.getKey(), node, n));
192 }
193 } else {
194 LOGGER.debug("Node {} is of type {}", entry.getKey(), n.getNodeType());
195 }
196 }
197
198 String t;
199 if (type == null) {
200 t = "null";
201 } else {
202 t = type.getElementName() + ':' + type.getPluginClass();
203 }
204
205 final String p = node.getParent() == null ? "null" : node.getParent().getName() == null ? "root" : node
206 .getParent().getName();
207 LOGGER.debug("Returning {} with parent {} of type {}", node.getName(), p, t);
208 return node;
209 }
210
211 private String getType(final JsonNode node, final String name) {
212 final Iterator<Map.Entry<String, JsonNode>> iter = node.fields();
213 while (iter.hasNext()) {
214 final Map.Entry<String, JsonNode> entry = iter.next();
215 if (entry.getKey().equalsIgnoreCase("type")) {
216 final JsonNode n = entry.getValue();
217 if (n.isValueNode()) {
218 return n.asText();
219 }
220 }
221 }
222 return name;
223 }
224
225 private void processAttributes(final Node parent, final JsonNode node) {
226 final Map<String, String> attrs = parent.getAttributes();
227 final Iterator<Map.Entry<String, JsonNode>> iter = node.fields();
228 while (iter.hasNext()) {
229 final Map.Entry<String, JsonNode> entry = iter.next();
230 if (!entry.getKey().equalsIgnoreCase("type")) {
231 final JsonNode n = entry.getValue();
232 if (n.isValueNode()) {
233 attrs.put(entry.getKey(), n.asText());
234 }
235 }
236 }
237 }
238
239 @Override
240 public String toString() {
241 return getClass().getSimpleName() + "[location=" + getConfigurationSource() + "]";
242 }
243
244
245
246
247 private enum ErrorType {
248 CLASS_NOT_FOUND
249 }
250
251
252
253
254 private static class Status {
255 private final JsonNode node;
256 private final String name;
257 private final ErrorType errorType;
258
259 public Status(final String name, final JsonNode node, final ErrorType errorType) {
260 this.name = name;
261 this.node = node;
262 this.errorType = errorType;
263 }
264
265 @Override
266 public String toString() {
267 return "Status [name=" + name + ", errorType=" + errorType + ", node=" + node + "]";
268 }
269 }
270 }