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  package ch.qos.logback.core.model.processor;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.model.Model;
18  
19  /**
20   * <p>RefContainerDependencyAnalyser pushes relevant models into the modelStack
21   * of ModelInterpretationContext.</p>
22   *
23   * <p>Relevant models are LoggerModel, RootLoggerModel and AppenderModel as defined
24   * in {@link ch.qos.logback.core.joran.ModelClassToModelHandlerLinkerBase#link}
25   * method.</p>
26   *
27   * <p>This class could have been called RefContainerDependencyAnalysisHelper.</p>
28   *
29   * @author Ceki G&uuml;lc&uuml;
30   *
31   */
32  @PhaseIndicator(phase = ProcessingPhase.DEPENDENCY_ANALYSIS)
33  public class RefContainerDependencyAnalyser extends ModelHandlerBase {
34  
35      final Class<?> modelClass;
36  
37      public RefContainerDependencyAnalyser(Context context, Class<?> modelClass) {
38          super(context);
39          this.modelClass = modelClass;
40      }
41  
42      @Override
43      protected boolean isSupportedModelType(Model model) {
44  
45          if (modelClass.isInstance(model)) {
46              return true;
47          }
48  
49          StringBuilder buf = new StringBuilder("This handler can only handle models of type ");
50          buf.append(modelClass.getName());
51          addError(buf.toString());
52          return false;
53      }
54  
55      @Override
56      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
57          mic.pushModel(model);
58      }
59  
60      @Override
61      public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
62          Model poppedModel = mic.popModel();
63          if (model != poppedModel) {
64              addError("Popped model [" + poppedModel + "] different than expected [" + model + "]");
65          }
66      }
67  }