EJB Environment

Almost every program requires a means for users to override configuration parameter values. Standalone Java programs typically use Java system properties, and these values can be changed by specifying -D options to the Java virtual machine. Likewise, EJB components can use the EJB environment to read configuration parameters whose values may be adjusted by the deployer. The EJB environment enables the bean code to refer to the configuration parameters with logical names that can then be assigned values during deployment. Because the EJB code only refers to the logical name, the values may be changed without touching the bean classes.

Declaring Environment Variables

EJB environment values are declared in the ejb-jar.xml deployment descriptor as name-value pairs. The values may be any of the Java wrapper types: string, character, integer, boolean, double, byte, short, long, or float. User-defined types may not be used as EJB environment values. The environment value is declared with an env-entry tag that consists of an optional description, the logical name, the type, and a value. For instance, an EJB might use a configuration parameter named maxWidgets. This can be declared as an EJB environment variable and assigned the value 100783 with the following entry in the ejb-jar.xml:

<env-entry>
  <description>
      Maximum number of widgets that our EJB accepts
  </description>
  <env-entry-name>maxWidgets</env-entry-name>
  <env-entry-type>java.lang.Integer</env-entry-type>
  <env-entry-value>100783</env-entry-value>
</env-entry>

The EJB environment values declared in the EJB deployment descriptor are made available through JNDI to the EJB code. When an EJB is deployed, the container creates a JNDI context at java:/comp/env that includes the EJB environment values. To obtain the value associated with a logical name, the bean writer makes a JNDI lookup for java:/comp/env/ followed by the env-entry-name declared in the EJB deployment descriptor. For instance, the writer of the widget bean could use the following code to find the value of maxWidgets at runtime:

Context ctx = new InitialContext();

Integer maxWidgets = (Integer)

  ctx.lookup("java:/comp/env/maxWidgets");

EJB environment variables are a convenient and standard method to store and retrieve configuration parameters from the bean class. It is important to note that these values are read-only. The bean code cannot use JNDI to bind in additional environment variables at runtime or modify the existing value for an environment entry. In addition, the bean writer should be aware that environment entries are local to the EJB deployment. They're not visible to other EJBs or components within the server. Also, there are no means for a client to look up these values. This is required since every EJB uses the java:/comp/env context to read its environment variables. In the WebLogic Server, environment variables are implemented within the EJB server by attaching the bean's java:/comp/env context immediately before a method on the bean class is called and removing the java:/comp/env context when the bean method returns.

Environment entries are read-only and local to the EJB.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.156.202