The Java Naming and Directory Interface (JNDI) is a Java
API
An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software Interface (computing), interface, offering a service to other pieces of software. A document or standa ...
for a
directory service
In computing, a directory service or name service maps the names of network resources to their respective network addresses. It is a shared information infrastructure for locating, managing, administering and organizing everyday items and network r ...
that allows Java software clients to discover and look up data and resources (in the form of Java
objects
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an ...
) via a name. Like all
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
APIs that interface with host systems, JNDI is independent of the underlying implementation. Additionally, it specifies a
service provider interface Service provider interface (SPI) is an API intended to be implemented or extended by a third party. It can be used to enable framework extension and replaceable components.
Details
From Java documentation:
The concept can be extended to other pla ...
(SPI) that allows
directory service
In computing, a directory service or name service maps the names of network resources to their respective network addresses. It is a shared information infrastructure for locating, managing, administering and organizing everyday items and network r ...
implementations to be plugged into the framework. The information looked up via JNDI may be supplied by a server, a flat file, or a database; the choice is up to the implementation used.
Typical uses of JNDI include:
* connecting a Java application to an external directory service (such as an address database or an
LDAP
The Lightweight Directory Access Protocol (LDAP ) is an open, vendor-neutral, industry standard application protocol for accessing and maintaining distributed directory information services over an Internet Protocol (IP) network. Directory servi ...
server)
* allowing a
Java Servlet
A Jakarta Servlet (formerly Java Servlet) is a Java software component that extends the capabilities of a server. Although servlets can respond to many types of requests, they most commonly implement web containers for hosting web application ...
to look up configuration information provided by the hosting
web container
A web container (also known as a servlet container;
and compare "webcontainer"
) is the component of a web server that interacts with Jakarta Servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a par ...
Background
The Java
RMI and
Java EE
Jakarta EE, formerly Java Platform, Enterprise Edition (Java EE) and Java 2 Platform, Enterprise Edition (J2EE), is a set of specifications, extending Java SE with specifications for enterprise features such as distributed computing and web serv ...
APIs use the JNDI API to look up objects in a network.
The API provides:
* a mechanism to bind an object to a name
* a directory-lookup interface that allows general queries
* an event interface that allows clients to determine when directory entries have been modified
* LDAP extensions to support the additional capabilities of an LDAP service
The
SPI portion allows support for practically any kind of naming or directory service, including:
*
LDAP
The Lightweight Directory Access Protocol (LDAP ) is an open, vendor-neutral, industry standard application protocol for accessing and maintaining distributed directory information services over an Internet Protocol (IP) network. Directory servi ...
*
DNS
The Domain Name System (DNS) is a hierarchical and distributed naming system for computers, services, and other resources in the Internet or other Internet Protocol (IP) networks. It associates various information with domain names assigned to ...
*
NIS
Nis, Niš, NiS or NIS may refer to:
Places
* Niš, a city in Serbia
* Nis, Iran, a village
* Ness, Lewis ( gd, Nis, links=no), a village in the Outer Hebrides islands
Businesses and organizations
* Naftna Industrija Srbije, Petroleum Industry o ...
*
CORBA
The Common Object Request Broker Architecture (CORBA) is a standard defined by the Object Management Group (OMG) designed to facilitate the communication of systems that are deployed on diverse platforms. CORBA enables collaboration between sys ...
name service
*
file system
In computing, file system or filesystem (often abbreviated to fs) is a method and data structure that the operating system uses to control how data is stored and retrieved. Without a file system, data placed in a storage medium would be one larg ...
Sun Microsystems
Sun Microsystems, Inc. (Sun for short) was an American technology company that sold computers, computer components, software, and information technology services and created the Java programming language, the Solaris operating system, ZFS, the ...
first released the JNDI specification on March 10, 1997. , the current version is JNDI 1.2.
Basic lookup
JNDI (Java Naming and Directory Interface) organizes its names into a hierarchy. A name can be any string such as "com.example.ejb.MyBean". A name can also be an object that implements the
Name
interface; however, a string is the most common way to name an object. A name is bound to an object in the directory by storing either the object or a
reference
Reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a ''name'' ...
to the object in the directory service identified by the name.
The JNDI API defines a context that specifies where to look for an object. The initial context is typically used as a starting point.
In the simplest case, an initial context must be created using the specific implementation and extra parameters required by the implementation. The initial context will be used to look up a name. The initial context is analogous to the root or top of a directory tree for a file system. Below is an example of creating an initial context:
Hashtable contextArgs = new Hashtable();
// First you must specify the context factory.
// This is how you choose between jboss implementation
// vs. an implementation from Sun or other vendors.
contextArgs.put( Context.INITIAL_CONTEXT_FACTORY, "com.jndiprovider.TheirContextFactory" );
// The next argument is the URL specifying where the data store is:
contextArgs.put( Context.PROVIDER_URL, "jndiprovider-database" );
// (You may also have to provide security credentials)
// Next you create the initial context
Context myCurrentContext = new InitialContext(contextArgs);
A context is then used to look up previously bound names in that context. For example:
MyBean myBean = (MyBean) myCurrentContext.lookup("com.mydomain.MyBean");
Alternative to above code is as below:
The Context object can also be configured by adding jndi.properties file in classpath containing initial context factory class name and provider URL. The above code will be reduced as shown below:
//just need to create initial context object, it will try to read jndi.properties file from the classpath.
Context myCurrentContext = new InitialContext();
A context is then used to look up previously bound names in that context. For example:
MyBean myBean = (MyBean) myCurrentContext.lookup("com.mydomain.MyBean");
Searching
Attributes may be attached to special entries called directories. Directories enable searching for objects by their associated attributes. Directories are a type of context; they restrict the name space much like a directory structure on a file system does.
See also
*
Service locator pattern
The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer. This pattern uses a central registry known as the "service locator", whic ...
*
Log4Shell
Log4Shell (CVE-2021-44228) was a zero-day vulnerability in Log4j, a popular Java logging framework, involving arbitrary code execution. The vulnerability had existed unnoticed since 2013 and was privately disclosed to the Apache Software Found ...
References
External links
Java SE 7 JNDI page*
ttp://docs.oracle.com/javase/jndi/tutorial/ The JNDI Tutorial
{{DEFAULTSORT:Java Naming And Directory Interface
Naming and Directory Interface
Java APIs
Application layer protocols