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
017import ch.qos.logback.core.model.processor.ModelInterpretationContext;
018import ch.qos.logback.core.spi.ContextAwareBase;
019import ch.qos.logback.core.spi.PropertyContainer;
020import ch.qos.logback.core.util.OptionHelper;
021
022/**
023 * <p>Abstract base class provides some scaffolding. It is intended to ease migration
024 * from <b>legacy</b> conditional processing in configuration files
025 * (e.g. &lt;if>, &lt;then&gt;, &lt;else>) using the Janino library. Nevertheless,
026 * it should also be useful in newly written code.</p>
027 *
028 * <p>Properties are looked up in the following order:</p>
029 *
030 * <ol>
031 *  <li>In the local property container, usually the {@link ModelInterpretationContext} </li>
032 *  <li>in the logger context</li>
033 *  <li>system properties</li>
034 *  <li>environment variables</li>
035 * </ol>
036 *
037 * @see OptionHelper#propertyLookup(String, PropertyContainer, PropertyContainer)
038 * @since 1.5.20
039 * @author Ceki G&uuml;lc&uuml;
040 */
041abstract public class PropertyConditionBase extends ContextAwareBase implements PropertyCondition {
042
043    /**
044     * Indicates whether this evaluator has been started.
045     */
046    boolean started;
047    /**
048     * <p>The local property container used for property lookups.</p>
049     *
050     * <p>Local properties correspond to the properties in the embedding
051     * configurator, i.e. usually the {@link ModelInterpretationContext} instance.</p>
052     */
053    PropertyContainer localPropertyContainer;
054
055    /**
056     * Returns the local property container used by this evaluator.
057     *
058     * <p>Local properties correspond to the properties in the embedding
059     * configurator, i.e. usually the {@link ModelInterpretationContext} instance.</p>
060     *
061     * @return the local property container
062     */
063    @Override
064    public PropertyContainer getLocalPropertyContainer() {
065        return localPropertyContainer;
066    }
067
068    /**
069     * Sets the local property container for this evaluator.
070     *
071     * <p>Local properties correspond to the properties in the embedding
072     * configurator, i.e. usually the {@link ModelInterpretationContext} instance.</p>
073     *
074     * @param aLocalPropertyContainer the local property container to set
075     */
076    @Override
077    public void setLocalPropertyContainer(PropertyContainer aLocalPropertyContainer) {
078        this.localPropertyContainer = aLocalPropertyContainer;
079    }
080
081    /**
082     * Checks if the property with the given key is null.
083     *
084     * <p>The property is looked up via the
085     * {@link OptionHelper#propertyLookup(String, PropertyContainer, PropertyContainer)} method.
086     * See above for the lookup order.</p>
087     *
088     * @param k the property key
089     * @return true if the property is null, false otherwise
090     */
091    public boolean isNull(String k) {
092        String val = OptionHelper.propertyLookup(k, localPropertyContainer, getContext());
093        return (val == null);
094    }
095
096    /**
097     * Checks if the property with the given key is defined (not null).
098     *
099     * <p>The property is looked up via the
100     * {@link OptionHelper#propertyLookup(String, PropertyContainer, PropertyContainer)} method.
101     * See above for the lookup order.</p>
102     *
103     * @param k the property key
104     * @return true if the property is defined, false otherwise
105     */
106    public boolean isDefined(String k) {
107        String val = OptionHelper.propertyLookup(k, localPropertyContainer, getContext());
108        return (val != null);
109    }
110
111    /**
112     * Retrieves the property value for the given key, returning an empty string if null.
113     * This is a shorthand for {@link #property(String)}.
114     *
115     * @param k the property key
116     * @return the property value or an empty string
117     */
118    public String p(String k) {
119        return property(k);
120    }
121
122    /**
123     * Retrieves the property value for the given key, returning an empty string if null.
124     *
125     * <p>The property is looked up via the
126     * {@link OptionHelper#propertyLookup(String, PropertyContainer, PropertyContainer)} method.
127     * See above for the lookup order.</p>
128     *
129     * @param k the property key
130     * @return the property value or an empty string
131     */
132    public String property(String k) {
133        String val = OptionHelper.propertyLookup(k, localPropertyContainer, getContext());
134        if (val != null)
135            return val;
136        else
137            return "";
138    }
139
140    /**
141     * Checks if this evaluator has been started.
142     *
143     * @return true if started, false otherwise
144     */
145    public boolean isStarted() {
146        return started;
147    }
148
149    /**
150     * Starts this evaluator.
151     */
152    public void start() {
153        started = true;
154    }
155
156    /**
157     * Stops this evaluator.
158     */
159    public void stop() {
160        started = false;
161    }
162}