1 /**
2 * Copyright (c) 2004-2011 QOS.ch
3 * All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25 package org.slf4j.helpers;
26
27 /**
28 * An internal utility class.
29 *
30 * @author Alexander Dorokhine
31 * @author Ceki Gülcü
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 ; // ignore
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 * In order to call {@link SecurityManager#getClassContext()}, which is a
61 * protected method, we add this wrapper which allows the method to be visible
62 * inside this package.
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 * Returns the name of the class which called the invoking method.
95 *
96 * @return the name of the class which called the invoking method.
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 // Advance until Util is found
106 int i;
107 for (i = 0; i < trace.length; i++) {
108 if (thisClassName.equals(trace[i].getName()))
109 break;
110 }
111
112 // trace[i] = Util; trace[i+1] = caller; trace[i+2] = caller's caller
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 }