001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2025, 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 public static final String NEW_OPERATOR_DISALLOWED_MSG = "The 'condition' attribute may not contain the 'new' operator."; 037 public static final String NEW_OPERATOR_DISALLOWED_SEE = "See also " + CoreConstants.CODES_URL + "#conditionNew"; 038 039 enum Branch {IF_BRANCH, ELSE_BRANCH; } 040 041 IfModel ifModel = null; 042 043 public IfModelHandler(Context context) { 044 super(context); 045 } 046 047 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) { 048 return new IfModelHandler(context); 049 } 050 051 @Override 052 protected Class<IfModel> getSupportedModelClass() { 053 return IfModel.class; 054 } 055 056 @Override 057 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 058 059 ifModel = (IfModel) model; 060 061 if (!EnvUtil.isJaninoAvailable()) { 062 addError(MISSING_JANINO_MSG); 063 addError(MISSING_JANINO_SEE); 064 return; 065 } 066 067 mic.pushModel(ifModel); 068 Condition condition = null; 069 int lineNum = model.getLineNumber(); 070 071 String conditionStr = ifModel.getCondition(); 072 if (!OptionHelper.isNullOrEmptyOrAllSpaces(conditionStr)) { 073 try { 074 conditionStr = OptionHelper.substVars(conditionStr, mic, context); 075 } catch (ScanException e) { 076 addError("Failed to parse input [" + conditionStr + "] on line "+lineNum, e); 077 ifModel.setBranchState(BranchState.IN_ERROR); 078 return; 079 } 080 081 // do not allow 'new' operator 082 if(hasNew(conditionStr)) { 083 addError(NEW_OPERATOR_DISALLOWED_MSG); 084 addError(NEW_OPERATOR_DISALLOWED_SEE); 085 return; 086 } 087 088 try { 089 PropertyEvalScriptBuilder pesb = new PropertyEvalScriptBuilder(mic); 090 pesb.setContext(context); 091 condition = pesb.build(conditionStr); 092 } catch (Exception|NoClassDefFoundError e) { 093 ifModel.setBranchState(BranchState.IN_ERROR); 094 addError("Failed to parse condition [" + conditionStr + "] on line "+lineNum, e); 095 return; 096 } 097 098 if (condition != null) { 099 boolean boolResult = condition.evaluate(); 100 addInfo("Condition ["+conditionStr+"] evaluated to "+boolResult+ " on line "+lineNum); 101 ifModel.setBranchState(boolResult); 102 } else { 103 addError("The condition variable is null. This should not occur."); 104 ifModel.setBranchState(BranchState.IN_ERROR); 105 return; 106 } 107 } 108 } 109 110 private boolean hasNew(String conditionStr) { 111 return conditionStr.contains("new "); 112 } 113 114 115 @Override 116 public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 117 118 if(mic.isModelStackEmpty()) { 119 addError("Unexpected unexpected empty model stack."); 120 return; 121 } 122 123 Object o = mic.peekModel(); 124 if (o != ifModel) { 125 addWarn("The object [" + o + "] on the top the of the stack is not the expected [" + ifModel); 126 } else { 127 mic.popModel(); 128 } 129 } 130 131}