Community

JBoss 4.2.1 RemoteEJB Lookup ClassCastException

reply from Marcus Linke in EJB 3.0 - View the full discussion

I've read this and found a simpler solution / workaround for this problem (without the need of patching). It's simple to integrate the appropriate code taken from EJBTHREE-1889 into the own JNDI lookup. Because of performance reasons this hack of course should be used only for JVM internal remote lookups (which is the case with isolated EAR's). This may be realized by using different JNDI lookup wrapper classes...

 

public class JBossRemoteJndiLookup<T> {

    public T lookup(String resourceName) {
        Object o = new InitialContext().lookup(resourceName);
        o = refineObjectInContextClassLoader(o);
        return (T) o;
    }

 

    /**
     * This is a workaround for http://community.jboss.org/thread/111016. It is taken from
     * https://jira.jboss.org/browse/EJBTHREE-1889. See modifications of ProxyObjectFactory.java
     */
    private Object refineObjectInContextClassLoader(Object o) {
        // EJBTHREE-1889: if the lookup has been performed locally on a remote (business)
        // interface that's defined in the current TCL, then the proxy must implement
        // that TCL defined interface.
        // redefine in TCL
        ClassLoader tcl = Thread.currentThread().getContextClassLoader();
        try {
            o = redefineObjectInClassLoader(o, tcl);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return o;
    }

 

    private Object redefineObjectInClassLoader(Object obj, ClassLoader target) throws ClassNotFoundException,
            IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeObject(obj);
        out.flush();
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        ObjectInputStream in = new ObjectInputStreamWithClassLoader(bin, target);
        Object result = in.readObject();
        in.close();
        return result;
    }

}

Reply to this message by going to Community

Start a new discussion in EJB 3.0 at Community