0

How can i make effective use of GroovyClassLoader GroovyScriptEngine both in a programe

anish
  • 6,884
  • 13
  • 74
  • 140
  • 2
    What problem are you trying to solve? What have you tried? What hasn't worked? This isn't really a question as it stands. – OverZealous Nov 12 '11 at 23:09
  • 1
    I have a server, written in java The server give you flexiblity to to upload the groovy script and uploaded groovy script will placed in db, during upload the given groovy script has to compiled.in a given time only one groovy script can be uploaded. The groovy script can contain dependency with other groovy scripts, during this scenario,so i want make use of GroovyClassloader and groovyScriptEngine both – anish Nov 13 '11 at 04:11
  • Actual problem statement http://stackoverflow.com/questions/8106082/compiling-a-groovy-scripts-that-come-from-a-database , i want to extend the GroovyClassloader and override the loadclass method how can i aceive this – anish Nov 13 '11 at 04:20

1 Answers1

1
 class testScriptEngine 
    {
    private final Map<String, CompiledScript> compiledScripts = new HashMap<String, CompiledScript>();
    GroovyClassLoader gcl = new GroovyClassLoader();
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("groovy");
        GroovyCompiledScript compilescript(ScriptEngine engine, String scriptName) {
                GroovyScriptEngineImpl groovyEngineImpl = (GroovyScriptEngineImpl) engine;
                gcl = groovyEngineImpl.getClassLoader();
                Class<?> scriptClass = null;
                try {
                    scriptClass = gcl.parseClass(new File(scriptName));
                    GroovyCompiledScript compiledScript = new GroovyCompiledScript(
                            groovyEngineImpl, scriptClass);
                    return compiledScript;
                } catch (MultipleCompilationErrorsException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (CompilationFailedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }
    public void run() {
            // System.out.println(compiledScripts);
            System.out.println(engine instanceof GroovyScriptEngineImpl);
            Bindings bindings = engine.createBindings();
            bindings.putAll(engine.getBindings(ScriptContext.ENGINE_SCOPE));
            String fileName = "script1.groovy";
            CompiledScript compiledScript = compiledScripts.get(fileName);
            try {
                compiledScript.eval(bindings);
            } catch (ScriptException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
private void compileScript() {
        String fileName = "Script1.groovy";
        compiledScripts.put(fileName, compilescript(engine, fileName));
        fileName = "Script2.groovy";
        compiledScripts.put(fileName, compilescript(engine, fileName));
        fileName = "Script3.groovy";
        compiledScripts.put(fileName, compilescript(engine, fileName));

    }
    }
anish
  • 6,884
  • 13
  • 74
  • 140