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