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.logging.log4j.util;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.Properties;
022
023import org.apache.logging.log4j.Logger;
024import org.apache.logging.log4j.status.StatusLogger;
025
026/**
027 * Helps access properties.
028 */
029public class PropertiesUtil {
030
031    private static final PropertiesUtil LOG4J_PROPERTIES = new PropertiesUtil("log4j2.component.properties");
032
033    private static final Logger LOGGER = StatusLogger.getLogger();
034    
035    private final Properties props;
036
037    public PropertiesUtil(final Properties props) {
038        this.props = props;
039    }
040
041    /**
042     * Loads and closes the given property input stream.
043     * If an error occurs, log to the status logger.
044     * 
045     * @param in
046     *            a property input stream.
047     * @param source
048     *            a source object describing the source, like a resource string
049     *            or a URL.
050     * @return a new Properties object
051     */
052    static Properties loadClose(InputStream in, Object source) {
053        Properties props = new Properties();
054        if (null != in) {
055            try {
056                props.load(in);
057            } catch (final IOException e) {
058                LOGGER.error("Unable to read " + source, e);
059            } finally {
060                try {
061                    in.close();
062                } catch (final IOException e) {
063                    LOGGER.error("Unable to close " + source, e);
064                }
065            }
066        }
067        return props;
068    }
069    
070    public PropertiesUtil(final String propsLocn) {
071        final ClassLoader loader = ProviderUtil.findClassLoader();
072        final InputStream in = loader.getResourceAsStream(propsLocn);
073        this.props = loadClose(in, propsLocn);
074    }
075
076    public static PropertiesUtil getProperties() {
077        return LOG4J_PROPERTIES;
078    }
079
080    public String getStringProperty(final String name) {
081        String prop = null;
082        try {
083            prop = System.getProperty(name);
084        } catch (final SecurityException e) {
085            // Ignore
086        }
087        return prop == null ? props.getProperty(name) : prop;
088    }
089
090
091    public int getIntegerProperty(final String name, final int defaultValue) {
092        String prop = null;
093        try {
094            prop = System.getProperty(name);
095        } catch (final SecurityException e) {
096            // Ignore
097        }
098        if (prop == null) {
099            prop = props.getProperty(name);
100        }
101        if (prop != null) {
102            try {
103                return Integer.parseInt(prop);
104            } catch (final Exception ex) {
105                return defaultValue;
106            }
107        }
108        return defaultValue;
109    }
110
111
112    public long getLongProperty(final String name, final long defaultValue) {
113        String prop = null;
114        try {
115            prop = System.getProperty(name);
116        } catch (final SecurityException e) {
117            // Ignore
118        }
119        if (prop == null) {
120            prop = props.getProperty(name);
121        }
122        if (prop != null) {
123            try {
124                return Long.parseLong(prop);
125            } catch (final Exception ex) {
126                return defaultValue;
127            }
128        }
129        return defaultValue;
130    }
131
132    public String getStringProperty(final String name, final String defaultValue) {
133        final String prop = getStringProperty(name);
134        return (prop == null) ? defaultValue : prop;
135    }
136
137    public boolean getBooleanProperty(final String name) {
138        return getBooleanProperty(name, false);
139    }
140
141    public boolean getBooleanProperty(final String name, final boolean defaultValue) {
142        final String prop = getStringProperty(name);
143        return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
144    }
145
146    /**
147     * Return the system properties or an empty Properties object if an error occurs.
148     * @return The system properties.
149     */
150    public static Properties getSystemProperties() {
151        try {
152            return new Properties(System.getProperties());
153        } catch (final SecurityException ex) {
154            StatusLogger.getLogger().error("Unable to access system properties.");
155            // Sandboxed - can't read System Properties
156            return new Properties();
157        }
158    }
159}