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.bridge.FilterAdapter;
20  import org.apache.log4j.bridge.FilterWrapper;
21  import org.apache.log4j.helpers.OptionConverter;
22  import org.apache.log4j.spi.Filter;
23  import org.apache.logging.log4j.Level;
24  import org.apache.logging.log4j.Logger;
25  import org.apache.logging.log4j.core.filter.CompositeFilter;
26  import org.apache.logging.log4j.core.filter.ThresholdFilter;
27  import org.apache.logging.log4j.status.StatusLogger;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Properties;
32  
33  /**
34   * Base class for Log4j 1 component builders.
35   */
36  public abstract class AbstractBuilder {
37  
38      private static Logger LOGGER = StatusLogger.getLogger();
39      protected static final String FILE_PARAM = "File";
40      protected static final String APPEND_PARAM = "Append";
41      protected static final String BUFFERED_IO_PARAM = "BufferedIO";
42      protected static final String BUFFER_SIZE_PARAM = "BufferSize";
43      protected static final String MAX_SIZE_PARAM = "MaxFileSize";
44      protected static final String MAX_BACKUP_INDEX = "MaxBackupIndex";
45      protected static final String RELATIVE = "RELATIVE";
46  
47      private final String prefix;
48      private final Properties props;
49  
50      public AbstractBuilder() {
51          this.prefix = null;
52          this.props = new Properties();
53      }
54  
55      public AbstractBuilder(String prefix, Properties props) {
56          this.prefix = prefix + ".";
57          this.props = props;
58      }
59  
60      public String getProperty(String key) {
61          return props.getProperty(prefix + key);
62      }
63  
64      public String getProperty(String key, String defaultValue) {
65          return props.getProperty(prefix + key, defaultValue);
66      }
67  
68      public boolean getBooleanProperty(String key) {
69          return Boolean.parseBoolean(props.getProperty(prefix + key, Boolean.FALSE.toString()));
70      }
71  
72      public int getIntegerProperty(String key, int defaultValue) {
73          String value = props.getProperty(key);
74          try {
75              if (value != null) {
76                  return Integer.parseInt(value);
77              }
78          } catch (Exception ex) {
79              LOGGER.warn("Error converting value {} of {} to an integer: {}", value, key, ex.getMessage());
80          }
81          return defaultValue;
82      }
83  
84      public Properties getProperties() {
85          return props;
86      }
87  
88  
89      protected org.apache.logging.log4j.core.Filter buildFilters(String level, Filter filter) {
90          if (level != null && filter != null) {
91              List<org.apache.logging.log4j.core.Filter> filterList = new ArrayList<>();
92              org.apache.logging.log4j.core.Filter thresholdFilter =
93                      ThresholdFilter.createFilter(OptionConverter.convertLevel(level, Level.TRACE),
94                              org.apache.logging.log4j.core.Filter.Result.NEUTRAL,
95                              org.apache.logging.log4j.core.Filter.Result.DENY);
96              filterList.add(thresholdFilter);
97              Filter f = filter;
98              while (f != null) {
99                  if (filter instanceof FilterWrapper) {
100                     filterList.add(((FilterWrapper) f).getFilter());
101                 } else {
102                     filterList.add(new FilterAdapter(f));
103                 }
104                 f = f.next;
105             }
106             return CompositeFilter.createFilters(filterList.toArray(new org.apache.logging.log4j.core.Filter[0]));
107         } else if (level != null) {
108             return ThresholdFilter.createFilter(OptionConverter.convertLevel(level, Level.TRACE),
109                     org.apache.logging.log4j.core.Filter.Result.NEUTRAL,
110                     org.apache.logging.log4j.core.Filter.Result.DENY);
111         } else if (filter != null) {
112             if (filter instanceof FilterWrapper) {
113                 return ((FilterWrapper) filter).getFilter();
114             } else {
115                 return new FilterAdapter(filter);
116             }
117         }
118         return null;
119     }
120 }