Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Java Naming and Directory Interface (JNDI) | Input-Output (I-O) and Networking
Advanced Java 2077
course content

Conteúdo do Curso

Advanced Java 2077

Advanced Java 2077

1. Data Structures
2. Sorting and Searching
3. Concurrent Programming
4. Input-Output (I-O) and Networking
5. Java GUI Development

bookJava Naming and Directory Interface (JNDI)

Java Naming and Directory Interface (JNDI) is a Java API that provides a framework to access different naming and directory services like LDAP, DNS, and NIS in a standard way. JNDI is used to locate and access resources and objects stored in naming and directory services, making it a powerful tool for developing distributed applications in Java.

In this chapter, we will go over the basics of JNDI, how it works, and how to use it in Java applications.

Overview of JNDI

JNDI provides a standard way to access naming and directory services in Java. Naming services are used to store and retrieve objects by name, while directory services provide a hierarchical structure for organizing and managing objects. JNDI provides a unified API to access both types of services, allowing Java applications to access objects and resources from a variety of sources.

JNDI works by creating a set of naming contexts, which are hierarchical structures that contain bindings to objects or resources. Each context is identified by a unique name, and it can contain other contexts or bindings to objects. JNDI uses a service provider interface (SPI) to provide access to naming and directory services, allowing developers to plug in different providers as needed.

JNDI Architecture

The JNDI architecture consists of four main components:

  • Context: A context is a set of name-to-object bindings. Each context can contain other contexts and bindings, creating a hierarchical structure. Contexts are identified by their names, which are used to look up objects or resources.
  • Initial Context: The initial context is the starting point for JNDI lookups. It is created by the JNDI service provider and is used to obtain a reference to the root context.
  • Service Provider Interface (SPI): The SPI defines a set of interfaces and classes that JNDI providers must implement. Providers can be added or removed at runtime, allowing developers to use different naming and directory services.
  • Naming Service Provider: The naming service provider is responsible for providing access to a specific naming or directory service. Each provider implements the SPI and provides a set of classes and methods for accessing the service.

JNDI Example

Here is a simple example of using JNDI to look up a resource:

java

Main

copy
12345678910111213141516
import javax.naming.*; import java.util.*; public class JndiExample { public static void main(String[] args) throws NamingException { Hashtable<String, String> env = new Hashtable<>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:/tmp"); Context context = new InitialContext(env); Object obj = context.lookup("myResource"); // Use the resource here... } }

In this example, we create a Hashtable to store the JNDI environment properties. We then set the INITIAL_CONTEXT_FACTORY property to the file system context factory and the PROVIDER_URL property to the file system directory where the resources are stored.
We then create an initial context using the environment properties and use the lookup() method to look up a resource called myResource(). The lookup method returns an Object that we can use to access the resource.

JNDI Environment Properties

JNDI environment properties are used to configure the JNDI context and service providers. Here are some of the most common environment properties used in JNDI:

  • java.naming.factory.initial: Specifies the initial context factory class name.
  • java.naming.provider.url: Specifies the URL for the naming or directory service.
  • java.naming.security.authentication: Specifies the authentication method to use.
  • java.naming.security.principal: Specifies the user ID for authentication.
  • java.naming.security.credentials: Specifies the password for authentication.
  • java.naming.batchsize: Specifies the batch size for directory operations.

Each naming or directory service provider may define additional environment properties that can be used to configure the provider.

JNDI Providers

JNDI providers are used to provide access to naming and directory services. There are several JNDI providers available for different types of services, including LDAP, DNS, and NIS.

Here is an example of configuring JNDI to use an LDAP provider:

java

Main

copy
12345678910111213141516171819
import javax.naming.*; import javax.naming.directory.*; import java.util.*; public class LdapExample { public static void main(String[] args) throws NamingException { Hashtable<String, String> env = new Hashtable<>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=my-domain,dc=com"); DirContext context = new InitialDirContext(env); Attributes attrs = context.getAttributes("uid=user1,ou=people"); Attribute attr = attrs.get("mail"); String email = (String) attr.get(); // Use the email here... } }

In this example, we create a Hashtable to store the JNDI environment properties. We then set the INITIAL_CONTEXT_FACTORY property to the LDAP context factory and the PROVIDER_URL property to the LDAP server URL.

We then create an initial directory context using the environment properties and use the getAttributes() method to retrieve the attributes for a user with the uid of user1 in the people organizational unit. We then extract the mail attribute and use it to get the email address for the user.

What does JNDI stand for?

What does JNDI stand for?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 5
some-alt