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