001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2022, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.model.processor.conditional; 015 016import ch.qos.logback.core.util.EnvUtil; 017import ch.qos.logback.core.util.OptionHelper; 018import ch.qos.logback.core.Context; 019import ch.qos.logback.core.CoreConstants; 020import ch.qos.logback.core.joran.conditional.Condition; 021import ch.qos.logback.core.joran.conditional.PropertyEvalScriptBuilder; 022import ch.qos.logback.core.model.Model; 023import ch.qos.logback.core.model.conditional.IfModel; 024import ch.qos.logback.core.model.conditional.IfModel.BranchState; 025import ch.qos.logback.core.model.processor.ModelHandlerBase; 026import ch.qos.logback.core.model.processor.ModelHandlerException; 027import ch.qos.logback.core.model.processor.ModelInterpretationContext; 028import ch.qos.logback.core.spi.ScanException; 029 030public class IfModelHandler extends ModelHandlerBase { 031 032 033 public static final String MISSING_JANINO_MSG = "Could not find Janino library on the class path. Skipping conditional processing."; 034 public static final String MISSING_JANINO_SEE = "See also " + CoreConstants.CODES_URL + "#ifJanino"; 035 036 enum Branch {IF_BRANCH, ELSE_BRANCH; } 037 038 IfModel ifModel = null; 039 040 public IfModelHandler(Context context) { 041 super(context); 042 } 043 044 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) { 045 return new IfModelHandler(context); 046 } 047 048 @Override 049 protected Class<IfModel> getSupportedModelClass() { 050 return IfModel.class; 051 } 052 053 @Override 054 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 055 056 ifModel = (IfModel) model; 057 058 if (!EnvUtil.isJaninoAvailable()) { 059 addError(MISSING_JANINO_MSG); 060 addError(MISSING_JANINO_SEE); 061 return; 062 } 063 064 mic.pushModel(ifModel); 065 Condition condition = null; 066 int lineNum = model.getLineNumber(); 067 068 String conditionStr = ifModel.getCondition(); 069 if (!OptionHelper.isNullOrEmptyOrAllSpaces(conditionStr)) { 070 try { 071 conditionStr = OptionHelper.substVars(conditionStr, mic, context); 072 } catch (ScanException e) { 073 addError("Failed to parse input [" + conditionStr + "] on line "+lineNum, e); 074 ifModel.setBranchState(BranchState.IN_ERROR); 075 return; 076 } 077 078 try { 079 PropertyEvalScriptBuilder pesb = new PropertyEvalScriptBuilder(mic); 080 pesb.setContext(context); 081 condition = pesb.build(conditionStr); 082 } catch (Exception|NoClassDefFoundError e) { 083 ifModel.setBranchState(BranchState.IN_ERROR); 084 addError("Failed to parse condition [" + conditionStr + "] on line "+lineNum, e); 085 return; 086 } 087 088 if (condition != null) { 089 boolean boolResult = condition.evaluate(); 090 addInfo("Condition ["+conditionStr+"] evaluated to "+boolResult+ " on line "+lineNum); 091 ifModel.setBranchState(boolResult); 092 } else { 093 addError("The condition variable is null. This should not occur."); 094 ifModel.setBranchState(BranchState.IN_ERROR); 095 return; 096 } 097 } 098 } 099 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}