    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&uuml;lc&uuml;
    032 */
    033public final class Util {
    034
    035
    036    private Util() {
    037    }
    038
    039    public static String safeGetSystemProperty(String key) {
    040        if (key == null)
    041            throw new IllegalArgumentException("null input");
    042
    043        String result = null;
    044        try {
    045            result = System.getProperty(key);
    046        } catch (java.lang.SecurityException sm) {
    047            ; // ignore
    048        }
    049        return result;
    050    }
    051
    052    public static boolean safeGetBooleanSystemProperty(String key) {
    053        String value = safeGetSystemProperty(key);
    054        if (value == null)
    055            return false;
    056        else
    057            return value.equalsIgnoreCase("true");
    058    }
    059
    060    /**
    061     * In order to call {@link SecurityManager#getClassContext()}, which is a
    062     * protected method, we add this wrapper which allows the method to be visible
    063     * inside this package.
    064     */
    065    private static final class ClassContextSecurityManager extends SecurityManager {
    066        protected Class<?>[] getClassContext() {
    067            return super.getClassContext();
    068        }
    069    }
    070
    071    private static ClassContextSecurityManager SECURITY_MANAGER;
    072    private static boolean SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = false;
    073
    074    private static ClassContextSecurityManager getSecurityManager() {
    075        if (SECURITY_MANAGER != null)
    076            return SECURITY_MANAGER;
    077        else if (SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED)
    078            return null;
    079        else {
    080            SECURITY_MANAGER = safeCreateSecurityManager();
    081            SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = true;
    082            return SECURITY_MANAGER;
    083        }
    084    }
    085
    086    private static ClassContextSecurityManager safeCreateSecurityManager() {
    087        try {
    088            return new ClassContextSecurityManager();
    089        } catch (java.lang.SecurityException sm) {
    090            return null;
    091        }
    092    }
    093
    094    /**
    095     * Returns the name of the class which called the invoking method.
    096     *
    097     * @return the name of the class which called the invoking method.
    098     */
    099    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}