Java Naming and Directory Interface

from Wikipedia, the free encyclopedia

Java Naming and Directory Interface ( JNDI ) is a programming interface (API) within the Java programming language for naming services and directory services . With the help of this interface, data and object references can be stored using a name and accessed by users of the interface.

The interface is independent of the actual implementation . Rather, JNDI is a so-called Service Provider Interface (SPI), which allows manufacturers to integrate their own solutions into this framework.

In practice, JNDI is mainly used to find databases and registrations, distributed objects in a network and call via Remote Method Invocation (RMI).

The API includes:

  • a mechanism for binding an object to a name
  • Methods for getting information by name
  • an event concept through which clients are informed of changes
  • special extensions for LDAP functionalities

JNDI allows the support of virtually all types of name and directory services, in particular:

JNDI lookup

Lookup ( English for Reference is), the process with which the designated objects are determined.

In JNDI the names are arranged hierarchically. Names are usually strings like "com.mydomain.MyBean" but can also any objects be that the interface javax.naming.Name implement. For each name, either the object assigned to it or a JNDI reference to the assigned object is stored in the name or directory service. The JNDI programming interface ("JNDI API") defines where to look for the object. The initial context is usually the starting point for this.

In the simplest case, an initial context is sufficient to search for a name:

Hashtable args = new Hashtable();

// zunächst muss die Kontext-Factory und somit die
// Implementierung des JNDI-Providers definiert werden:
args.put(Context.INITIAL_CONTEXT_FACTORY, "com.jndiprovider.TheirContextFactory");

// dann die URL, die definiert wo die Daten zu finden sind:
args.put(Context.PROVIDER_URL, "jndiprovider-database");

// damit bekommt man im einfachsten Fall den initialen Kontext:
Context myCurrentContext = new InitialContext(args);

// mit Hilfe dieses Kontextes kann man dann Objekte,
// die zuvor an den Kontext gebunden wurden, finden:
Object reference = myCurrentContext.lookup("com.mydomain.MyBean");

See also

Web links