Accessing an EJB s Environment

   

Accessing an EJB's Environment

One of the questions that new EJB developers are always asking is how to provide configuration properties and values to an enterprise bean. Because enterprise beans are not supposed to access the file system, how does an enterprise bean get access to general configuration properties?

When we say configuration properties, we're talking about properties that help enforce business rules. An example might be the maximum number of auctions a user can participate in. There are many different ways to give this data to the enterprise that needs it. We could hard-code it inside the bean, but that's not a very flexible solution. We could store it into a database, but in some cases that's overkill and too much trouble. Another way is to put it into the enterprise bean's deployment descriptor so that it can be modified without recompiling. When we add it to the deployment descriptor, it becomes available to the enterprise bean through the environment for the bean.

To add it to the deployment descriptor, we add an env-entry tag like this:

 <env-entry>    <description>Max number of auctions a user can bid on.</description>    <env-entry-name>maxAuctionsParticipation</env-entry-name>    <env-entry-type>java.lang.Integer</env-entry-type>    <env-entry-value>10</env-entry-value>  </env-entry> 

An enterprise bean that declared this environment entry in its deployment descriptor could use this value to limit the number of auctions in which a user could participate. This tag would be placed under a specific enterprise bean in the deployment descriptor. An enterprise bean gets access to its declared environment properties by using an InitialContext object and looking up the environment property by its name.

Note

This environment entry is available only to the enterprise bean for which it's defined. It's not available to other enterprise beans. The same entry name can be defined in other deployment descriptors without causing a conflict.


The following code fragment illustrates how an enterprise bean method might locate and use the environment entry:

 public void submitBid( User user, EnglishAuction auction, Bid aBid ) {   // Get the bean's environment naming context    Context initCtx = new InitialContext();    Context myCtx = (Context)initCtx.lookup( "java:comp/env" );    // Get the environment property for the max number of auctions    Integer maxAuctionParticipate =          (Integer)myCtx.lookup("maxAuctionsParticipation")    if ( user.auctionsAlreadyIn < maxAuctionsParticipate.intValue() ) {     // Allow the bid to go through    }else{     // Inform the user that they are already in too many auctions    }  } 

Using the enterprise bean's environment entries is a great way to provide customizable business rules when the rules can be expressed in a set of properties. The entry types can be one of the following Java types:

  • String

  • Integer

  • Boolean

  • Double

  • Byte

  • Short

  • Long

  • Float

  • Character



Special Edition Using Enterprise JavaBeans 2.0
Special Edition Using Enterprise JavaBeans 2.0
ISBN: 0789725673
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net