Local Interfaces, Remote Interfaces, and the Client View
A local client is one that is located in the same Java Virtual Machine as its component and uses a local
interface. A remote client uses the remote interface of the component. The two different types of clients
have different privileges for accessing details of the object model.
A local interface may use other local interfaces as parameters and return values, and may also use the
collection classes that represent multi-valued relationships as parameters and return values. A remote
interface must not ever use a local interface or relationship collection as a parameter or a return value.
The reason for this rule is easily understood: a remote client may not be using the same Java Virtual
Machine, but these classes are dependent on a particular virtual machine for their correct execution.
The client view of an EJB component is defined by the methods in the EJB's home and remote interface.
This view is independent of the EJB's implementation and specifically, independent of the EJB's object
model. From the point of view of a client, local interfaces and relationships are just an irrelevant
implementation detail. All the client of an EJB cares about are the remote methods that it can invoke.
For instance, let's consider the example of an entity bean representing an order. This entity bean might
have the following remote interface:
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Order extends EJBObject {
public Long getOrderId() throws RemoteException;
public String getCustomerName() throws RemoteException;
public String getStreetAddress() throws RemoteException;
public String getCity() throws RemoteException;
public String getState() throws RemoteException;
public String getZip() throws RemoteException;
}
The implementation class for this EJB (perhaps named OrderEJB) might have state variables
representing the customer's street address, city, state, and zip code. On the other hand, it might have a
one-to-one relationship with a local address EJB. It might have a regular Java object that gets serialized
into a database column. It might even have a one-to-many relationship with local address EJBs, and use
an algorithm to select an appropriate shipping address for an order based on the order's manufacturing
facility. From the client's point of view, it doesn't matter. The same street, city, state, and zip code
strings are returned through the public interface.
In fact, it is illegal to return a local component interface or a relationship collection class from a remote
interface method in an entity. You could not simply write the above interface as follows (assuming
Address is a local component interface):
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Order extends EJBObject {
public Long getOrderId() throws RemoteException;
public Address getAddress() throws RemoteException;
}
The local address remote interface cannot be the return value in a remote method invocation.
Transferring State to and from the Client
Even though it is illegal to return a local interface or a relationship collection to the client, it is still often
necessary to transfer a view of the entity's state to the client, or to apply an update of the entity's state
from the client. A common pattern is to use a Java class to represent the state or updates to be
transferred. This class might be generic (a custom java.sql.ResultSet implementation) or specific
to the state that needs to be transported (an AddressView or LineItemView class).
For instance, the interface for the order bean might be defined as follows:
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
import javax.ejb.CreateException;
public interface Order extends EJBObject {
public Long getOrderId() throws RemoteException;
public LineItemView[] getLineItemViews() throws RemoteException;
public void addLineItem(LineItemView liv)
throws RemoteException, CreateException;
public void updateLineItem(LineItemView liv) throws RemoteException;
public void removeLineItem(long lineItemID) throws RemoteException;
}
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|