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(InputStream in, Object source) {
58          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          Properties properties = new Properties();
79              try {
80                  Enumeration<URL> enumeration = loader.getResources(propsLocn);
81                  while (enumeration.hasMoreElements()) {
82                      final URL url = enumeration.nextElement();
83                      final InputStream in = url.openStream();
84                      try {
85                          properties.load(in);
86                      } catch (IOException ioe) {
87                          LOGGER.error("Unable to read {}", url.toString());
88                      } finally {
89                          try {
90                              in.close();
91                          } catch (IOException ioe) {
92                              LOGGER.error("Unable to close {}", url.toString(), ioe);
93                          }
94                      }
95  
96                  }
97  
98              } catch (IOException ioe) {
99                  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 }