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