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.blackbox.model.processor;
16  
17  import java.util.Stack;
18  
19  import ch.qos.logback.core.Context;
20  import ch.qos.logback.core.ContextBase;
21  import ch.qos.logback.core.blackbox.model.BlackboxStackModel;
22  import ch.qos.logback.core.model.Model;
23  import ch.qos.logback.core.model.processor.ModelHandlerBase;
24  import ch.qos.logback.core.model.processor.ModelHandlerException;
25  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
26  
27  public class BlackboxStackModelHandler extends ModelHandlerBase {
28  
29      static public final String STACK_TEST = "STACK_TEST"; 
30      
31      public BlackboxStackModelHandler(Context context) {
32          super(context);
33      }
34  
35      static public BlackboxStackModelHandler makeInstance(Context context, ModelInterpretationContext ic) {
36          return new BlackboxStackModelHandler(context);
37      }
38  
39      @Override
40      protected Class<BlackboxStackModel> getSupportedModelClass() {
41          return BlackboxStackModel.class;
42      }
43      
44      @Override
45      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
46  
47          BlackboxStackModel stackModel = (BlackboxStackModel) model;
48          
49          String name = stackModel.getName();
50          
51          ContextBase contextBase = (ContextBase) context;
52          
53          @SuppressWarnings("unchecked")
54          Stack<String> aStack = (Stack) context.getObject(STACK_TEST);
55          if(aStack == null) {
56              aStack = new Stack<>();
57              contextBase.putObject(STACK_TEST, aStack);
58          }
59          aStack.push(name);
60      }
61      
62      @Override
63      public void postHandle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException {
64      }
65  
66  }