Two Laptop Bag*
The moose likes EJB and other Java EE Technologies and the fly likes Doubt in Narrowing 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 "Doubt in Narrowing" Watch "Doubt in Narrowing" New topic
Author

Doubt in Narrowing

Sri Ram
Greenhorn

Joined: Apr 04, 2006
Posts: 10
Hi all

Im doing a look up for my home interface of the bean in my service locator in the following manner

InitialContext ic = new InitialContext(ht);
Object o = ic.lookUp("mybean");
MYHome home = (MYHome)PortableRemoteObject.narrow(obj,obj.getClass());//instead of using the class name of MYHome im using the getClass method

The problem is this code is working for me fine when im running it on weblogic server sp4 but the same gives an ClassCastException: Cannot narrow remote object to java.lang.class when run on weblogic sp3

im sure that the problem is not with the version


is it a wrong idea to use obj.getClass() to get the class object of the Homeobject instead of using MYHome.class

pls guide me, im really in a soup
Andreas Schaefer
Ranch Hand

Joined: Feb 13, 2006
Posts: 63
Your code strucks me as quite odd. The narrow() needs the interface it needs to narrow to and for sure the obj.getClass() does not deliver the interface.
For example in JBoss that code would have failed too because the remote interface is of type ProxyXXX which is a generated class.

I would suggest that you try to read the spec. to understand what is required when you want to use any shortcuts otherwise it may blow up on you when you move to a newer version, different JDK or different app. server.

-Andy
Prem Kumar
Greenhorn

Joined: Jan 29, 2006
Posts: 14
My suggestion is there is some thing wrong with the code that
you have posted.

This is your code

InitialContext ic = new InitialContext(ht);
Object o = ic.lookUp("mybean");
MYHome home = (MYHome)PortableRemoteObject.narrow(obj,obj.getClass());//

I Changed it like this

InitialContext ic = new InitialContext(ht);
Object o = ic.lookUp("mybean");
MYHome home = (MYHome)PortableRemoteObject.narrow(o,MYHome.class);//

Try out this.


Have a Great Day!
Sri Ram
Greenhorn

Joined: Apr 04, 2006
Posts: 10
Hai Prem

Thanks for the solution, i know if i can directly put my class name as an argument it would work, the problem is my application design prevents me from having the myhome.class in the same package as in the service locator, because if i have 1000 EJBs then i have to have 1000 Home interface classes in my service locator package that would blow up my main idea of using a service locator

the main idea is

the developer who is gonna use my service locator will use the following method

MyHome home = ServiceLocator.getHome(jndiName)

so inside the Servicelocator's getHome method i would find the home object of the ejb with the corresponding JNDi name and use the narrow method and send it back to the developer



so what would be the solution for this problem


so i thought of using this kinda shortcuts
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
The developer has to cast the return object from the ServiceLocator anyways right (ie ServiceLocator returns a SuperInterface or Object)? Just have the developer pass in the home interface.

Client Code:


ServiceLocator Code:


This is the standard way of solving the ServiceLocator problem for EJBs. In my example I left out exception handling and caching of EJBHomes... but I am sure you get the idea.
Sri Ram
Greenhorn

Joined: Apr 04, 2006
Posts: 10
Thanks Chris


Im following the same methodology now, but just curious to know why the getclass wont work , wont the class which the container is going to implement the Home Interface will be of the same type as the Home.class???

what procedure does the container follow to implement a home interface and return a home object
Andreas Schaefer
Ranch Hand

Joined: Feb 13, 2006
Posts: 63
The stub the lookup returns will of a class that implements the remote interface but be of that type. A way to fix your ServiceLocator is to hand down the desired interface:


But your original code:

is actually referencing the remote interface (MYHome) and so you would have to provide the class anyhow so why not just using it for the narrow (you use it just left of it for the upcast.

-Andy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Narrowing
 
Similar Threads
Doubt in Narrowing
How to deploy a Simple EJB application?
help me
help me
Getting Error while runnning Session Bean Client