【问题标题】:Using GroovyClassLoader GroovyScriptEngine both同时使用 GroovyClassLoader GroovyScriptEngine
【发布时间】:2011-11-12 16:47:23
【问题描述】:

如何在程序中有效地使用 GroovyClassLoader GroovyScriptEngine

【问题讨论】:

  • 您要解决什么问题?你试过什么?什么没有奏效?就目前而言,这并不是一个真正的问题。
  • 我有一个服务器,用java编写的服务器让你可以灵活地上传groovy脚本,上传的groovy脚本将放在db中,在上传期间给定的groovy脚本必须在给定的时间内编译。只能上传一个 groovy 脚本。在这种情况下,groovy 脚本可以包含与其他 groovy 脚本的依赖关系,所以我想同时使用 GroovyClassloader 和 groovyScriptEngine
  • 实际问题陈述stackoverflow.com/questions/8106082/… ,我想扩展 GroovyClassloader 并覆盖 loadclass 方法我该如何接受这个

标签: groovy


【解决方案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));

    }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    相关资源
    最近更新 更多