Skip to content

Commit af92be4

Browse files
committed
migrate IncludeAction code to IncludeModelHandler, LOGBACK-1746
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent b95d2a0 commit af92be4

File tree

16 files changed

+433
-227
lines changed

16 files changed

+433
-227
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/joran/JoranConfigurator.java

+15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import ch.qos.logback.classic.model.processor.ConfigurationModelHandlerFull;
2727
import ch.qos.logback.classic.model.processor.LogbackClassicDefaultNestedComponentRules;
2828
import ch.qos.logback.classic.spi.ILoggingEvent;
29+
import ch.qos.logback.core.joran.GenericXMLConfigurator;
2930
import ch.qos.logback.core.joran.JoranConfiguratorBase;
3031
import ch.qos.logback.core.joran.action.AppenderRefAction;
3132
import ch.qos.logback.core.joran.action.IncludeAction;
@@ -34,6 +35,7 @@
3435
import ch.qos.logback.core.joran.spi.RuleStore;
3536
import ch.qos.logback.core.model.Model;
3637
import ch.qos.logback.core.model.processor.DefaultProcessor;
38+
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
3739

3840
/**
3941
* JoranConfigurator class adds rules specific to logback-classic.
@@ -42,6 +44,8 @@
4244
*/
4345
public class JoranConfigurator extends JoranConfiguratorBase<ILoggingEvent> {
4446

47+
48+
4549
@Override
4650
public void addElementSelectorAndActionAssociations(RuleStore rs) {
4751
// add parent rules
@@ -81,6 +85,17 @@ protected void addDefaultNestedComponentRegistryRules(DefaultNestedComponentRegi
8185
LogbackClassicDefaultNestedComponentRules.addDefaultNestedComponentRegistryRules(registry);
8286
}
8387

88+
private JoranConfigurator makeAnotherInstance() {
89+
JoranConfigurator jc = new JoranConfigurator();
90+
jc.setContext(context);
91+
return jc;
92+
}
93+
94+
public void buildModelInterpretationContext() {
95+
super.buildModelInterpretationContext();
96+
this.modelInterpretationContext.setConfiguratorSupplier( () -> this.makeAnotherInstance() );
97+
}
98+
8499
@Override
85100
protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
86101
ModelClassToModelHandlerLinker m = new ModelClassToModelHandlerLinker(context);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<included>
2+
<root level="ERROR"/>
3+
</included>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
<!--
4+
~ Logback: the reliable, generic, fast and flexible logging framework.
5+
~ Copyright (C) 1999-2024, 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+
<configuration>
18+
19+
<variable name="JO_PREFIX" value="src/test/input/joran" />
20+
<include file="${JO_PREFIX}/include/included0.xml"/>
21+
22+
</configuration>

logback-classic/src/test/java/ch/qos/logback/classic/LoggerContextDeadlockTest.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,28 @@
2727
public class LoggerContextDeadlockTest {
2828

2929
LoggerContext loggerContext = new LoggerContext();
30-
JoranConfigurator jc = new JoranConfigurator();
30+
3131
GetLoggerThread getLoggerThread = new GetLoggerThread(loggerContext);
3232

3333
@BeforeEach
3434
public void setUp() throws Exception {
35-
jc.setContext(loggerContext);
35+
3636
}
3737

3838
@AfterEach
3939
public void tearDown() throws Exception {
4040
}
4141

42+
// LBCLASSIC_81
43+
// LOGBACK-394
4244
@Test
4345
@Timeout(value = 20, unit= TimeUnit.SECONDS)
44-
public void testLBCLASSIC_81() throws JoranException {
46+
public void test_LOGBACK_394() throws JoranException {
4547

4648
getLoggerThread.start();
4749
for (int i = 0; i < 500; i++) {
50+
JoranConfigurator jc = new JoranConfigurator();
51+
jc.setContext(loggerContext);
4852
ByteArrayInputStream baos = new ByteArrayInputStream(
4953
"<configuration><root level=\"DEBUG\"/></configuration>".getBytes());
5054
jc.doConfigure(baos);

logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import ch.qos.logback.core.testUtil.StringListAppender;
4747
import ch.qos.logback.core.util.CachingDateFormatter;
4848
import ch.qos.logback.core.util.StatusPrinter;
49+
import ch.qos.logback.core.util.StatusPrinter2;
4950
import org.junit.jupiter.api.Disabled;
5051
import org.junit.jupiter.api.Test;
5152
import org.slf4j.MDC;
@@ -73,6 +74,7 @@ public class JoranConfiguratorTest {
7374
LoggerContext loggerContext = new LoggerContext();
7475
Logger logger = loggerContext.getLogger(this.getClass().getName());
7576
Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
77+
StatusPrinter2 statusPrinter2 = new StatusPrinter2();
7678
StatusChecker checker = new StatusChecker(loggerContext);
7779
int diff = RandomUtil.getPositiveInt();
7880

@@ -669,7 +671,16 @@ public void kvp() throws JoranException {
669671
assertTrue(slAppender.strList.get(2).contains("null=\"" + kvpNullKey.value + "\" " + msg));
670672
assertTrue(slAppender.strList.get(3).contains(kvpNullValue.key + "=\"null\" " + msg));
671673
}
672-
674+
675+
676+
@Test
677+
public void inclusionWithVariables() throws JoranException {
678+
configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "include/topLevel0.xml");
679+
680+
Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
681+
statusPrinter2.print(loggerContext);
682+
assertEquals(Level.ERROR, root.getLevel());
683+
}
673684

674685
// https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=46697
675686
@Test

logback-core/src/main/java/ch/qos/logback/core/joran/GenericXMLConfigurator.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public abstract class GenericXMLConfigurator extends ContextAwareBase {
5151
public ModelInterpretationContext getModelInterpretationContext() {
5252
return this.modelInterpretationContext;
5353
}
54+
private RuleStore ruleStore;
5455

5556
public final void doConfigure(URL url) throws JoranException {
5657
InputStream in = null;
@@ -135,14 +136,21 @@ protected ElementPath initialElementPath() {
135136
}
136137

137138
protected void buildSaxEventInterpreter(List<SaxEvent> saxEvents) {
138-
RuleStore rs = new SimpleRuleStore(context);
139+
RuleStore rs = getRuleStore();
139140
addElementSelectorAndActionAssociations(rs);
140141
this.saxEventInterpreter = new SaxEventInterpreter(context, rs, initialElementPath(), saxEvents);
141142
SaxEventInterpretationContext interpretationContext = saxEventInterpreter.getSaxEventInterpretationContext();
142143
interpretationContext.setContext(context);
143144
setImplicitRuleSupplier(saxEventInterpreter);
144145
}
145146

147+
public RuleStore getRuleStore() {
148+
if(this.ruleStore == null) {
149+
this.ruleStore = new SimpleRuleStore(context);
150+
}
151+
return this.ruleStore;
152+
}
153+
146154
protected void buildModelInterpretationContext() {
147155
this.modelInterpretationContext = new ModelInterpretationContext(context);
148156
addDefaultNestedComponentRegistryRules(modelInterpretationContext.getDefaultNestedComponentRegistry());

logback-core/src/main/java/ch/qos/logback/core/joran/JoranConfiguratorBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
138138

139139
defaultProcessor.addHandler(EventEvaluatorModel.class, EventEvaluatorModelHandler::makeInstance);
140140
defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance);
141-
defaultProcessor.addHandler(IncludeModel.class, NOPModelHandler::makeInstance);
141+
defaultProcessor.addHandler(IncludeModel.class, IncludeModelHandler::makeInstance);
142142

143143

144144
defaultProcessor.addHandler(ParamModel.class, ParamModelHandler::makeInstance);

logback-core/src/main/java/ch/qos/logback/core/joran/JoranConstants.java

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
public abstract class JoranConstants {
2424
public static final String INCLUDED_TAG = "included";
25+
public static final String CONFIGURATION_TAG = "configuration";
26+
2527
public static final String INCLUDE_TAG = "include";
2628

2729
public static final String APPENDER_TAG = "appender";

logback-core/src/main/java/ch/qos/logback/core/joran/ModelClassToModelHandlerLinkerBase.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import ch.qos.logback.core.model.processor.EventEvaluatorModelHandler;
3737
import ch.qos.logback.core.model.processor.ImplicitModelHandler;
3838
import ch.qos.logback.core.model.processor.ImportModelHandler;
39+
import ch.qos.logback.core.model.processor.IncludeModelHandler;
3940
import ch.qos.logback.core.model.processor.NOPModelHandler;
4041
import ch.qos.logback.core.model.processor.PropertyModelHandler;
4142
import ch.qos.logback.core.model.processor.SequenceNumberGeneratorModelHandler;
@@ -74,7 +75,7 @@ public void link(DefaultProcessor defaultProcessor) {
7475

7576
defaultProcessor.addHandler(EventEvaluatorModel.class, EventEvaluatorModelHandler::makeInstance);
7677
defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance);
77-
defaultProcessor.addHandler(IncludeModel.class, NOPModelHandler::makeInstance);
78+
defaultProcessor.addHandler(IncludeModel.class, IncludeModelHandler::makeInstance);
7879

7980

8081
defaultProcessor.addHandler(ParamModel.class, ParamModelHandler::makeInstance);

0 commit comments

Comments
 (0)