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            // TODO: prevent substitution of references
051            String ref = appenderRefModel.getRef();
052            DependencyDefinition dd = new DependencyDefinition(parentModel, ref);
053            mic.addDependencyDefinition(dd);
054        }
055
056    }
057
058    /**
059     * Recursively processes the given Model object and its submodels, extracting instances
060     * of AppenderRefModel and adding them to the provided list.
061     *
062     * @param list the list to which AppenderRefModel instances are added
063     * @param model the root Model object from which to start the extraction
064     */
065    public void collectAllAppenderRefModels(List<AppenderRefModel> list, Model model) {
066        if(model == null)
067            return;
068        if(model instanceof AppenderRefModel) {
069            list.add((AppenderRefModel) model);
070        }
071        model.getSubModels().forEach(subModel -> collectAllAppenderRefModels(list, subModel));
072    }
073
074}