2 Replies Latest reply on Dec 13, 2007 8:25 AM by Henry Jones

    Accessing ejbs from outside an enterprise application

    Henry Jones Newbie

      I have a requirement to access an ejb from a web application(servlet) outside of the original ear the ejb is deployed to.
      The ejb application is using container managed security.

      I have created a web application containing a servlet that makes a call to the ejb. The web application contains the home and remote interfaces for the ejb in the WEB-INF/lib directory. the web.xml of the application contains a reference to the ejb.
      InitialContext context = new InitialContext();
      Object object = context.lookup("java:comp/env/ejb/myEJB");
      MyEJBHome home = (MyEJBHome)PortableRemoteObject.narrow

      Now the lookup seems to be working...
      however a CastClassException is thrown on the narrow method call.

      Any ideas as to why this may be happening ?

      Thanks for any help.

        • 1. Re: Accessing ejbs from outside an enterprise application
          Peter Johnson Master

          Remove the home and remote interfaces from your war file. The problem is that the proxy handed to your servelt was built using the interface defined in the ejb jar/ear file, and is not the same (according to the classloader) as the interface in your war file.

          You can package things in two ways:

          a) place the ejbs in a jar and put the jar in the deploy directory, and put your war in the deploy directory

          b) if you need to put your ejbs in an ear, add the war to the ear (don't forget to add the web module entry to the application.xml)

          Alternate explanation:

          What you are getting from the JNDI call is not the EJB. To see if that is the case, after you get 'object', do this:

          System.out.println("object=" + object);

          That will tell you if your really have the ejb (it will be some sort of proxy).

          If it is not a proxy, then you have to look at the JNDI tree using jndiview.

          • 2. Re: Accessing ejbs from outside an enterprise application
            Henry Jones Newbie

            Thanks for the quick response. Actually I had read a similar post regarding the ClassCastException i was getting. However I assumed that my case was different since i was not packaging the war file in question within the same ear as the ejbs needed.

            Took a look @ the jboss class loading architecture documentation in the admin guide and I'm clear with it now.

            Thanks for the help.