First we create an abstract class like this:
package test
public abstract class IncludeScript extends Script {
def includeScript(scriptClass) {
GroovyClassLoader gcl = new GroovyClassLoader();
Class clazz = gcl.parseClass(new File(scriptClass));
def scriptInstance = clazz.newInstance()
scriptInstance.metaClass = this.metaClass
scriptInstance.binding = new ConfigBinding(this.getBinding().callable)
scriptInstance.&run.call()
}
}
Compile the IncludeScript.groovy with groovyc and add the class-file to your classpath.
Second we create something to test on like:
service{
endpoint ="http://mysite.se/Service"
port=4322
user="admin"
password="YmlsbDR5b3U="
}
//----------------------------------------------------------------------------------------
//Copy the code above to a file and name it to ServiceConfig.groovy
//----------------------------------------------------------------------------------------
import test.IncludeScript
class SecurityConfig extends IncludeScript {
def run() { // normal contents of a config file go in here
security {
includeScript( "c:\\tmp\\ServiceConfig.groovy" )
active = true
}
}
}
//----------------------------------------------------------------------------------------
//Copy the code above to a file and name it to what you want, I named my to prop.groovy
//----------------------------------------------------------------------------------------
Now we test our modularity...
def conf = new ConfigSlurper().parse( new File("c:\\tmp\\prop.groovy").toURL() )
println conf.security.service.password
println conf.security.active
Inga kommentarer:
Skicka en kommentar