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 v1.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.core.model.processor;
016
017import ch.qos.logback.core.Context;
018import ch.qos.logback.core.model.AppenderModel;
019import ch.qos.logback.core.model.Model;
020
021import java.util.HashSet;
022import java.util.Set;
023
024/**
025 * The AppenderAvailabilityAnalyser class is responsible for analyzing the availability
026 * of appenders. By available, we mean whether an appender with a given name is declared
027 * somewhere in the configuration. This availability information is later used by
028 * AppenderRefModelHandler to attempt to attach only those appenders that were previously
029 * declared.
030 */
031@PhaseIndicator(phase = ProcessingPhase.DEPENDENCY_ANALYSIS)
032public class AppenderDeclarationAnalyser extends ModelHandlerBase {
033
034    static final String DECLARED_APPENDER_NAME_SET_KEY = "DECLARED_APPENDER_NAME_SET";
035
036
037    public AppenderDeclarationAnalyser(Context context) {
038        super(context);
039    }
040
041    @Override
042    protected Class<AppenderModel> getSupportedModelClass() {
043        return AppenderModel.class;
044    }
045
046
047    @Override
048    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
049        AppenderModel appenderModel = (AppenderModel) model;
050        String appenderName = mic.subst(appenderModel.getName());
051
052        addAppenderDeclaration(mic, appenderName);
053    }
054
055
056    static public Set<String> getAppenderNameSet(ModelInterpretationContext mic) {
057        Set<String> set = (Set<String>) mic.getObjectMap().get(DECLARED_APPENDER_NAME_SET_KEY);
058        if(set == null) {
059            set = new HashSet<>();
060            mic.getObjectMap().put(DECLARED_APPENDER_NAME_SET_KEY, set);
061        }
062        return set;
063    }
064
065    static public void addAppenderDeclaration(ModelInterpretationContext mic, String appenderName) {
066        Set<String> set = getAppenderNameSet(mic);
067        set.add(appenderName);
068    }
069
070
071    static public boolean isAppenderDeclared(ModelInterpretationContext mic, String appenderName) {
072        Set<String> set = getAppenderNameSet(mic);
073        return set.contains(appenderName);
074    }
075
076}