passing an interface/parent class to narrow an object ?


EJB design: passing an interface/parent class to narrow an object ?

  1. as a design consideration we are planning to cache all the EJB Homes as soon as the Application server starts .
    Also we want to make the code as generic as possible and not dependent on the specific classes .
    So while narrowing the Object references can we do a generic narrow as shown below ?

    ejbHome = (EJBHome)
    PortableRemoteObject.narrow(objref, EJBHome.class);

    Note that the object is being narrowed to EJBHome .

    Later in the application when ever a jndi lookup is done the object is taken from the cache and narrowed to the specific class ...

    is this ok ? does this cause any problems ? if so what ?

    Thanks in advance for the help ...
  2. You have to call PortableRemoteObject.narrow for the actual interface you intend to cast the EJBHome to later. Is you only narrow to EJBHome.class, you can't be sure that the result be be cast to your specific home interface.

    You can do:
    ejbHome = (EJBHome)PortableRemoteObject.narrow(objref, EJBHome.class);
    Class ejbHomeInterface = ejbHome.getEJBMetaData().getHomeInterfaceClass();
    EJBHome narrowEJBHome = (EJBHome)PortableRemoteObject.narrow(objref, ejbHomeInterface);

  3. what about the different create and finder methods..they will be different for different beans...right...????
  4. Probably.

    Could we use reflection?
    Something like:

    Class c = narrowEJBHome.getClass();
    Method create = c.getMethod("create");

    In my case, I need some method that returns a created bean instance based only on the jndi name of the bean (or perhaps the class name of the home interface and bean class).
    Any ideas?