001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, 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 v2.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 */
014
015package ch.qos.logback.classic.model.processor;
016
017import ch.qos.logback.classic.LoggerContext;
018import ch.qos.logback.classic.model.LoggerContextListenerModel;
019import ch.qos.logback.classic.spi.LoggerContextListener;
020import ch.qos.logback.core.Context;
021import ch.qos.logback.core.model.Model;
022import ch.qos.logback.core.model.processor.ModelHandlerBase;
023import ch.qos.logback.core.model.processor.ModelHandlerException;
024import ch.qos.logback.core.model.processor.ModelInterpretationContext;
025import ch.qos.logback.core.spi.ContextAware;
026import ch.qos.logback.core.spi.LifeCycle;
027import ch.qos.logback.core.util.OptionHelper;
028
029public class LoggerContextListenerModelHandler extends ModelHandlerBase {
030    boolean inError = false;
031    LoggerContextListener lcl;
032
033    public LoggerContextListenerModelHandler(Context context) {
034        super(context);
035    }
036
037    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
038        return new LoggerContextListenerModelHandler(context);
039    }
040
041    @Override
042    protected Class<LoggerContextListenerModel> getSupportedModelClass() {
043        return LoggerContextListenerModel.class;
044    }
045
046    @Override
047    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
048        LoggerContextListenerModel lclModel = (LoggerContextListenerModel) model;
049
050        String className = lclModel.getClassName();
051        if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) {
052            addError("Empty class name for LoggerContextListener");
053            inError = true;
054        } else {
055            className = mic.getImport(className);
056        }
057
058        try {
059            lcl = (LoggerContextListener) OptionHelper.instantiateByClassName(className, LoggerContextListener.class,
060                    context);
061
062            if (lcl instanceof ContextAware) {
063                ((ContextAware) lcl).setContext(context);
064            }
065
066            mic.pushObject(lcl);
067            addInfo("Adding LoggerContextListener of type [" + className + "] to the object stack");
068
069        } catch (Exception oops) {
070            inError = true;
071            addError("Could not create LoggerContextListener of type " + className + "].", oops);
072        }
073    }
074
075    @Override
076    public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
077        if (inError) {
078            return;
079        }
080        Object o = mic.peekObject();
081
082        if (o != lcl) {
083            addWarn("The object on the top the of the stack is not the LoggerContextListener pushed earlier.");
084        } else {
085            if (lcl instanceof LifeCycle) {
086                ((LifeCycle) lcl).start();
087                addInfo("Starting LoggerContextListener");
088            }
089            ((LoggerContext) context).addListener(lcl);
090            mic.popObject();
091        }
092    }
093}