Home > Articles > Programming > Java

📄 Contents

  1. An Overview of JNDI
  2. J2EE and JNDI—The Application Component Environment
  3. The JBossNS Architecture
  4. Additional Naming MBeans
  5. Summary
  • Print
  • + Share This
This chapter is from the book

J2EE and JNDI—The Application Component Environment

JNDI is a fundamental aspect of the J2EE specifications. One key usage is the isolation of J2EE component code from the environment in which the code is deployed. Use of the application component's environment allows the application component to be customized without the need to access or change the application component's source code. The application component environment is sometimes referred to as the enterprise naming context (ENC). It is the responsibility of the application component container to make an ENC available to the container components in the form of JNDI Context. The ENC is utilized by the participants involved in the life cycle of a J2EE component in the following ways:

  • Application component business logic should be coded to access information from its ENC. The component provider uses the standard deployment descriptor for the component to specify the required ENC entries. The entries are declarations of the information and resources the component requires at runtime.

  • The container provides tools that allow a deployer of a component to map the ENC references made by the component developer to the deployment environment entity that satisfies the reference.

  • The component deployer utilizes the container tools to ready a component for final deployment.

  • The component container uses the deployment package information to build the complete component ENC at runtime.

NOTE

The complete specification regarding the use of JNDI in the J2EE platform can be found in Section 5 of the J2EE 1.3 specification. The J2EE specification is available at http://java.sun.com/j2ee/download.html.

An application component instance locates the ENC using the JNDI API. An application component instance creates a javax.naming.InitialContext object by using the no argument constructor and then looks up the naming environment under the name java:comp/env. The application component's environment entries are stored directly in the ENC, or in its subcontexts. Listing 3.2 illustrates the prototypical lines of code a component uses to access its ENC.

Listing 3.2 ENC Access Sample Code

// Obtain the application component's ENC
Context iniCtx = new InitialContext();
Context compEnv = (Context) iniCtx.lookup("java:comp/env");

An application component environment is a local environment that is accessible only by the component when the application server container thread of control is interacting with the application component. This means that an EJB Bean1 cannot access the ENC elements of EJB Bean2, and visa-versa. Similarly, Web application Web1 cannot access the ENC elements of Web application Web2 or Bean1 or Bean2 for that matter. Also, arbitrary client code, whether it is executing inside of the application server VM or externally cannot access a component's java:comp JNDI context. The purpose of the ENC is to provide an isolated, read-only namespace that the application component can rely on regardless of the type of environment in which the component is deployed. The ENC must be isolated from other components because each component defines its own ENC content, and components A and B may define the same name to refer to different objects. For example, EJB Bean1 may define an environment entry java:comp/env/red to refer to the hexadecimal value for the RGB color for red, while Web application Web1 may bind the same name to the deployment environment language locale representation of red.

NOTE

There are three levels of naming scope in the JBossNS implementation—names under java:comp, names under java:, and any other name. As discussed, the java:comp context and its subcontexts are only available to the application component associated with the java:comp context. Subcontexts and object bindings directly under java: are only visible within the JBoss server virtual machine. Any other context or object binding is available to remote clients, provided the context or object supports serialization. You'll see how the isolation of these naming scopes is achieved in the section "The JBossNS Architecture" later in this chapter.

An example of where the restricting a binding to the java: context is useful would be a javax.sql.DataSource connection factory that can only be used inside of the JBoss VM where the associated database pool resides. An example of a globally visible name that should accessible by remote client is an EJB home interface.

ENC Usage Conventions

JNDI is used as the API for externalizing a great deal of information from an application component. The JNDI name that the application component uses to access the information is declared in the standard ejb-jar.xml deployment descriptor for EJB components, and the standard web.xml deployment descriptor for Web components. Several different types of information may be stored in and retrieved from JNDI including the following:

  • Environment entries as declared by the env-entry elements

  • EJB references as declared by ejb-ref and ejb-local-ref elements

  • Resource manager connection factory references as declared by the resource-ref elements

  • Resource environment references as declared by the resource-env-ref elements

Each type of deployment descriptor element has a JNDI usage convention with regard to the name of the JNDI context under which the information is bound. Also, in addition to the standard deployment descriptor element, there is a JBoss server specific deployment descriptor element that maps the JNDI name as used by the application component to the deployment environment JNDI name.

The ejb-jar.xml ENC Elements

The EJB 2.0 deployment descriptor describes a collection of EJB components and their environment. Each of the three types of EJB components—session, entity, and message-driven—support the specification of an EJB local naming context. The ejb-jar.xml description is a logical view of the environment that the EJB needs to operate. Because the EJB component developer generally cannot know into what environment the EJB will be deployed, the developer describes the component environment in a deployment environment independent manner using logical names. It is the responsibility of a deployment administrator to link the EJB component logical names to the corresponding deployment environment resources.

