1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, 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 v1.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.conditional;
15  
16  import ch.qos.logback.core.util.EnvUtil;
17  import ch.qos.logback.core.util.OptionHelper;
18  import ch.qos.logback.core.Context;
19  import ch.qos.logback.core.CoreConstants;
20  import ch.qos.logback.core.joran.conditional.Condition;
21  import ch.qos.logback.core.joran.conditional.PropertyEvalScriptBuilder;
22  import ch.qos.logback.core.model.Model;
23  import ch.qos.logback.core.model.conditional.IfModel;
24  import ch.qos.logback.core.model.conditional.IfModel.BranchState;
25  import ch.qos.logback.core.model.processor.ModelHandlerBase;
26  import ch.qos.logback.core.model.processor.ModelHandlerException;
27  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
28  import ch.qos.logback.core.spi.ScanException;
29  
30  public class IfModelHandler extends ModelHandlerBase {
31  
32  
33      public static final String MISSING_JANINO_MSG = "Could not find Janino library on the class path. Skipping conditional processing.";
34      public static final String MISSING_JANINO_SEE = "See also " + CoreConstants.CODES_URL + "#ifJanino";
35  
36      enum Branch {IF_BRANCH, ELSE_BRANCH; }
37      
38      IfModel ifModel = null;
39      
40      public IfModelHandler(Context context) {
41          super(context);
42      }
43  
44      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
45          return new IfModelHandler(context);
46      }
47  
48      @Override
49      protected Class<IfModel> getSupportedModelClass() {
50          return IfModel.class;
51      }
52      
53      @Override
54      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
55          
56          ifModel = (IfModel) model;
57          
58          if (!EnvUtil.isJaninoAvailable()) {
59              addError(MISSING_JANINO_MSG);
60              addError(MISSING_JANINO_SEE);
61              return;
62          }
63          
64          mic.pushModel(ifModel);
65          Condition condition = null;
66          int lineNum = model.getLineNumber();
67  
68          String conditionStr = ifModel.getCondition();
69          if (!OptionHelper.isNullOrEmptyOrAllSpaces(conditionStr)) {
70              try {
71                  conditionStr = OptionHelper.substVars(conditionStr, mic, context);
72              } catch (ScanException e) {
73                 addError("Failed to parse input [" + conditionStr + "] on line "+lineNum, e);
74                 ifModel.setBranchState(BranchState.IN_ERROR);
75                 return;
76              }
77  
78              try {
79                  PropertyEvalScriptBuilder pesb = new PropertyEvalScriptBuilder(mic);
80                  pesb.setContext(context);
81                  condition = pesb.build(conditionStr);
82              } catch (Exception|NoClassDefFoundError e) {
83                  ifModel.setBranchState(BranchState.IN_ERROR);
84                  addError("Failed to parse condition [" + conditionStr + "] on line "+lineNum, e);
85                  return;
86              }
87  
88              if (condition != null) {
89                  boolean boolResult = condition.evaluate();
90                  addInfo("Condition ["+conditionStr+"] evaluated to "+boolResult+ " on line "+lineNum);
91                  ifModel.setBranchState(boolResult);
92              } else {
93                  addError("The condition variable is null. This should not occur.");
94                  ifModel.setBranchState(BranchState.IN_ERROR);
95                  return;
96              }
97          }
98      }
99      
100     
101     @Override
102     public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
103 
104         if(mic.isModelStackEmpty()) {
105             addError("Unexpected unexpected empty model stack.");
106             return;
107         }
108 
109         Object o = mic.peekModel();
110         if (o != ifModel) {
111             addWarn("The object [" + o + "] on the top the of the stack is not the expected [" + ifModel);
112         } else {
113             mic.popModel();
114         }
115     }
116 
117 }