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.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 AppenderDeclarationAnalyser 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
051        String originalAppenderName = appenderModel.getName();
052        String substAppenderName = mic.subst(originalAppenderName);
053
054        addAppenderDeclaration(mic, substAppenderName);
055    }
056
057
058    static public Set<String> getAppenderNameSet(ModelInterpretationContext mic) {
059        Set<String> set = (Set<String>) mic.getObjectMap().get(DECLARED_APPENDER_NAME_SET_KEY);
060        if(set == null) {
061            set = new HashSet<>();
062            mic.getObjectMap().put(DECLARED_APPENDER_NAME_SET_KEY, set);
063        }
064        return set;
065    }
066
067    static public void addAppenderDeclaration(ModelInterpretationContext mic, String appenderName) {
068        Set<String> set = getAppenderNameSet(mic);
069        set.add(appenderName);
070    }
071
072
073    static public boolean isAppenderDeclared(ModelInterpretationContext mic, String appenderName) {
074        Set<String> set = getAppenderNameSet(mic);
075        return set.contains(appenderName);
076    }
077
078}