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.joran.action;
015
016import ch.qos.logback.core.spi.ContextAware;
017import org.xml.sax.Attributes;
018
019import ch.qos.logback.core.joran.spi.ActionException;
020import ch.qos.logback.core.joran.spi.InterpretationContext;
021import ch.qos.logback.core.spi.LifeCycle;
022import ch.qos.logback.core.status.StatusListener;
023import ch.qos.logback.core.util.OptionHelper;
024
025public class StatusListenerAction extends Action {
026
027    boolean inError = false;
028    Boolean effectivelyAdded = null;
029    StatusListener statusListener = null;
030
031    public void begin(InterpretationContext ec, String name, Attributes attributes) throws ActionException {
032        inError = false;
033        effectivelyAdded = null;
034        String className = attributes.getValue(CLASS_ATTRIBUTE);
035        if (OptionHelper.isEmpty(className)) {
036            addError("Missing class name for statusListener. Near [" + name + "] line " + getLineNumber(ec));
037            inError = true;
038            return;
039        }
040
041        try {
042            statusListener = (StatusListener) OptionHelper.instantiateByClassName(className, StatusListener.class, context);
043            effectivelyAdded = ec.getContext().getStatusManager().add(statusListener);
044            if (statusListener instanceof ContextAware) {
045                ((ContextAware) statusListener).setContext(context);
046            }
047            addInfo("Added status listener of type [" + className + "]");
048            ec.pushObject(statusListener);
049        } catch (Exception e) {
050            inError = true;
051            addError("Could not create an StatusListener of type [" + className + "].", e);
052            throw new ActionException(e);
053        }
054
055    }
056
057    public void finish(InterpretationContext ec) {
058    }
059
060    public void end(InterpretationContext ec, String e) {
061        if (inError) {
062            return;
063        }
064        if (isEffectivelyAdded() && statusListener instanceof LifeCycle) {
065            ((LifeCycle) statusListener).start();
066        }
067        Object o = ec.peekObject();
068        if (o != statusListener) {
069            addWarn("The object at the of the stack is not the statusListener pushed earlier.");
070        } else {
071            ec.popObject();
072        }
073    }
074
075    private boolean isEffectivelyAdded() {
076        if (effectivelyAdded == null)
077            return false;
078        return effectivelyAdded;
079    }
080}