Skip to content

Commit 0b4432a

Browse files
committed
provide an alternative to Janino based conditional configuration processing - Part 4
Signed-off-by: ceki <[email protected]>
1 parent 258558f commit 0b4432a

File tree

15 files changed

+282
-98
lines changed

15 files changed

+282
-98
lines changed

logback-core-blackbox/src/test/blackboxInput/joran/conditional/if0_NoJoran.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<x>
1616
<stack name="BEGIN"/>
17-
<condition class="ch.qos.logback.core.boolex.PropertyEqualsValue">
17+
<condition class="ch.qos.logback.core.boolex.PropertyEqualityCondition">
1818
<key>ki1</key>
1919
<value>val1</value>
2020
</condition>

logback-core-blackbox/src/test/blackboxInput/joran/conditional/ifSystem_NoJoran.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<x>
1818
<stack name="BEGIN"/>
1919

20-
<condition class="ch.qos.logback.core.blackbox.boolex.IsPropertyNullCondition">
20+
<condition class="ch.qos.logback.core.boolex.IsPropertyNullCondition">
2121
<key>sysKey</key>
2222
</condition>
2323
<if>

logback-core-blackbox/src/test/blackboxInput/joran/conditional/ifWithoutElse_NoJoran.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<x>
1616
<stack name="BEGIN"/>
17-
<condition class="ch.qos.logback.core.boolex.PropertyEqualsValue">
17+
<condition class="ch.qos.logback.core.boolex.PropertyEqualityCondition">
1818
<key>ki1</key>
1919
<value>val1</value>
2020
</condition>

logback-core-blackbox/src/test/blackboxInput/joran/conditional/if_localProperty_NoJoran.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<x>
1616
<stack name="BEGIN"/>
1717
<property name="Ki1" value="Val1"/>
18-
<condition class="ch.qos.logback.core.boolex.PropertyEqualsValue">
18+
<condition class="ch.qos.logback.core.boolex.PropertyEqualityCondition">
1919
<key>Ki1</key>
2020
<value>Val1</value>
2121
</condition>

logback-core-blackbox/src/test/java/ch/qos/logback/core/blackbox/boolex/AlwaysFalseCondition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
package ch.qos.logback.core.blackbox.boolex;
1616

17-
import ch.qos.logback.core.boolex.PropertyEvaluatorBase;
17+
import ch.qos.logback.core.boolex.PropertyConditionBase;
1818

