1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.util;
18
19 import java.io.InputStream;
20 import java.util.Properties;
21
22
23
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
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
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 }