0

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.

euphon
  • 1
  • Where RuleDataB is located? – daggett Feb 14 '22 at 07:31
  • Here `List scriptList = dataBaseRepository.findAll();` – euphon Feb 14 '22 at 09:29
  • you could parse them in a right order. or you could store all those classes into file system and add the folder as a classpath using addClasspath(). or you could create custom classloader that creates classes on demand by requesting them by name from storage (database)... – daggett Feb 14 '22 at 09:55
  • this answer will help: https://stackoverflow.com/questions/32033980/how-compile-groovy-source-but-not-from-filesystem – euphon Feb 17 '22 at 03:40

0 Answers0