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.log4j.builders;
18  
19  import org.apache.log4j.Appender;
20  import org.apache.log4j.Layout;
21  import org.apache.log4j.builders.appender.AppenderBuilder;
22  import org.apache.log4j.builders.filter.FilterBuilder;
23  import org.apache.log4j.builders.layout.LayoutBuilder;
24  import org.apache.log4j.builders.rewrite.RewritePolicyBuilder;
25  import org.apache.log4j.config.PropertiesConfiguration;
26  import org.apache.log4j.rewrite.RewritePolicy;
27  import org.apache.log4j.spi.Filter;
28  import org.apache.log4j.xml.XmlConfiguration;
29  import org.apache.logging.log4j.Logger;
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.status.StatusLogger;
33  import org.apache.logging.log4j.util.LoaderUtil;
34  import org.w3c.dom.Element;
35  
36  import java.lang.reflect.Constructor;
37  import java.lang.reflect.InvocationTargetException;
38  import java.util.Map;
39  import java.util.Properties;
40  
41  /**
42   *
43   */
44  public class BuilderManager {
45  
46      public static final String CATEGORY = "Log4j Builder";
47      private static final Logger LOGGER = StatusLogger.getLogger();
48      private final Map<String, PluginType<?>> plugins;
49      private static Class<?>[] constructorParams = new Class[] { String.class, Properties.class};
50  
51      public BuilderManager() {
52          final PluginManager manager = new PluginManager(CATEGORY);
53          manager.collectPlugins();
54          plugins = manager.getPlugins();
55      }
56  
57      public Appender parseAppender(String className, Element appenderElement, XmlConfiguration config) {
58          PluginType<?> plugin = plugins.get(className.toLowerCase());
59          if (plugin != null) {
60              try {
61                  @SuppressWarnings("unchecked")
62                  AppenderBuilder builder = (AppenderBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
63                  return builder.parseAppender(appenderElement, config);
64              } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
65                  LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
66              }
67          }
68          return null;
69      }
70  
71      public Appender parseAppender(String name, String className, String prefix, String layoutPrefix,
72              String filterPrefix, Properties props, PropertiesConfiguration config) {
73          PluginType<?> plugin = plugins.get(className.toLowerCase());
74          if (plugin != null) {
75              AppenderBuilder builder = createBuilder(plugin, prefix, props);
76              if (builder != null) {
77                  return builder.parseAppender(name, prefix, layoutPrefix, filterPrefix, props, config);
78              }
79          }
80          return null;
81      }
82  
83      public Filter parseFilter(String className, Element filterElement, XmlConfiguration config) {
84          PluginType<?> plugin = plugins.get(className.toLowerCase());
85          if (plugin != null) {
86              try {
87                  @SuppressWarnings("unchecked")
88                  FilterBuilder builder = (FilterBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
89                  return builder.parseFilter(filterElement, config);
90              } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
91                  LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
92              }
93          }
94          return null;
95      }
96  
97      public Filter parseFilter(String className, String filterPrefix, Properties props, PropertiesConfiguration config) {
98          PluginType<?> plugin = plugins.get(className.toLowerCase());
99          if (plugin != null) {
100             FilterBuilder builder = createBuilder(plugin, filterPrefix, props);
101             if (builder != null) {
102                 return builder.parseFilter(config);
103             }
104         }
105         return null;
106     }
107 
108     public Layout parseLayout(String className, Element layoutElement, XmlConfiguration config) {
109         PluginType<?> plugin = plugins.get(className.toLowerCase());
110         if (plugin != null) {
111             try {
112                 @SuppressWarnings("unchecked")
113                 LayoutBuilder builder = (LayoutBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
114                 return builder.parseLayout(layoutElement, config);
115             } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
116                 LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
117             }
118         }
119         return null;
120     }
121     public Layout parseLayout(String className, String layoutPrefix, Properties props, PropertiesConfiguration config) {
122         PluginType<?> plugin = plugins.get(className.toLowerCase());
123         if (plugin != null) {
124             LayoutBuilder builder = createBuilder(plugin, layoutPrefix, props);
125             if (builder != null) {
126                 return builder.parseLayout(config);
127             }
128         }
129         return null;
130     }
131 
132     public RewritePolicy parseRewritePolicy(String className, Element rewriteElement, XmlConfiguration config) {
133         PluginType<?> plugin = plugins.get(className.toLowerCase());
134         if (plugin != null) {
135             try {
136                 @SuppressWarnings("unchecked")
137                 RewritePolicyBuilder builder = (RewritePolicyBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
138                 return builder.parseRewritePolicy(rewriteElement, config);
139             } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
140                 LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
141             }
142         }
143         return null;
144     }
145     public RewritePolicy parseRewritePolicy(String className, String policyPrefix, Properties props, PropertiesConfiguration config) {
146         PluginType<?> plugin = plugins.get(className.toLowerCase());
147         if (plugin != null) {
148             RewritePolicyBuilder builder = createBuilder(plugin, policyPrefix, props);
149             if (builder != null) {
150                 return builder.parseRewritePolicy(config);
151             }
152         }
153         return null;
154     }
155 
156     private <T extends AbstractBuilder> T createBuilder(PluginType<?> plugin, String prefix, Properties props) {
157         try {
158             Class<?> clazz = plugin.getPluginClass();
159             if (AbstractBuilder.class.isAssignableFrom(clazz)) {
160                 @SuppressWarnings("unchecked")
161                 Constructor<T> constructor =
162                         (Constructor<T>) clazz.getConstructor(constructorParams);
163                 return constructor.newInstance(prefix, props);
164             } else {
165                 @SuppressWarnings("unchecked")
166                 T builder = (T) LoaderUtil.newInstanceOf(clazz);
167                 return builder;
168             }
169         } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ex) {
170             LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
171             return null;
172         }
173     }
174 
175 }