Locating EJB Objects

   

Up to this point, we have stayed away from talking specifically about how JNDI and EJB are used together. This was done intentionally so that you can have time to understand the JNDI concepts without being clouded by the EJB aspects of it. However, it's now appropriate to introduce how a client uses JNDI to locate enterprise beans. Even though we haven't fully discussed the details of enterprise beans, the procedure is pretty much the same regardless of whether it's an entity or session bean.

Note

Remember, the message-driven bean is not exposed to the client. So, you will not be able to perform a lookup and obtain a reference to a message-driven bean.


You learned in Chapter 3, "EJB Concepts," that each enterprise bean that is exposed to a client has a home interface. If you remember, the home interface, or actually the object that implements the home interface, acts as a factory for the enterprise bean that it is associated with. A client locates the factory object through JNDI just as it would locate any other object in the JNDI namespace.

The EJB container is responsible for making the home factories for the enterprise beans that are deployed in the container available to clients through JNDI. This is normally done when the EJB container starts up. The client must get an InitialContext to the JNDI service and then locate the home interface reference for an enterprise bean. Once a client has the home interface, it can invoke methods on the home and obtain remote references to the enterprise bean.

The Context interface defines two different lookup methods that can be used to look up objects within the JNDI service. One of the methods takes a javax.naming. Name reference and the other takes a string. Typically, you'll see the string version in EJB applications just because it's easier to use. The string argument to the lookup method is the name of the binding that you want a reference to. If there is no binding with that name, a javax.nam ing.NameNotFoundException is thrown.

Narrowing the Home Interface

Because the lookup method on the Context interface returns an Object , client applications must narrow and then cast the instance to the correct type. In earlier versions of EJB, only a Java cast operation had to be performed. However, starting with EJB 1.1, a client should use the static narrow method located on the javax.rmi.PortableRemoteObject class. This helps to ensure portability with other containers that are using RMI-IIOP as the underlying communication transport. The following code fragment illustrates how to narrow the Object returned from a lookup:

 Context initCtx = new InitialContext();  Object obj = initCtx.lookup( "java:comp/env/EnglishAuctionHome" );  EnglishAuctionHome home =    (EnglishAuctionHome)javax.rmi.PortableRemoteObject.narrow( obj,                                            EnglishAuctionHome.class ); 

All objects returned from a lookup should be narrowed before using the Java cast operator.



Special Edition Using Enterprise JavaBeans 2.0
Special Edition Using Enterprise JavaBeans 2.0
ISBN: 0789725673
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net