My question is: do I have to narrow the result of any of these methods or a simple Java cast to my Component or Home interface will suffice?
[ September 04, 2005: Message edited by: Mihai Alexe ] [ September 04, 2005: Message edited by: Mihai Alexe ]
SCJP for Java 1.4<br />SCBCD for J2EE 1.3
Joined: Sep 29, 2002
Let's be clear about what does and does not need to be narrowed.
The only things that must be narrowed are remote stubs. Nothing else retrieved from an RMI-IIOP call must be narrowed. To be specific, the only stubs that need to be narrowed are those for which the declared return type does not match the interface to which you are casting it. So:
* The home stub needs to be narrowed, because it is coming from a method with a return type of Object. This means all JNDI lookups of the remote home interface (your initial lookup of the bean's home).
* All calls to getHandle() or getHomeHandle() (because they always return a remote stub).
But you should not narrow:
* Lookups of a local home interface. * All calls to create() or find(), regardless of whether it is remote or local. The EJBObject stub that you get from create() or find() methods do not need to be narrowed because the return types already specify the exact interface type to which you would be casting.
* All other remote calls unless the remote business method is returning a remote stub, and only if the return type of the method is not the same as the interface type to which you intend to cast it.
Narrowing has been described as a form of exotic casting for stubs. If you need to cast the stub, then you probably need to narrow it as well. But if it is not a stub, then you never need to narrow it, regardless of whether it needs to be cast, and regardless of whether it is coming from a remote call.