Java Naming and Directory Interface (JNDI) provides a standard interface for locating
users, machines, networks, objects and services. Naming service allows you to
store various types of objects and associate or bind names to these objects.
It also provides facility to search or look up an object based on a name.
The objects can be a file on your disk or a computer in the network
or an EJB object. Directory service is similar to naming service except
that it allows you to store attributes with the directory objects. These
attributes can be used by your applications in various ways. For example, the
employee information like name, email address, phone number etc. can be stored
as attributes with the employee object and can be used by an application to display
the information when requested.
JNDI is defined independent of any specific naming or directory service implementation,
so it enables Java applications to access different, possibly multiple naming
and directory services using a single API.
This article describes how to look up various objects in Oracle9iAS
Container for J2EE (OC4J) using JNDI. It contains the following sections:
In general, the steps to look up an object are as follows:
1. Acquire an initial context
2. Look up the object using JNDI name.
A context is an object that contain object to name bindings. So,
for example, a folder named /bin
is a context and the files it contains (like
java.exe) are bindings with distinct names. All operations
in naming and directory service are performed relative to some context.
The starting context for resolution of names for naming and directory operations
is called initial context. To acquire an initial context you use an initial
context factory. Initial context factory is provided by specific naming and
directory service provider. It is responsible for acquiring an arbitrary initial
context that the application can use. To acquire an initial context :
Select a service provider : Specify the service provider to
use for the initial context. Use environment variable to specify the
initial context factory for the naming and directory service.
Provide information to the initial context : The service provider
might need specific information to connect to the directory service.
This information can be location of the machine in the network on which the
directory server in running or the username and password to authenticate the
client to the service. This information is provided to the service provider
using the environment properties specified by the JNDI.
Get the initial context : Use the initial context constructor
to obtain initial context. It can then be used to perform lookup and
other operations on the directory service.
JNDI with JDBC
This section shows how to retrieve a database connection from datasource object
in OC4J.
In OC4J, a data source is defined and configured in data-sources.xml
file located at J2EEHome/config/
directory. This is an example to define
a datasource.
Retrieving the Connection
Assuming the datasource entry in above example, here
is the code that illustrates how to obtain a connection by looking up the datasource.
This connection can be used to execute SQL statements and perform database operations.
JNDI with Enterprise Java Beans
(EJB)
One of the primary operation in an EJB application is to locate the EJB objects
that will be used to execute business logic of the application. JNDI can be
used to look up and locate local and remote EJBs . This section describes how
EJB objects can be looked up in different environments in OC4J. The steps involved
are as follows:
Creating an Initial Context
The concept of Initial Context, as seen earlier, is central to JNDI.
When OC4J server starts up, it creates a JNDI context for each application
defined in server.xml using
the xml configuration files of the application that define references to
the application resources (like ejb objects). The InitialContext class
provides two constructors to create the initial context.
InitialContext()
This constructor creates a context using the default context environment
for the application as set by the OC4J container during start up.
This is typically used in JSP or servlet or an EJB code that runs in the
server.
InitialContext(HashTable env)
This constructor is used to create a context using the environment properties
that are set in the Hashtable variable passed as parameter to the
constructor. It is used in the client code where it is necessary to
supply the JNDI properties to create initial context. Following are the
environment properties that need to be supplied :
INITIAL_CONTEXT_FACTORY : Specifies which initial context factory to use when creating initial context.
PROVIDER_URL : The URL that application client code uses to look up the objects on the server.
SECURITY_PRINCIPAL : The username that will be used to authenticate the application client.
SECURITY_CREDENTIAL : The password that will be used to authenticate the application client.
Perform look up using JNDI name of EJB object using initial context.
This section describes various ways to look up EJB objects under following
situations.
In order to look up the EJB objects that are part of same J2EE application,
the initial context factory used is ApplicationClientInitialContextFactory.
References to the remote objects that are part of the application are
specified in application-client.xml
and orion-application-client.xml
files located in Meta-Inf
directory of the application ear file. The context factory reads
these files when accessing the remote object. The reference
to the EJB object in application-client.xml
file is specified like this
Using the ApplicationClientInitialContextFactory
to construct JNDI initial contexts allows the client to look up local
objects (objects contained in the immediate application, or in its parent
application) using the java:comp/env
mechanism. Remote objects can be looked up using ORMI.
Here is the code to look up and create an
instance of an EJB object.
EJB
object is part of some other J2EE application in the same server
Sometimes the application might need to use an object that is part of
another J2EE application. It is also possible that the EJB object
is a part of current application but the reference to the resource
objects cannot be specified or is not specified in the current application's
application-client.xml
file. In such cases, RMIInitialContextFactory is usedfor
creating the initial context . Here is the code to use RMIInitialContextFactory
and obtain EJB instance.
Look up
an EJB from client code running in the same server
Environment properties need not be set in this case because the client
code is running in the same server as the EJB object. JNDI uses default
properties set by OC4J container during start up. The default initial
context factory used is ApplicationInitialContextFactory. All the
resource references specified in web.xml
and ejb-jar.xml are available
with this context.
Here is the code showing the look up to obtain
EJB instance.