简体   繁体   English

在Mule ESB中运行编译的Groovy代码时,无法调用Java newInstance()而不是Groovy

[英]Cannot call Java newInstance() instead of Groovy's When Running Compiled Groovy Code in Mule ESB

Inside my Mule ESB 3.3.0 app, running in standalone mode, I have a transformer that calls Groovy code that is included as a maven dependent jar. 在以独立模式运行的Mule ESB 3.3.0应用程序内部,我有一个转换器来调用Groovy代码,该代码作为依赖于Maven的jar包含在其中。 In my groovy code, I am calling a Java object that has a newInstance() static method, like so: 在我的常规代码中,我正在调用一个具有newInstance()静态方法的Java对象,如下所示:

TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null)

This is causing errors in Groovy because I think its trying to call the Groovy enhanced Class.newInstance() method. 这在Groovy中引起错误,因为我认为它试图调用Groovy增强的Class.newInstance()方法。 This does not happen when running outside Mule standalone (see update below for description of test). 在Mule独立环境之外运行时不会发生这种情况(有关测试的说明,请参见下面的更新)。 How can I make it call the Java method and not the Groovy one? 如何使它称为Java方法而不是Groovy方法?

Here is a snippet of my stack trace. 这是我的堆栈跟踪的片段。 You can see its calling DefaultGroovyMethods.newInstance() . 您可以看到其调用DefaultGroovyMethods.newInstance() But I want to call the javax.xml.transform.TransformerFactory.newInstance(String, ClassLoader) method instead. 但是我想改为调用javax.xml.transform.TransformerFactory.newInstance(String, ClassLoader)方法。 How? 怎么样?

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: javax.xml.transform.TransformerFactory(java.lang.String, null)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1459)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1375)
at org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:824)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.newInstance(DefaultGroovyMethods.java:18025)

UPDATE: More info about the runtime environment. 更新:有关运行时环境的更多信息。 The Groovy code is packaged as a jar file and called from a Mule ESB app. Groovy代码打包为jar文件,并从Mule ESB应用程序调用。 Mule 3.3.0 ships with groovy-all-1.8.6.jar. Mule 3.3.0带有groovy-all-1.8.6.jar。

UPDATE2: I did some further tests and compiled a Groovy class into a jar file and called it from a standalone Java program and did not experience this issue. UPDATE2:我进行了一些进一步的测试,并将Groovy类编译为jar文件,并从独立的Java程序中对其进行了调用,但没有遇到此问题。 This leads me to believe that it is an issue running from the Mule 3.3.0 standalone environment. 这使我相信这是从Mule 3.3.0独立环境运行的问题。 I am adding a mule tag to this post. 我在这篇文章中添加了ule子标签。 Hopefully a Mule expert can tell me what is going on. 希望a子专家能告诉我发生了什么事。

With some help from the Groovy user mailing list, we determined that there must be another jar on the mule classpath that is providing the javax.xml.transformer.Transformer class. 在Groovy用户邮件列表的帮助下,我们确定在the子类路径上必须存在另一个提供javax.xml.transformer.Transformer类的jar。 I added this sample code: 我添加了以下示例代码:

Class klass = TransformerFactory.class; 
URL location = klass.getResource('/'+klass.getName().replace('.', '/')+".class"); 

And sure enough! 果然! Mule is adding their own XML API jar. Mule正在添加自己的XML API jar。

TranformerFactory JAR: jar:file:/D:/java/mule/mule-standalone-3.3.0/lib/endorsed/xml-apis-1.3.04.jar!/javax/xml/transform/TransformerFactory.class

Why are you overriding the JDK Mule? 为什么要覆盖JDK Mule?

My work around was to instantiate the Saxon TransformerFactory directly. 我的解决方法是直接实例化Saxon TransformerFactory。 Its not very elegant, but my only option. 它不是很优雅,但我唯一的选择。

tFactory = new net.sf.saxon.TransformerFactoryImpl()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM