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.logging.log4j.util;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.net.URL;
22  import java.util.Enumeration;
23  import java.util.Properties;
24  
25  import org.apache.logging.log4j.Logger;
26  import org.apache.logging.log4j.status.StatusLogger;
27  
28  /**
29   * <em>Consider this class private.</em>
30   * <p>
31   * Helps access properties.
32   * </p>
33   */
34  public final class PropertiesUtil {
35  
36      private static final PropertiesUtil LOG4J_PROPERTIES = new PropertiesUtil("log4j2.component.properties");
37  
38      private static final Logger LOGGER = StatusLogger.getLogger();
39  
40      private final Properties props;
41  
42      public PropertiesUtil(final Properties props) {
43          this.props = props;
44      }
45  
46      /**
47       * Loads and closes the given property input stream.
48       * If an error occurs, log to the status logger.
49       *
50       * @param in
51       *            a property input stream.
52       * @param source
53       *            a source object describing the source, like a resource string
54       *            or a URL.
55       * @return a new Properties object
56       */
57      static Properties loadClose(final InputStream in, final Object source) {
58          final Properties props = new Properties();
59          if (null != in) {
60              try {
61                  props.load(in);
62              } catch (final IOException e) {
63                  LOGGER.error("Unable to read {}", source, e);
64              } finally {
65                  try {
66                      in.close();
67                  } catch (final IOException e) {
68                      LOGGER.error("Unable to close {}", source, e);
69                  }
70              }
71          }
72          return props;
73      }
74  
75      public PropertiesUtil(final String propsLocn) {
76          final ClassLoader loader = ProviderUtil.findClassLoader();
77          @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
78          final
79          Properties properties = new Properties();
80              try {
81                  final Enumeration<URL> enumeration = loader.getResources(propsLocn);
82                  while (enumeration.hasMoreElements()) {
83                      final URL url = enumeration.nextElement();
84                      final InputStream in = url.openStream();
85                      try {
86                          properties.load(in);
87                      } catch (final IOException ioe) {
88                          LOGGER.error("Unable to read {}", url.toString());
89                      } finally {
90                          try {
91                              in.close();
92                          } catch (final IOException ioe) {
93                              LOGGER.error("Unable to close {}", url.toString(), ioe);
94                          }
95                      }
96  
97                  }
98  
99              } catch (final IOException ioe) {
100                 LOGGER.error("Unable to access {}", propsLocn, ioe);
101             }
102         this.props = properties;
103     }
104 
105     public static PropertiesUtil getProperties() {
106         return LOG4J_PROPERTIES;
107     }
108 
109     public String getStringProperty(final String name) {
110         String prop = null;
111         try {
112             prop = System.getProperty(name);
113         } catch (final SecurityException ignored) {
114             // Ignore
115         }
116         return prop == null ? props.getProperty(name) : prop;
117     }
118 
119 
120     public int getIntegerProperty(final String name, final int defaultValue) {
121         String prop = null;
122         try {
123             prop = System.getProperty(name);
124         } catch (final SecurityException ignored) {
125             // Ignore
126         }
127         if (prop == null) {
128             prop = props.getProperty(name);
129         }
130         if (prop != null) {
131             try {
132                 return Integer.parseInt(prop);
133             } catch (final Exception ex) {
134                 return defaultValue;
135             }
136         }
137         return defaultValue;
138     }
139 
140 
141     public long getLongProperty(final String name, final long defaultValue) {
142         String prop = null;
143         try {
144             prop = System.getProperty(name);
145         } catch (final SecurityException ignored) {
146             // Ignore
147         }
148         if (prop == null) {
149             prop = props.getProperty(name);
150         }
151         if (prop != null) {
152             try {
153                 return Long.parseLong(prop);
154             } catch (final Exception ex) {
155                 return defaultValue;
156             }
157         }
158         return defaultValue;
159     }
160 
161     public String getStringProperty(final String name, final String defaultValue) {
162         final String prop = getStringProperty(name);
163         return (prop == null) ? defaultValue : prop;
164     }
165 
166     public boolean getBooleanProperty(final String name) {
167         return getBooleanProperty(name, false);
168     }
169 
170     public boolean getBooleanProperty(final String name, final boolean defaultValue) {
171         final String prop = getStringProperty(name);
172         return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
173     }
174 
175     /**
176      * Return the system properties or an empty Properties object if an error occurs.
177      * @return The system properties.
178      */
179     public static Properties getSystemProperties() {
180         try {
181             return new Properties(System.getProperties());
182         } catch (final SecurityException ex) {
183             LOGGER.error("Unable to access system properties.", ex);
184             // Sandboxed - can't read System Properties
185             return new Properties();
186         }
187     }
188 }