모두

2011-11-12 5 views
0

어떻게 당신이 해결하려고하는 어떤 문제 프로그램 자체모두

+2

에 GroovyClassLoader Groovy 스크립트 모두의 효과적인 사용을 만들 수 있습니까? 너 뭐 해봤 니? 무엇이 효과가 없습니까? 이것은 실제로 서있는 것처럼 문제가되지 않습니다. – OverZealous

+1

나는 자바로 작성된 서버를 가지고있다. 서버는 groovy 스크립트를 업로드 할 수있는 유연성을 제공하고 업로드 된 그루비 스크립트는 업로드 중에 주어진 groovy 스크립트가 컴파일해야한다. 주어진 시간에 오직 하나의 그루비 스크립트 만 업로드 될 수있다. . Groovy 스크립트는이 시나리오에서 다른 Groovy 스크립트와의 종속성을 포함 할 수 있으므로 GroovyClassloader 및 groovyScriptEngine을 모두 사용하고자합니다. – anish

+0

실제 문제 설명 http://stackoverflow.com/questions/8106082/compiling-a-groovy-scripts-that -come-a-database, GroovyClassloader를 확장하고 loadclass 메서드를 재정의하려면 어떻게해야합니까? – anish

답변

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)); 

    } 
    }