001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.spi;
015
016import ch.qos.logback.core.Context;
017import ch.qos.logback.core.status.ErrorStatus;
018import ch.qos.logback.core.status.InfoStatus;
019import ch.qos.logback.core.status.Status;
020import ch.qos.logback.core.status.StatusManager;
021import ch.qos.logback.core.status.WarnStatus;
022
023/**
024 * A helper class that implements ContextAware methods. A class can implement
025 * the ContextAware interface by deriving from this class.
026 * 
027 * @author Ceki Gülcü
028 */
029public class ContextAwareBase implements ContextAware {
030    private int noContextWarning = 0;
031    protected Context context;
032    final Object declaredOrigin;
033
034    public ContextAwareBase() {
035        declaredOrigin = this;
036    }
037
038    public ContextAwareBase(ContextAware declaredOrigin) {
039        this.declaredOrigin = declaredOrigin;
040    }
041
042    public void setContext(Context context) {
043        if (this.context == null) {
044            this.context = context;
045        } else if (this.context != context) {
046            throw new IllegalStateException("Context has been already set");
047        }
048    }
049
050    public Context getContext() {
051        return this.context;
052    }
053
054    public StatusManager getStatusManager() {
055        if (context == null) {
056            return null;
057        }
058        return context.getStatusManager();
059    }
060
061    /**
062     * The declared origin of status messages. By default 'this'. Derived classes
063     * may override this method to declare other origin.
064     * 
065     * @return the declared origin, by default 'this'
066     */
067    protected Object getDeclaredOrigin() {
068        return declaredOrigin;
069    }
070
071    public void addStatus(Status status) {
072        if (context == null) {
073            if (noContextWarning++ == 0) {
074                System.out.println("LOGBACK: No context given for " + this);
075            }
076            return;
077        }
078        StatusManager sm = context.getStatusManager();
079        if (sm != null) {
080            sm.add(status);
081        }
082    }
083
084    public void addInfo(String msg) {
085        addStatus(new InfoStatus(msg, getDeclaredOrigin()));
086    }
087
088    public void addInfo(String msg, Throwable ex) {
089        addStatus(new InfoStatus(msg, getDeclaredOrigin(), ex));
090    }
091
092    public void addWarn(String msg) {
093        addStatus(new WarnStatus(msg, getDeclaredOrigin()));
094    }
095
096    public void addWarn(String msg, Throwable ex) {
097        addStatus(new WarnStatus(msg, getDeclaredOrigin(), ex));
098    }
099
100    public void addError(String msg) {
101        addStatus(new ErrorStatus(msg, getDeclaredOrigin()));
102    }
103
104    public void addError(String msg, Throwable ex) {
105        addStatus(new ErrorStatus(msg, getDeclaredOrigin(), ex));
106    }
107}