web server - Borland Technical Publications

web server - Borland Technical Publications web server - Borland Technical Publications

techpubs.borland.com
from techpubs.borland.com More from this publisher
12.11.2014 Views

Chapter 11 11Writing enterprise bean clients Chapter Important For documentation updates, go to www.borland.com/techpubs/bes. Client view of an enterprise bean A client of an enterprise bean is an application, a stand-alone application, an application client container, servlet, or applet--or another enterprise bean. In all cases, the client must do the following things to use an enterprise bean: ■ ■ ■ Locate the bean's home interface. The EJB specification states that the client should use the JNDI (Java Naming and Directory Interface) API to locate home interfaces. Obtain a reference to an enterprise bean object's remote interface. This involves using methods defined on the bean's home interface. You can either create a session bean, or you can create or find an entity bean. Invoke one or more methods defined by the enterprise bean. A client does not directly invoke the methods defined by the enterprise bean. Instead, the client invokes the methods on the enterprise bean object's remote interface. The methods defined in the remote interface are the methods that the enterprise bean has exposed to clients. 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). Chapter 11: Writing enterprise bean clients 85

Client view of an enterprise bean 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 85 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(); ... //do the sort and merge work sort.remove(); } } The main() routine of the client program throws the generic exception Exception. When coded this way, the SortClient program does not have to catch any exceptions that might occur, though if an exception occurs it will terminate the program. Obtaining the remote interface Now that we have obtained the home interface of an enterprise bean we can get a reference to the enterprise bean's remote interface. To do this, we use the home interface's create or finder methods. The exact method to invoke depends on the type of the enterprise bean and the methods the enterprise bean provider has defined in the home interface. For example, the first code sample shows how SortClient obtains a reference to the Sort remote interface. Once SortClient obtains the reference to the home interface and casts it to its proper type (SortHome), then the code can create an instance of the bean and call its methods. It calls the home interface's create() method, which returns a reference to the bean's remote interface, Sort. (Because SortBean is a stateless session bean, its home interface has only one create() method and that method by definition takes no parameters.) SortClient can then call the methods defined on the 86 BES Developer’s Guide

Client view of an enterprise bean<br />

Locating the home interface<br />

A client locates an enterprise bean's home interface using JNDI, as shown in the code<br />

sample below. The client first needs to obtain a JNDI initial naming context. The code<br />

instantiates a new javax.naming.Context object, which in our example it calls<br />

initialContext. Then, the client uses the context lookup() method to resolve the name to<br />

a home interface. Note that the initialization of the initial naming context factory is EJB<br />

container/<strong>server</strong> specific.<br />

A client application can also use logical names to access a resource such as the home<br />

interface. Go to “Initializing the client” on page 85 for more information.<br />

The context's lookup() method returns an object of type java.lang.Object. Your code<br />

must cast this returned object to the expected type. The following code sample shows<br />

a portion of the client code for the sort example. The main() routine begins by using the<br />

JNDI naming service and its context lookup method to locate the home interface. You<br />

pass the name of the remote interface, which in this case is sort, to the<br />

context.lookup() method. Notice that the program eventually casts the results of the<br />

context.lookup() method to SortHome, the type of the home interface.<br />

// SortClient.java<br />

import javax.naming.InitialContext;<br />

import SortHome; // import the bean's home interface<br />

import Sort; // import the bean's remote interface<br />

public class SortClient {<br />

...<br />

public static void main(String[] args) throws Exception {<br />

javax.naming.Context context;<br />

// preferred JNDI context lookup<br />

// get a JNDI context using a logical JNDI name in the local JNDI<br />

context, i.e.,ejb-ref<br />

javax.naming.Context context = new javax.naming.InitialContext();<br />

Object ref = context.lookup("java:comp/env/ejb/Sort");<br />

SortHome home = (SortHome) javax.rmi.PortableRemoteObject.narrow<br />

(ref, SortHome.class);<br />

Sort sort = home.create();<br />

... //do the sort and merge work<br />

sort.remove();<br />

}<br />

}<br />

The main() routine of the client program throws the generic exception Exception. When<br />

coded this way, the SortClient program does not have to catch any exceptions that<br />

might occur, though if an exception occurs it will terminate the program.<br />

Obtaining the remote interface<br />

Now that we have obtained the home interface of an enterprise bean we can get a<br />

reference to the enterprise bean's remote interface. To do this, we use the home<br />

interface's create or finder methods. The exact method to invoke depends on the type<br />

of the enterprise bean and the methods the enterprise bean provider has defined in the<br />

home interface.<br />

For example, the first code sample shows how SortClient obtains a reference to the<br />

Sort remote interface. Once SortClient obtains the reference to the home interface and<br />

casts it to its proper type (SortHome), then the code can create an instance of the bean<br />

and call its methods. It calls the home interface's create() method, which returns a<br />

reference to the bean's remote interface, Sort. (Because SortBean is a stateless<br />

session bean, its home interface has only one create() method and that method by<br />

definition takes no parameters.) SortClient can then call the methods defined on the<br />

86 BES Developer’s Guide

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!