How to create a GroovyShell object using the context path of a context application class

For the background, I'm experimenting with writing a DSL parser using this great example . Unfortunately, when I adapt this line for use in my application:

Script dslScript = new GroovyShell().parse(dsl.text)

I get class resolution errors at runtime since my DSL domain files have code that refers to other external classes. The contextual application has access to these classes, but I don’t know how to give them access to the new GroovyShell object or, as an alternative, somehow use the contextual application runtime to parse the file.

+5
source share
2 answers

Have you tried using the following constructor: public GroovyShell(ClassLoader parent)

Example: Script dslScript = new GroovyShell(this.class.classLoader).parse(dsl.text)

, ...

+6

, , , .

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();
}

, , DSL . Cloudify . http://cloudifysource.tumblr.com/post/23046765169/parsing-complex-dsls-using-groovy

+7

All Articles