001package ch.qos.logback.core.model.processor;
002
003import ch.qos.logback.core.Context;
004import ch.qos.logback.core.model.Model;
005import ch.qos.logback.core.model.StatusListenerModel;
006import ch.qos.logback.core.spi.ContextAware;
007import ch.qos.logback.core.spi.LifeCycle;
008import ch.qos.logback.core.status.StatusListener;
009import ch.qos.logback.core.util.OptionHelper;
010
011public class StatusListenerModelHandler extends ModelHandlerBase {
012
013    boolean inError = false;
014    Boolean effectivelyAdded = null;
015    StatusListener statusListener = null;
016
017    public StatusListenerModelHandler(Context context) {
018        super(context);
019    }
020
021    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
022        return new StatusListenerModelHandler(context);
023    }
024
025    @Override
026    protected Class<StatusListenerModel> getSupportedModelClass() {
027        return StatusListenerModel.class;
028    }
029
030    @Override
031    public void handle(ModelInterpretationContext ic, Model model) throws ModelHandlerException {
032
033        StatusListenerModel slModel = (StatusListenerModel) model;
034
035        String className = slModel.getClassName();
036
037        if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) {
038            addError("Empty class name for StatusListener");
039            inError = true;
040            return;
041        } else {
042            className = ic.getImport(className);
043        }
044
045        try {
046            statusListener = (StatusListener) OptionHelper.instantiateByClassName(className, StatusListener.class,
047                    context);
048            effectivelyAdded = ic.getContext().getStatusManager().add(statusListener);
049            if (statusListener instanceof ContextAware) {
050                ((ContextAware) statusListener).setContext(context);
051            }
052            addInfo("Added status listener of type [" + slModel.getClassName() + "]");
053            ic.pushObject(statusListener);
054        } catch (Exception e) {
055            inError = true;
056            addError("Could not create an StatusListener of type [" + slModel.getClassName() + "].", e);
057            throw new ModelHandlerException(e);
058        }
059    }
060
061    @Override
062    public void postHandle(ModelInterpretationContext mic, Model m) {
063        if (inError) {
064            return;
065        }
066
067        if (isEffectivelyAdded() && statusListener instanceof LifeCycle) {
068            ((LifeCycle) statusListener).start();
069        }
070        Object o = mic.peekObject();
071        if (o != statusListener) {
072            addWarn("The object at the of the stack is not the statusListener pushed earlier.");
073        } else {
074            mic.popObject();
075        }
076    }
077
078    private boolean isEffectivelyAdded() {
079        if (effectivelyAdded == null)
080            return false;
081        return effectivelyAdded;
082    }
083}