We got it to work, thanks to the help of Simon Knott and Lachezar Dobrev.
The reason was that the class file of the EJB was in multiple archive files.
Simon also added a - in my opinion - good explanation why it didn't work to
his reply. I will post here now with his permission, as he replied directly
to me. 

Simon wrote:
> If this is the case [having the class in multiple archives], then what is
likely to be happening is the following:
>
> 1) JSP class loader loads the EJB class into memory, from the WAR file.
> 2) JSP does a lookup for the EJB object.
> 3) EJB container class loader loads the EJB class into memory, from the
JAR
>    file.
> 4) EJB container returns its loaded class to the JSP
> 5) Class cast exception is thrown.
> 
> Now the key point here is that different class loaders are used for both
the
> JSP and the EJB container.  When one class is loaded by two different
class
> loaders, they are essentially different classes, with different class
> signatures.
>
> What happens with the class loader is that it takes the quickest route to
> loading a class - in the case of the JSP it will get it from the WAR, in
the
> case of the EJB container it will get it from the JAR.  What you really
need
> to do is to create one archive, which is then placed in either the
> webapp/lib directory or the orion/lib directory, and exclude the EJB
classes
> from the WAR and JAR files.  This means that the classes in this one
central
> JAR file will only be loaded by one class loader, removing the problem.
>
> This does not occur on the standalone client, because it uses only the
> system class loader, no others.


Peter

-----Original Message-----
From: Lachezar Dobrev [mailto:[EMAIL PROTECTED]]
Sent: Donnerstag, 11. Oktober 2001 12:37
To: Orion-Interest
Subject: Re: Error Accessing an EJB through a Bean


   Hello.
   INLINE.

> We created a Bean to access an EJB from JSPs. First we tested the Bean
from
> a Java Client and everything worked fine. Then we copied the Bean to the
> Orion server and tried to acces the EJB by using a JSP that instanciates
the
> Bean that accesses the EJB. But we can not get it to work. The test with
the
> Java client still works properly. But using the Bean from JSP always
creates
> a ClassCastException.
>
> Here's the sniplet from the Bean where the lookup is performed.
>
> private static GUIServiceSBHome getHome(){
>     GUIServiceSBHome toReturn = null;
>     try
>     {

   This part with the Hashtable is not correct.
   You should avoid it. Better create a jndi.properties file in the root
directory of your JAR, or the root rirectory of your classes.
   then just use "new InitialContext()" which will get the required
properties from the jndi.properties file.

>       Hashtable env = new Hashtable();
>
>       //ToDO if this bean runs inside a container this Env property will
not be
>       //necessary
>       env.put(Context.INITIAL_CONTEXT_FACTORY,
> "com.evermind.server.rmi.RMIInitialContextFactory");
>       env.put(Context.SECURITY_PRINCIPAL, "admin");
>
>       env.put(Context.SECURITY_CREDENTIALS, "xyz");
>       env.put(Context.PROVIDER_URL,
> "ormi://164.139.33.191:23791/bizDataNew");



   Let's see.

>       // Replace <EJB ModuleName> below with the EJB module name
identified
>       // in your deployment profile

   As said... Especialy in a web-bean use "new InitialContext()"

>       Context ctx = new InitialContext(env);
>       Object ref = ctx.lookup("GUIServiceSB");
>       logger.debug("> > > > > >>>>>>>   Got an Object.. Class = " +
> ref.getClass());
>       toReturn = (GUIServiceSBHome)PortableRemoteObject.narrow(ref,
> GUIServiceSBHome.class);
>       logger.debug("> > > > > >>>>>>>   Lookup........5");
>     }
>     catch(Throwable ex){
>       logger.error( "Unable to perform lookup for GUIServiceBean Home
> interface\nException = " + ex.toString() );
>     }
>     return toReturn;
>   }
>
> The logger.debug statements create the following output.
>
> From the Client:
> de.mgi.frontend.GUIServiceProxyBean: > > > > > >>>>>>>   Got an Object..
> Class = class __Proxy0
> de.mgi.frontend.GUIServiceProxyBean: > > > > > >>>>>>>   Lookup........5
>
> On the Server (Bean used by JSP):
> de.mgi.frontend.GUIServiceProxyBean: > > > > > >>>>>>>   Got an Object..
> Class = class GUIServiceSBHome_StatelessSessionHomeWrapper215
> de.mgi.frontend.GUIServiceProxyBean: Unable to perform lookup for
> GUIServiceBean Home interface Exception = java.lang.ClassCastException:
> GUIServiceSBHome_StatelessSessionHomeWrapper215 was not an instance of
> interface de.mgi.orca.business_data.ejb.session.gui.GUIServiceSBHome, the
> interface it implements is
> de.mgi.orca.business_data.ejb.session.gui.GUIServiceSBHome
>
> Next to the relevant question why does the Bean get an exception when it
is
> executed on the server, there are also two more questions that I have when
I
> look at the ouput.
>
> 1. Why is the return value of ref.getClass() different? The client and the
> JSP are accessing the same EJB on the same Orion server.
> 2. Why does it says the retrieved object is not an instance of the
interface
> ...GUIServiceSBHome but an instance of ...GUIServiceSBHome? For me the two
> interface classe names look the same.

   Ok. I had the same problem. It was, that I had one and the same class
(the Home Interface) in two places. Both classes, though identical these
classes were not compatible :(.

   It is possible (like in my case) that you have the home interface both in
the ejb-jar and the web-app. Try removing it from the web-app.

>
> And hint why the bean is not working on the server is greatly appreciated.
>
> Peter Cugier


   Lachezar.



Reply via email to