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.AppenderRefModel;
018import ch.qos.logback.core.model.Model;
019
020import java.util.List;
021
022/**
023 * The AppenderRefDependencyAnalyser class is responsible for analyzing dependencies
024 * related to appender references within a logging model. This class extends
025 * ModelHandlerBase and operates during the dependency analysis phase of processing.
026 *
027 * The primary responsibilities of this class include:
028 * - Identifying instances of {@link AppenderRefModel} within a model hierarchy.
029 * - Substituting references to appender models using the context's interpretation logic.
030 * - Adding dependency definitions for the identified appender references to the interpretation context.
031 */
032@PhaseIndicator(phase = ProcessingPhase.DEPENDENCY_ANALYSIS)
033public class AppenderRefDependencyAnalyser extends ModelHandlerBase {
034
035    public AppenderRefDependencyAnalyser(Context context) {
036        super(context);
037    }
038
039    @Override
040    protected Class<Model> getSupportedModelClass() {
041        return Model.class;
042    }
043
044    @Override
045    public void handle(ModelInterpretationContext mic, Model parentModel) throws ModelHandlerException {
046        List<AppenderRefModel> appenderRefModelList = new java.util.ArrayList<>();
047        collectAllAppenderRefModels(appenderRefModelList, parentModel);
048
049        for (AppenderRefModel appenderRefModel : appenderRefModelList) {
050            String ref = mic.subst(appenderRefModel.getRef());
051            DependencyDefinition dd = new DependencyDefinition(parentModel, ref);
052            mic.addDependencyDefinition(dd);
053        }
054
055    }
056
057    /**
058     * Recursively processes the given Model object and its submodels, extracting instances
059     * of AppenderRefModel and adding them to the provided list.
060     *
061     * @param list the list to which AppenderRefModel instances are added
062     * @param model the root Model object from which to start the extraction
063     */
064    public void collectAllAppenderRefModels(List<AppenderRefModel> list, Model model) {
065        if(model == null)
066            return;
067        if(model instanceof AppenderRefModel) {
068            list.add((AppenderRefModel) model);
069        }
070        model.getSubModels().forEach(subModel -> collectAllAppenderRefModels(list, subModel));
071    }
072
073}