Quantcast
Channel: XWiki Forum - Latest topics
Viewing all articles
Browse latest Browse all 1369

Temporarily change configuration parameter at runtime

$
0
0

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 the ConfigurationSource interface to read and set configuration values)
  • modify DefaultConfigurationSource to check first the ExecutionContextConfigurationSource, 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

Read full topic


Viewing all articles
Browse latest Browse all 1369

Trending Articles