user manual
102 BES Developer’s Guide
Initializing the client
The SortClient application imports the necessary JNDI classes and the
SortBean home and remote interfaces. The client uses the JNDI API to locate
an enterprise bean's home interface.
A client application can also use logical names (as recommended in the
various J2EE specifications) to access resources such as database
connections, remote enterprise beans, and environment variables. The
container, per the J2EE specification, exposes these resources as
administered objects in the local JNDI name space (that is, java:comp/env).
Locating the home interface
A client locates an enterprise bean's home interface using JNDI, as shown in
the code sample below. The client first needs to obtain a JNDI initial naming
context. The code instantiates a new javax.naming.Context object, which in our
example it calls initialContext. Then, the client uses the context lookup()
method to resolve the name to a home interface. Note that the initialization of
the initial naming context factory is EJB container/server specific.
A client application can also use logical names to access a resource such as
the home interface. Go to “Initializing the client” on page 102 for more
information.
The context's lookup() method returns an object of type java.lang.Object. Your
code must cast this returned object to the expected type. The following code
sample shows a portion of the client code for the sort example. The main()
routine begins by using the JNDI naming service and its context lookup
method to locate the home interface. You pass the name of the remote
interface, which in this case is sort, to the context.lookup() method. Notice that
the program eventually casts the results of the context.lookup() method to
SortHome, the type of the home interface.
// SortClient.java
import javax.naming.InitialContext;
import SortHome; // import the bean's home interface
import Sort; // import the bean's remote interface
public class SortClient {
...
public static void main(String[] args) throws Exception {
javax.naming.Context context;
// preferred JNDI context lookup
// get a JNDI context using a logical JNDI name in the local JNDI
context, i.e.,ejb-ref
javax.naming.Context context = new javax.naming.InitialContext();
Object ref = context.lookup("java:comp/env/ejb/Sort");
SortHome home = (SortHome) javax.rmi.PortableRemoteObject.narrow
(ref, SortHome.class);
Sort sort = home.create();










