Hi everyone,
I need to temporarily change the value of a configuration parameter at runtime. While discussing this on the #xwiki chat @tmortagne suggested introducing a configuration source that reads from the Execution Context. This practically means:
- Adding
ConfigurationSource#removeProperty(String key)
(see below for the usage) - Implement
ExecutionContextConfigurationSource
with read & write support (the way the configuration is stored on the Execution Context is an implementation detail, the users of this class should use theConfigurationSource
interface to read and set configuration values) - modify
DefaultConfigurationSource
to check first theExecutionContextConfigurationSource
, i.e. look first in the Execution Context before checking the current document
This is how the new configuration source would be used:
@Inject
@Named("executionContext")
private ConfigurationSource ecConfigSource;
...
// Backup before overwritting.
boolean wasAlreadySet = this.ecConfigSource.containsKey(key);
Object previousValue = this.ecConfigSource.getProperty(key);
try {
this.ecConfigSource.setProperty(key, tempValue);
// Do stuff that relies on the modified configuration.
} finally {
// Restore or cleanup.
if (wasAlreadySet) {
this.ecConfigSource.setProperty(key, previousValue);
} else {
this.ecConfigSource.removeProperty(key);
}
}
WDYT?
Thanks,
Marius
6 posts - 5 participants