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 */ 014 015package ch.qos.logback.core.model.processor.conditional; 016 017import ch.qos.logback.core.Context; 018import ch.qos.logback.core.boolex.PropertyCondition; 019import ch.qos.logback.core.model.Model; 020import ch.qos.logback.core.model.conditional.IfModel; 021import ch.qos.logback.core.model.conditional.ByPropertiesConditionModel; 022import ch.qos.logback.core.model.processor.ModelHandlerBase; 023import ch.qos.logback.core.model.processor.ModelHandlerException; 024import ch.qos.logback.core.model.processor.ModelInterpretationContext; 025import ch.qos.logback.core.util.OptionHelper; 026 027import static ch.qos.logback.core.model.conditional.IfModel.BranchState.ELSE_BRANCH; 028import static ch.qos.logback.core.model.conditional.IfModel.BranchState.IF_BRANCH; 029 030public class ByPropertiesConditionModelHandler extends ModelHandlerBase { 031 032 static final String EVALUTATOR_IS_IN_ERROR_MSG0 = "The [if] statement will be marked IN_ERROR and none of its branches will be executed."; 033 static final String EVALUTATOR_IS_IN_ERROR_MSG1 = "The [if] statement will be marked IN_ERROR and none of its branches will be executed."; 034 035 036 private boolean inError = false; 037 PropertyCondition propertyEvaluator; 038 039 public ByPropertiesConditionModelHandler(Context context) { 040 super(context); 041 } 042 043 @Override 044 protected Class<ByPropertiesConditionModel> getSupportedModelClass() { 045 return ByPropertiesConditionModel.class; 046 } 047 048 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext mic) { 049 return new ByPropertiesConditionModelHandler(context); 050 } 051 052 053 @Override 054 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 055 056 ByPropertiesConditionModel byPropertiesConditionModel = (ByPropertiesConditionModel) model; 057 String className = byPropertiesConditionModel.getClassName(); 058 if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) { 059 addWarn("Missing className. This should have been caught earlier."); 060 inError = true; 061 return; 062 } else { 063 className = mic.getImport(className); 064 } 065 try { 066 addInfo("About to instantiate PropertyEvaluator of type [" + className + "]"); 067 068 propertyEvaluator = (PropertyCondition) OptionHelper.instantiateByClassName(className, 069 PropertyCondition.class, context); 070 propertyEvaluator.setContext(context); 071 propertyEvaluator.setLocalPropertyContainer(mic); 072 mic.pushObject(propertyEvaluator); 073 } catch (Exception e) { 074 inError = true; 075 mic.pushObject(IfModel.BranchState.IN_ERROR); 076 addError("Could not create a SequenceNumberGenerator of type [" + className + "].", e); 077 throw new ModelHandlerException(e); 078 } 079 } 080 081 public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 082 if (inError) { 083 return; 084 } 085 Object o = mic.peekObject(); 086 if (o != propertyEvaluator) { 087 addWarn("The object at the of the stack is not the propertyEvaluator instance pushed earlier."); 088 } else { 089 mic.popObject(); 090 } 091 092 propertyEvaluator.start(); 093 if(!propertyEvaluator.isStarted()) { 094 addError("PropertyEvaluator of type ["+propertyEvaluator.getClass().getName()+"] did not start successfully."); 095 addError(EVALUTATOR_IS_IN_ERROR_MSG0); 096 mic.pushObject(IfModel.BranchState.IN_ERROR); 097 return; 098 } 099 boolean evaluationResult = propertyEvaluator.evaluate(); 100 IfModel.BranchState branchState = evaluationResult ? IF_BRANCH : ELSE_BRANCH; 101 mic.pushObject(branchState); 102 103 } 104}