1

Groovy scripts are placed in a Database entry (data type BLOB). I can read the bytes and convert to a String object. How can I use GroovyClassLoader or GroovyScriptEngine to compile and execute the script? I need to track dependencies between scripts so that if any dependent script is modified, the whole tree will be recompiled and reloaded.

Antoine
  • 5,158
  • 1
  • 24
  • 37
anish
  • 6,884
  • 13
  • 74
  • 140

2 Answers2

1

Say you take the text to string scriptAsString

Eval.me(scriptAsString)

Perhaps you should store the script(s) as bytecode so you can use them with classLoader and make sure dependent scripts are up-to-date.

Ryan Berger
  • 9,644
  • 6
  • 44
  • 56
hgoz
  • 641
  • 6
  • 18
0

extends GroovyClassLoader overeide loadClass(), and add your implemetation

@Override
    public Class loadClass(String name, boolean lookupScriptFiles,
            boolean preferClassOverScript) throws ClassNotFoundException,
            CompilationFailedException {
        // TODO Auto-generated method stub
        try
        {
        Class<?> loadedClass = super.loadClass(name, lookupScriptFiles, preferClassOverScript);
        if (loadedClass !=null) {
            return loadedClass;
        }
        }
        catch (ClassNotFoundException e)
        {

        }

         int indx = name.lastIndexOf('.');
            String substr = name;
            if (indx != -1)
            {
                substr = name.substring(indx + 1);
            }
            String groovyFileName =  substr + ".groovy";
            String path = "C:\\" + groovyFileName;

            try
            {
                return parseClass(new File(path).toString(), groovyFileName);
            }
            catch (CompilationFailedException exception)
            {
                throw exception;
            }
    }
anish
  • 6,884
  • 13
  • 74
  • 140
  • Compiling "main" source from database etc is easy. But problem is when this script imports other. I intensively search solution to compile it "in fly", without success. Class GroovyResourceLoader knows what import is required (I check it, good) but I can't resolve next problems. – Jacek Cz Aug 19 '15 at 08:37
  • 1
    I receive very good answer (plus my own tests) in http://stackoverflow.com/questions/32033980/how-compile-groovy-source-but-not-from-filesystem – Jacek Cz Aug 20 '15 at 11:19