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.InputStream;
20  import java.util.Properties;
21  
22  /**
23   * Utility class to help with accessing System Properties.
24   */
25  public class PropsUtil {
26  
27      private Properties props;
28  
29      public PropsUtil(Properties props) {
30          this.props = props;
31      }
32  
33      public PropsUtil(String propsLocn) {
34          this.props = new Properties();
35          ClassLoader loader = findClassLoader();
36          InputStream in = loader.getResourceAsStream(propsLocn);
37          if (null != in) {
38              try {
39                  this.props.load(in);
40                  in.close();
41              } catch(java.io.IOException e) {
42                  // ignored
43              }
44          }
45      }
46  
47      public String getStringProperty(String name) {
48          String prop = null;
49          try {
50              prop = System.getProperty(name);
51          } catch (SecurityException e) {
52              // Ignore
53          }
54          return (prop == null) ? props.getProperty(name) : prop;
55      }
56  
57      public String getStringProperty(String name, String defaultValue) {
58          String prop = getStringProperty(name);
59          return (prop == null) ? defaultValue : prop;
60      }
61  
62      public boolean getBooleanProperty(String name, boolean defaultValue) {
63          String prop = getStringProperty(name);
64          return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
65      }
66  
67      private static ClassLoader findClassLoader() {
68          ClassLoader cl;
69          if (System.getSecurityManager() == null) {
70              cl = Thread.currentThread().getContextClassLoader();
71          } else {
72              cl = java.security.AccessController.doPrivileged(
73                  new java.security.PrivilegedAction<ClassLoader>() {
74                      public ClassLoader run() {
75                          return Thread.currentThread().getContextClassLoader();
76                      }
77                  }
78              );
79          }
80          if (cl == null) {
81              cl = PropsUtil.class.getClassLoader();
82          }
83  
84          return cl;
85      }
86  }