Figure 3.1 gives a graphical view of the EJB deployment descriptor DTD without the non-ENC elements. Only the session element is shown fully expanded as the ENC elements for entity and message-driven are identical.

Figure 3.1 The ENC elements in the standard EJB 2.0 ejb-jar.xml deployment descriptor.

NOTE

The full ejb-jar.xml DTD is available from the Sun Web site at http://java.sun.com/dtd/ejb-jar_2_0.dtd.

The web.xml ENC Elements

The Servlet 2.3 deployment descriptor describes a collection of Web components and their environment. The ENC for a Web application is declared globally for all servlets and JSP pages in the Web application. Because the Web application developer generally cannot know into what environment the Web application will be deployed, the developer describes the component environment in a deployment environment independent manner using logical names. It is the responsibility of a deployment administrator to link the Web component logical names to the corresponding deployment environment resources.

Figure 3.2 gives a graphical view of the Web application deployment descriptor DTD without the non-ENC elements.

Figure 3.2 The ENC elements in the standard servlet 2.3 web.xml deployment descriptor.

NOTE

The full web.xml DTD is available from the Sun Web site at http://java.sun.com/dtd/web-app_2_3.dtd.

The jboss.xml ENC Elements

The JBoss EJB deployment descriptor provides the mapping from the EJB component ENC JNDI names to the actual deployed JNDI name. It is the responsibility of the application deployer to map the logical references made by the application component to the corresponding physical resource deployed in a given application server configuration. In JBoss, this is done for the ejb-jar.xml descriptor using the jboss.xml deployment descriptor. Figure 3.3 gives a graphical view of the JBoss EJB deployment descriptor DTD without the non-ENC elements. Only the session element is shown fully expanded as the ENC elements for entity and message-driven are identical.

NOTE

The full jboss.xml DTD is available from the JBoss Web site at http://www.jboss.org/j2ee/dtd/jboss_2_4.dtd.

The jboss-web.xml ENC Elements

The JBoss Web deployment descriptor provides the mapping from the Web application ENC JNDI names to the actual deployed JNDI name. It is the responsibility of the application deployer to map the logical references made by the Web application to the corresponding physical resource deployed in a given application server configuration. In JBoss, this is done for the web.xml descriptor using the jboss-web.xml deployment descriptor. Figure 3.4 gives a graphical view of the JBoss Web deployment descriptor DTD without the non-ENC elements.

NOTE

The full jboss-web.xml DTD is available from the JBoss Web site at http://www.jboss.org/j2ee/dtd/jboss_web.dtd.

Environment Entries

Environment entries are the simplest form of information stored in a component ENC, and are similar to operating system environment variables like those found in Unix or Windows. Environment entries are a name-to-value binding that allows a component to externalize a value and refer to the value using a name.

Figure 3.3 The ENC elements in the JBoss 2.4 jboss.xml deployment descriptor.

An environment entry is declared using an env-entry element in the standard deployment descriptors. The env-entry element contains the following child elements:

  • An optional description element that provides a description of the entry

  • An env-entry-name element giving the name of the entry relative to java:comp/env

  • An env-entry-type element giving the Java type of the entry value that must be one of:

    • java.lang.Byte
    • java.lang.Boolean
    • java.lang.Character
    • java.lang.Double
    • java.lang.Float
    • java.lang.Integer
    • java.lang.Long
    • java.lang.Short
    • java.lang.String
    • An env-entry-value element giving the value of entry as a string
Figure 3.4 The ENC elements in the JBoss 2.4 jboss-web.xml deployment descriptor.

An example of an env-entry fragment from an ejb-jar.xml deployment descriptor is given in Listing 3.3. There is no JBoss specific deployment descriptor element because an env-entry is a complete name and value specification. Listing 3.4 shows a sample code fragment for accessing the maxExemptions and taxRate env-entry values declared in Listing 3.3.

Listing 3.3 ejb-jar.xml env-entry Fragment

...
<session>
 <ejb-name>ASessionBean</ejb-name>
...
 <env-entry>
  <description>The maximum number of tax exemptions allowed
  </description>
  <env-entry-name>maxExemptions</env-entry-name>
  <env-entry-type>java.lang.Integer</env-entry-type>
  <env-entry-value>15</env-entry-value>
 </env-entry>

 <env-entry>
  <description>The tax rate
  </description>
  <env-entry-name>taxRate</env-entry-name>
  <env-entry-type>java.lang.Float</env-entry-type>
  <env-entry-value>0.23</env-entry-value>
 </env-entry>
