How can I execute groovy scripts in an isolated classloader?

I am trying to run groovy scripts in an isolated class loader so that they do not run in the context of the dependencies of the calling class.

Path log4j = Paths.get("..../lib/log4j-1.2.17.jar"); Path groovy = Paths.get("..../lib/groovy-all-2.1.3.jar"); RootLoader rootLoader = new RootLoader(new URL[] { log4j.toUri().toURL(), groovy.toUri().toURL() }, null); GroovyScriptEngine engine = new GroovyScriptEngine(".../src/main/resources", rootLoader); engine.run("Standalone.groovy", ""); 
Standalone.groovy :
 import org.apache.log4j.BasicConfigurator import org.apache.log4j.Logger Logger logger = Logger.getLogger(getClass()) BasicConfigurator.configure() logger.info("hello world") 
pom.xml excerpt :
  <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.1.3</version> </dependency> 

Any variation of the above that I have tried always leads to

 Exception in thread "main" groovy.lang.GroovyRuntimeException: Failed to create Script instance for class: class Standalone. Reason: java.lang.ClassCastException: Standalone cannot be cast to groovy.lang.GroovyObject at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:443) at groovy.util.GroovyScriptEngine.createScript(GroovyScriptEngine.java:564) at groovy.util.GroovyScriptEngine.run(GroovyScriptEngine.java:551) at groovy.util.GroovyScriptEngine.run(GroovyScriptEngine.java:537) 

I tracked this to groovy.util.GroovyScriptEngine#loadScriptByName , where the script is parsed to Class<T> , where T is the name of the script itself.

My theory is that this is caused by binary incompatibility between groovy runtime running in the calling class and runtime groovy running in the standalone class loader, due to the way groovy creates synthetic classes from scripts via reflection.

Any ideas on how to do this?

+5
source share
1 answer

try to create GroovyScriptEngine not directly, but via rootLoader.loadClass() and call engine.run through reflection.

+2
source

Source: https://habr.com/ru/post/1268364/


All Articles