This week's book giveaway is in the Java in General forum. We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line! See this thread for details.
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.
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
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