Download
FAQ History |
API
Search Feedback |
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 assignDiscount
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 invokinglookup
using thejava:comp/env
parameter. Then it callslookup
on the environment to get the values for theDiscount
Level
andDiscount
Percent
names. For example, if you assign a value of 0.05 to theDiscount
Percent
entry, the code will assign 0.05 to thediscountPercent
variable. TheapplyDiscount
method, which follows, is in theCheckerBean
class. The source code for this example is in<
J2EE_HOME
>/j2eetutorial14/examples/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 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 returnstrue
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) 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 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 theSessionContext
interface. An entity bean would call thegetEJBObject
method of theEntityContext
interface. These interfaces provide beans with access to the instance contexts maintained by the EJB container. Typically, the bean saves the context in thesetSessionContext
method. The following code fragment shows how a session bean might use these methods.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.