| CONTENTS | PREV | NEXT | INDEX | J2EE BluePrints |
As we discussed in the previous section, business objects need to provide some generic services to clients, such as support for transactions, security, and remote access, These common services are very complex in nature and are outside the domain of the business logic required to implement an application. To simplify development, enterprise applications need a standard server-side infrastructure that can provide such services.
The EJB tier of the J2EE platform provides a standard server-side distributed component model that greatly simplifies the task of writing business logic. In the EJB architecture, system experts provide the framework for delivering system-level services and application domain experts provide the components that hold only business-specific knowledge. The J2EE platform enables enterprise developers to concentrate on solving the problems of the enterprise instead of struggling with system-level issues.
To use the services provided by the J2EE platform, business objects are implemented by EJB components, or enterprise beans. There are two primary kinds of enterprise beans: entity beans and session beans. Session beans are intended to be private resources used only by the client that created them. For this reason, session beans, from the client's perspective, appear anonymous. In contrast, every entity bean has a unique identity which is exposed as a primary key. Later sections in this chapter discuss each type of enterprise bean in detail.
In addition to components, the EJB architecture defines three other entities: servers, containers, and clients. Enterprise beans live inside EJB containers, which provide life cycle management and a variety of other services. An EJB container is part of an EJB server, which provides naming and directory services, email services, and so on. When a client invokes an operation on an enterprise bean the call is intercepted by its container. By interceding between clients and components at the method call level, containers can manage services that propagate across calls and components, and even across containers running on different servers and different machines. This mechanism simplifies development of both components and clients.
The EJB architecture endows enterprise beans and EJB containers with a number of unique features that enable portability and reusability:
- Enterprise bean instances are created and managed at runtime by a container. If an enterprise bean uses only the services defined by the EJB specification, the enterprise bean can be deployed in any compliant EJB container. Specialized containers can provide additional services beyond those defined by the EJB specification. An enterprise bean that depends on such a service can be deployed only in a container that supports that service.
- The behavior of enterprise beans is not wholly contained in its implementation. Service information, including transaction (described in Chapter 8) and security (described in Chapter 9) information, is separate from the enterprise bean implementation. This allows the service information to be customized during application assembly and deployment. The behavior of an enterprise bean is customized at deployment time by editing its deployment descriptor entries (described in Chapter 7). This makes it possible to include an enterprise bean in an assembled application without requiring source code changes or recompilation.
- The Bean Provider defines a client view of an enterprise bean. The client view is unaffected by the container and server in which the bean is deployed. This ensures that both the beans and their clients can be deployed in multiple execution environments without changes or recompilation. The client view of an enterprise bean is provided through two interfaces. These interfaces are implemented by classes constructed by the container when a bean is deployed, based on information provided by the bean. It is by implementing these interfaces that the container can intercede in client operations on a bean and offer the client a simplified view of the component. The following sections describe these interfaces and classes: the home and remote interfaces, and enterprise bean class.
5.2.1.1 Home Interface
The home interface provides methods for creating and removing enterprise beans. This interface must extend javax.EJB.EJBHome. The enterprise bean's home interface allows a client to do the following:
- Create new enterprise bean instance
- Remove an enterprise bean instance
- Get the meta-data for the enterprise bean through the
javax.ejb.EJBMetaDatainterface. Thejavax.ejb.EJBMetaDatainterface is provided to allow application assembly tools to discover the meta-data information about the enterprise bean at deployment time.- Obtain a handle to the home interface, which provides the mechanism for persistent enterprise beans. The home handle can be serialized and written to stable storage. Later, possibly in a different Java virtual machine, the handle can be deserialized from stable storage and used to obtain a reference to the home interface.
In addition, the home interface of an entity bean provides methods for finding existing entity bean instances within the home. A client that knows the primary key of an entity object can obtain a reference to the entity object by invoking the findByPrimaryKey method on the entity bean's home interface.
5.2.1.2 Remote Interface
The remote interface defines the client view of an enterprise bean--the set of business methods available to the clients. This interface must extend javax.ejb.EJBObject. An EJBObject supports:
The javax.ejb.EJBObject interface defines the methods that allow clients to perform the following operations on a reference to an enterprise bean instance:
5.2.1.3 Enterprise Bean Class
The enterprise bean class is the second part of the mechanism that allows for container-managed services in the EJB architecture. It provides the actual implementation of the business methods of the bean. It is called by the container when the client calls the corresponding methods listed in the remote interface. This class must implement the javax.ejb.EntityBean or javax.ejb.SessionBean interface.
In addition to business methods, the remote interface and enterprise bean class also share responsibility for two specialized categories of methods: create methods and finder methods. The create methods provide ways to customize the bean at the time it is created, and the finder methods provide ways to locate a bean.
For each create method listed in the home interface, the bean class implements the corresponding ejbCreate method. For each finder method listed in home interface, the bean class provides the corresponding ejbFindBy... method. The enterprise bean class must also provide implementations of the methods listed in the interface it extends. A developer can choose to provide empty implementations of any methods in the interface that aren't required for the specific purposes of a bean.
Figure 5.1 illustrates the implementation of the client view of an enterprise bean.
Figure 5.1 Implementation of Client View of Enterprise Beans
The following two sections contain in-depth discussions of the properties and uses of entity and session beans.