</session>
...

Listing 3.4 ENC env-entry Access Code Fragment

InitialContext iniCtx = new InitialContext();
Context envCtx = (Context) iniCtx.lookup("java:comp/env");
Integer maxExemptions = (Integer) envCtx.lookup("maxExemptions");
Float taxRate = (Float) envCtx.lookup("taxRate");

EJB References

It is common for EJBs and Web components to interact with other EJBs. Because the JNDI name under which an EJB home interface is bound is a deployment time decision, there needs to be a way for a component developer to declare a reference to an EJB that will be linked by the deployer. EJB references satisfy this requirement.

An EJB reference is a link in an application component naming environment that points to a deployed EJB home interface. The name used by the application component is a logical link that isolates the component from the actual name of the EJB home in the deployment environment. The J2EE specification recommends that all references to enterprise beans be organized in the java:comp/env/ejb context of the application component's environment.

An EJB reference is declared using an ejb-ref element in the deployment descriptor. Each ejb-ref element describes the interface requirements that the referencing application component has for the referenced enterprise bean. The ejb-ref element contains the following child elements:

  • An optional description element that provides the purpose of the reference.

  • An ejb-ref-name element that specifies the name of the reference relative to the java:comp/env context. To place the reference under the recommended java:comp/env/ejb context, use an ejb/link-name form for the ejb-ref-name value.

  • An ejb-ref-type element that specifies the type of the EJB. This must be either Entity or Session.

  • A home element that gives the fully qualified class name of the EJB home interface.

  • A remote element that gives the fully qualified class name of the EJB remote interface.

  • An optional ejb-link element that links the reference to another enterprise bean in the ejb-jar file or in the same J2EE application unit. The ejb-link value is the ejb-name of the referenced bean. If there are multiple enterprise beans with the same ejb-name, the value uses the path name specifying the location of the ejb-jar file that contains the referenced component. The path name is relative to the referencing ejb-jar file. The Application Assembler appends the ejb-name of the referenced bean to the path name separated by #. This allows multiple beans with the same name to be uniquely identified.

An EJB reference is scoped to the application component whose declaration contains the ejb-ref element. This means that the EJB reference is not accessible from other application components at runtime, and that other application components may define ejb-ref elements with the same ejb-ref-name without causing a name conflict. Listing 3.5 provides an ejb-jar.xml fragment that illustrates the use of the ejb-ref element. A code sample that illustrates accessing the ShoppingCartHome reference declared in listing 3.5 is given in Listing 3.6.

Listing 3.5 Example ejb-jar.xml ejb-ref Descriptor Fragment

...
<session>
 <ejb-name>ShoppingCartBean</ejb-name>
 ...
</session>

<session>
<ejb-name>ProductBeanUser</ejb-name>
...
<ejb-ref>
 <description>This is a reference to the store products entity
 </description>
 <ejb-ref-name>ejb/ProductHome</ejb-ref-name>
 <ejb-ref-type>Entity</ejb-ref-type>
 <home>org.jboss.store.ejb.ProductHome</home>
 <remote> org.jboss.store.ejb.Product</remote>
</ejb-ref>
</session>

<session>
<ejb-ref>
 <ejb-name>ShoppingCartUser</ejb-name>
 ...
 <ejb-ref-name>ejb/ShoppingCartHome</ejb-ref-name>
 <ejb-ref-type>Session</ejb-ref-type>
 <home>org.jboss.store.ejb.ShoppingCartHome</home>
 <remote> org.jboss.store.ejb.ShoppingCart</remote>
 <ejb-link>ShoppingCartBean</ejb-link>
</ejb-ref>
</session>

<entity>
 <description>The Product entity bean
 </description>
 <ejb-name>ProductBean</ejb-name>
 ...
</entity>
...

Listing 3.6 ENC ejb-ref Access Code Fragment

InitialContext iniCtx = new InitialContext();
Context ejbCtx = (Context) iniCtx.lookup("java:comp/env/ejb");
ShoppingCartHome home = (ShoppingCartHome) ejbCtx.lookup("ShoppingCartHome");

EJB References with jboss.xml and jboss-web.xml

The JBoss server jboss.xml EJB deployment descriptor affects EJB references in two ways. First, the jndi-name child element of the session and entity elements allows the user to specify the deployment JNDI name for the EJB home interface. In the absence of a jboss.xml specification of the jndi-name for an EJB, the home interface is bound under the ejb-jar.xml ejb-name value. For example, the session EJB with the ejb-name of ShoppingCartBean in Listing 3.5 would have its home interface bound under the JNDI name ShoppingCartBean in the absence of a jboss.xml jndi-name specification.

