Jump to content

Java Naming and Directory Interface

From Alfawiki
Java Naming and Directory Interface (JNDI)
Company typeAPI
IndustrySoftware, Directory services
FoundedMarch 10, 1997; 29 years ago (1997-03-10)
FounderSun Microsystems
ProductsJava API for directory services

The Java Naming and Directory Interface (JNDI) serves as a Java API designed for directory services, allowing Java clients to locate and access data and resources as Java objects through the use of names. Being independent of any specific implementation, JNDI incorporates a service provider interface (SPI) that facilitates the integration of diverse directory service implementations. Depending on the underlying system, the information accessed may originate from servers, flat files, or databases.

Typical business uses of JNDI encompass:

  • Linking Java applications with external directory services such as address repositories or LDAP servers
  • Enabling Java Servlets to retrieve configuration information from hosting web container environments

Background

JNDI is utilized by Java RMI and Java EE APIs to identify networked objects.

Key features provided by JNDI include:

  • Associating objects with names
  • Performing directory lookups with querying capabilities
  • Supporting event interfaces to notify about directory changes
  • Offering LDAP-specific extensions for advanced operations

Its SPI accommodates multiple naming and directory services, including:

The specification for JNDI was introduced by Sun Microsystems on March 10, 1997. The most recent version available as of 2006 is JNDI 1.2.

Basic lookup

JNDI arranges names in a hierarchical manner; these names are typically strings such as "com.example.ejb.MyBean." Objects or references stored within the directory are bound to these names.

A context in JNDI defines the location where objects can be found, with the initial context representing the root context. To create an initial context, one must specify parameters like the factory implementation and provider URL, often including authentication details:

<syntaxhighlight lang="java"> Hashtable contextArgs = new Hashtable<String, String>(); contextArgs.put(Context.INITIAL_CONTEXT_FACTORY, "com.jndiprovider.TheirContextFactory"); contextArgs.put(Context.PROVIDER_URL, "jndiprovider-database"); Context myCurrentContext = new InitialContext(contextArgs); </syntaxhighlight>

Once established, this context allows for retrieving bound objects:

<syntaxhighlight lang="java"> MyBean myBean = (MyBean) myCurrentContext.lookup("com.mydomain.MyBean"); </syntaxhighlight>

Alternatively, configuration settings can be externalized in a jndi.properties file placed on the classpath, simplifying context creation:

<syntaxhighlight lang="java"> // Configuration loaded automatically from jndi.properties Context myCurrentContext = new InitialContext(); </syntaxhighlight>

Lookup operations proceed identically:

<syntaxhighlight lang="java"> MyBean myBean = (MyBean) myCurrentContext.lookup("com.mydomain.MyBean"); </syntaxhighlight>

Searching

Directories in JNDI can have associated attributes that allow searching for objects based on these attributes. Such directories act as specialized contexts that limit the namespace in a manner comparable to directories within a file system hierarchy.

See also

References


External links