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