The second use of the jboss.xml descriptor with respect to ejb-refs is the setting of the destination to which a component's ENC ejb-ref refers. The ejb-link element cannot be used to refer to EJBs in another enterprise application. If your ejb-ref needs to access an external EJB, you can specify the JNDI name of the deployed EJB home using the jboss.xml ejb-ref/jndi-name element.

The jboss-web.xml descriptor is used only to set the destination to which a Web application ENC ejb-ref refers. The content model for the JBoss ejb-ref is as follows:

  • An ejb-ref-name element that corresponds to the ejb-ref-name element in the ejb-jar.xml or web.xml standard descriptor

  • A jndi-name element that specifies the JNDI name of the EJB home interface in the deployment environment

Listing 3.7 provides an example jboss.xml descriptor fragment that illustrates the following usage points:

  • The ProductBeanUser ejb-ref link destination is set to the deployment name of jboss/store/ProductHome

  • The deployment JNDI name of the ProductBean is set to jboss/store/ProductHome

Listing 3.7 Example jboss.xml ejb-ref Fragment

 ...
<session>
<ejb-name>ProductBeanUser</ejb-name>
<ejb-ref>
 <ejb-ref-name>ejb/ProductHome</ejb-ref-name>
 <jndi-name>jboss/store/ProductHome</jndi-name>
</ejb-ref>
</session>

<entity>
 <ejb-name>ProductBean</ejb-name>
 <jndi-name>jboss/store/ProductHome</jndi-name>
 ...
</entity>
...

Resource Manager Connection Factory References

Resource manager connection factory references allow application component code to refer to resource factories using logical names called resource manager connection factory references. Resource manager connection factory references are defined by the resource-ref elements in the standard deployment descriptors. The Deployer binds the resource manager connection factory references to the actual resource manager connection factories that exist in the target operational environment using the jboss.xml and jboss-web.xml descriptors.

Each resource-ref element describes a single resource manager connection factory reference. The resource-ref element consists of the following child elements:

  • An optional description element that provides the purpose of the reference.

  • A res-ref-name element that specifies the name of the reference relative to the java:comp/env context. The resource type based naming convention for which subcontext to place the res-ref-name into is discussed in the following bulleted list.

  • A res-type element that specifies the fully qualified class name of the resource manager connection factory.

  • A res-auth element that indicates whether the application component code performs resource signon programmatically, or whether the container signs on to the resource based on the principal mapping information supplied by the Deployer. It must be one of Application or Container.

  • An option res-sharing-scope element. This currently is not supported by JBoss.

The J2EE specification recommends that all resource manager connection factory references be organized in the subcontexts of the application component's environment, using a different subcontext for each resource manager type. The recommended resource manager type to subcontext name is as follows:

  • JDBC DataSource references should be declared in the java:comp/env/jdbc subcontext.

  • JMS connection factories should be declared in the java:comp/env/jms subcontext.

  • JavaMail connection factories should be declared in the java:comp/env/mail subcontext.

  • URL connection factories should be declared in the java:comp/env/url subcontext.

Listing 3.8 shows an example web.xml descriptor fragment that illustrates the resource-ref element usage. Listing 3.9 provides a code fragment that an application component would use to access the DefaultMail resource defined in Listing 3.8.

Listing 3.8 web.xml resource-ref Descriptor Fragment

<web>
...
<servlet>
 <servlet-name>AServlet</servlet-name>
 ...
</servlet>
...
 <!-- JDBC DataSources (java:comp/env/jdbc) -->
 <resource-ref>
   <description>The default DS</description>
   <res-ref-name>jdbc/DefaultDS</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
 </resource-ref>
 <!-- JavaMail Connection Factories (java:comp/env/mail) -->
 <resource-ref>
   <description>Default Mail</description>
   <res-ref-name>mail/DefaultMail</res-ref-name>
   <res-type>javax.mail.Session</res-type>
   <res-auth>Container</res-auth>
 </resource-ref>
 <!-- JMS Connection Factories (java:comp/env/jms) -->
 <resource-ref>
   <description>Default QueueFactory</description>
   <res-ref-name>jms/QueFactory</res-ref-name>
   <res-type>javax.jms.QueueConnectionFactory</res-type>
   <res-auth>Container</res-auth>
 </resource-ref>
</web>

Listing 3.9 ENC resource-ref Access Sample Code Fragment

 Context initCtx = new InitialContext();
 javax.mail.Session s = (javax.mail.Session)
  initCtx.lookup("java:comp/env/mail/DefaultMail");

