Skip to content

Commit a95bbc2

Browse files
committed
minor refactorings to allow ConfigurationModelHandlerFull and PropertiesConfiguratorModelHandler funtionality to be invoked/called by logback-tyler
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent cbbd820 commit a95bbc2

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/model/processor/ConfigurationModelHandlerFull.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static public ModelHandlerBase makeInstance2(Context context, ModelInterpretatio
4848
}
4949

5050
@Override
51-
protected void processScanAttrib( ModelInterpretationContext mic, ConfigurationModel configurationModel) {
51+
protected void processScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {
5252

5353
}
5454

@@ -60,8 +60,22 @@ public void postHandle(ModelInterpretationContext mic, Model model) throws Model
6060

6161
protected void postProcessScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {
6262
String scanStr = mic.subst(configurationModel.getScanStr());
63-
if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanStr) && !"false".equalsIgnoreCase(scanStr)) {
63+
String scanPeriodStr = mic.subst(configurationModel.getScanPeriodStr());
64+
detachedPostProcess(scanStr, scanPeriodStr);
65+
}
6466

67+
/**
68+
* This method is called from this class but also from logback-tyler.
69+
*
70+
* This method assumes that the variables scanStr and scanPeriodStr have undergone variable substitution
71+
* as applicable to their current environment
72+
*
73+
* @param scanStr
74+
* @param scanPeriodStr
75+
* @since 1.5.0
76+
*/
77+
public void detachedPostProcess(String scanStr, String scanPeriodStr) {
78+
if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanStr) && !"false".equalsIgnoreCase(scanStr)) {
6579
ScheduledExecutorService scheduledExecutorService = context.getScheduledExecutorService();
6680
boolean watchPredicateFulfilled = ConfigurationWatchListUtil.watchPredicateFulfilled(context);
6781
if (!watchPredicateFulfilled) {
@@ -76,7 +90,6 @@ protected void postProcessScanAttrib(ModelInterpretationContext mic, Configurati
7690

7791
context.fireConfigurationEvent(ConfigurationEvent.newConfigurationChangeDetectorRegisteredEvent(rocTask));
7892

79-
String scanPeriodStr = mic.subst(configurationModel.getScanPeriodStr());
8093
Duration duration = getDurationOfScanPeriodAttribute(scanPeriodStr, SCAN_PERIOD_DEFAULT);
8194

8295
addInfo("Will scan for changes in [" + ConfigurationWatchListUtil.getConfigurationWatchList(context) + "] ");
@@ -91,6 +104,7 @@ protected void postProcessScanAttrib(ModelInterpretationContext mic, Configurati
91104
rocTask.setScheduredFuture(scheduledFuture);
92105
context.addScheduledFuture(scheduledFuture);
93106
}
107+
94108
}
95109

96110
private Duration getDurationOfScanPeriodAttribute(String scanPeriodAttrib, Duration defaultDuration) {

logback-classic/src/main/java/ch/qos/logback/classic/model/processor/PropertiesConfiguratorModelHandler.java

+23-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import ch.qos.logback.core.model.processor.ModelHandlerException;
2525
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
2626
import ch.qos.logback.core.model.processor.ResourceHandlerBase;
27+
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
2728
import ch.qos.logback.core.util.OptionHelper;
2829

2930
import java.io.InputStream;
@@ -44,6 +45,21 @@ static public PropertiesConfiguratorModelHandler makeInstance(Context context, M
4445

4546
@Override
4647
public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
48+
detachedHandle(mic, model);
49+
}
50+
51+
/**
52+
*
53+
* Used by {@link #handle(ModelInterpretationContext, Model)} as well as logback-tyler. Note the widening of the
54+
* base from {@link ModelInterpretationContext} to {@link ContextAwarePropertyContainer}.
55+
*
56+
* @param capc
57+
* @param model
58+
* @throws ModelHandlerException
59+
* @since 1.5.10
60+
*/
61+
public void detachedHandle(ContextAwarePropertyContainer capc, Model model) throws ModelHandlerException {
62+
4763
PropertiesConfiguratorModel propertyConfiguratorModel = (PropertiesConfiguratorModel) model;
4864

4965
this.optional = OptionHelper.toBoolean(propertyConfiguratorModel.getOptional(), false);
@@ -53,27 +69,27 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand
5369
return;
5470
}
5571

56-
InputStream in = getInputStream(mic, propertyConfiguratorModel);
57-
if(in == null) {
72+
InputStream in = getInputStream(capc, propertyConfiguratorModel);
73+
if (in == null) {
5874
inError = true;
5975
return;
6076
}
6177

62-
addInfo("Reading configuration from ["+getAttribureInUse()+"]");
78+
addInfo("Reading configuration from [" + getAttribureInUse() + "]");
6379

6480
PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator();
65-
propertiesConfigurator.setContext(mic.getContext());
81+
propertiesConfigurator.setContext(capc.getContext());
6682
try {
6783
propertiesConfigurator.doConfigure(in);
6884
} catch (JoranException e) {
69-
addError("Could not configure from "+getAttribureInUse());
85+
addError("Could not configure from " + getAttribureInUse());
7086
throw new ModelHandlerException(e);
7187
}
7288

7389
}
7490

75-
protected InputStream getInputStream(ModelInterpretationContext mic, ResourceModel resourceModel) {
76-
URL inputURL = getInputURL(mic, resourceModel);
91+
protected InputStream getInputStream(ContextAwarePropertyContainer capc, ResourceModel resourceModel) {
92+
URL inputURL = getInputURL(capc, resourceModel);
7793
if (inputURL == null)
7894
return null;
7995

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import ch.qos.logback.core.Context;
1818
import ch.qos.logback.core.model.ResourceModel;
19+
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
1920
import ch.qos.logback.core.util.Loader;
2021
import ch.qos.logback.core.util.OptionHelper;
2122

@@ -78,23 +79,23 @@ protected String getAttribureInUse() {
7879
return this.attributeInUse;
7980
}
8081

81-
protected URL getInputURL(ModelInterpretationContext mic, ResourceModel resourceModel) {
82+
protected URL getInputURL(ContextAwarePropertyContainer contextAwarePropertyContainer, ResourceModel resourceModel) {
8283
String fileAttribute = resourceModel.getFile();
8384
String urlAttribute = resourceModel.getUrl();
8485
String resourceAttribute = resourceModel.getResource();
8586

8687
if (!OptionHelper.isNullOrEmptyOrAllSpaces(fileAttribute)) {
87-
this.attributeInUse = mic.subst(fileAttribute);
88+
this.attributeInUse = contextAwarePropertyContainer.subst(fileAttribute);
8889
return filePathAsURL(attributeInUse);
8990
}
9091

9192
if (!OptionHelper.isNullOrEmptyOrAllSpaces(urlAttribute)) {
92-
this.attributeInUse = mic.subst(urlAttribute);
93+
this.attributeInUse = contextAwarePropertyContainer.subst(urlAttribute);
9394
return attributeToURL(attributeInUse);
9495
}
9596

9697
if (!OptionHelper.isNullOrEmptyOrAllSpaces(resourceAttribute)) {
97-
this.attributeInUse = mic.subst(resourceAttribute);
98+
this.attributeInUse = contextAwarePropertyContainer.subst(resourceAttribute);
9899
return resourceAsURL(attributeInUse);
99100
}
100101
// given preceding checkAttributes() check we cannot reach this line

0 commit comments

Comments
 (0)