001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.config.json;
018
019import com.fasterxml.jackson.core.JsonParser;
020import com.fasterxml.jackson.databind.JsonNode;
021import com.fasterxml.jackson.databind.ObjectMapper;
022import org.apache.logging.log4j.core.config.AbstractConfiguration;
023import org.apache.logging.log4j.core.config.Configuration;
024import org.apache.logging.log4j.core.config.ConfigurationSource;
025import org.apache.logging.log4j.core.config.ConfiguratonFileWatcher;
026import org.apache.logging.log4j.core.config.LoggerConfig;
027import org.apache.logging.log4j.core.config.Node;
028import org.apache.logging.log4j.core.config.Reconfigurable;
029import org.apache.logging.log4j.core.config.plugins.util.PluginType;
030import org.apache.logging.log4j.core.config.plugins.util.ResolverUtil;
031import org.apache.logging.log4j.core.config.status.StatusConfiguration;
032import org.apache.logging.log4j.core.util.FileWatcher;
033import org.apache.logging.log4j.core.util.Patterns;
034
035import java.io.ByteArrayInputStream;
036import java.io.File;
037import java.io.IOException;
038import java.io.InputStream;
039import java.util.ArrayList;
040import java.util.Arrays;
041import java.util.Iterator;
042import java.util.List;
043import java.util.Map;
044
045/**
046 * Creates a Node hierarchy from a JSON file.
047 */
048public class JsonConfiguration extends AbstractConfiguration implements Reconfigurable {
049
050    private static final long serialVersionUID = 1L;
051    private static final String[] VERBOSE_CLASSES = new String[] { ResolverUtil.class.getName() };
052    private final List<Status> status = new ArrayList<>();
053    private JsonNode root;
054
055    public JsonConfiguration(final ConfigurationSource configSource) {
056        super(configSource);
057        final File configFile = configSource.getFile();
058        byte[] buffer;
059        try {
060            try (final InputStream configStream = configSource.getInputStream()) {
061                buffer = toByteArray(configStream);
062            }
063            final InputStream is = new ByteArrayInputStream(buffer);
064            root = getObjectMapper().readTree(is);
065            if (root.size() == 1) {
066                for (final JsonNode node : root) {
067                    root = node;
068                }
069            }
070            processAttributes(rootNode, root);
071            final StatusConfiguration statusConfig = new StatusConfiguration().withVerboseClasses(VERBOSE_CLASSES)
072                    .withStatus(getDefaultStatus());
073            for (final Map.Entry<String, String> entry : rootNode.getAttributes().entrySet()) {
074                final String key = entry.getKey();
075                final String value = getStrSubstitutor().replace(entry.getValue());
076                // TODO: this duplicates a lot of the XmlConfiguration constructor
077                if ("status".equalsIgnoreCase(key)) {
078                    statusConfig.withStatus(value);
079                } else if ("dest".equalsIgnoreCase(key)) {
080                    statusConfig.withDestination(value);
081                } else if ("shutdownHook".equalsIgnoreCase(key)) {
082                    isShutdownHookEnabled = !"disable".equalsIgnoreCase(value);
083                } else if ("verbose".equalsIgnoreCase(entry.getKey())) {
084                    statusConfig.withVerbosity(value);
085                } else if ("packages".equalsIgnoreCase(key)) {
086                    pluginPackages.addAll(Arrays.asList(value.split(Patterns.COMMA_SEPARATOR)));
087                } else if ("name".equalsIgnoreCase(key)) {
088                    setName(value);
089                } else if ("monitorInterval".equalsIgnoreCase(key)) {
090                    final int intervalSeconds = Integer.parseInt(value);
091                    if (intervalSeconds > 0) {
092                        getWatchManager().setIntervalSeconds(intervalSeconds);
093                        if (configFile != null) {
094                            FileWatcher watcher = new ConfiguratonFileWatcher(this, listeners);
095                            getWatchManager().watchFile(configFile, watcher);
096                        }
097                    }
098                } else if ("advertiser".equalsIgnoreCase(key)) {
099                    createAdvertiser(value, configSource, buffer, "application/json");
100                }
101            }
102            statusConfig.initialize();
103            if (getName() == null) {
104                setName(configSource.getLocation());
105            }
106        } catch (final Exception ex) {
107            LOGGER.error("Error parsing {}", configSource.getLocation(), ex);
108        }
109    }
110
111    protected ObjectMapper getObjectMapper() {
112        return new ObjectMapper().configure(JsonParser.Feature.ALLOW_COMMENTS, true);
113    }
114
115    @Override
116    public void setup() {
117        final Iterator<Map.Entry<String, JsonNode>> iter = root.fields();
118        final List<Node> children = rootNode.getChildren();
119        while (iter.hasNext()) {
120            final Map.Entry<String, JsonNode> entry = iter.next();
121            final JsonNode n = entry.getValue();
122            if (n.isObject()) {
123                LOGGER.debug("Processing node for object {}", entry.getKey());
124                children.add(constructNode(entry.getKey(), rootNode, n));
125            } else if (n.isArray()) {
126                LOGGER.error("Arrays are not supported at the root configuration.");
127            }
128        }
129        LOGGER.debug("Completed parsing configuration");
130        if (status.size() > 0) {
131            for (final Status s : status) {
132                LOGGER.error("Error processing element " + s.name + ": " + s.errorType);
133            }
134        }
135    }
136
137    @Override
138    public Configuration reconfigure() {
139        try {
140            final ConfigurationSource source = getConfigurationSource().resetInputStream();
141            if (source == null) {
142                return null;
143            }
144            return new JsonConfiguration(source);
145        } catch (final IOException ex) {
146            LOGGER.error("Cannot locate file {}", getConfigurationSource(), ex);
147        }
148        return null;
149    }
150
151    private Node constructNode(final String name, final Node parent, final JsonNode jsonNode) {
152        final PluginType<?> type = pluginManager.getPluginType(name);
153        final Node node = new Node(parent, name, type);
154        processAttributes(node, jsonNode);
155        final Iterator<Map.Entry<String, JsonNode>> iter = jsonNode.fields();
156        final List<Node> children = node.getChildren();
157        while (iter.hasNext()) {
158            final Map.Entry<String, JsonNode> entry = iter.next();
159            final JsonNode n = entry.getValue();
160            if (n.isArray() || n.isObject()) {
161                if (type == null) {
162                    status.add(new Status(name, n, ErrorType.CLASS_NOT_FOUND));
163                }
164                if (n.isArray()) {
165                    LOGGER.debug("Processing node for array {}", entry.getKey());
166                    for (int i = 0; i < n.size(); ++i) {
167                        final String pluginType = getType(n.get(i), entry.getKey());
168                        final PluginType<?> entryType = pluginManager.getPluginType(pluginType);
169                        final Node item = new Node(node, entry.getKey(), entryType);
170                        processAttributes(item, n.get(i));
171                        if (pluginType.equals(entry.getKey())) {
172                            LOGGER.debug("Processing {}[{}]", entry.getKey(), i);
173                        } else {
174                            LOGGER.debug("Processing {} {}[{}]", pluginType, entry.getKey(), i);
175                        }
176                        final Iterator<Map.Entry<String, JsonNode>> itemIter = n.get(i).fields();
177                        final List<Node> itemChildren = item.getChildren();
178                        while (itemIter.hasNext()) {
179                            final Map.Entry<String, JsonNode> itemEntry = itemIter.next();
180                            if (itemEntry.getValue().isObject()) {
181                                LOGGER.debug("Processing node for object {}", itemEntry.getKey());
182                                itemChildren.add(constructNode(itemEntry.getKey(), item, itemEntry.getValue()));
183                            } else if (itemEntry.getValue().isArray()) {
184                                final JsonNode array = itemEntry.getValue();
185                                final String entryName = itemEntry.getKey();
186                                LOGGER.debug("Processing array for object {}", entryName);
187                                for (int j = 0; j < array.size(); ++j) {
188                                    itemChildren.add(constructNode(entryName, item, array.get(j)));
189                                }
190                            }
191
192                        }
193                        children.add(item);
194                    }
195                } else {
196                    LOGGER.debug("Processing node for object {}", entry.getKey());
197                    children.add(constructNode(entry.getKey(), node, n));
198                }
199            } else {
200                LOGGER.debug("Node {} is of type {}", entry.getKey(), n.getNodeType());
201            }
202        }
203
204        String t;
205        if (type == null) {
206            t = "null";
207        } else {
208            t = type.getElementName() + ':' + type.getPluginClass();
209        }
210
211        final String p = node.getParent() == null ? "null"
212                : node.getParent().getName() == null ? LoggerConfig.ROOT : node.getParent().getName();
213        LOGGER.debug("Returning {} with parent {} of type {}", node.getName(), p, t);
214        return node;
215    }
216
217    private String getType(final JsonNode node, final String name) {
218        final Iterator<Map.Entry<String, JsonNode>> iter = node.fields();
219        while (iter.hasNext()) {
220            final Map.Entry<String, JsonNode> entry = iter.next();
221            if (entry.getKey().equalsIgnoreCase("type")) {
222                final JsonNode n = entry.getValue();
223                if (n.isValueNode()) {
224                    return n.asText();
225                }
226            }
227        }
228        return name;
229    }
230
231    private void processAttributes(final Node parent, final JsonNode node) {
232        final Map<String, String> attrs = parent.getAttributes();
233        final Iterator<Map.Entry<String, JsonNode>> iter = node.fields();
234        while (iter.hasNext()) {
235            final Map.Entry<String, JsonNode> entry = iter.next();
236            if (!entry.getKey().equalsIgnoreCase("type")) {
237                final JsonNode n = entry.getValue();
238                if (n.isValueNode()) {
239                    attrs.put(entry.getKey(), n.asText());
240                }
241            }
242        }
243    }
244
245    @Override
246    public String toString() {
247        return getClass().getSimpleName() + "[location=" + getConfigurationSource() + "]";
248    }
249
250    /**
251     * The error that occurred.
252     */
253    private enum ErrorType {
254        CLASS_NOT_FOUND
255    }
256
257    /**
258     * Status for recording errors.
259     */
260    private static class Status {
261        private final JsonNode node;
262        private final String name;
263        private final ErrorType errorType;
264
265        public Status(final String name, final JsonNode node, final ErrorType errorType) {
266            this.name = name;
267            this.node = node;
268            this.errorType = errorType;
269        }
270
271        @Override
272        public String toString() {
273            return "Status [name=" + name + ", errorType=" + errorType + ", node=" + node + "]";
274        }
275    }
276}