ticktock
ticktock

Reputation: 637

Rescanning the configuration file in groovy

I need to implement a configuration file, which should be rescanned periodically or after an edit, what should I do?

I tried

config = new ConfigSlurper().parse(Config);

its not working when Config.groovy changes dynamically.

Example (from comment below)

MyConfig.groovy

class MyConfig {
  public static ConfigObject config
  public static void run() {
    config = new ConfigSlurper().parse(Config)
  }
  public static void printconfig() {
    println config.options.video.enable
  }
}

MyConfig.run()
for( int i = 0 ; i < 10 ; i++ ) {
  Thread.sleep(3000)
  MyConfig.printconfig()
}

Config.groovy

options { video { enable = false } } 

Upvotes: 1

Views: 533

Answers (1)

tim_yates
tim_yates

Reputation: 171124

You seem to parse the config file once, then never re-parse it...

What you could do is store the last modified date of the file, and call run() again from printConfig if it detects the file has been modified...

Also, I assume you have a copy/paste error... Shouldn't:

config = new ConfigSlurper().parse(Config)

be:

config = new ConfigSlurper().parse( MyConfig.class.getResource( 'Config.groovy' ) )

Or something?

Upvotes: 2

Related Questions