• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting using PortableRemoteObject

 
Giju George
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we are looking an Entity bean from a session bean, and both are running in the same JVM, do we still need to do a PortableRemoteObject.narrow()..or can we just do a cast on the lokkup itself . I remember I have asked the same ques before.. but still confused with this.

Eg:-
public class EmployeeServiceBean implements SessionBean {
public void changePhoneNumber(...) {
...

// Obtain the default initial JNDI context.
Context initCtx = new InitialContext();

// Look up the home interface of the EmployeeRecord
// enterprise bean in the environment.
Object result = initCtx.lookup("java:comp/env/ejb/EmplRecord");

// Convert the result to the proper type.

EmployeeRecordHome emplRecordHome = (EmployeeRecordHome)
javax.rmi.PortableRemoteObject.narrow(result,
EmployeeRecordHome.class);

OR

EmployeeRecordHome result = (EmployeeRecordHome )initCtx.lookup("java:comp/env/ejb/EmplRecord");





...
}
}
 
Nitin Mehhta
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wrote ->

Object result = initCtx.lookup("java:comp/env/ejb/EmplRecord");

Now thie means that probably you are looking up the entity bean using the ejb-ref tag. If that is the case then as a part of that you declare whether the bean associated will have a remote interface or a local interface...using the tag <local> or <remote>.

I guess this is just a part of the answer to your question. But if you are performing the look up using the actual JNDI name and both the beans reside in the same JVM I don't see a need to perform the narrow.
 
Giju George
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both uses a lookup from an <ejb-ref> .

So.... what u r saying is, both are correct ???
 
Nitin Mehhta
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean to say that if you have explicitly mentioned in your ejb-ref that you would be using a remote/local reference then you should do accordingly. But say you have not defined the ejb-ref tag in your DD then you have to give the actual JNDI name in your code. And in that case if your entity bean and session bean are on the same jvm then you don't need to do narrowing. The basic idea behind my answer is that ejb-ref tag is not a necessity. If you have it then you should abide by it.
 
Severin Stoeckli
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spec, 5.4:

Narrowing remote types requires the use of javax.rmi.PortableRemoteObject.
narrow rather than Java language casts.


=> if you have a local client view (means, your home-interface inherits from javax.ejb.EJBLocalHome) you can (or must?) cast. If you have a remote client view (...inherits from javax.ejb.EJBHome), you must narrow.

Narrowing comes historically from the IIOP protocol, a protocol defined by OMG for CORBA-services. RMI/IIOP (the today's remote protocol implementation for EJB's) is based on this protocol. Since local clients are not involved in RMI, you can (or must) simple cast after a lookup.

It may be that you can use only a cast for the remote client view, if the client and the server are running in the same container, but that's server dependet and then, your application is not a "pure J2EE-APP".

Severin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic