001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.helpers;
026
027/**
028 * An internal utility class.
029 *
030 * @author Alexander Dorokhine
031 * @author Ceki Gülcü
032 */
033public final class Util {
034
035    private Util() {
036    }
037
038    public static String safeGetSystemProperty(String key) {
039        if (key == null)
040            throw new IllegalArgumentException("null input");
041
042        String result = null;
043        try {
044            result = System.getProperty(key);
045        } catch (java.lang.SecurityException sm) {
046            ; // ignore
047        }
048        return result;
049    }
050
051    public static boolean safeGetBooleanSystemProperty(String key) {
052        String value = safeGetSystemProperty(key);
053        if (value == null)
054            return false;
055        else
056            return value.equalsIgnoreCase("true");
057    }
058
059    /**
060     * In order to call {@link SecurityManager#getClassContext()}, which is a
061     * protected method, we add this wrapper which allows the method to be visible
062     * inside this package.
063     */
064    private static final class ClassContextSecurityManager extends SecurityManager {
065        protected Class<?>[] getClassContext() {
066            return super.getClassContext();
067        }
068    }
069
070    private static ClassContextSecurityManager SECURITY_MANAGER;
071    private static boolean SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = false;
072
073    private static ClassContextSecurityManager getSecurityManager() {
074        if (SECURITY_MANAGER != null)
075            return SECURITY_MANAGER;
076        else if (SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED)
077            return null;
078        else {
079            SECURITY_MANAGER = safeCreateSecurityManager();
080            SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = true;
081            return SECURITY_MANAGER;
082        }
083    }
084
085    private static ClassContextSecurityManager safeCreateSecurityManager() {
086        try {
087            return new ClassContextSecurityManager();
088        } catch (java.lang.SecurityException sm) {
089            return null;
090        }
091    }
092
093    /**
094     * Returns the name of the class which called the invoking method.
095     *
096     * @return the name of the class which called the invoking method.
097     */
098    public static Class<?> getCallingClass() {
099        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    /**
121     * See  {@link Reporter#error(String, Throwable)} class for alternative.
122     *
123     * @deprecated replaced by the {@link Reporter#error(String, Throwable)} method.
124     * @param msg message to print
125     * @param t throwable to print
126     */
127    static final public void report(String msg, Throwable t) {
128        System.err.println(msg);
129        System.err.println("Reported exception:");
130        t.printStackTrace();
131    }
132
133    /**
134     * See  {@link Reporter} class for alternatives.
135     *
136     * @deprecated replaced by one of {@link Reporter#info(String)},
137     * {@link Reporter#warn(String)} or {@link Reporter#error(String)} methods.
138     * @param msg message to print
139     */
140    static final public void report(String msg) {
141        System.err.println("SLF4J: " + msg);
142    }
143
144}