Resource Manager Connection Factory References with jboss.xml and jboss-web.xml

The purpose of the JBoss jboss.xml EJB deployment descriptor and jboss-web.xml Web application deployment descriptor is to provide the link from the logical name defined by the res-ref-name element to the JNDI name of the resource factory as deployed in JBoss. This is accomplished by providing a resource-ref element in the jboss.xml or jboss-web.xml descriptor. The JBoss resource-ref element consists of the following child elements:

  • A res-ref-name element that must match the res-ref-name of a corresponding resource-ref element from the ejb-jar.xml or web.xml standard descriptors

  • An optional res-type element that specifies the fully qualified class name of the resource manager connection factory

  • A jndi-name element that specifies the JNDI name of the resource factory as deployed in JBoss

Listing 3.10 provides a sample jboss-web.xml descriptor fragment that shows sample mappings of the resource-ref elements given in Listing 3.8.

Listing 3.10 Sample jboss-web.xml resource-ref Descriptor Fragment

<jboss-web>
...
  <resource-ref>
    <res-ref-name>jdbc/DefaultDS</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <jndi-name>java:/DefaultDS</jndi-name>
  </resource-ref>
  <resource-ref>
    <res-ref-name>mail/DefaultMail</res-ref-name>
    <res-type>javax.mail.Session</res-type>
    <jndi-name>java:/Mail</jndi-name>
  </resource-ref>
  <resource-ref>
    <res-ref-name>jms/QueFactory</res-ref-name>
    <res-type>javax.jms.QueueConnectionFactory</res-type>
    <jndi-name>QueueConnectionFactory</jndi-name>
  </resource-ref>
...
</jboss-web>

Resource Environment References

Resource environment references are elements that refer to administered objects associated with a resource, such as JMS destinations, by using logical names. Resource environment references are defined by the resource-env-ref elements in the standard deployment descriptors. The Deployer binds the resource environment references to the actual administered objects location in the target operational environment using the jboss.xml and jboss-web.xml descriptors.

Each resource-env-ref element describes the requirements that the referencing application component has for the referenced administered object. The resource-env-ref element consists of the following child elements:

  • An optional description element that provides the purpose of the reference.

  • A resource-env-ref-name element that specifies the name of the reference relative to the java:comp/env context. Convention places the name in a subcontext that corresponds to the associated resource factory type. For example, a JMS queue reference named MyQueue should have a resource-env-ref-name of jms/MyQueue.

  • A resource-env-ref-type element that specifies the fully qualified class name of the referenced object. For example, in the case of a JMS queue, the value would be javax.jms.Queue.

Listing 3.11 provides an example resource-ref-env element declaration by a session bean. Listing 3.12 gives a code fragment that illustrates how the session bean would access the queue from its ENC.

Listing 3.11 An Example ejb-jar.xml resource-env-ref Fragment

<session>
 <ejb-name>MyBean</ejb-name>
 ...
 <resource-env-ref>
  <description>This is a reference to a JMS queue used in the
  processing of Stock info
  </description>
  <resource-env-ref-name>jms/StockInfo</resource-env-ref-name>
  <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
 </resource-env-ref>
...
</session>

Listing 3.12 ENC resource-env-ref Access Code Fragment

InitialContext iniCtx = new InitialContext();
javax.jms.Queue q = (javax.jms.Queue)
 envCtx.lookup("java:comp/env/jms/StockInfo");

Resource Environment References and jboss.xml, jboss-web.xml

The purpose of the JBoss jboss.xml EJB deployment descriptor and jboss-web.xml Web application deployment descriptor is to provide the link from the logical name defined by the resource-env-ref-name element to the JNDI name of the administered object deployed in JBoss. This is accomplished by providing a resource-env-ref element in the jboss.xml or jboss-web.xml descriptor. The JBoss resource-env-ref element consists of the following child elements:

  • A resource-env-ref-name element that must match the resource-env-ref-name of a corresponding resource-env-ref element from the ejb-jar.xml or web.xml standard descriptors

  • A jndi-name element that specifies the JNDI name of the resource as deployed in JBoss

Listing 3.13 provides a sample jboss.xml descriptor fragment that shows a sample mapping for the resource-env-ref element given in Listing 3.11.

Listing 3.13 Sample jboss.xml resource-env-ref Descriptor Fragment

<session>
 <ejb-name>MyBean</ejb-name>
 ...
 <resource-env-ref>
  <resource-env-ref-name>jms/StockInfo</resource-env-ref-name>
  <jndi-name>queue/StockInfoQue</jndi-name>
 </resource-env-ref>
...
</session>
  • + Share This
  • 🔖 Save To Your Account