Other Enterprise Bean Features

The topics that follow apply to session beans 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 a development tool to assign Discount Percent a value of 0.05 in the bean's deployment descriptor. When you run the application, the bean fetches the 0.05 value from its environment.

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 using 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 0.05 to the Discount Percent entry, the code will assign 0.05 to the discountPercent variable. The applyDiscount method, which follows, is in the CheckerBean class. The source code for this example is in <INSTALL>/j2eetutorial14/examples/ejb/checker.

public double applyDiscount(double amount) {

  try {

    double discount;

    Context initial = new InitialContext();
      Context environment = 

    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: "+

Comparing Enterprise Beans

A client can determine whether 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 whether 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)

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 can 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) {