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