Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

casting using PortableRemoteObject

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");





...
}
}
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic