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