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                  AppenderBuilder builder = (AppenderBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
62                  return builder.parseAppender(appenderElement, config);
63              } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
64                  LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
65              }
66          }
67          return null;
68      }
69  
70      public Appender parseAppender(String name, String className, String prefix, String layoutPrefix,
71              String filterPrefix, Properties props, PropertiesConfiguration config) {
72          PluginType<?> plugin = plugins.get(className.toLowerCase());
73          if (plugin != null) {
74              AppenderBuilder builder = createBuilder(plugin, prefix, props);
75              if (builder != null) {
76                  return builder.parseAppender(name, prefix, layoutPrefix, filterPrefix, props, config);
77              }
78          }
79          return null;
80      }
81  
82      public Filter parseFilter(String className, Element filterElement, XmlConfiguration config) {
83          PluginType<?> plugin = plugins.get(className.toLowerCase());
84          if (plugin != null) {
85              try {
86                  FilterBuilder builder = (FilterBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
87                  return builder.parseFilter(filterElement, config);
88              } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
89                  LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
90              }
91          }
92          return null;
93      }
94  
95      public Filter parseFilter(String className, String filterPrefix, Properties props, PropertiesConfiguration config) {
96          PluginType<?> plugin = plugins.get(className.toLowerCase());
97          if (plugin != null) {
98              FilterBuilder builder = createBuilder(plugin, filterPrefix, props);
99              if (builder != null) {
100                 return builder.parseFilter(config);
101             }
102         }
103         return null;
104     }
105 
106     public Layout parseLayout(String className, Element layoutElement, XmlConfiguration config) {
107         PluginType<?> plugin = plugins.get(className.toLowerCase());
108         if (plugin != null) {
109             try {
110                 LayoutBuilder builder = (LayoutBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
111                 return builder.parseLayout(layoutElement, config);
112             } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
113                 LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
114             }
115         }
116         return null;
117     }
118     public Layout parseLayout(String className, String layoutPrefix, Properties props, PropertiesConfiguration config) {
119         PluginType<?> plugin = plugins.get(className.toLowerCase());
120         if (plugin != null) {
121             LayoutBuilder builder = createBuilder(plugin, layoutPrefix, props);
122             if (builder != null) {
123                 return builder.parseLayout(config);
124             }
125         }
126         return null;
127     }
128 
129     public RewritePolicy parseRewritePolicy(String className, Element rewriteElement, XmlConfiguration config) {
130         PluginType<?> plugin = plugins.get(className.toLowerCase());
131         if (plugin != null) {
132             try {
133                 RewritePolicyBuilder builder = (RewritePolicyBuilder) LoaderUtil.newInstanceOf(plugin.getPluginClass());
134                 return builder.parseRewritePolicy(rewriteElement, config);
135             } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
136                 LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
137             }
138         }
139         return null;
140     }
141     public RewritePolicy parseRewritePolicy(String className, String policyPrefix, Properties props, PropertiesConfiguration config) {
142         PluginType<?> plugin = plugins.get(className.toLowerCase());
143         if (plugin != null) {
144             RewritePolicyBuilder builder = createBuilder(plugin, policyPrefix, props);
145             if (builder != null) {
146                 return builder.parseRewritePolicy(config);
147             }
148         }
149         return null;
150     }
151 
152     private <T extends AbstractBuilder> T createBuilder(PluginType<?> plugin, String prefix, Properties props) {
153         try {
154             Class<?> clazz = plugin.getPluginClass();
155             if (AbstractBuilder.class.isAssignableFrom(clazz)) {
156                 @SuppressWarnings("unchecked")
157                 Constructor<T> constructor =
158                         (Constructor<T>) clazz.getConstructor(constructorParams);
159                 return constructor.newInstance(prefix, props);
160             }
161             @SuppressWarnings("unchecked")
162             T builder = (T) LoaderUtil.newInstanceOf(clazz);
163             return builder;
164         } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ex) {
165             LOGGER.warn("Unable to load plugin: {} due to: {}", plugin.getKey(), ex.getMessage());
166             return null;
167         }
168     }
169 
170 }