4 Replies Latest reply on Jan 27, 2004 7:09 AM by juha

    how to read EJB- and Home- Interface from EJB

    tom140978

      I use this code to call ejbs functions from an Servlet:

       String ejbName = "ejbs/idgen/IDGen";
       String funcName = "generate";
       Object[] params = new Object[]{};
       Class[] paramsClasses = new Class[]{};
      
       String ejbHomeI = null; // "idgen.interfaces.IDGenHome"
       String ejbI = null; // "idgen.interfaces.IDGen"
      
       try{
       Context context = new InitialContext();
       Object ref = context.lookup(ejbName);
       Class ejbClassHome = Class.forName(ejbHomeI);
       Class ejbClass = Class.forName(ejbI);
      
       javax.ejb.EJBHome homeObject = (javax.ejb.EJBHome)PortableRemoteObject.narrow(ref, ejbClassHome);
      
       Method createMethod = ejbClassHome.getMethod("create", new Class[]{});
       EJBObject ejbObject = (EJBObject)createMethod.invoke(homeObject, new Object[]{});
      
       Method generateMethod = ejbClass.getMethod(funcName, paramsClasses);
       value = generateMethod.invoke(ejbObject, params);
       } catch (Exception e){
       e.printStackTrace();
       }
      


      if I set "ejbHomeI" and "ejbI" interface names, the code work properly. The problem is, i have only information about jndi-name of EJB, about a function i want to call and ther's params and param's classes.

      is it possible, to become interfaces names of an EJB???
      i hope it is easy to do, becouse interfaces are writed in ejb-jar.xml file...

       <session >
       <description>EJB that generates IDs for all</description>
       <display-name>IDGen EJB</display-name>
      
       <ejb-name>IDGen</ejb-name>
      
       <home>idgen.interfaces.IDGenHome</home>
       <remote>idgen.interfaces.IDGen</remote>
       <ejb-class>idgen.ejb.IDGenBean</ejb-class>
       <session-type>Stateless</session-type>
       <transaction-type>Container</transaction-type>
       </session>
      


      can i read ejb-informations about "", , etc... with simply jndi lookup function??? With "env-entry" it's work!

      please help, it's important for my study!
      Tom

        • 1. Re: how to read EJB- and Home- Interface from EJB
          tom140978

           


          can i read ejb-informations about "", , etc... with simply jndi lookup function??? With "env-entry" it's work!


          should be:
          can i read ejb-informations about "home","remote","description" , etc... with simply jndi lookup function??? With "env-entry" it's work!


          the corrected ejb-jar.xml file is:
           (...)
           <description.>EJB that generates IDs for all</description>
           <display-name>IDGen EJB</display-name>
          
           <ejb-name>IDGen</ejb-name>
          
           <home.>idgen.interfaces.IDGenHome</home>
           <remote.>idgen.interfaces.IDGen</remote>
           <ejb-class >idgen.ejb.IDGenBean</ejb-class>
           <session-type >Stateless</session-type>
           <transaction-type >Container</transaction-type>
           (...)
          


          • 2. Re: how to read EJB- and Home- Interface from EJB

            EJBHome.getEJBMetaData().getHomeInterfaceClass();
            EJBHome.getEJBMetaData().getRemoteInterfaceClass();

            • 3. Re: how to read EJB- and Home- Interface from EJB
              tom140978

              Thanks @juha! It's works!
              It's very compact solution, I like it!
              I have searched 4 days for this solution!

              the code work now:

              String ejbName = "ejbs/idgen/IDGen";
              String funcName = "generate";
              Object[] params = new Object[]{};
              Class[] paramsClasses = new Class[]{};
              
              try{
              
               Context context = new InitialContext();
               Object ref = context.lookup(ejbName);
              
               javax.ejb.EJBHome homeObject = (javax.ejb.EJBHome)PortableRemoteObject.narrow(ref, javax.ejb.EJBHome.class);
              
               Method createMethod = homeObject.getEJBMetaData().getHomeInterfaceClass().getMethod("create", new Class[]{});
               EJBObject ejbObject = (EJBObject)createMethod.invoke(homeObject, new Object[]{});
              
               Method generateMethod = homeObject.getEJBMetaData().getRemoteInterfaceClass().getMethod(funcName, paramsClasses);
               value = generateMethod.invoke(ejbObject, params);
               } catch (Exception e){
               e.printStackTrace();
               }
              
              


              I founded today (before I readed your message) other possiblity. But it's more Difficulty to write (the part of code, come from JNDIView.java)...
               InitialContext ic = new InitialContext();
               RMIAdaptor server = (RMIAdaptor)ic.lookup("jmx/rmi/RMIAdaptor");
               ObjectName name = new ObjectName("jboss.j2ee:module=IDGenEJB.jar,service=EjbModule");
               Collection containers = (Collection)server.getAttribute(name, "Containers");
               for (Iterator iter = containers.iterator(); iter.hasNext();) {
               Container con = (Container) iter.next();
               Thread.currentThread().setContextClassLoader(con.getClassLoader());
               // String
               System.out.println(" + home:" + con.getBeanMetaData().getHome());
               System.out.println(" + remote:" + con.getBeanMetaData().getRemote());
               System.out.println(" + ejbClass:" + con.getBeanMetaData().getEjbClass());
               }
              


              One Question is very important for me too:
              How can I become "description" information from ejb-jar.xml? I try to create an ejb browser tool and need this information for every deployed ejb...
              If it's not possible to do, why informations like "description" are saved in ejb-jar.xml???

              thanks!
              Tom

              • 4. Re: how to read EJB- and Home- Interface from EJB

                For additional information like the descriptions you need to go through the proprietary API of the container, these are not exposed as part of standard J2EE APIs.

                An example of this is the code snippet you already posted, con.getBeanMetaData().getXXX().

                It is also a possibility that some of this is exposed through the JSR-77 spec MBeans. I'm not familiar with all the details these MBeans expose, so you'd have to check that spec yourself.