my dog learned polymorphism*
The moose likes EJB and other Java EE Technologies and the fly likes Retrieving home object question  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Retrieving home object question  " Watch "Retrieving home object question  " New topic
Author

Retrieving home object question

Quang Pham
Ranch Hand

Joined: Nov 29, 2005
Posts: 47
Hi
I am just a beginner and my boss has the code (shown below) that is diffent from any EJB book to retrieve the home object.
The code directly get the home stub thru JINDI and use to call the create method without properly narrow it to home object.
And it works. I am confused!
Head First EJB says home need to be casted using narrow method before it can be used to call create method.
I would appreciate if you can straight me out on this subject.

public class UserSelfChekkInquiryAction extends MYCOMAction {
private static PilotFacadeHome home;
protected MYCOM_TO invokeService(MYCOM_TO transObj)
throws EJBException, RemoteException, NamingException, CreateException {
try {
if (home == null) {
InitialContext jndi = new InitialContext();
home = (PilotFacadeHome) jndi.lookup("java:comp/env/PilotFacade");
}
PilotFacade remote = home.create();
PortableRemoteObject.narrow(remote, PilotFacade.class);
transObj =(UserSelfCheckInquiry_TO) remote.processTransaction(
"UserSelfCheckInquiry",
(UserSelfCheckInquiry_TO) transObj);
return transObj;
} catch (EJBException ee) {
if ((transObj.getReturnMessage() == null)|| (transObj.getReturnMessage().trim().length() == 0)) {
transObj.setReturnMessage(ee.getMessage());
transObj.setReturnCode(1);
}
throw ee;
}
}
}

Thanks for your time
[ September 09, 2006: Message edited by: Quang Pham ]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Looks similar to alot of code I've written though.

Really get scared when I see that home as a static instance. I guess some people don't mind. I'm a WebSphere dude, and with WebSphere, that's a no-no.

You get the InitialContext with JNDI, you lookup the home object, you strip off the corba garbage with the narrow, you then grab the remote interface through the home, and then you call the business methods. That's like EJB 101.

So, I like what I see. Which part perplexes you. Tell me specifically what irks you about your code/the books you read, and maybe the JavaRanch can enlighten you a little bit more.

Cheers!

-Cameron
Quang Pham
Ranch Hand

Joined: Nov 29, 2005
Posts: 47
Thank you for taking time looking into this.
Head First EJB is the book I am studying and it has code like this to retrieve home object as well as the example on Java Sun web site:

public class SimpleSessionClient{
public static void main(String [] args) {
try {
InitialContext jndiContext = new InitialContext();
Object o = jndiContext.lookup("ejb/beans.SimpleSession");
SimpleSessionHome home = (SimpleSessionHome) PortableRemoteObject.narrow(o,SimpleSessionHome.class);
SimpleRemoteObject remote = home.create();
for (int i = 0; i < args.length; i++ ){
String returnedString = remote.getEchoString(args[i]);
System.out.println("Sent string: " + args[i] + returnedString);
}
}
catch (Exception e){
e.printStackTrace();
}
}
}

First, an Object is used to retrieve the home stub then it is narrow to the SimpleSessionHome object.
Are both ways (the previous code and Head First EJB code) doing the same thing?
By the way, what is wrong with using home static object?

Thanks again.
Quang Pham
Ranch Hand

Joined: Nov 29, 2005
Posts: 47
Help! - Does any one have any other comment on this?
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

One difference is just the fact that two steps have been combined into one.


Object o = jndi.lookup("java:comp/env/PilotFacade");

home = (PilotFacadeHome) o;

That breaks your single line of code into code that looks closer to the Heads First Book.
Quang Pham
Ranch Hand

Joined: Nov 29, 2005
Posts: 47
Thanks and can you explain a little bit what is wrong with using static home object.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Retrieving home object question
 
Similar Threads
Error: cannot narrow to remote object
EJB Beginner
using instance variables in Stateless session bean
Need help on EJB2.0 Local interfaces
JSP on JBoss3.2