View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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.config.AbstractConfiguration;
26  import org.apache.logging.log4j.core.config.ConfigurationSource;
27  import org.apache.logging.log4j.core.config.ConfiguratonFileWatcher;
28  import org.apache.logging.log4j.core.config.Node;
29  import org.apache.logging.log4j.core.config.Reconfigurable;
30  import org.apache.logging.log4j.core.config.builder.api.Component;
31  import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
32  import org.apache.logging.log4j.core.config.plugins.util.PluginType;
33  import org.apache.logging.log4j.core.config.plugins.util.ResolverUtil;
34  import org.apache.logging.log4j.core.config.status.StatusConfiguration;
35  import org.apache.logging.log4j.core.util.FileWatcher;
36  import org.apache.logging.log4j.core.util.Patterns;
37  
38  /**
39   * This is the general version of the Configuration created by the Builder. It may be extended to
40   * enhance its functionality.
41   *
42   * @since 2.4
43   */
44  public class BuiltConfiguration extends AbstractConfiguration {
45      private static final String[] VERBOSE_CLASSES = new String[] { ResolverUtil.class.getName() };
46      private final StatusConfiguration statusConfig;
47      protected Component rootComponent;
48      private Component loggersComponent;
49      private Component appendersComponent;
50      private Component filtersComponent;
51      private Component propertiesComponent;
52      private Component customLevelsComponent;
53      private Component scriptsComponent;
54      private String contentType = "text";
55  
56      public BuiltConfiguration(final ConfigurationSource source, final Component rootComponent) {
57          super(source);
58          statusConfig = new StatusConfiguration().withVerboseClasses(VERBOSE_CLASSES).withStatus(getDefaultStatus());
59          for (final Component component : rootComponent.getComponents()) {
60              switch (component.getPluginType()) {
61                  case "Scripts": {
62                      scriptsComponent = component;
63                      break;
64                  }
65                  case "Loggers": {
66                      loggersComponent = component;
67                      break;
68                  }
69                  case "Appenders": {
70                      appendersComponent = component;
71                      break;
72                  }
73                  case "Filters": {
74                      filtersComponent = component;
75                      break;
76                  }
77                  case "Properties": {
78                      propertiesComponent = component;
79                      break;
80                  }
81                  case "CustomLevels": {
82                      customLevelsComponent = component;
83                      break;
84                  }
85              }
86          }
87          this.rootComponent = rootComponent;
88      }
89  
90      @Override
91      public void setup() {
92          final List<Node> children = rootNode.getChildren();
93          if (propertiesComponent.getComponents().size() > 0) {
94              children.add(convertToNode(rootNode, propertiesComponent));
95          }
96          if (scriptsComponent.getComponents().size() > 0) {
97              children.add(convertToNode(rootNode, scriptsComponent));
98          }
99          if (customLevelsComponent.getComponents().size() > 0) {
100             children.add(convertToNode(rootNode, customLevelsComponent));
101         }
102         children.add(convertToNode(rootNode, loggersComponent));
103         children.add(convertToNode(rootNode, appendersComponent));
104         if (filtersComponent.getComponents().size() > 0) {
105             if (filtersComponent.getComponents().size() == 1) {
106                 children.add(convertToNode(rootNode, filtersComponent.getComponents().get(0)));
107             } else {
108                 children.add(convertToNode(rootNode, filtersComponent));
109             }
110         }
111         rootComponent = null;
112     }
113 
114     public String getContentType() {
115         return this.contentType;
116     }
117 
118     public void setContentType(final String contentType) {
119         this.contentType = contentType;
120     }
121 
122     public void createAdvertiser(final String advertiserString, final ConfigurationSource configSource) {
123         byte[] buffer = null;
124         try {
125             if (configSource != null) {
126                 final InputStream is = configSource.getInputStream();
127                 if (is != null) {
128                     buffer = toByteArray(is);
129                 }
130             }
131         } catch (final IOException ioe) {
132             LOGGER.warn("Unable to read configuration source " + configSource.toString());
133         }
134         super.createAdvertiser(advertiserString, configSource, buffer, contentType);
135     }
136 
137     public StatusConfiguration getStatusConfiguration() {
138         return statusConfig;
139     }
140 
141     public void setPluginPackages(final String packages) {
142         pluginPackages.addAll(Arrays.asList(packages.split(Patterns.COMMA_SEPARATOR)));
143     }
144 
145     public void setShutdownHook(final String flag) {
146         isShutdownHookEnabled = !"disable".equalsIgnoreCase(flag);
147     }
148 
149     public void setMonitorInterval(final int intervalSeconds) {
150         if (this instanceof Reconfigurable && intervalSeconds > 0) {
151             final ConfigurationSource configSource = getConfigurationSource();
152             if (configSource != null) {
153                 final File configFile = configSource.getFile();
154                 if (intervalSeconds > 0) {
155                     getWatchManager().setIntervalSeconds(intervalSeconds);
156                     if (configFile != null) {
157                         final FileWatcher watcher = new ConfiguratonFileWatcher((Reconfigurable) this, listeners);
158                         getWatchManager().watchFile(configFile, watcher);
159                     }
160                 }
161             }
162         }
163     }
164 
165     public PluginManager getPluginManager() {
166         return pluginManager;
167     }
168 
169     protected Node convertToNode(final Node parent, final Component component) {
170         final String name = component.getPluginType();
171         final PluginType<?> pluginType = pluginManager.getPluginType(name);
172         final Node node = new Node(parent, name, pluginType);
173         node.getAttributes().putAll(component.getAttributes());
174         node.setValue(component.getValue());
175         final List<Node> children = node.getChildren();
176         for (final Component child : component.getComponents()) {
177             children.add(convertToNode(node, child));
178         }
179         return node;
180     }
181 }