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