-
-
Notifications
You must be signed in to change notification settings - Fork 975
/
Util.java
executable file
·172 lines (148 loc) · 5.74 KB
/
Util.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Copyright (c) 2004-2011 QOS.ch
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.helpers;
/**
* An internal utility class.
*
* @author Alexander Dorokhine
* @author Ceki Gülcü
*/
public final class Util {
private Util() {
}
public static String safeGetSystemProperty(String key) {
if (key == null)
throw new IllegalArgumentException("null input");
String result = null;
try {
result = System.getProperty(key);
} catch (java.lang.SecurityException sm) {
; // ignore
}
return result;
}
public static boolean safeGetBooleanSystemProperty(String key) {
String value = safeGetSystemProperty(key);
if (value == null)
return false;
else
return value.equalsIgnoreCase("true");
}
/**
* In order to call {@link SecurityManager#getClassContext()}, which is a
* protected method, we add this wrapper which allows the method to be visible
* inside this package.
*/
private static final class ClassContextSecurityManager extends SecurityManager {
protected Class<?>[] getClassContext() {
return super.getClassContext();
}
}
private static ClassContextSecurityManager SECURITY_MANAGER;
private static boolean SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = false;
private static ClassContextSecurityManager getSecurityManager() {
if (SECURITY_MANAGER != null)
return SECURITY_MANAGER;
else if (SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED)
return null;
else {
SECURITY_MANAGER = safeCreateSecurityManager();
SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = true;
return SECURITY_MANAGER;
}
}
private static ClassContextSecurityManager safeCreateSecurityManager() {
try {
return new ClassContextSecurityManager();
} catch (java.lang.SecurityException sm) {
return null;
}
}
/**
* Returns the name of the class which called the invoking method.
*
* @return the name of the class which called the invoking method.
*/
public static Class<?> getCallingClass() {
ClassContextSecurityManager securityManager = getSecurityManager();
if (securityManager == null)
return null;
Class<?>[] trace = securityManager.getClassContext();
String thisClassName = Util.class.getName();
// Advance until Util is found
int i;
for (i = 0; i < trace.length; i++) {
if (thisClassName.equals(trace[i].getName()))
break;
}
// trace[i] = Util; trace[i+1] = caller; trace[i+2] = caller's caller
if (i >= trace.length || i + 2 >= trace.length) {
throw new IllegalStateException("Failed to find org.slf4j.helpers.Util or its caller in the stack; " + "this should not happen");
}
return trace[i + 2];
}
static final public void report(String msg, Throwable t) {
System.err.println(msg);
System.err.println("Reported exception:");
t.printStackTrace();
}
static final public void report(String msg) {
System.err.println("SLF4J: " + msg);
}
/**
* Helper method to determine if an {@link Object} array contains an {@link Throwable} as last element
*
* @param argArray
* The arguments off which we want to know if it contains a {@link Throwable} as last element
* @return if the last parameter in argArray is a Throwable this method will return it, otherwise it returns null
*/
public static Throwable getThrowableCandidate(final Object[] argArray) {
if (argArray == null || argArray.length == 0) {
return null;
}
final Object lastEntry = argArray[argArray.length - 1];
if (lastEntry instanceof Throwable) {
return (Throwable) lastEntry;
}
return null;
}
/**
* Helper method to get all but the last element of an array
*
* @param argArray
* The arguments from which we want to remove the last element
*
* @return a copy of the array without the last element
*/
public static Object[] trimmedCopy(final Object[] argArray) {
if (argArray == null || argArray.length == 0) {
throw new IllegalStateException("non-sensical empty or null argument array");
}
final int trimmedLen = argArray.length - 1;
Object[] trimmed = new Object[trimmedLen];
System.arraycopy(argArray, 0, trimmed, 0, trimmedLen);
return trimmed;
}
}