Skip Headers

Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g Release 3 (10.1.3) B14428-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Configuring the Initial Context Factory

You use an initial context factory to obtain an initial context: a reference to a JNDI namespace. Using the initial context, you can use the JNDI API to look up an EJB, resource manager connection factory, environment variable, or other JNDI-accessible object.

The type of initial context factory you use depends on the type of client you are using it in as Table 19-2 shows.

Table 19-2 Client Initial Context Requirements

Client Type | Relationship to Target EJB | Initial Context Factory
--- | --- | ---
Any Client | Client and target EJB are collocated | Default (see "Configuring the Default Initial Context Factory")
Any Client | Client and target EJB are deployed in the same application | Default (see "Configuring the Default Initial Context Factory")
Any Client | Target EJB deployed in an application that is designated as the client's parentFoot 1 | Default (see "Configuring the Default Initial Context Factory")
EJB Client Servlet or JSP Client | Client and target EJB are not collocated, not deployed in the same application, and target EJB application is not client's parentFootref 1. | `oracle.j2ee.rmi. RMIInitialContextFactory` (see "Configuring an Oracle Initial Context Factory")
Stand-alone Java Client | Client and target EJB are not collocated, not deployed in the same application, and target EJB application is not client's parentFootref 1. | `oracle.j2ee.naming. ApplicationClientInitialContextFactory` see "Configuring an Oracle Initial Context Factory")

Footnote 1 See the Oracle Containers for J2EE Developer's Guide for more information on how to set the parent of an application.

Note: In this release, note the new package names for the RMI and application client initial context factories.

For more information, see:

- Oracle Containers for J2EE Security Guide

- Oracle Containers for J2EE Services Guide.

## Configuring the Default Initial Context Factory

A client that is collocated with the target bean (see Table 19-2) automatically accesses the JNDI properties for the node. Thus, accessing the EJB is simple: no JNDI properties are required.

Example 19-16 Configuring the Default Initial Context

    //Get the Initial Context for the JNDI lookup for a local EJB
    InitialContext ic = new InitialContext();
    //Retrieve the Home interface using JNDI lookup
    Object helloObject = ic.lookup("java:comp/env/ejb/HelloBean");

## Configuring an Oracle Initial Context Factory

If your client requires an Oracle initial context factory (see Table 19-2), you must set the following JNDI properties:

For more information about setting JNDI properties, see "Setting JNDI Properties in an EJB".

1. Define the `java.naming.factory.initial` property with the Oracle inital context factory appropriate for your client (see Table 19-2).

2. Define the `java.naming.provider.url` property with the naming provider URL appropriate for your OC4J installation:

   - "Configuring the Naming Provider URL for OC4J and Oracle Application Server"

   - "Configuring the Naming Provider URL for OC4J Standalone"

3. Create a `HashTable` and populate it with the required properties using `javax.naming.Context` fields as keys and `String` objects as values as Example 19-17 shows.

   Example 19-17 Specifying Initial Context Factory Properties

       Hashtable env = new Hashtable();
       env.put("java.naming.factory.initial",
               "oracle.j2ee.server.ApplicationClientInitialContextFactory");
       env.put("java.naming.provider.url",
               "opmn:ormi://opmnhost:6004:oc4j_inst1/ejbsamples");

4. When you instantiate the initial context, pass the `HashTable` into the initial context constructor as Example 19-18 shows.

   Example 19-18 Instantiate the Initail Context Looking Up a JNDI-Accessible Resource

       Context ic = new InitialContext (env);

5. Use the initial context to look up a JNDI-accessible resource:

   - Looking Up an EJB 3.0 Resource Manager Connection Factory

   - Looking Up an EJB 3.0 Environment Variable

   - Looking Up an EJB 2.1 Resource Manager Connection Factory

   - Looking Up an EJB 2.1 Enviornment Variable

   - "Accessing an EJB from a Client"

### Configuring the Naming Provider URL for OC4J and Oracle Application Server

In an Oracle Application Server install, OPMN manages one or more OC4J instances. In this case the value for `java.naming.provider.url` should be of the format:

    opmn:ormi://<hostname>:<opmn-request-port>:<oc4j-instance-name>/<application-name>

The fields in this provider URL are defined as follows:

- `<hostname>`: The name of the host on which the Oracle Application Server is running.

- `<opmn-request-port>`: In this configuration, you have to use the OPMN request port instead of using the ORMI port. You can find the OPMN request port in the `opmn.xml` file, as follows:

      <notification-server>
          <port local="6003" remote="6200" request="6004"/>
          ...
      </notification-server>

  The default OPMN request port is 6003.

- `<oc4j-instance-name>`: In this configuration, you may have more than one OC4J process that OPMN uses for load balancing/failover. You use the name of the instance to which you deployed your application.

  The default instance name is `home`.

For example, if the hostname is `dpanda-us`, request port is 6004, and instances name is `home1`, then the provider URL would be:

    opmn:ormi://dpanda-us:6003:home1/ejbsamples

### Configuring the Naming Provider URL for OC4J Standalone

In a standalone OC4J install, the value for `java.naming.provider.url` should be of the format:

    ormi://<hostname>:<ormi-port>/<application-name>

The fields in this provider URL are defined as follows:

- `<hostname>`: The name of the host on which OC4J is running

- `<ormi-port>`: The ORMI port as configured in the `rmi.xml` file, as follows:

      <rmi-server
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/rmi-server-10_0.xsd"
          port="23791"
          schema-major-version="10"
          schema-minor-version="0"
      >
      ...
      </rmi-server>

  The default port is 23791.

- `<application-name>`: The application name as configured in the `server.xml` file.

For example, if the hostname is `dpanda-us`, ORMI port is 23793, and the application name is `ejb30slsb`, then the provider URL would be:

    ormi://dpanda-us:23793/ejb30slsb