1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.config;
18
19 import org.apache.logging.log4j.Level;
20 import org.apache.logging.log4j.core.config.plugins.PluginManager;
21 import org.apache.logging.log4j.core.config.plugins.PluginType;
22 import org.apache.logging.log4j.core.config.plugins.ResolverUtil;
23 import org.apache.logging.log4j.core.helpers.FileUtils;
24 import org.apache.logging.log4j.status.StatusConsoleListener;
25 import org.apache.logging.log4j.status.StatusListener;
26 import org.apache.logging.log4j.status.StatusLogger;
27 import org.codehaus.jackson.JsonNode;
28 import org.codehaus.jackson.JsonParser;
29 import org.codehaus.jackson.map.ObjectMapper;
30
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.PrintStream;
40 import java.net.URI;
41 import java.net.URISyntaxException;
42 import java.util.ArrayList;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Locale;
46 import java.util.Map;
47
48
49
50
51 public class JSONConfiguration extends BaseConfiguration implements Reconfigurable {
52
53 private static final String[] VERBOSE_CLASSES = new String[] {ResolverUtil.class.getName()};
54
55 private static final int BUF_SIZE = 16384;
56
57 private List<Status> status = new ArrayList<Status>();
58
59 private JsonNode root;
60
61 private List<String> messages = new ArrayList<String>();
62
63 private File configFile;
64
65 public JSONConfiguration(ConfigurationFactory.ConfigurationSource configSource) {
66 this.configFile = configSource.getFile();
67 byte[] buffer;
68
69 try {
70 InputStream configStream = configSource.getInputStream();
71 buffer = toByteArray(configStream);
72 configStream.close();
73 InputStream is = new ByteArrayInputStream(buffer);
74 ObjectMapper mapper = new ObjectMapper().configure(JsonParser.Feature.ALLOW_COMMENTS, true);
75 root = mapper.readTree(is);
76 if (root.size() == 1) {
77 Iterator<JsonNode> i = root.getElements();
78 root = i.next();
79 }
80 processAttributes(rootNode, root);
81 Level status = Level.OFF;
82 boolean verbose = false;
83 PrintStream stream = System.out;
84 for (Map.Entry<String, String> entry : rootNode.getAttributes().entrySet()) {
85 if ("status".equalsIgnoreCase(entry.getKey())) {
86 status = Level.toLevel(getSubst().replace(entry.getValue()), null);
87 if (status == null) {
88 status = Level.ERROR;
89 messages.add("Invalid status specified: " + entry.getValue() + ". Defaulting to ERROR");
90 }
91 } else if ("dest".equalsIgnoreCase(entry.getKey())) {
92 String dest = entry.getValue();
93 if (dest != null) {
94 if (dest.equalsIgnoreCase("err")) {
95 stream = System.err;
96 } else {
97 try {
98 File destFile = FileUtils.fileFromURI(new URI(dest));
99 stream = new PrintStream(new FileOutputStream(destFile));
100 } catch (URISyntaxException use) {
101 System.err.println("Unable to write to " + dest + ". Writing to stdout");
102 }
103 }
104 }
105 } else if ("verbose".equalsIgnoreCase(entry.getKey())) {
106 verbose = Boolean.parseBoolean(getSubst().replace(entry.getValue()));
107 } else if ("packages".equalsIgnoreCase(entry.getKey())) {
108 String[] packages = getSubst().replace(entry.getValue()).split(",");
109 for (String p : packages) {
110 PluginManager.addPackage(p);
111 }
112 } else if ("name".equalsIgnoreCase(entry.getKey())) {
113 setName(getSubst().replace(entry.getValue()));
114 } else if ("monitorInterval".equalsIgnoreCase(entry.getKey())) {
115 int interval = Integer.parseInt(getSubst().replace(entry.getValue()));
116 if (interval > 0 && configFile != null) {
117 monitor = new FileConfigurationMonitor(this, configFile, listeners, interval);
118 }
119 }
120 }
121
122 Iterator<StatusListener> statusIter = ((StatusLogger) LOGGER).getListeners();
123 boolean found = false;
124 while (statusIter.hasNext()) {
125 StatusListener listener = statusIter.next();
126 if (listener instanceof StatusConsoleListener) {
127 found = true;
128 ((StatusConsoleListener) listener).setLevel(status);
129 if (!verbose) {
130 ((StatusConsoleListener) listener).setFilters(VERBOSE_CLASSES);
131 }
132 }
133 }
134 if (!found && status != Level.OFF) {
135 StatusConsoleListener listener = new StatusConsoleListener(status, stream);
136 if (!verbose) {
137 listener.setFilters(VERBOSE_CLASSES);
138 }
139 ((StatusLogger) LOGGER).registerListener(listener);
140 for (String msg : messages) {
141 LOGGER.error(msg);
142 }
143 }
144 if (getName() == null) {
145 setName(configSource.getLocation());
146 }
147 } catch (Exception ex) {
148 LOGGER.error("Error parsing " + configSource.getLocation(), ex);
149 ex.printStackTrace();
150 }
151 }
152
153 @Override
154 public void setup() {
155 Iterator<Map.Entry<String, JsonNode>> iter = root.getFields();
156 List<Node> children = rootNode.getChildren();
157 while (iter.hasNext()) {
158 Map.Entry<String, JsonNode> entry = iter.next();
159 JsonNode n = entry.getValue();
160 if (n.isObject()) {
161 LOGGER.debug("Processing node for object " + entry.getKey());
162 children.add(constructNode(entry.getKey(), rootNode, n));
163 } else if (n.isArray()) {
164 LOGGER.error("Arrays are not supported at the root configuration.");
165 }
166 }
167 LOGGER.debug("Completed parsing configuration");
168 if (status.size() > 0) {
169 for (Status s : status) {
170 LOGGER.error("Error processing element " + s.name + ": " + s.errorType);
171 }
172 return;
173 }
174 }
175
176 public Configuration reconfigure() {
177 if (configFile != null) {
178 try {
179 ConfigurationFactory.ConfigurationSource source =
180 new ConfigurationFactory.ConfigurationSource(new FileInputStream(configFile), configFile);
181 return new JSONConfiguration(source);
182 } catch (FileNotFoundException ex) {
183 LOGGER.error("Cannot locate file " + configFile, ex);
184 }
185 }
186 return null;
187 }
188
189 private Node constructNode(String name, Node parent, JsonNode jsonNode) {
190 PluginType type = getPluginManager().getPluginType(name);
191 Node node = new Node(parent, name, type);
192 processAttributes(node, jsonNode);
193 Iterator<Map.Entry<String, JsonNode>> iter = jsonNode.getFields();
194 List<Node> children = node.getChildren();
195 while (iter.hasNext()) {
196 Map.Entry<String, JsonNode> entry = iter.next();
197 JsonNode n = entry.getValue();
198 if (n.isArray() || n.isObject()) {
199 if (type == null) {
200 status.add(new Status(name, n, ErrorType.CLASS_NOT_FOUND));
201 }
202 if (n.isArray()) {
203 LOGGER.debug("Processing node for array " + entry.getKey());
204 for (int i = 0; i < n.size(); ++i) {
205 String pluginType = getType(n.get(i), entry.getKey());
206 PluginType entryType = getPluginManager().getPluginType(pluginType);
207 Node item = new Node(node, entry.getKey(), entryType);
208 processAttributes(item, n.get(i));
209 if (pluginType.equals(entry.getKey())) {
210 LOGGER.debug("Processing " + entry.getKey() + "[" + i + "]");
211 } else {
212 LOGGER.debug("Processing " + pluginType + " " + entry.getKey() + "[" + i + "]");
213 }
214 Iterator<Map.Entry<String, JsonNode>> itemIter = n.get(i).getFields();
215 List<Node> itemChildren = item.getChildren();
216 while (itemIter.hasNext()) {
217 Map.Entry<String, JsonNode> itemEntry = itemIter.next();
218 if (itemEntry.getValue().isObject()) {
219 LOGGER.debug("Processing node for object " + itemEntry.getKey());
220 itemChildren.add(constructNode(itemEntry.getKey(), item, itemEntry.getValue()));
221 }
222 }
223 children.add(item);
224 }
225 } else {
226 LOGGER.debug("Processing node for object " + entry.getKey());
227 children.add(constructNode(entry.getKey(), node, n));
228 }
229 }
230 }
231
232 String t;
233 if (type == null) {
234 t = "null";
235 } else {
236 t = type.getElementName() + ":" + type.getPluginClass();
237 }
238
239 String p = node.getParent() == null ? "null" : node.getParent().getName() == null ?
240 "root" : node.getParent().getName();
241 LOGGER.debug("Returning " + node.getName() + " with parent " + p + " of type " + t);
242 return node;
243 }
244
245 private String getType(JsonNode node, String name) {
246 Iterator<Map.Entry<String, JsonNode>> iter = node.getFields();
247 while (iter.hasNext()) {
248 Map.Entry<String, JsonNode> entry = iter.next();
249 if (entry.getKey().equalsIgnoreCase("type")) {
250 JsonNode n = entry.getValue();
251 if (n.isValueNode()) {
252 return n.asText();
253 }
254 }
255 }
256 return name;
257 }
258
259 private void processAttributes(Node parent, JsonNode node) {
260 Map<String, String> attrs = parent.getAttributes();
261 Iterator<Map.Entry<String, JsonNode>> iter = node.getFields();
262 while (iter.hasNext()) {
263 Map.Entry<String, JsonNode> entry = iter.next();
264 if (!entry.getKey().equalsIgnoreCase("type")) {
265 JsonNode n = entry.getValue();
266 if (n.isValueNode()) {
267 attrs.put(entry.getKey(), n.asText());
268 }
269 }
270 }
271 }
272
273 protected byte[] toByteArray(InputStream is) throws IOException {
274 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
275
276 int nRead;
277 byte[] data = new byte[BUF_SIZE];
278
279 while ((nRead = is.read(data, 0, data.length)) != -1) {
280 buffer.write(data, 0, nRead);
281 }
282
283 return buffer.toByteArray();
284 }
285
286
287
288
289 private enum ErrorType {
290 CLASS_NOT_FOUND
291 }
292
293
294
295
296 private class Status {
297 private JsonNode node;
298 private String name;
299 private ErrorType errorType;
300
301 public Status(String name, JsonNode node, ErrorType errorType) {
302 this.name = name;
303 this.node = node;
304 this.errorType = errorType;
305 }
306 }
307 }