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