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 */
014package ch.qos.logback.core.model.processor;
015
016import ch.qos.logback.core.Context;
017import ch.qos.logback.core.model.Model;
018
019/**
020 * <p>RefContainerDependencyAnalyser pushes relevant models into the modelStack
021 * of ModelInterpretationContext.</p>
022 *
023 * <p>Relevant models are LoggerModel, RootLoggerModel and AppenderModel as defined
024 * in {@link ch.qos.logback.core.joran.ModelClassToModelHandlerLinkerBase#link}
025 * method.</p>
026 *
027 * <p>This class could have been called RefContainerDependencyAnalysisHelper.</p>
028 *
029 * @author Ceki G&uuml;lc&uuml;
030 *
031 */
032@PhaseIndicator(phase = ProcessingPhase.DEPENDENCY_ANALYSIS)
033public class RefContainerDependencyAnalyser extends ModelHandlerBase {
034
035    final Class<?> modelClass;
036
037    public RefContainerDependencyAnalyser(Context context, Class<?> modelClass) {
038        super(context);
039        this.modelClass = modelClass;
040    }
041
042    @Override
043    protected boolean isSupportedModelType(Model model) {
044
045        if (modelClass.isInstance(model)) {
046            return true;
047        }
048
049        StringBuilder buf = new StringBuilder("This handler can only handle models of type ");
050        buf.append(modelClass.getName());
051        addError(buf.toString());
052        return false;
053    }
054
055    @Override
056    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
057        mic.pushModel(model);
058    }
059
060    @Override
061    public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
062        Model poppedModel = mic.popModel();
063        if (model != poppedModel) {
064            addError("Popped model [" + poppedModel + "] different than expected [" + model + "]");
065        }
066    }
067}