1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.slf4j.helpers;
26
27
28
29
30
31
32
33 public final class Util {
34
35
36 private Util() {
37 }
38
39 public static String safeGetSystemProperty(String key) {
40 if (key == null)
41 throw new IllegalArgumentException("null input");
42
43 String result = null;
44 try {
45 result = System.getProperty(key);
46 } catch (java.lang.SecurityException sm) {
47 ;
48 }
49 return result;
50 }
51
52 public static boolean safeGetBooleanSystemProperty(String key) {
53 String value = safeGetSystemProperty(key);
54 if (value == null)
55 return false;
56 else
57 return value.equalsIgnoreCase("true");
58 }
59
60
61
62
63
64
65 private static final class ClassContextSecurityManager extends SecurityManager {
66 protected Class<?>[] getClassContext() {
67 return super.getClassContext();
68 }
69 }
70
71 private static ClassContextSecurityManager SECURITY_MANAGER;
72 private static boolean SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = false;
73
74 private static ClassContextSecurityManager getSecurityManager() {
75 if (SECURITY_MANAGER != null)
76 return SECURITY_MANAGER;
77 else if (SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED)
78 return null;
79 else {
80 SECURITY_MANAGER = safeCreateSecurityManager();
81 SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = true;
82 return SECURITY_MANAGER;
83 }
84 }
85
86 private static ClassContextSecurityManager safeCreateSecurityManager() {
87 try {
88 return new ClassContextSecurityManager();
89 } catch (java.lang.SecurityException sm) {
90 return null;
91 }
92 }
93
94
95
96
97
98
99 public static Class<?> getCallingClass() {
100 ClassContextSecurityManager securityManager = getSecurityManager();
101 if (securityManager == null)
102 return null;
103 Class<?>[] trace = securityManager.getClassContext();
104 String thisClassName = Util.class.getName();
105
106
107 int i;
108 for (i = 0; i < trace.length; i++) {
109 if (thisClassName.equals(trace[i].getName()))
110 break;
111 }
112
113
114 if (i >= trace.length || i + 2 >= trace.length) {
115 throw new IllegalStateException("Failed to find org.slf4j.helpers.Util or its caller in the stack; " + "this should not happen");
116 }
117
118 return trace[i + 2];
119 }
120
121 static final public void report(String msg, Throwable t) {
122 System.err.println(msg);
123 System.err.println("Reported exception:");
124 t.printStackTrace();
125 }
126
127 static final public void report(String msg) {
128 System.err.println("SLF4J: " + msg);
129 }
130
131
132
133 }