1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.model.processor;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.model.AppenderModel;
19  import ch.qos.logback.core.model.Model;
20  
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  /**
25   * The AppenderAvailabilityAnalyser class is responsible for analyzing the availability
26   * of appenders. By available, we mean whether an appender with a given name is declared
27   * somewhere in the configuration. This availability information is later used by
28   * AppenderRefModelHandler to attempt to attach only those appenders that were previously
29   * declared.
30   */
31  @PhaseIndicator(phase = ProcessingPhase.DEPENDENCY_ANALYSIS)
32  public class AppenderDeclarationAnalyser extends ModelHandlerBase {
33  
34      static final String DECLARED_APPENDER_NAME_SET_KEY = "DECLARED_APPENDER_NAME_SET";
35  
36  
37      public AppenderDeclarationAnalyser(Context context) {
38          super(context);
39      }
40  
41      @Override
42      protected Class<AppenderModel> getSupportedModelClass() {
43          return AppenderModel.class;
44      }
45  
46  
47      @Override
48      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
49          AppenderModel appenderModel = (AppenderModel) model;
50          String appenderName = mic.subst(appenderModel.getName());
51  
52          addAppenderDeclaration(mic, appenderName);
53      }
54  
55  
56      static public Set<String> getAppenderNameSet(ModelInterpretationContext mic) {
57          Set<String> set = (Set<String>) mic.getObjectMap().get(DECLARED_APPENDER_NAME_SET_KEY);
58          if(set == null) {
59              set = new HashSet<>();
60              mic.getObjectMap().put(DECLARED_APPENDER_NAME_SET_KEY, set);
61          }
62          return set;
63      }
64  
65      static public void addAppenderDeclaration(ModelInterpretationContext mic, String appenderName) {
66          Set<String> set = getAppenderNameSet(mic);
67          set.add(appenderName);
68      }
69  
70  
71      static public boolean isAppenderDeclared(ModelInterpretationContext mic, String appenderName) {
72          Set<String> set = getAppenderNameSet(mic);
73          return set.contains(appenderName);
74      }
75  
76  }