001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.log4j.builders;
018
019import org.apache.log4j.bridge.FilterAdapter;
020import org.apache.log4j.bridge.FilterWrapper;
021import org.apache.log4j.helpers.OptionConverter;
022import org.apache.log4j.spi.Filter;
023import org.apache.logging.log4j.Level;
024import org.apache.logging.log4j.Logger;
025import org.apache.logging.log4j.core.filter.CompositeFilter;
026import org.apache.logging.log4j.core.filter.ThresholdFilter;
027import org.apache.logging.log4j.core.lookup.ConfigurationStrSubstitutor;
028import org.apache.logging.log4j.core.lookup.StrSubstitutor;
029import org.apache.logging.log4j.status.StatusLogger;
030
031import java.util.ArrayList;
032import java.util.HashMap;
033import java.util.List;
034import java.util.Map;
035import java.util.Properties;
036
037/**
038 * Base class for Log4j 1 component builders.
039 */
040public abstract class AbstractBuilder {
041
042    private static Logger LOGGER = StatusLogger.getLogger();
043    protected static final String FILE_PARAM = "File";
044    protected static final String APPEND_PARAM = "Append";
045    protected static final String BUFFERED_IO_PARAM = "BufferedIO";
046    protected static final String BUFFER_SIZE_PARAM = "BufferSize";
047    protected static final String MAX_SIZE_PARAM = "MaxFileSize";
048    protected static final String MAX_BACKUP_INDEX = "MaxBackupIndex";
049    protected static final String RELATIVE = "RELATIVE";
050
051    private final String prefix;
052    private final Properties props;
053    private final StrSubstitutor strSubstitutor;
054
055    public AbstractBuilder() {
056        this.prefix = null;
057        this.props = new Properties();
058        strSubstitutor = new ConfigurationStrSubstitutor(System.getProperties());
059    }
060
061    public AbstractBuilder(String prefix, Properties props) {
062        this.prefix = prefix + ".";
063        this.props = props;
064        Map<String, String> map = new HashMap<>();
065        System.getProperties().forEach((k, v) -> map.put(k.toString(), v.toString()));
066        props.forEach((k, v) -> map.put(k.toString(), v.toString()));
067        strSubstitutor = new ConfigurationStrSubstitutor(map);
068    }
069
070    public String getProperty(String key) {
071        return strSubstitutor.replace(props.getProperty(prefix + key));
072    }
073
074    public String getProperty(String key, String defaultValue) {
075        return strSubstitutor.replace(props.getProperty(prefix + key, defaultValue));
076    }
077
078    public boolean getBooleanProperty(String key) {
079        return Boolean.parseBoolean(strSubstitutor.replace(props.getProperty(prefix + key, Boolean.FALSE.toString())));
080    }
081
082    public int getIntegerProperty(String key, int defaultValue) {
083        String value = getProperty(key);
084        try {
085            if (value != null) {
086                return Integer.parseInt(value);
087            }
088        } catch (Exception ex) {
089            LOGGER.warn("Error converting value {} of {} to an integer: {}", value, key, ex.getMessage());
090        }
091        return defaultValue;
092    }
093
094    public Properties getProperties() {
095        return props;
096    }
097
098
099    protected org.apache.logging.log4j.core.Filter buildFilters(String level, Filter filter) {
100        if (level != null && filter != null) {
101            List<org.apache.logging.log4j.core.Filter> filterList = new ArrayList<>();
102            org.apache.logging.log4j.core.Filter thresholdFilter =
103                    ThresholdFilter.createFilter(OptionConverter.convertLevel(level, Level.TRACE),
104                            org.apache.logging.log4j.core.Filter.Result.NEUTRAL,
105                            org.apache.logging.log4j.core.Filter.Result.DENY);
106            filterList.add(thresholdFilter);
107            Filter f = filter;
108            while (f != null) {
109                if (filter instanceof FilterWrapper) {
110                    filterList.add(((FilterWrapper) f).getFilter());
111                } else {
112                    filterList.add(new FilterAdapter(f));
113                }
114                f = f.next;
115            }
116            return CompositeFilter.createFilters(filterList.toArray(new org.apache.logging.log4j.core.Filter[0]));
117        } else if (level != null) {
118            return ThresholdFilter.createFilter(OptionConverter.convertLevel(level, Level.TRACE),
119                    org.apache.logging.log4j.core.Filter.Result.NEUTRAL,
120                    org.apache.logging.log4j.core.Filter.Result.DENY);
121        } else if (filter != null) {
122            if (filter instanceof FilterWrapper) {
123                return ((FilterWrapper) filter).getFilter();
124            }
125            return new FilterAdapter(filter);
126        }
127        return null;
128    }
129}