Skip to content

Commit ee77a70

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

File tree

13 files changed

+337
-11
lines changed

13 files changed

+337
-11
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
<x>
16+
<stack name="BEGIN"/>
17+
<condition class="ch.qos.logback.core.boolex.PropertyEqualsValue">
18+
<key>ki1</key>
19+
<value>val1</value>
20+
</condition>
21+
<if>
22+
<then>
23+
<stack name="a"/>
24+
</then>
25+
<else>
26+
<stack name="b"/>
27+
</else>
28+
</if>
29+
<stack name="END"/>
30+
</x>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
<x>
16+
<stack name="BEGIN"/>
17+
<condition class="ch.qos.logback.core.boolex.PropertyEqualsValue">
18+
<key>ki1</key>
19+
<value>val1</value>
20+
</condition>
21+
<if>
22+
<then>
23+
<stack name="a"/>
24+
</then>
25+
</if>
26+
<stack name="END"/>
27+
</x>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
<x>
16+
<stack name="BEGIN"/>
17+
<property name="Ki1" value="Val1"/>
18+
<condition class="ch.qos.logback.core.boolex.PropertyEqualsValue">
19+
<key>Ki1</key>
20+
<value>Val1</value>
21+
</condition>
22+
<if>
23+
<then>
24+
<stack name="a"/>
25+
</then>
26+
<else>
27+
<stack name="b"/>
28+
</else>
29+
</if>
30+
<stack name="END"/>
31+
</x>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
<x>
16+
<stack name="BEGIN"/>
17+
18+
<condition class="ch.qos.logback.core.blackbox.boolex.AlwaysTrueCondition"/>
19+
<if>
20+
<then>
21+
<stack name="a"/>
22+
<condition class="ch.qos.logback.core.blackbox.boolex.AlwaysFalseCondition"/>
23+
<if condition='1 != 1'>
24+
<then>
25+
<stack name="b"/>
26+
</then>
27+
<else>
28+
<stack name="c"/>
29+
</else>
30+
</if>
31+
</then>
32+
<else>
33+
<stack name="d"/>
34+
</else>
35+
</if>
36+
<stack name="END"/>
37+
38+
</x>
39+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 AlwaysFalseCondition extends PropertyEvaluatorBase {
20+
21+
@Override
22+
public boolean evaluate() {
23+
return false;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 AlwaysTrueCondition extends PropertyEvaluatorBase {
20+
21+
@Override
22+
public boolean evaluate() {
23+
return true;
24+
}
25+
}

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import ch.qos.logback.core.blackbox.model.processor.BlackboxStackModelHandler;
2525
import ch.qos.logback.core.joran.action.Action;
2626
import ch.qos.logback.core.joran.action.PropertyAction;
27+
import ch.qos.logback.core.joran.conditional.ByPropertiesConditionAction;
2728
import ch.qos.logback.core.joran.conditional.ElseAction;
2829
import ch.qos.logback.core.joran.conditional.IfAction;
2930
import ch.qos.logback.core.joran.conditional.ThenAction;
@@ -32,20 +33,23 @@
3233
import ch.qos.logback.core.joran.spi.RuleStore;
3334
import ch.qos.logback.core.model.ImplicitModel;
3435
import ch.qos.logback.core.model.PropertyModel;
36+
import ch.qos.logback.core.model.conditional.ByPropertiesConditionModel;
3537
import ch.qos.logback.core.model.conditional.ElseModel;
3638
import ch.qos.logback.core.model.conditional.IfModel;
3739
import ch.qos.logback.core.model.conditional.ThenModel;
3840
import ch.qos.logback.core.model.processor.DefaultProcessor;
3941
import ch.qos.logback.core.model.processor.ImplicitModelHandler;
4042
import ch.qos.logback.core.model.processor.NOPModelHandler;
4143
import ch.qos.logback.core.model.processor.PropertyModelHandler;
44+
import ch.qos.logback.core.model.processor.conditional.ByPropertiesConditionModelHandler;
4245
import ch.qos.logback.core.model.processor.conditional.ElseModelHandler;
4346
import ch.qos.logback.core.model.processor.conditional.IfModelHandler;
4447
import ch.qos.logback.core.model.processor.conditional.ThenModelHandler;
4548
import ch.qos.logback.core.status.Status;
4649
import ch.qos.logback.core.status.StatusUtil;
4750
import ch.qos.logback.core.testUtil.RandomUtil;
4851
import ch.qos.logback.core.util.StatusPrinter;
52+
import ch.qos.logback.core.util.StatusPrinter2;
4953
import org.junit.jupiter.api.AfterEach;
5054
import org.junit.jupiter.api.Assertions;
5155
import org.junit.jupiter.api.BeforeEach;
@@ -60,6 +64,7 @@ public class IfThenElseTest {
6064

6165
Context context = new ContextBase();
6266
StatusUtil checker = new StatusUtil(context);
67+
StatusPrinter2 statusPrinter2 = new StatusPrinter2();
6368
BlackboxSimpleConfigurator simpleConfigurator;
6469
int diff = RandomUtil.getPositiveInt();
6570
static final String CONDITIONAL_DIR_PREFIX = BlackboxCoreTestConstants.JORAN_INPUT_PREFIX + "conditional/";
@@ -75,6 +80,7 @@ public void setUp() throws Exception {
7580
rulesMap.put(new ElementSelector("x"), BlackboxTopElementAction::new);
7681
rulesMap.put(new ElementSelector("x/stack"), BlackboxStackAction::new);
7782
rulesMap.put(new ElementSelector("x/property"), PropertyAction::new);
83+
rulesMap.put(new ElementSelector("*/condition"), ByPropertiesConditionAction::new);
7884
rulesMap.put(new ElementSelector("*/if"), IfAction::new);
7985
rulesMap.put(new ElementSelector("*/if/then"), ThenAction::new);
8086
rulesMap.put(new ElementSelector("*/if/else"), ElseAction::new);
@@ -98,6 +104,7 @@ protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
98104
defaultProcessor.addHandler(BlackboxStackModel.class, BlackboxStackModelHandler::makeInstance);
99105
defaultProcessor.addHandler(PropertyModel.class, PropertyModelHandler::makeInstance);
100106
defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);
107+
defaultProcessor.addHandler(ByPropertiesConditionModel.class, ByPropertiesConditionModelHandler::makeInstance);
101108
defaultProcessor.addHandler(IfModel.class, IfModelHandler::makeInstance);
102109
defaultProcessor.addHandler(ThenModel.class, ThenModelHandler::makeInstance);
103110
defaultProcessor.addHandler(ElseModel.class, ElseModelHandler::makeInstance);
@@ -109,7 +116,7 @@ protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
109116

110117
@AfterEach
111118
public void tearDown() throws Exception {
112-
StatusPrinter.printIfErrorsOccured(context);
119+
statusPrinter2.printIfErrorsOccured(context);
113120
System.clearProperty(sysKey);
114121
}
115122

@@ -120,14 +127,22 @@ public void ifWithExec() throws JoranException {
120127
checker.containsException(org.codehaus.commons.compiler.CompileException.class);
121128
checker.containsMatch(Status.ERROR, "Failed to parse condition");
122129
}
123-
130+
// ----------------------------------------------------------------------------------------------------
124131
@Test
125132
public void whenContextPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException {
126133
context.putProperty(ki1, val1);
127134
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if0.xml");
128135
verifyConfig(new String[] { "BEGIN", "a", "END" });
129136
}
130137

138+
@Test
139+
public void whenContextPropertyIsSet_IfThenBranchIsEvaluated_WithoutJoran() throws JoranException {
140+
context.putProperty(ki1, val1);
141+
142+
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if0_NoJoran.xml");
143+
verifyConfig(new String[] { "BEGIN", "a", "END" });
144+
}
145+
// ----------------------------------------------------------------------------------------------------
131146
@Test
132147
public void ifWithNew() throws JoranException {
133148
context.putProperty(ki1, val1);
@@ -137,47 +152,83 @@ public void ifWithNew() throws JoranException {
137152
verifyConfig(new String[] { "BEGIN", "END" });
138153
}
139154

140-
155+
// ----------------------------------------------------------------------------------------------------
141156
@Test
142157
public void whenLocalPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException {
143158
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if_localProperty.xml");
144159
verifyConfig(new String[] { "BEGIN", "a", "END" });
145160
}
146161

162+
@Test
163+
public void whenLocalPropertyIsSet_IfThenBranchIsEvaluated_NoJoran() throws JoranException {
164+
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if_localProperty_NoJoran.xml");
165+
verifyConfig(new String[] { "BEGIN", "a", "END" });
166+
}
167+
// ----------------------------------------------------------------------------------------------------
147168
@Test
148169
public void whenNoPropertyIsDefined_ElseBranchIsEvaluated() throws JoranException {
149170
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if0.xml");
150171
verifyConfig(new String[] { "BEGIN", "b", "END" });
151172
}
152173

174+
@Test
175+
public void whenNoPropertyIsDefined_ElseBranchIsEvaluated_NoJoran() throws JoranException {
176+
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if0_NoJoran.xml");
177+
verifyConfig(new String[] { "BEGIN", "b", "END" });
178+
}
179+
// ----------------------------------------------------------------------------------------------------
180+
153181
@Test
154182
public void whenContextPropertyIsSet_IfThenBranchIsEvaluated_NO_ELSE_DEFINED() throws JoranException {
155183
context.putProperty(ki1, val1);
156184
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml");
157185
verifyConfig(new String[] { "BEGIN", "a", "END" });
158186
}
159187

188+
@Test
189+
public void whenContextPropertyIsSet_IfThenBranchIsEvaluated_NO_ELSE_DEFINED_NoJoran() throws JoranException {
190+
context.putProperty(ki1, val1);
191+
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse_NoJoran.xml");
192+
verifyConfig(new String[] { "BEGIN", "a", "END" });
193+
}
194+
// ----------------------------------------------------------------------------------------------------
160195
@Test
161196
public void whenNoPropertyIsDefined_IfThenBranchIsNotEvaluated_NO_ELSE_DEFINED() throws JoranException {
162197
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml");
163198
verifyConfig(new String[] { "BEGIN", "END" });
164199
Assertions.assertTrue(checker.isErrorFree(0));
165200
}
166201

202+
@Test
203+
public void whenNoPropertyIsDefined_IfThenBranchIsNotEvaluated_NO_ELSE_DEFINED_NoJoran() throws JoranException {
204+
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse_NoJoran.xml");
205+
verifyConfig(new String[] { "BEGIN", "END" });
206+
Assertions.assertTrue(checker.isErrorFree(0));
207+
}
208+
// ----------------------------------------------------------------------------------------------------
167209
@Test
168210
public void nestedIf() throws JoranException {
169211
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "nestedIf.xml");
170212
//StatusPrinter.print(context);
171213
verifyConfig(new String[] { "BEGIN", "a", "c", "END" });
172214
Assertions.assertTrue(checker.isErrorFree(0));
173215
}
216+
@Test
217+
public void nestedIf_NoJoran() throws JoranException {
218+
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "nestedIf_NoJoran.xml");
219+
//StatusPrinter.print(context);
220+
verifyConfig(new String[] { "BEGIN", "a", "c", "END" });
221+
Assertions.assertTrue(checker.isErrorFree(0));
222+
}
223+
224+
// ----------------------------------------------------------------------------------------------------
174225

175226
@Test
176227
public void useNonExistenceOfSystemPropertyToDefineAContextProperty() throws JoranException {
177228
Assertions.assertNull(System.getProperty(sysKey));
178229
Assertions.assertNull(context.getProperty(dynaKey));
179230
simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem.xml");
180-
System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
231+
//System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
181232
Assertions.assertNotNull(context.getProperty(dynaKey));
182233
}
183234

logback-core-blackbox/src/test/java/module-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
requires org.junit.jupiter.engine;
77

88
requires janino;
9+
requires commons.compiler;
10+
911
requires org.fusesource.jansi;
1012

1113
requires org.tukaani.xz;
1214

15+
exports ch.qos.logback.core.blackbox.boolex;
1316
exports ch.qos.logback.core.blackbox.joran.conditional;
1417
exports ch.qos.logback.core.blackbox.joran;
1518
exports ch.qos.logback.core.blackbox.appender;

0 commit comments

Comments
 (0)