Skip to content

Commit 258558f

Browse files
committed
provide an alternative to Janino based conditional configuration processing - Part 3
Signed-off-by: ceki <ceki@qos.ch>
1 parent ee77a70 commit 258558f

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<!--
4+
~ Logback: the reliable, generic, fast and flexible logging framework.
5+
~ Copyright (C) 1999-2025, QOS.ch. All rights reserved.
6+
~
7+
~ This program and the accompanying materials are dual-licensed under
8+
~ either the terms of the Eclipse Public License v1.0 as published by
9+
~ the Eclipse Foundation
10+
~
11+
~ or (per the licensee's choosing)
12+
~
13+
~ under the terms of the GNU Lesser General Public License version 2.1
14+
~ as published by the Free Software Foundation.
15+
-->
16+
17+
<x>
18+
<stack name="BEGIN"/>
19+
20+
<condition class="ch.qos.logback.core.blackbox.boolex.IsPropertyNullCondition">
21+
<key>sysKey</key>
22+
</condition>
23+
<if>
24+
<then>
25+
<property scope="context" name="dynaKey" value="dynaVal"/>
26+
</then>
27+
</if>
28+
<stack name="END"/>
29+
</x>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.blackbox.boolex;
16+
17+
import ch.qos.logback.core.boolex.PropertyEvaluatorBase;
18+
19+
public class IsPropertyNullCondition extends PropertyEvaluatorBase {
20+
21+
String key;
22+
23+
public void start() {
24+
if (key == null) {
25+
addError("In IsPropertyNullCondition 'key' parameter cannot be null");
26+
return;
27+
}
28+
super.start();
29+
}
30+
31+
32+
public String getKey() {
33+
return key;
34+
}
35+
36+
public void setKey(String key) {
37+
this.key = key;
38+
}
39+
40+
@Override
41+
public boolean evaluate() {
42+
return isNull(key);
43+
}
44+
}

logback-core-blackbox/src/test/java/ch/qos/logback/core/blackbox/joran/conditional/IfThenElseTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ public void nestedIf_NoJoran() throws JoranException {
220220
verifyConfig(new String[] { "BEGIN", "a", "c", "END" });
221221
Assertions.assertTrue(checker.isErrorFree(0));
222222
}
223-
224223
// ----------------------------------------------------------------------------------------------------
225-
226224
@Test
227225
public void useNonExistenceOfSystemPropertyToDefineAContextProperty() throws JoranException {
228226
Assertions.assertNull(System.getProperty(sysKey));
@@ -231,7 +229,15 @@ public void useNonExistenceOfSystemPropertyToDefineAContextProperty() throws Jor
231229
//System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
232230
Assertions.assertNotNull(context.getProperty(dynaKey));
233231
}
234-
232+
@Test
233+
public void useNonExistenceOfSystemPropertyToDefineAContextProperty_NoJoran() throws JoranException {
234+
Assertions.assertNull(System.getProperty(sysKey));
235+
Assertions.assertNull(context.getProperty(dynaKey));
236+
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem_NoJoran.xml");
237+
//System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
238+
Assertions.assertNotNull(context.getProperty(dynaKey));
239+
}
240+
// ----------------------------------------------------------------------------------------------------
235241
@Test
236242
public void noContextPropertyShouldBeDefinedIfSystemPropertyExists() throws JoranException {
237243
System.setProperty(sysKey, "a");
@@ -242,6 +248,17 @@ public void noContextPropertyShouldBeDefinedIfSystemPropertyExists() throws Jora
242248
Assertions.assertNull(context.getProperty(dynaKey));
243249
}
244250

251+
@Test
252+
public void noContextPropertyShouldBeDefinedIfSystemPropertyExists_NoJoran() throws JoranException {
253+
System.setProperty(sysKey, "a");
254+
Assertions.assertNull(context.getProperty(dynaKey));
255+
System.out.println("before " + dynaKey + "=" + context.getProperty(dynaKey));
256+
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem_NoJoran.xml");
257+
System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
258+
Assertions.assertNull(context.getProperty(dynaKey));
259+
}
260+
// ----------------------------------------------------------------------------------------------------
261+
245262
private void verifyConfig(String[] expected) {
246263
Stack<String> witness = new Stack<>();
247264
witness.addAll(Arrays.asList(expected));

logback-core/src/main/java/ch/qos/logback/core/model/processor/conditional/ByPropertiesConditionModelHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public void postHandle(ModelInterpretationContext mic, Model model) throws Model
8585
mic.popObject();
8686
}
8787

88+
propertyEvaluator.start();
89+
if(!propertyEvaluator.isStarted()) {
90+
addError("PropertyEvaluator of type ["+propertyEvaluator.getClass().getName()+"] did not start successfully.");
91+
mic.pushObject(IfModel.BranchState.IN_ERROR);
92+
return;
93+
}
8894
boolean evaluationResult = propertyEvaluator.evaluate();
8995
IfModel.BranchState branchState = evaluationResult ? IF_BRANCH : ELSE_BRANCH;
9096
mic.pushObject(branchState);

0 commit comments

Comments
 (0)