-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ongoing work on model based configuration
- Loading branch information
Showing
9 changed files
with
239 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
logback-classic/src/main/java/ch/qos/logback/classic/model/ConfigurationModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ch.qos.logback.classic.model; | ||
|
||
import ch.qos.logback.core.model.Model; | ||
import ch.qos.logback.core.util.Duration; | ||
|
||
public class ConfigurationModel extends Model { | ||
|
||
static final String DEBUG_SYSTEM_PROPERTY_KEY = "logback.debug"; | ||
static final Duration SCAN_PERIOD_DEFAULT = Duration.buildByMinutes(1); | ||
|
||
|
||
String debugStr; | ||
String scanStr; | ||
String scanPeriodStr; | ||
String packagingDataStr; | ||
|
||
|
||
public String getDebugStr() { | ||
return debugStr; | ||
} | ||
public void setDebugStr(String debugStr) { | ||
this.debugStr = debugStr; | ||
} | ||
public String getScanStr() { | ||
return scanStr; | ||
} | ||
public void setScanStr(String scanStr) { | ||
this.scanStr = scanStr; | ||
} | ||
public String getScanPeriodStr() { | ||
return scanPeriodStr; | ||
} | ||
public void setScanPeriodStr(String scanPeriodStr) { | ||
this.scanPeriodStr = scanPeriodStr; | ||
} | ||
|
||
public String getPackagingDataStr() { | ||
return packagingDataStr; | ||
} | ||
public void setPackagingDataStr(String packagingDataStr) { | ||
this.packagingDataStr = packagingDataStr; | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
...assic/src/main/java/ch/qos/logback/classic/model/processor/ConfigurationModelHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package ch.qos.logback.classic.model.processor; | ||
|
||
import java.net.URL; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.joran.ReconfigureOnChangeTask; | ||
import ch.qos.logback.classic.model.ConfigurationModel; | ||
import ch.qos.logback.classic.util.EnvUtil; | ||
import ch.qos.logback.core.CoreConstants; | ||
import ch.qos.logback.core.joran.spi.InterpretationContext; | ||
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil; | ||
import ch.qos.logback.core.model.Model; | ||
import ch.qos.logback.core.model.processor.ModelHandlerBase; | ||
import ch.qos.logback.core.status.OnConsoleStatusListener; | ||
import ch.qos.logback.core.util.ContextUtil; | ||
import ch.qos.logback.core.util.Duration; | ||
import ch.qos.logback.core.util.OptionHelper; | ||
import ch.qos.logback.core.util.StatusListenerConfigHelper; | ||
|
||
public class ConfigurationModelHandler extends ModelHandlerBase { | ||
|
||
static final String DEBUG_SYSTEM_PROPERTY_KEY = "logback.debug"; | ||
static final Duration SCAN_PERIOD_DEFAULT = Duration.buildByMinutes(1); | ||
|
||
ConfigurationModelHandler(LoggerContext context) { | ||
super(context); | ||
} | ||
|
||
@Override | ||
public void handle(InterpretationContext interpretationContext, Model model) { | ||
|
||
if (!(model instanceof ConfigurationModel)) { | ||
addError("Can only handle models of type [" + ConfigurationModel.class + "]"); | ||
return; | ||
} | ||
ConfigurationModel configurationModel = (ConfigurationModel) model; | ||
|
||
// See LOGBACK-527 (the system property is looked up first. Thus, it overrides | ||
// the equivalent property in the config file. This reversal of scope priority is justified | ||
// by the use case: the admin trying to chase rogue config file | ||
String debugAttrib = getSystemProperty(DEBUG_SYSTEM_PROPERTY_KEY); | ||
if (debugAttrib == null) { | ||
debugAttrib = interpretationContext.subst(configurationModel.getDebugStr()); | ||
} | ||
|
||
if (!(OptionHelper.isEmpty(debugAttrib) || debugAttrib.equalsIgnoreCase("false") || debugAttrib.equalsIgnoreCase("null"))) { | ||
StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener()); | ||
} | ||
|
||
processScanAttrib(interpretationContext, configurationModel); | ||
|
||
LoggerContext lc = (LoggerContext) context; | ||
boolean packagingData = OptionHelper.toBoolean(interpretationContext.subst(configurationModel.getPackagingDataStr()), | ||
LoggerContext.DEFAULT_PACKAGING_DATA); | ||
lc.setPackagingDataEnabled(packagingData); | ||
|
||
if (EnvUtil.isGroovyAvailable()) { | ||
ContextUtil contextUtil = new ContextUtil(context); | ||
contextUtil.addGroovyPackages(lc.getFrameworkPackages()); | ||
} | ||
} | ||
|
||
String getSystemProperty(String name) { | ||
/* | ||
* LOGBACK-743: accessing a system property in the presence of a SecurityManager (e.g. applet sandbox) can | ||
* result in a SecurityException. | ||
*/ | ||
try { | ||
return System.getProperty(name); | ||
} catch (SecurityException ex) { | ||
return null; | ||
} | ||
} | ||
|
||
void processScanAttrib(InterpretationContext ic, ConfigurationModel configurationModel) { | ||
String scanStr = ic.subst(configurationModel.getScanStr()); | ||
if (!OptionHelper.isEmpty(scanStr) && !"false".equalsIgnoreCase(scanStr)) { | ||
|
||
ScheduledExecutorService scheduledExecutorService = context.getScheduledExecutorService(); | ||
URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(context); | ||
if (mainURL == null) { | ||
addWarn("Due to missing top level configuration file, reconfiguration on change (configuration file scanning) cannot be done."); | ||
return; | ||
} | ||
ReconfigureOnChangeTask rocTask = new ReconfigureOnChangeTask(); | ||
rocTask.setContext(context); | ||
|
||
context.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, rocTask); | ||
|
||
String scanPeriodStr = ic.subst(configurationModel.getScanPeriodStr()); | ||
Duration duration = getDurationOfScanPeriodAttribute(scanPeriodStr, SCAN_PERIOD_DEFAULT); | ||
|
||
addInfo("Will scan for changes in [" + mainURL + "] "); | ||
// Given that included files are encountered at a later phase, the complete list of files | ||
// to scan can only be determined when the configuration is loaded in full. | ||
// However, scan can be active if mainURL is set. Otherwise, when changes are detected | ||
// the top level config file cannot be accessed. | ||
addInfo("Setting ReconfigureOnChangeTask scanning period to " + duration); | ||
|
||
ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(rocTask, duration.getMilliseconds(), duration.getMilliseconds(), | ||
TimeUnit.MILLISECONDS); | ||
context.addScheduledFuture(scheduledFuture); | ||
} | ||
} | ||
|
||
private Duration getDurationOfScanPeriodAttribute(String scanPeriodAttrib, Duration defaultDuration) { | ||
Duration duration = null; | ||
|
||
if (!OptionHelper.isEmpty(scanPeriodAttrib)) { | ||
try { | ||
duration = Duration.valueOf(scanPeriodAttrib); | ||
} catch (IllegalStateException | IllegalArgumentException e) { | ||
addWarn("Failed to parse 'scanPeriod' attribute [" + scanPeriodAttrib + "]", e); | ||
// default duration will be set below | ||
} | ||
} | ||
|
||
if (duration == null) { | ||
addInfo("No 'scanPeriod' specified. Defaulting to " + defaultDuration.toString()); | ||
duration = defaultDuration; | ||
} | ||
return duration; | ||
} | ||
|
||
} |
Oops, something went wrong.