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
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.
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!
Joined: Apr 04, 2006
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
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.
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.
Joined: Apr 04, 2006
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
Joined: Feb 13, 2006
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.