The specs are very clear. When you get an EJB Home object from the naming service, or when you get an EJB Remote reference back from a finder method in an Enumeration or Collection, you can't immediately cast it to the type you are expecting, you must first pass it through the PortableRemoteObject.narrow()
method. This has something to do with IIOP, I believe.
I'm left with only one question. Why in the nine layers of Hell should I care about this CORBA shite?
Why didn't the writers of this spec take that tiny step back and think “OK, they're writing this in Java, so we should obey simple Java casting semantics”. PortableRemoteObject.narrow()
was the first step in my steady, and now complete disillusionment in Enterprise JavaBeans. The first time I saw it, even though I was a relative newcomer to OO modeling at the time, something clicked in my head, something said to me this is so very, very wrong.
A part of me is convinced that the only reason there's so much CORBA stuff tied to the EJB spec is because IBM was one of the big backers of J2EE, and WebSphere Application Server was built on top of Component Broker, their CORBA ORB. (Understanding this fact is a crucial step towards understanding some of Websphere's more weird behaviours)1
PortableRemoteObject can bite me.
Update: From the comments so far, it seems I was a trifle too flippant. Yes, I know the technical reasons that a CORBA object needs to be explicitly narrowed. An EJB, however, is not a CORBA object. It's an Enterprise JavaBean. It may be implemented as a layer on top of CORBA, and may be implemented in such a way that CORBA clients can talk to it. But when I'm talking to it in pure Java, I don't care how it's implemented under the hood, and I don't want to care.
What I object to is that even if one accepts that EJB is a Java layer on top of CORBA, I would expect that Java layer to hide the CORBA implementation details, and expose EJBs to me in the idioms of the Java language. If I wanted to talk CORBA to the damned things, Java comes with a perfectly good CORBA API that I can use.
1 Disclaimer: My employer is an IBM Business Partner. Websphere pays my rent. As such, the above is obviously said in the context of a deep and abiding affection for the product. ;)