Skip to content

Commit 75bee86

Browse files
committed
refactorings in support of logback-tyler
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 8749edc commit 75bee86

File tree

4 files changed

+83
-18
lines changed

4 files changed

+83
-18
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/tyler/TylerConfiguratorBase.java

+32-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
import ch.qos.logback.classic.Level;
1818
import ch.qos.logback.classic.Logger;
1919
import ch.qos.logback.classic.LoggerContext;
20+
import ch.qos.logback.classic.joran.JoranConfigurator;
21+
import ch.qos.logback.classic.model.ConfigurationModel;
2022
import ch.qos.logback.classic.util.LevelUtil;
2123
import ch.qos.logback.core.Context;
24+
import ch.qos.logback.core.joran.GenericXMLConfigurator;
25+
import ch.qos.logback.core.model.Model;
2226
import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
2327
import ch.qos.logback.core.model.util.VariableSubstitutionsHelper;
2428
import ch.qos.logback.core.spi.ContextAwareBase;
@@ -29,6 +33,7 @@
2933
import ch.qos.logback.core.util.StringUtil;
3034

3135
import java.util.Map;
36+
import java.util.function.Supplier;
3237

3338
public class TylerConfiguratorBase extends ContextAwareBase implements ContextAwarePropertyContainer {
3439

@@ -64,13 +69,13 @@ public void setContext(Context context) {
6469
}
6570

6671
protected void setContextName(String name) {
67-
if(StringUtil.isNullOrEmpty(name)) {
72+
if (StringUtil.isNullOrEmpty(name)) {
6873
addError("Cannot set context name to null or empty string");
6974
return;
7075
}
7176
try {
7277
String substName = subst(name);
73-
addInfo("Setting context name to ["+substName+"]");
78+
addInfo("Setting context name to [" + substName + "]");
7479
context.setName(substName);
7580
} catch (IllegalStateException e) {
7681
addError("Failed to rename context as [" + name + "]");
@@ -153,4 +158,29 @@ public String property(String k) {
153158
else
154159
return "";
155160
}
161+
162+
private JoranConfigurator makeAnotherInstance() {
163+
JoranConfigurator jc = new JoranConfigurator();
164+
jc.setContext(context);
165+
return jc;
166+
}
167+
168+
/**
169+
* Return a supplier which supplies an instance of {@link JoranConfigurator} set to
170+
* the same context the context of 'this'.
171+
* @since 1.5.11
172+
*/
173+
@Override
174+
public Supplier<? extends GenericXMLConfigurator> getConfiguratorSupplier() {
175+
Supplier<? extends GenericXMLConfigurator> supplier = () -> this.makeAnotherInstance();
176+
return supplier;
177+
}
178+
179+
protected void processModelFromIncludedFile(Model modelFromIncludedFile) {
180+
Supplier<? extends GenericXMLConfigurator > configuratorSupplier = this.getConfiguratorSupplier();
181+
GenericXMLConfigurator genericXMLConfigurator = configuratorSupplier.get();
182+
ConfigurationModel configururationModel = new ConfigurationModel();
183+
configururationModel.addSubModel(modelFromIncludedFile);
184+
genericXMLConfigurator.processModel(configururationModel);
185+
}
156186
}

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

+33-16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
2323
import ch.qos.logback.core.model.IncludeModel;
2424
import ch.qos.logback.core.model.Model;
25+
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
2526
import ch.qos.logback.core.spi.ErrorCodes;
2627
import ch.qos.logback.core.util.Loader;
2728
import ch.qos.logback.core.util.OptionHelper;
@@ -60,18 +61,35 @@ protected Class<IncludeModel> getSupportedModelClass() {
6061
@Override
6162
public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
6263
IncludeModel includeModel = (IncludeModel) model;
64+
Model modelFromIncludedFile = buildModelFromIncludedFile(mic, includeModel);
65+
if (modelFromIncludedFile == null) {
66+
warnIfRequired("Failed to build include model from included file");
67+
return;
68+
}
69+
processModelFromIncludedFile(includeModel, modelFromIncludedFile);
70+
}
71+
72+
/**
73+
* This method is called by logback-tyler at TylerConfigurator run-time.
74+
*
75+
* @param capc
76+
* @param includeModel
77+
* @throws ModelHandlerException
78+
* @since 1.5.11
79+
*/
80+
public Model buildModelFromIncludedFile(ContextAwarePropertyContainer capc, IncludeModel includeModel) throws ModelHandlerException {
6381

6482
this.optional = OptionHelper.toBoolean(includeModel.getOptional(), false);
6583

6684
if (!checkAttributes(includeModel)) {
6785
inError = true;
68-
return;
86+
return null;
6987
}
7088

71-
InputStream in = getInputStream(mic, includeModel);
72-
if(in == null) {
89+
InputStream in = getInputStream(capc, includeModel);
90+
if (in == null) {
7391
inError = true;
74-
return;
92+
return null;
7593
}
7694

7795
SaxEventRecorder recorder = null;
@@ -82,41 +100,40 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand
82100
List<SaxEvent> saxEvents = recorder.getSaxEventList();
83101
if (saxEvents.isEmpty()) {
84102
addWarn("Empty sax event list");
85-
return;
103+
return null;
86104
}
87105

88-
Supplier<? extends GenericXMLConfigurator> jcSupplier = mic.getConfiguratorSupplier();
106+
Supplier<? extends GenericXMLConfigurator> jcSupplier = capc.getConfiguratorSupplier();
89107
if (jcSupplier == null) {
90108
addError("null configurator supplier. Abandoning inclusion of [" + attributeInUse + "]");
91109
inError = true;
92-
return;
110+
return null;
93111
}
94112

95113
GenericXMLConfigurator genericXMLConfigurator = jcSupplier.get();
96114
genericXMLConfigurator.getRuleStore().addPathPathMapping(INCLUDED_TAG, CONFIGURATION_TAG);
97115

98116
Model modelFromIncludedFile = genericXMLConfigurator.buildModelFromSaxEventList(recorder.getSaxEventList());
99-
if (modelFromIncludedFile == null) {
100-
addError(ErrorCodes.EMPTY_MODEL_STACK);
101-
return;
102-
}
103-
104-
includeModel.getSubModels().addAll(modelFromIncludedFile.getSubModels());
105-
117+
return modelFromIncludedFile;
106118
} catch (JoranException e) {
107119
inError = true;
108120
addError("Error processing XML data in [" + attributeInUse + "]", e);
121+
return null;
109122
}
110123
}
111124

125+
private void processModelFromIncludedFile(IncludeModel includeModel, Model modelFromIncludedFile) {
126+
includeModel.getSubModels().addAll(modelFromIncludedFile.getSubModels());
127+
}
128+
112129
public SaxEventRecorder populateSaxEventRecorder(final InputStream inputStream) throws JoranException {
113130
SaxEventRecorder recorder = new SaxEventRecorder(context);
114131
recorder.recordEvents(inputStream);
115132
return recorder;
116133
}
117134

118-
private InputStream getInputStream(ModelInterpretationContext mic, IncludeModel includeModel) {
119-
URL inputURL = getInputURL(mic, includeModel);
135+
private InputStream getInputStream(ContextAwarePropertyContainer capc, IncludeModel includeModel) {
136+
URL inputURL = getInputURL(capc, includeModel);
120137
if (inputURL == null)
121138
return null;
122139
ConfigurationWatchListUtil.addToWatchList(context, inputURL);

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

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ public String getImport(String stem) {
289289
*
290290
* @return a supplier of {@link GenericXMLConfigurator} instance, may be null
291291
*/
292+
@Override
292293
public Supplier<? extends GenericXMLConfigurator> getConfiguratorSupplier() {
293294
return this.configuratorSupplier;
294295
}

logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwarePropertyContainer.java

+17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

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

17+
import ch.qos.logback.core.joran.GenericXMLConfigurator;
18+
19+
import java.util.function.Supplier;
20+
1721
/**
1822
* An interface extending both {@link PropertyContainer} and {@link ContextAware}
1923
*
@@ -30,4 +34,17 @@ public interface ContextAwarePropertyContainer extends PropertyContainer, Contex
3034
*/
3135
String subst(String input);
3236

37+
38+
/**
39+
* Returns a supplier of {@link GenericXMLConfigurator} instance. The returned value may be null.
40+
*
41+
* <p>This method could/should have been part of a new interface. It is added here for reasons
42+
* of commodity and not coherence.</p>
43+
*
44+
* @return a supplier of {@link GenericXMLConfigurator} instance, may be null
45+
* @since 1.5.11
46+
*/
47+
default public Supplier<? extends GenericXMLConfigurator> getConfiguratorSupplier() {
48+
return null;
49+
}
3350
}

0 commit comments

Comments
 (0)