View Javadoc

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  	
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              ; // ignore
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       * In order to call {@link SecurityManager#getClassContext()}, which is a
62       * protected method, we add this wrapper which allows the method to be visible
63       * inside this package.
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       * Returns the name of the class which called the invoking method.
96       *
97       * @return the name of the class which called the invoking method.
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         // Advance until Util is found
107         int i;
108         for (i = 0; i < trace.length; i++) {
109             if (thisClassName.equals(trace[i].getName()))
110                 break;
111         }
112 
113         // trace[i] = Util; trace[i+1] = caller; trace[i+2] = caller's caller
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 }