Home TOC |
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 assign "Discount Percent" a value of .05 on the Environment tabbed pane of the deploytool. When you run the application, the enterprise bean fetches the .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 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 the deploytool, the code will assign .05 to the discountPercent variable. The applyDiscount method, which follows, is in the CheckerEJB class. The source code for this example is in examples/src/ejb/checker.
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 the bean is not a remote object. Instead, the bean must pass an object reference for the instance. A session bean gets the reference to itself 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 WagonEJB implements SessionBean { SessionContext context; . . . public void setSessionContext(SessionContext sc) { this.context = sc; } . . . public void passItOn(Basket basket) { . . . basket.copyItems(context.getEJBObject()); } . . .
Home TOC |
Search
Feedback |