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.conditional;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.boolex.PropertyCondition;
19  import ch.qos.logback.core.model.Model;
20  import ch.qos.logback.core.model.conditional.IfModel;
21  import ch.qos.logback.core.model.conditional.ByPropertiesConditionModel;
22  import ch.qos.logback.core.model.processor.ModelHandlerBase;
23  import ch.qos.logback.core.model.processor.ModelHandlerException;
24  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
25  import ch.qos.logback.core.util.OptionHelper;
26  
27  import static ch.qos.logback.core.model.conditional.IfModel.BranchState.ELSE_BRANCH;
28  import static ch.qos.logback.core.model.conditional.IfModel.BranchState.IF_BRANCH;
29  
30  public class ByPropertiesConditionModelHandler extends ModelHandlerBase {
31  
32      private boolean inError = false;
33      PropertyCondition propertyEvaluator;
34  
35      public ByPropertiesConditionModelHandler(Context context) {
36          super(context);
37      }
38  
39      @Override
40      protected Class<ByPropertiesConditionModel> getSupportedModelClass() {
41          return ByPropertiesConditionModel.class;
42      }
43  
44      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext mic) {
45          return new ByPropertiesConditionModelHandler(context);
46      }
47  
48  
49      @Override
50      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
51  
52          ByPropertiesConditionModel byPropertiesConditionModel = (ByPropertiesConditionModel) model;
53          String className = byPropertiesConditionModel.getClassName();
54          if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) {
55              addWarn("Missing className. This should have been caught earlier.");
56              inError = true;
57              return;
58          } else {
59              className = mic.getImport(className);
60          }
61          try {
62              addInfo("About to instantiate PropertyEvaluator of type [" + className + "]");
63  
64              propertyEvaluator = (PropertyCondition) OptionHelper.instantiateByClassName(className,
65                      PropertyCondition.class, context);
66              propertyEvaluator.setContext(context);
67              propertyEvaluator.setLocalPropertyContainer(mic);
68              mic.pushObject(propertyEvaluator);
69          } catch (Exception e) {
70              inError = true;
71              mic.pushObject(IfModel.BranchState.IN_ERROR);
72              addError("Could not create a SequenceNumberGenerator of type [" + className + "].", e);
73              throw new ModelHandlerException(e);
74          }
75      }
76  
77      public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
78          if (inError) {
79              return;
80          }
81          Object o = mic.peekObject();
82          if (o != propertyEvaluator) {
83              addWarn("The object at the of the stack is not the propertyEvaluator instance pushed earlier.");
84          } else {
85              mic.popObject();
86          }
87  
88          propertyEvaluator.start();
89          if(!propertyEvaluator.isStarted()) {
90              addError("PropertyEvaluator of type ["+propertyEvaluator.getClass().getName()+"] did not start successfully.");
91              mic.pushObject(IfModel.BranchState.IN_ERROR);
92              return;
93          }
94          boolean evaluationResult = propertyEvaluator.evaluate();
95          IfModel.BranchState branchState = evaluationResult ? IF_BRANCH : ELSE_BRANCH;
96          mic.pushObject(branchState);
97  
98      }
99  }