Sun Java System Application Server Enterprise Edition 8.1 2005Q2 Developer's Guide

- Previous: Chapter 12 Using the Transaction Service
- Next: Configuring Resources

# Accessing the Naming Context

The Application Server provides a naming environment, or context, which is compliant with standard J2EE 1.4 requirements. A `Context` object provides the methods for binding names to objects, unbinding names from objects, renaming objects, and listing the bindings. The `InitialContext` is the handle to the J2EE naming service that application components and clients use for lookups.

The JNDI API also provides subcontext functionality. Much like a directory in a file system, a subcontext is a context within a context. This hierarchical structure permits better organization of information. For naming services that support subcontexts, the `Context` class also provides methods for creating and destroying subcontexts.

The rest of this section covers these topics:

- Naming Environment for J2EE Application Components

- Accessing EJB Components Using the CosNaming Naming Context

- Accessing EJB Components in a Remote Application Server

- Naming Environment for Lifecycle Modules

Note –

Each resource within a server instance must have a unique name. However, two resources in different server instances or different domains can have the same name.

## Naming Environment for J2EE Application Components

The namespace for objects looked up in a J2EE environment is organized into different subcontexts, with the standard prefix `java:comp/env`.

The following table describes standard JNDI subcontexts for connection factories in the Application Server.

Table 13–1 Standard JNDI Subcontexts for Connection Factories

Resource Manager | Connection Factory Type | JNDI Subcontext
--- | --- | ---
JDBCTM | `javax.sql.DataSource` | `java:comp/env/jdbc`
Transaction Service | `javax.transaction.UserTransaction` | `java:comp/UserTransaction`
JMS | `javax.jms.TopicConnectionFactory` `javax.jms.QueueConnectionFactory` | `java:comp/env/jms`
JavaMailTM | `javax.mail.Session` | `java:comp/env/mail`
URL | `java.net.URL` | `java:comp/env/url`
Connector | `javax.resource.cci.ConnectionFactory` | `java:comp/env/eis`

## Accessing EJB Components Using the CosNaming Naming Context

The preferred way of accessing the naming service, even in code that runs outside of a J2EE container, is to use the no-argument `InitialContext` constructor. However, if EJB client code explicitly instantiates an `InitialContext` that points to the CosNaming naming service, it is necessary to set these properties in the client JVM when accessing EJB components:

    -Djavax.rmi.CORBA.UtilClass=com.sun.corba.ee.impl.javax.rmi.CORBA.Util
    -Dorg.omg.CORBA.ORBClass=com.sun.corba.ee.impl.orb.ORBImpl
    -Dorg.omg.CORBA.ORBSingletonClass=com.sun.corba.ee.impl.orb.ORBSingleton
    -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory

## Accessing EJB Components in a Remote Application Server

The recommended approach for looking up an EJB component in a remote Application Server from a client that is a servlet or EJB component is to use the Interoperable Naming Service syntax. Host and port information is prepended to any global JNDI names and is automatically resolved during the lookup. The syntax for an interoperable global name is as follows:

    corbaname:iiop:host:port#a/b/name

This makes the programming model for accessing EJB components in another Application Server exactly the same as accessing them in the same server. The deployer can change the way the EJB components are physically distributed without having to change the code.

For J2EE components, the code still performs a `java:comp/env` lookup on an EJB reference. The only difference is that the deployer maps the `ejb-reference` element to an interoperable name in an Application Server deployment descriptor file instead of a simple global JNDI name.

For example, suppose a servlet looks up an EJB reference using `java:comp/env/ejb/Foo`, and the target EJB component has a global JNDI name of `a/b/Foo`.

The `ejb-ref` element in `sun-web.xml` looks like this:

    <ejb-ref>
       <ejb-ref-name>ejb/Foo</ejb-ref-name>
       <jndi-name>corbaname:iiop:host:port#a/b/Foo</jndi-name>
    <ejb-ref>

The code looks like this:

    Context ic = new InitialContext();
    Object o = ic.lookup("java:comp/env/ejb/Foo");

For a client that doesn’t run within a J2EE container, the code just uses the interoperable global name instead of the simple global JNDI name. For example:

    Context ic = new InitialContext();
    Object o = ic.lookup("corbaname:iiop:host:port#a/b/Foo");

Objects stored in the interoperable naming context and component-specific (`java:comp/env`) naming contexts are transient. On each server startup or application reloading, all relevant objects are re-bound to the namespace.

## Naming Environment for Lifecycle Modules

Lifecycle listener modules provide a means of running short or long duration Java-based tasks within the application server environment, such as instantiation of singletons or RMI servers. These modules are automatically initiated at server startup and are notified at various phases of the server life cycle. For details about lifecycle modules, see Chapter 10, Developing Lifecycle Listeners.

The configured properties for a lifecycle module are passed as properties during server initialization (the `INIT_EVENT`). The initial JNDI naming context is not available until server initialization is complete. A lifecycle module can get the `InitialContext` for lookups using the method `LifecycleEventContext.``getInitialContext()` during, and only during, the `STARTUP_EVENT`, `READY_EVENT`, or `SHUTDOWN_EVENT` server life cycle events.

- Previous: Chapter 12 Using the Transaction Service
- Next: Configuring Resources

- © 2010, Oracle Corporation and/or its affiliates