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