2 Replies Latest reply on Dec 2, 2005 6:18 PM by Peter Palmreuther

    ClassCastException on PortableRemoteObject.narrow()

    Peter Palmreuther Newbie

      Hello,

      I've got a ClassCastException problem when trying to get the correct object instance using InitialContext.lookup() and PortableRemoteObject.narrow().

      The situation is me having two applications, one carrying the SessionBean(s), the other one a central MessageDrivenBean. While the business objects repesented by the SessionBean-application might be updated from time to time the MDB-application should be more or less "final". I therefore decided to split them into the two applications, especially as further applications which should be accessed through the MDB might follow, and I don't want to and can't redeploy *all* Beans whenever a little thing in one business SessionBean changes.

      I therefore do a

      Object objRef;
      InitialContext ctx = new InitialContext();
      objRef = ctx.lookup("ejb/MyBean");
      


      and get a "$ProxyXX" object back which is implementing correct remote Home interface 'MyBeanHome' (I played a little with reflections to gather information about Interfaces and methods).

      Now if I'm trying

      MyBeanHome tmp;
      tmp = (MyBeanHome) PortableRemoteObject.narrow(objRef, MyBeanHome.class);
      


      I get a ClassCastException; even wth

      Object tmp;
      tmp = PortableRemoteObject.narrow(objRef, MyBeanHome.class);
      


      Using

      Obj tmp;
      tmp = objRef.getClass().getMethod("create", null).invoke(objRef, null);
      


      'tmp' contains an object implementing Interface 'MyBean', which indicates 'objRef' in fact contains an object implementing 'MyBeanHome' and it's 'create()' knows what to do.

      Sadly I can't cast this 'tmp' to 'MyBean' for invoking my 'dispose(...)' method on it. I get a ClassCastException then as well.

      String tmp2;
      tmp2 = tmp.getClass().getMethod("dispose", new Class[] {...}).invoke(tmp, new Object[] {...});
      


      Gives me the correct result in 'tmp2', but I can't believe using reflections all the way along is the way to go; I'd expect 'PortableRemoteObject.narrow()' and having correctly typed object instances to be the intended solution.

      Can anybody help me on this issue and tell my why my $ProxyXX-objects don't "cooperate" and behave as I'd expect them to do?

      Thanks in advance,
      Peter