Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving home object question

 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help! - Does any one have any other comment on this?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks and can you explain a little bit what is wrong with using static home object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic