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.util.Properties;
22  
23  import org.apache.logging.log4j.Logger;
24  import org.apache.logging.log4j.status.StatusLogger;
25  
26  /**
27   * Helps access properties.
28   */
29  public class PropertiesUtil {
30  
31      private static final PropertiesUtil LOG4J_PROPERTIES = new PropertiesUtil("log4j2.component.properties");
32  
33      private static final Logger LOGGER = StatusLogger.getLogger();
34      
35      private final Properties props;
36  
37      public PropertiesUtil(final Properties props) {
38          this.props = props;
39      }
40  
41      /**
42       * Loads and closes the given property input stream.
43       * If an error occurs, log to the status logger.
44       * 
45       * @param in
46       *            a property input stream.
47       * @param source
48       *            a source object describing the source, like a resource string
49       *            or a URL.
50       * @return a new Properties object
51       */
52      static Properties loadClose(InputStream in, Object source) {
53          Properties props = new Properties();
54          if (null != in) {
55              try {
56                  props.load(in);
57              } catch (final IOException e) {
58                  LOGGER.error("Unable to read " + source, e);
59              } finally {
60                  try {
61                      in.close();
62                  } catch (final IOException e) {
63                      LOGGER.error("Unable to close " + source, e);
64                  }
65              }
66          }
67          return props;
68      }
69      
70      public PropertiesUtil(final String propsLocn) {
71          final ClassLoader loader = ProviderUtil.findClassLoader();
72          final InputStream in = loader.getResourceAsStream(propsLocn);
73          this.props = loadClose(in, propsLocn);
74      }
75  
76      public static PropertiesUtil getProperties() {
77          return LOG4J_PROPERTIES;
78      }
79  
80      public String getStringProperty(final String name) {
81          String prop = null;
82          try {
83              prop = System.getProperty(name);
84          } catch (final SecurityException e) {
85              // Ignore
86          }
87          return prop == null ? props.getProperty(name) : prop;
88      }
89  
90  
91      public int getIntegerProperty(final String name, final int defaultValue) {
92          String prop = null;
93          try {
94              prop = System.getProperty(name);
95          } catch (final SecurityException e) {
96              // Ignore
97          }
98          if (prop == null) {
99              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 }