web server - Borland Technical Publications
web server - Borland Technical Publications web server - Borland Technical Publications
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
- Page 45 and 46: Borland web container implementatio
- Page 47 and 48: Borland web container implementatio
- Page 49 and 50: Smart Agent implementation Smart Ag
- Page 51 and 52: 40 BES Developer’s Guide
- Page 53 and 54: Apache web server to Borland web co
- Page 55 and 56: Apache web server to Borland web co
- Page 57 and 58: Apache web server to Borland web co
- Page 59 and 60: Large data transfer Downloading lar
- Page 61 and 62: Large data transfer Uploading large
- Page 63 and 64: IIS web server to Borland web conta
- Page 65 and 66: IIS web server to Borland web conta
- Page 67 and 68: IIS web server to Borland web conta
- Page 69 and 70: Session management with JSS If an i
- Page 71 and 72: Managing and configuring the JSS Co
- Page 73 and 74: The Borland IIOP connector BES supp
- Page 75 and 76: Setting up your web container with
- Page 77 and 78: 66 BES Developer’s Guide
- Page 79 and 80: Web-enabling your CORBA server Impo
- Page 81 and 82: Configuring your Apache web server
- Page 83 and 84: Configuring your Apache web server
- Page 85 and 86: Web Services and Partitions ■ ■
- Page 87 and 88: Web Service providers Java:RPC prov
- Page 89 and 90: How Borland Web Services work
- Page 91 and 92: Packaging Web Service Application A
- Page 93 and 94: Tools Overview Java2WSDL tool Note
- Page 95: 84 BES Developer’s Guide
- Page 99 and 100: Client view of an enterprise bean E
- Page 101 and 102: Managing transactions Managing tran
- Page 103 and 104: Support for JNDI Support for JNDI T
- Page 105 and 106: EJB to CORBA mapping A CORBA progra
- Page 107 and 108: 96 BES Developer’s Guide
- Page 109 and 110: Application Client architecture Pac
- Page 111 and 112: Document Type Definitions (DTDs) my
- Page 113 and 114: Support of references and links The
- Page 115 and 116: Use of Manifest files Use of Manife
- Page 117 and 118: 106 BES Developer’s Guide
- Page 119 and 120: Sessions in secondary storage If yo
- Page 121 and 122: 110 BES Developer’s Guide
- Page 123 and 124: Container-managed persistence and R
- Page 125 and 126: Implementing an entity bean Generat
- Page 127 and 128: Container-Managed Persistence in Bo
- Page 129 and 130: Container-Managed Persistence in Bo
- Page 131 and 132: Setting Properties Setting Properti
- Page 133 and 134: Setting Properties into a BLOB. The
- Page 135 and 136: Setting Properties Automatic table
- Page 137 and 138: 126 BES Developer’s Guide
- Page 139 and 140: Container-managed persistence and R
- Page 141 and 142: Container-Managed Persistence in Bo
- Page 143 and 144: Container-Managed Persistence in Bo
- Page 145 and 146: Container-Managed Persistence in Bo
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