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 067 String conditionStr = ifModel.getCondition(); 068 if (!OptionHelper.isNullOrEmpty(conditionStr)) { 069 try { 070 conditionStr = OptionHelper.substVars(conditionStr, mic, context); 071 } catch (ScanException e) { 072 addError("Failed to parse input [" + conditionStr + "]", e); 073 ifModel.setBranchState(BranchState.IN_ERROR); 074 return; 075 } 076 PropertyEvalScriptBuilder pesb = new PropertyEvalScriptBuilder(mic); 077 pesb.setContext(context); 078 try { 079 condition = pesb.build(conditionStr); 080 } catch (Exception e) { 081 ifModel.setBranchState(BranchState.IN_ERROR); 082 addError("Failed to parse condition [" + conditionStr + "]", e); 083 return; 084 } 085 086 if (condition != null) { 087 boolean boolResult = condition.evaluate(); 088 ifModel.setBranchState(boolResult); 089 } else { 090 addError("The condition variable is null. This should not occur."); 091 ifModel.setBranchState(BranchState.IN_ERROR); 092 return; 093 } 094 } 095 } 096 097 098 @Override 099 public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 100 101 Object o = mic.peekModel(); 102 if (o != ifModel) { 103 addWarn("The object [" + o + "] on the top the of the stack is not the expected [" + ifModel); 104 } else { 105 mic.popModel(); 106 } 107 } 108 109}