1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.config.builder.impl;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.apache.logging.log4j.core.LoggerContext;
26 import org.apache.logging.log4j.core.config.AbstractConfiguration;
27 import org.apache.logging.log4j.core.config.ConfigurationSource;
28 import org.apache.logging.log4j.core.config.ConfiguratonFileWatcher;
29 import org.apache.logging.log4j.core.config.Node;
30 import org.apache.logging.log4j.core.config.Reconfigurable;
31 import org.apache.logging.log4j.core.config.builder.api.Component;
32 import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
33 import org.apache.logging.log4j.core.config.plugins.util.PluginType;
34 import org.apache.logging.log4j.core.config.plugins.util.ResolverUtil;
35 import org.apache.logging.log4j.core.config.status.StatusConfiguration;
36 import org.apache.logging.log4j.core.util.FileWatcher;
37 import org.apache.logging.log4j.core.util.Patterns;
38
39
40
41
42
43
44
45 public class BuiltConfiguration extends AbstractConfiguration {
46 private static final String[] VERBOSE_CLASSES = new String[] { ResolverUtil.class.getName() };
47 private final StatusConfiguration statusConfig;
48 protected Component rootComponent;
49 private Component loggersComponent;
50 private Component appendersComponent;
51 private Component filtersComponent;
52 private Component propertiesComponent;
53 private Component customLevelsComponent;
54 private Component scriptsComponent;
55 private String contentType = "text";
56
57 public BuiltConfiguration(final LoggerContext loggerContext, final ConfigurationSource source, final Component rootComponent) {
58 super(loggerContext, source);
59 statusConfig = new StatusConfiguration().withVerboseClasses(VERBOSE_CLASSES).withStatus(getDefaultStatus());
60 for (final Component component : rootComponent.getComponents()) {
61 switch (component.getPluginType()) {
62 case "Scripts": {
63 scriptsComponent = component;
64 break;
65 }
66 case "Loggers": {
67 loggersComponent = component;
68 break;
69 }
70 case "Appenders": {
71 appendersComponent = component;
72 break;
73 }
74 case "Filters": {
75 filtersComponent = component;
76 break;
77 }
78 case "Properties": {
79 propertiesComponent = component;
80 break;
81 }
82 case "CustomLevels": {
83 customLevelsComponent = component;
84 break;
85 }
86 }
87 }
88 this.rootComponent = rootComponent;
89 }
90
91 @Override
92 public void setup() {
93 final List<Node> children = rootNode.getChildren();
94 if (propertiesComponent.getComponents().size() > 0) {
95 children.add(convertToNode(rootNode, propertiesComponent));
96 }
97 if (scriptsComponent.getComponents().size() > 0) {
98 children.add(convertToNode(rootNode, scriptsComponent));
99 }
100 if (customLevelsComponent.getComponents().size() > 0) {
101 children.add(convertToNode(rootNode, customLevelsComponent));
102 }
103 children.add(convertToNode(rootNode, loggersComponent));
104 children.add(convertToNode(rootNode, appendersComponent));
105 if (filtersComponent.getComponents().size() > 0) {
106 if (filtersComponent.getComponents().size() == 1) {
107 children.add(convertToNode(rootNode, filtersComponent.getComponents().get(0)));
108 } else {
109 children.add(convertToNode(rootNode, filtersComponent));
110 }
111 }
112 rootComponent = null;
113 }
114
115 public String getContentType() {
116 return this.contentType;
117 }
118
119 public void setContentType(final String contentType) {
120 this.contentType = contentType;
121 }
122
123 public void createAdvertiser(final String advertiserString, final ConfigurationSource configSource) {
124 byte[] buffer = null;
125 try {
126 if (configSource != null) {
127 final InputStream is = configSource.getInputStream();
128 if (is != null) {
129 buffer = toByteArray(is);
130 }
131 }
132 } catch (final IOException ioe) {
133 LOGGER.warn("Unable to read configuration source " + configSource.toString());
134 }
135 super.createAdvertiser(advertiserString, configSource, buffer, contentType);
136 }
137
138 public StatusConfiguration getStatusConfiguration() {
139 return statusConfig;
140 }
141
142 public void setPluginPackages(final String packages) {
143 pluginPackages.addAll(Arrays.asList(packages.split(Patterns.COMMA_SEPARATOR)));
144 }
145
146 public void setShutdownHook(final String flag) {
147 isShutdownHookEnabled = !"disable".equalsIgnoreCase(flag);
148 }
149
150 public void setMonitorInterval(final int intervalSeconds) {
151 if (this instanceof Reconfigurable && intervalSeconds > 0) {
152 final ConfigurationSource configSource = getConfigurationSource();
153 if (configSource != null) {
154 final File configFile = configSource.getFile();
155 if (intervalSeconds > 0) {
156 getWatchManager().setIntervalSeconds(intervalSeconds);
157 if (configFile != null) {
158 final FileWatcher watcher = new ConfiguratonFileWatcher((Reconfigurable) this, listeners);
159 getWatchManager().watchFile(configFile, watcher);
160 }
161 }
162 }
163 }
164 }
165
166 @Override
167 public PluginManager getPluginManager() {
168 return pluginManager;
169 }
170
171 protected Node convertToNode(final Node parent, final Component component) {
172 final String name = component.getPluginType();
173 final PluginType<?> pluginType = pluginManager.getPluginType(name);
174 final Node node = new Node(parent, name, pluginType);
175 node.getAttributes().putAll(component.getAttributes());
176 node.setValue(component.getValue());
177 final List<Node> children = node.getChildren();
178 for (final Component child : component.getComponents()) {
179 children.add(convertToNode(node, child));
180 }
181 return node;
182 }
183 }