The J2EETM Tutorial
Home
TOC
Index
PREV TOP NEXT Search
Feedback

Other Enterprise Bean Features

The topics that follow apply to both session and entity beans.

Accessing Environment Entries

Stored in an enterprise bean's deployment descriptor, an environment entry is a name-value pair that allows you to customize the bean's business logic without changing its source code. An enterprise bean that calculates discounts, for example, might have an environment entry named Discount Percent. Before deploying the bean's application, you could run deploytool and assign Discount Percent a value of .05 on the Env. Entries tab. (See Figure 4-2.) When you run the application, the enterprise bean fetches the .05 value from its environment.

Figure 4-2 Env. Entries Tab of CheckerBean

In the following code example, the applyDiscount method uses environment entries to calculate a discount based on the purchase amount. First, the method locates the environment naming context by invoking lookup with the java:comp/env parameter. Then it calls lookup on the environment to get the values for the Discount Level and Discount Percent names. For example, if you assign a value of .05 to the Discount Percent name in deploytool, the code will assign .05 to the discountPercent variable. The applyDiscount method, which follows, is in the CheckerBean class. The source code for this example is in j2eetotorial/examples/src/ejb/checker. A sample CheckerApp.ear file is in the j2eetutorial/examples/ears directory.

public double applyDiscount(double amount) {	
	
      try {	
	
         double discount;	
   	
         Context initial = new InitialContext();	
         Context environment = 	
           (Context)initial.lookup("java:comp/env");	
   	
         Double discountLevel = 	
            (Double)environment.lookup("Discount Level");	
         Double discountPercent = 	
            (Double)environment.lookup("Discount Percent");	
   	
         if (amount >= discountLevel.doubleValue()) {	
            discount = discountPercent.doubleValue();	
         }	
         else {	
            discount = 0.00;	
         }	
    	
         return amount * (1.00 - discount);	
	
      } catch (NamingException ex) {	
           throw new EJBException("NamingException: "+	
              ex.getMessage());	
      }	
   }
 

Comparing Enterprise Beans

A client can determine if two stateful session beans are identical by invoking the isIdentical method:

bookCart = home.create("Bill Shakespeare"); 	
videoCart = home.create("Lefty Lee");	
...	
if (bookCart.isIdentical(bookCart)) { 	
   // true ... }	
if (bookCart.isIdentical(videoCart)) { 	
   // false ... }
 

Because stateless session beans have the same object identity, the isIdentical method always returns true when used to compare them.

To determine if two entity beans are identical, the client can invoke the isIdentical method, or it can fetch and compare the beans's primary keys:

String key1 = (String)accta.getPrimaryKey();	
String key2 = (String)acctb.getPrimaryKey();	
	
if (key1.compareTo(key2) == 0)	
   System.out.println("equal");
 

Passing an Enterprise Bean's Object Reference

Suppose that your enterprise bean needs to pass a reference to itself to another bean. You might want to pass the reference, for example, so that the second bean can call the first bean's methods. You can't pass the this reference because it points to the bean's instance, which is running in the EJB container. Only the container may directly invoke methods on the bean's instance. Clients access the instance indirectly by invoking methods on the object whose type is the bean's remote interface. It is the reference to this object (the bean's remote reference) that the first bean would pass to the second bean.

A session bean obtains its remote reference by calling the getEJBObject method of the SessionContext interface. An entity bean would call the getEJBObject method of the EntityContext interface. These interfaces provide beans with access to the instance contexts maintained by the EJB container. Typically, the bean saves the context in the setSessionContext method. The following code fragment shows how a session bean might use these methods.

public class WagonBean implements SessionBean {	
   	
   SessionContext context;	
   ...	
   public void setSessionContext(SessionContext sc) { 	
      this.context = sc; 	
   }	
   ...	
   public void passItOn(Basket basket) {	
   ...	
      basket.copyItems(context.getEJBObject()); 	
   } 	
   ...
 
Home
TOC
Index
PREV TOP NEXT Search
Feedback