简体   繁体   中英

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. In my groovy code, I am calling a Java object that has a newInstance() static method, like so:

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. This does not happen when running outside Mule standalone (see update below for description of test). How can I make it call the Java method and not the Groovy one?

Here is a snippet of my stack trace. You can see its calling DefaultGroovyMethods.newInstance() . But I want to call the javax.xml.transform.TransformerFactory.newInstance(String, ClassLoader) method instead. 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. Mule 3.3.0 ships with 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. This leads me to believe that it is an issue running from the Mule 3.3.0 standalone environment. I am adding a mule tag to this post. Hopefully a Mule expert can tell me what is going on.

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. 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.

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?

My work around was to instantiate the Saxon TransformerFactory directly. Its not very elegant, but my only option.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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