Skip to content

Commit

Permalink
minor refactorings to allow ConfigurationModelHandlerFull and Propert…
Browse files Browse the repository at this point in the history
…iesConfiguratorModelHandler funtionality to be invoked/called by logback-tyler

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Oct 11, 2024
1 parent cbbd820 commit a95bbc2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static public ModelHandlerBase makeInstance2(Context context, ModelInterpretatio
}

@Override
protected void processScanAttrib( ModelInterpretationContext mic, ConfigurationModel configurationModel) {
protected void processScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {

}

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

protected void postProcessScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {
String scanStr = mic.subst(configurationModel.getScanStr());
if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanStr) && !"false".equalsIgnoreCase(scanStr)) {
String scanPeriodStr = mic.subst(configurationModel.getScanPeriodStr());
detachedPostProcess(scanStr, scanPeriodStr);
}

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

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

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

addInfo("Will scan for changes in [" + ConfigurationWatchListUtil.getConfigurationWatchList(context) + "] ");
Expand All @@ -91,6 +104,7 @@ protected void postProcessScanAttrib(ModelInterpretationContext mic, Configurati
rocTask.setScheduredFuture(scheduledFuture);
context.addScheduledFuture(scheduledFuture);
}

}

private Duration getDurationOfScanPeriodAttribute(String scanPeriodAttrib, Duration defaultDuration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ch.qos.logback.core.model.processor.ModelHandlerException;
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
import ch.qos.logback.core.model.processor.ResourceHandlerBase;
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
import ch.qos.logback.core.util.OptionHelper;

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

@Override
public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
detachedHandle(mic, model);
}

/**
*
* Used by {@link #handle(ModelInterpretationContext, Model)} as well as logback-tyler. Note the widening of the
* base from {@link ModelInterpretationContext} to {@link ContextAwarePropertyContainer}.
*
* @param capc
* @param model
* @throws ModelHandlerException
* @since 1.5.10
*/
public void detachedHandle(ContextAwarePropertyContainer capc, Model model) throws ModelHandlerException {

PropertiesConfiguratorModel propertyConfiguratorModel = (PropertiesConfiguratorModel) model;

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

InputStream in = getInputStream(mic, propertyConfiguratorModel);
if(in == null) {
InputStream in = getInputStream(capc, propertyConfiguratorModel);
if (in == null) {
inError = true;
return;
}

addInfo("Reading configuration from ["+getAttribureInUse()+"]");
addInfo("Reading configuration from [" + getAttribureInUse() + "]");

PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator();
propertiesConfigurator.setContext(mic.getContext());
propertiesConfigurator.setContext(capc.getContext());
try {
propertiesConfigurator.doConfigure(in);
} catch (JoranException e) {
addError("Could not configure from "+getAttribureInUse());
addError("Could not configure from " + getAttribureInUse());
throw new ModelHandlerException(e);
}

}

protected InputStream getInputStream(ModelInterpretationContext mic, ResourceModel resourceModel) {
URL inputURL = getInputURL(mic, resourceModel);
protected InputStream getInputStream(ContextAwarePropertyContainer capc, ResourceModel resourceModel) {
URL inputURL = getInputURL(capc, resourceModel);
if (inputURL == null)
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import ch.qos.logback.core.Context;
import ch.qos.logback.core.model.ResourceModel;
import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
import ch.qos.logback.core.util.Loader;
import ch.qos.logback.core.util.OptionHelper;

Expand Down Expand Up @@ -78,23 +79,23 @@ protected String getAttribureInUse() {
return this.attributeInUse;
}

protected URL getInputURL(ModelInterpretationContext mic, ResourceModel resourceModel) {
protected URL getInputURL(ContextAwarePropertyContainer contextAwarePropertyContainer, ResourceModel resourceModel) {
String fileAttribute = resourceModel.getFile();
String urlAttribute = resourceModel.getUrl();
String resourceAttribute = resourceModel.getResource();

if (!OptionHelper.isNullOrEmptyOrAllSpaces(fileAttribute)) {
this.attributeInUse = mic.subst(fileAttribute);
this.attributeInUse = contextAwarePropertyContainer.subst(fileAttribute);
return filePathAsURL(attributeInUse);
}

if (!OptionHelper.isNullOrEmptyOrAllSpaces(urlAttribute)) {
this.attributeInUse = mic.subst(urlAttribute);
this.attributeInUse = contextAwarePropertyContainer.subst(urlAttribute);
return attributeToURL(attributeInUse);
}

if (!OptionHelper.isNullOrEmptyOrAllSpaces(resourceAttribute)) {
this.attributeInUse = mic.subst(resourceAttribute);
this.attributeInUse = contextAwarePropertyContainer.subst(resourceAttribute);
return resourceAsURL(attributeInUse);
}
// given preceding checkAttributes() check we cannot reach this line
Expand Down

0 comments on commit a95bbc2

Please sign in to comment.