Here is a code snippet that shows how to inject a context object, configuration properties and the classpath.
Service parse(
String dslFile, List<String> classpath,
Map<String, Object> properties, ServiceContext context) {
// add POJO base class, and classpath
CompilerConfiguration cc = new CompilerConfiguration();
cc.setScriptBaseClass( BaseDslScript.class.getName() );
cc.setClasspathList(classpath);
// inject default imports
ic = new ImportCustomizer();
ic.addImports( ServiceUtils.class.getName() );
cc.addCompilationCustomizers(ic);
// inject context and properties
Binding binding = new Binding();
binding.setVariable("context", context);
for (prop: properties.entrySet()) {
binding.setVariable( prop.getKey(), prop.getValue());
}
// parse the recipe text file
ClassLoader classloader = this.class.getClassLoader();
GroovyShell gs = new GroovyShell(classloader, binding, cc);
FileReader reader = new FileReader(dslFile);
try {
return (Service) gs.evaluate(reader);
} finally {
reader.close();
}
Notice that this code also injects the base class in order to gain fine grained control on property parsing and support for inheritance between different DSL files.
For more information and working source code from the Cloudify project see
http://cloudifysource.tumblr.com/post/23046765169/parsing-complex-dsls-using-groovy