19-
public class AlwaysFalseCondition extends PropertyEvaluatorBase {
19+
public class AlwaysFalseCondition extends PropertyConditionBase {
2020

2121
@Override
2222
public boolean evaluate() {

logback-core-blackbox/src/test/java/ch/qos/logback/core/blackbox/boolex/AlwaysTrueCondition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
package ch.qos.logback.core.blackbox.boolex;
1616

17-
import ch.qos.logback.core.boolex.PropertyEvaluatorBase;
17+
import ch.qos.logback.core.boolex.PropertyConditionBase;
1818

19-
public class AlwaysTrueCondition extends PropertyEvaluatorBase {
19+
public class AlwaysTrueCondition extends PropertyConditionBase {
2020

2121
@Override
2222
public boolean evaluate() {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2025, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
15+
package ch.qos.logback.core.boolex;
16+
17+
/**
18+
* Checks whether a named property is defined in the
19+
* context (e.g. system properties, environment, or the configured
20+
* property map used by the surrounding framework).
21+
*
22+
* <p>This condition expects a property name to be provided via
23+
* {@link #setKey(String)}. When {@link #evaluate()} is called it returns
24+
* {@code true} if the named property is defined and {@code false}
25+
* otherwise.
26+
*/
27+
public class IsPropertyDefinedCondition extends PropertyConditionBase {
28+
29+
/**
30+
* The property name to check for definition. Must be set before
31+
* starting this evaluator.
32+
*/
33+
String key;
34+
35+
/**
36+
* Start the evaluator. If the required {@link #key} is not set an
37+
* error is reported and startup is aborted.
38+
*/
39+
public void start() {
40+
if (key == null) {
41+
addError("In IsPropertyDefinedEvaluator 'key' parameter cannot be null");
42+
return;
43+
}
44+
super.start();
45+
}
46+
47+
/**
48+
* Return the configured property name (key) that this evaluator will
49+
* test for definition.
50+
*
51+
* @return the property key, or {@code null} if not set
52+
*/
53+
public String getKey() {
54+
return key;
55+
}
56+
57+
/**
58+
* Set the property name (key) to be checked by this evaluator.
59+
*
60+
* @param key the property name to check; must not be {@code null}
61+
*/
62+
public void setKey(String key) {
63+
this.key = key;
64+
}
65+
66+
67+
/**
68+
* Evaluate whether the configured property is defined.
69+
*
70+
* @return {@code true} if the property named by {@link #key} is
71+
* defined, {@code false} otherwise
72+
*/
73+
@Override
74+
public boolean evaluate() {
75+
return isDefined(key);
76+
}
77+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
* as published by the Free Software Foundation.
1313
*/
1414

15-
package ch.qos.logback.core.blackbox.boolex;
15+
package ch.qos.logback.core.boolex;
1616

17-
import ch.qos.logback.core.boolex.PropertyEvaluatorBase;
18-
19-
public class IsPropertyNullCondition extends PropertyEvaluatorBase {
17+
public class IsPropertyNullCondition extends PropertyConditionBase {
2018

2119
String key;
2220

logback-core/src/main/java/ch/qos/logback/core/boolex/PropertyEvaluator.java renamed to logback-core/src/main/java/ch/qos/logback/core/boolex/PropertyCondition.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import ch.qos.logback.core.Context;
1818
import ch.qos.logback.core.joran.conditional.Condition;
19-
import ch.qos.logback.core.joran.conditional.PropertyEvalScriptBuilder;
2019
import ch.qos.logback.core.spi.ContextAware;
2120
import ch.qos.logback.core.spi.LifeCycle;
2221
import ch.qos.logback.core.spi.PropertyContainer;
@@ -39,7 +38,7 @@
3938
* @since 1.5.20
4039
* @author Ceki G&uuml;lc&uuml;
4140
*/
42-
public interface PropertyEvaluator extends Condition, ContextAware, LifeCycle {
41+
public interface PropertyCondition extends Condition, ContextAware, LifeCycle {
4342

4443
/**
4544
* Returns the local {@link PropertyContainer} used for property lookups specific to the embedding configurator.

logback-core/src/main/java/ch/qos/logback/core/boolex/PropertyEvaluatorBase.java renamed to logback-core/src/main/java/ch/qos/logback/core/boolex/PropertyConditionBase.java

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,31 @@
1414

1515
package ch.qos.logback.core.boolex;
1616

17+
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
1718
import ch.qos.logback.core.spi.ContextAwareBase;
1819
import ch.qos.logback.core.spi.PropertyContainer;
1920
import ch.qos.logback.core.util.OptionHelper;
2021

2122
/**
22-
* <p>Abstract base class provides some scaffolding. it is intended to ease migration
23+
* <p>Abstract base class provides some scaffolding. It is intended to ease migration
2324
* from <b>legacy</b> conditional processing in configuration files
24-
* (e.g. &lt;if>, &lt;then>, &lt;else>) using the Janino library.
25-
* </p>
25+
* (e.g. &lt;if>, &lt;then&gt;, &lt;else>) using the Janino library. Nevertheless,
26+
* it should also be useful in newly written code.</p>
2627
*
27-
* <p>Nevertheless, it should also be useful in newly written code. </p>
28+
* <p>Properties are looked up in the following order:</p>
2829
*
30+
* <ol>
31+
* <li>In the local property container, usually the {@link ModelInterpretationContext} </li>
32+
* <li>in the logger context</li>
33+
* <li>system properties</li>
34+
* <li>environment variables</li>
35+
* </ol>
36+
*
37+
* @see OptionHelper#propertyLookup(String, PropertyContainer, PropertyContainer)
2938
* @since 1.5.20
3039
* @author Ceki G&uuml;lc&uuml;
3140
*/
32-
abstract public class PropertyEvaluatorBase extends ContextAwareBase implements PropertyEvaluator {
41+
abstract public class PropertyConditionBase extends ContextAwareBase implements PropertyCondition {
3342

3443
/**
3544
* Indicates whether this evaluator has been started.
@@ -39,33 +48,43 @@ abstract public class PropertyEvaluatorBase extends ContextAwareBase implements
3948
* <p>The local property container used for property lookups.</p>
4049
*
4150
* <p>Local properties correspond to the properties in the embedding
42-
* configurator.</p>
51+
* configurator, i.e. usually the {@link ModelInterpretationContext} instance.</p>
4352
*/
4453
PropertyContainer localPropertyContainer;
4554

4655
/**
47-
* Returns the property container used by this evaluator.
56+
* Returns the local property container used by this evaluator.
57+
*
58+
* <p>Local properties correspond to the properties in the embedding
59+
* configurator, i.e. usually the {@link ModelInterpretationContext} instance.</p>
4860
*
49-
* @return the property container
61+
* @return the local property container
5062
*/
5163
@Override
5264
public PropertyContainer getLocalPropertyContainer() {
5365
return localPropertyContainer;
5466
}
5567

5668
/**
57-
* Sets the property container for this evaluator.
69+
* Sets the local property container for this evaluator.
70+
*
71+
* <p>Local properties correspond to the properties in the embedding
72+
* configurator, i.e. usually the {@link ModelInterpretationContext} instance.</p>
5873
*
59-
* @param aPropertyContainer the property container to set
74+
* @param aLocalPropertyContainer the local property container to set
6075
*/
6176
@Override
62-
public void setLocalPropertyContainer(PropertyContainer aPropertyContainer) {
63-
this.localPropertyContainer = aPropertyContainer;
77+
public void setLocalPropertyContainer(PropertyContainer aLocalPropertyContainer) {
78+
this.localPropertyContainer = aLocalPropertyContainer;
6479
}
6580

6681
/**
6782
* Checks if the property with the given key is null.
6883
*
84+
* <p>The property is looked up via the
85+
* {@link OptionHelper#propertyLookup(String, PropertyContainer, PropertyContainer)} method.
86+
* See above for the lookup order.</p>
87+
*
6988
* @param k the property key
7089
* @return true if the property is null, false otherwise
7190
*/
@@ -77,6 +96,10 @@ public boolean isNull(String k) {
7796
/**
7897
* Checks if the property with the given key is defined (not null).
7998
*
99+
* <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+
*
80103
* @param k the property key
81104
* @return true if the property is defined, false otherwise
82105
*/
@@ -99,6 +122,10 @@ public String p(String k) {
99122
/**
100123
* Retrieves the property value for the given key, returning an empty string if null.
101124
*
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+
*
102129
* @param k the property key
103130
* @return the property value or an empty string
104131
*/

0 commit comments

Comments
 (0)