我们想使用 groovy/grails 作为 Glassfish-Remote-Client,但我们遇到了奇怪的类加载问题。
首先是通过 groovy/grails 调用时触发错误的测试代码。没关系,当它通过 java 调用时。通过groovy运行时,会触发java.lang.ClassCastException: com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject cannot be cast to javax.rmi.CORBA.PortableRemoteObjectDelegate in (PortableRemoteObject.java:77) 之前错误,可以手动进行转换。
==========来源==========
package test;
import java.lang.reflect.Field;
public class J2EETest {
public static void main(String[] args) throws Exception {
new J2EETest().classLoaderTest();
}
public static void classLoaderTest() throws Exception {
ClassLoader classLoader = J2EETest.class.getClassLoader();
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
System.out.println("classLoader: " + classLoader);
System.out.println("threadClassLoader: " + threadClassLoader);
Class klass = Class.forName( "com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject", false, threadClassLoader );
Object obj = klass.newInstance();
System.out.println("Object -> " + obj.toString());
// This works
javax.rmi.CORBA.PortableRemoteObjectDelegate del = (javax.rmi.CORBA.PortableRemoteObjectDelegate) obj;
try {
System.out.println("Init of javax.rmi.PortableRemoteObject");
// This doesn't work !
System.out.println( Class.forName("javax.rmi.PortableRemoteObject", true, threadClassLoader));
Class prOklass = javax.rmi.PortableRemoteObject.class;
Field staticfield = prOklass.getDeclaredField("proDelegate");
staticfield.setAccessible(true);
System.out.println("Reflection: PortableRemoteObject.proDelegate ->" + staticfield.get(null));
System.out.println("OK");
}
catch(Throwable e) {
System.err.println("BOOM");
e.printStackTrace();
}
}
}
======= JAVA 测试运行 =======
$ export CLASSPATH=build:lib/glassfish/gf-client-module.jar # gf-client-module.jar -> GLASSFISH-Client Libs
$ java -Djavax.rmi.CORBA.PortableRemoteObjectClass=com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject test.J2EETest
classLoader: sun.misc.Launcher$AppClassLoader@e776f7
threadClassLoader: sun.misc.Launcher$AppClassLoader@e776f7
Object -> com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject@5511e28
Init of javax.rmi.PortableRemoteObject class javax.rmi.PortableRemoteObject
Reflection: PortableRemoteObject.proDelegate -> com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject@20e5f01b
OK
======= 现在是 Groovy 版本 =======
$ groovy -Djavax.rmi.CORBA.PortableRemoteObjectClass=com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject -e "test.J2EETest.classLoaderTest()"
classLoader: org.codehaus.groovy.tools.RootLoader@5dcba031
threadClassLoader: org.codehaus.groovy.tools.RootLoader@5dcba031
Object -> com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject@1bf3f158
Init of javax.rmi.PortableRemoteObject
BOOM
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at test.J2EETest.classLoaderTest(J2EETest.java:83)
at test.J2EETest$classLoaderTest.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at script_from_command_line.run(script_from_command_line:1)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
at groovy.lang.GroovyShell.run(GroovyShell.java:517)
at groovy.lang.GroovyShell.run(GroovyShell.java:172)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:553)
at groovy.ui.GroovyMain.run(GroovyMain.java:337)
at groovy.ui.GroovyMain.process(GroovyMain.java:323)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:120)
at groovy.ui.GroovyMain.main(GroovyMain.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)
Caused by: java.lang.ClassCastException: com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject cannot be cast to javax.rmi.CORBA.PortableRemoteObjectDelegate at javax.rmi.PortableRemoteObject.<clinit>(PortableRemoteObject.java:77)
... 22 more
======= 现在是没有 -Djavax.rmi.CORBA.PortableRemoteObjectClass 的 Groovy 版本 =======
这可行,但不是 glassfish 客户端的选项,因为 InitialContext (或那里的其他东西)将设置 javax.rmi.CORBA.PortableRemoteObjectClass = com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject
$ groovy -e "test.J2EETest.classLoaderTest()"
classLoader: org.codehaus.groovy.tools.RootLoader@3b4d82e1
threadClassLoader: org.codehaus.groovy.tools.RootLoader@3b4d82e1
Object -> com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject@527e2f47
Init of javax.rmi.PortableRemoteObject
class javax.rmi.PortableRemoteObject
Reflection: PortableRemoteObject.proDelegate -> com.sun.corba.se.impl.javax.rmi.PortableRemoteObject@5b3bd1c0 # com.sun.corba.ee != com.sun.corba.se
好的
有没有人遇到过类似的问题或知道如何让这段代码工作?
亲切的问候
坦率