使用模板引擎时的Groovy类加载器异常

时间:2014-10-22 18:54:15

标签: java groovy template-engine gstring

我是Groovy的新手并尝试使用GStringTemplateEngine在JBoss 5.1上执行一些groovy脚本

在我的本地开发环境中一切正常,但在移动到由其他团队管理的开发服务器时失败。

我发现异常是在

专门抛出的
try {
    groovyClass = loader.parseClass(new GroovyCodeSource(templateExpressions.toString(), "GStringTemplateScript" + counter.incrementAndGet() + ".groovy", "x"));
} catch (Exception e) {
    throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
}

on GStringTemplateEngine.class,第190行

异常消息是

groovy.lang.GroovyRuntimeException: startup failed:
General error during class generation: URI is not hierarchical

java.lang.IllegalArgumentException: URI is not hierarchical
    at java.io.File.<init>(File.java:363)
    at org.jboss.net.protocol.file.FileURLConnection.<init>(FileURLConnection.java:62)
    at org.jboss.net.protocol.file.Handler.openConnection(Handler.java:40)
    at java.net.URL.openConnection(URL.java:945)
    at java.net.URLClassLoader.getPermissions(URLClassLoader.java:474)
    at groovy.lang.GroovyClassLoader.getPermissions(GroovyClassLoader.java:335)
    at java.security.SecureClassLoader.getProtectionDomain(SecureClassLoader.java:235)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at groovy.lang.GroovyClassLoader.access$300(GroovyClassLoader.java:55)
    at groovy.lang.GroovyClassLoader$ClassCollector.createClass(GroovyClassLoader.java:475)
    at groovy.lang.GroovyClassLoader$ClassCollector.onClassNode(GroovyClassLoader.java:492)
    at groovy.lang.GroovyClassLoader$ClassCollector.call(GroovyClassLoader.java:496)
    at org.codehaus.groovy.control.CompilationUnit$14.call(CompilationUnit.java:792)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1024)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:562)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:540)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:517)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:283)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:260)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:244)

我不确定为什么抛出URI而不是分层错误。我假设它与权限有关,而GroovyClassLoader无法解析生成的类。

之前有没有人见过这个错误?如果有人可以就调试/解决问题提供一些建议,将会很有帮助。

1 个答案:

答案 0 :(得分:0)

不确定问题的真正原因,但找到了解决方法。

创建了GStringTemplateEngine的副本并替换了

try {
    groovyClass = loader.parseClass(new GroovyCodeSource(templateExpressions.toString(), "GStringTemplateScript" + counter.incrementAndGet() + ".groovy", "x"));
} catch (Exception e) {
    throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
}

try {
    groovyClass = loader.parseClass(templateExpressions.toString(), "GStringTemplateScript" + counter.incrementAndGet() + ".groovy");
} catch (Exception e) {
    throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
}

删除了GroovyCodeSource并将模板字符串传递给parseClass方法。