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