I need to load a bunch of groovy scripts from DataBase , like this:
List<ScriptDAO> scriptList = dataBaseRepository.findAll();
GroovyClassLoader groovyClassLoader = new
GroovyClassLoader(this.getClass().getClassLoader());
for (ScriptDAO script : CollectionUtils.emptyIfNull(scriptList)) {
groovyClassLoader.parseClass(script.getContent);
}
When the parseClass method is invoked, this exeception raises:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script_30b09946e4afa374dc45b9929ce7d050.groovy: 3: unable to resolve class com.xxx.groovy.model.RuleDataB
I saw this question:GroovyClassLoader and imports. I know this error was caused by RuleDataB
which was not loaded.
Scrips are like this:
package com.xxx.groovy.business
import com.google.common.collect.Lists
import com.xxx.groovy.model.RuleDataB
import org.apache.commons.collections4.CollectionUtils
class RuleDataA {
/**
* getData
*/
static List<RuleDataB> getData(List<String> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Lists.newArrayList()
}
return RemoteXXXService.getRuleDataBByIds(ids)
}
}
What confuse me is how to use the addClasspath()
method. My Scripts are loaded from BD, should I trans String
into File
?
I also need to reload
this groovy class by updating ScriptDAO
.