EJB Interfaces

Each session or entity bean has two interfaces (home and component) and a bean class. A message-driven bean has only a bean class and defines no interfaces.

In the following sections, you'll learn the fundamentals of the home interface, component interface, and bean class.

The Home Interface

Clients use the home interface to create, remove, and find Enterprise JavaBean instances. You can think of the home object as the Enterprise JavaBean factory; you write the interface, the container tools generate the home class that corresponds to it, and the home interface defines the creation methods for the Enterprise JavaBean.

For example, the home interface EnrollmentCartHome is defined as follows:

public interface EnrollmentCartHome extends EJBHome {
  EnrollmentCart create() throws CreateException, RemoteException;
}

Therefore, to create an EnrollmentCart bean instance, you call the create() method with no parameters.

The Component Interface

The component interface exposes an Enterprise JavaBean's business methods to a client. The client calls the methods defined in the component interface to invoke the business logic implemented by the bean. You can think of the component object as a proxy to the Enterprise JavaBean instance. You write this interface. The container tools generate the component class corresponding to this interface.

The component interface EnrollmentCart is defined as follows:

public interface EnrollmentCart extends EJBObject {
   public void addCourses(String[] courseIds) throws RemoteException;
   public Collection getCourses() throws RemoteException;
   public void empty() throws RemoteException;
 }

Therefore, to add courses to the enrollment cart, you call the addCourses method on the component interface and pass the proper parameters.

Note

The component interface has only the methods that are callable by the client. The Enterprise JavaBean may have other methods in it, but if they aren't listed in the component interface, clients cannot call them.


When compiling both the home and component interfaces, a class may be generated for each interface by using a vendor-specific EJB compiler.

Caution

You cannot assume that any particular implementation of classes will be generated by the container tools. It is highly vendor-specific. The container might or might not generate a class corresponding to each interface.


The Enterprise JavaBean Class

The Enterprise JavaBean class is where you implement the business logic defined in the component interface. Session beans implement the javax.ejb.SessionBean interface, whereas entity beans implement the javax.ejb.EntityBean interface, and message-driven beans implement the javax.ejb.MessageDrivenBean interface.

For example, the EnrollmentCartEJB session bean is implemented as follows:

public class EnrollmentCartEJB implements javax.ejb.SessionBean  {
      private SessionContext ctx;
      private HashSet cart;
      /*  callback methods  */
      public void setSessionContext(SessionContext ctx) {
         this.ctx = ctx;
      }
      public void ejbCreate() throws CreateException {
         cart = new HashSet();
      }
      public void ejbActivate() {}
      public void ejbPassivate() {}
      public void ejbRemove() {}

     /* Here you implement all business methods
         as defined in the component interface...
     */
     public void addCourses(String[] courseIds) {
         if (courseIds == null) {
            return;
         }
         for (int i = 0; i < courseIds.length ; i ++ ) {
            cart.add(courseIds[i]);
         }
      }
      public Collection getCourses() {
         return cart;
      }
      public void empty() {
         cart.clear();
      }
}

First, the EnrollmentCartBean implements all the business methods it advertised in its interfaces. Because it is a session bean, EnrollmentCartBean implements javax.ejb.SessionBean. The javax.ejb.SessionBean interface provides few callback methods to be implemented by any session bean. In the preceding example, the EnrollmentCartEJB implements the callback methods setSessionContext() and ejbCreate(). The EJB container calls these callback methods to perform some of its functionality and also to notify the instance of important events. Clients will never call such methods.

Note

The Enterprise JavaBean class implements neither the home interface nor the component interface. This is often a source of confusion for new developers.


Note

An Enterprise JavaBean may include other classes, or even other packages, but the classes listed earlier are the minimum.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.253.223