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.boolex; 016 017/** 018 * Condition that evaluates to {@code true} when a property 019 * equals a specified expected value. 020 * 021 * <p>The property named by {@link #key} is resolved using the 022 * inherited property lookup mechanism (see {@code PropertyConditionBase}). 023 * If the resolved property value equals {@link #value} (using 024 * {@link String#equals(Object)}), this condition evaluates to {@code true}. 025 * 026 * @since 1.5.20 027 */ 028public class PropertyEqualityCondition extends PropertyConditionBase { 029 030 /** 031 * The property name (key) to look up. Must be set before starting. 032 */ 033 String key; 034 035 /** 036 * The expected value to compare the resolved property against. 037 */ 038 String value; 039 040 /** 041 * Start the component and validate required parameters. 042 * If either {@link #key} or {@link #value} is {@code null}, an error 043 * is reported and the component does not start. 044 */ 045 public void start() { 046 if (key == null) { 047 addError("In PropertyEqualsValue 'key' parameter cannot be null"); 048 return; 049 } 050 if (value == null) { 051 addError("In PropertyEqualsValue 'value' parameter cannot be null"); 052 return; 053 } 054 super.start(); 055 } 056 057 /** 058 * Return the configured expected value. 059 * 060 * @return the expected value, or {@code null} if not set 061 */ 062 public String getValue() { 063 return value; 064 } 065 066 /** 067 * Set the expected value that the resolved property must equal for 068 * this condition to evaluate to {@code true}. 069 * 070 * @param value the expected value 071 */ 072 public void setValue(String value) { 073 this.value = value; 074 } 075 076 /** 077 * Return the property key that will be looked up when evaluating the 078 * condition. 079 * 080 * @return the property key, or {@code null} if not set 081 */ 082 public String getKey() { 083 return key; 084 } 085 086 /** 087 * Set the property key to resolve during evaluation. 088 * 089 * @param key the property key 090 */ 091 public void setKey(String key) { 092 this.key = key; 093 } 094 095 /** 096 * Evaluate the condition: resolve the property named by {@link #key} 097 * and compare it to {@link #value}. 098 * 099 * @return {@code true} if the resolved property equals the expected 100 * value; {@code false} otherwise 101 */ 102 @Override 103 public boolean evaluate() { 104 if (key == null) { 105 addError("key cannot be null"); 106 return false; 107 } 108 109 String val = p(key); 110 if (val == null) 111 return false; 112 else { 113 return val.equals(value); 114 } 115 } 116 117}