View RSS Feed

My Java Tips

Java Naming and Directory Interface

Rate this Entry
by , 11-01-2011 at 06:14 PM (887 Views)
I will introduce Java Naming and Directory Interface (JNDI) in this post. Its an API for accessing different kinds of naming and directory services.


Many people believe that JNDI is specific to a particular naming or directory service, which is not correct. JNDI can be used to access many different kinds of systems including file systems; distributed objects systems (CORBA, Java RMI, and EJB) and directory services like LDAP, Novell NetWare, and NIS+.

JNDI is similar to JDBC in many cases. Like both are Object-Oriented Java APIs that provide a common abstraction for accessing services from different vendors. Difference is that JDBC is used to access different relational databases from different vendors, JNDI is used to access a variety of naming and directory services.

The idea is to use one API to access many different brands of a service and it is possible because the JNDI defines a common abstraction that most vendors can implement.


JNDI provides an objectified view of a service while hiding the details specific to any brand of service. The implementation is provided by the vendor, it plugs into the API and implements code specific to accessing that vendor's product.

JNDI provides two APIs and one SPI. JNDI has a naming API that allows Java applications to access naming systems like CORBA's Naming services and a directory API that extends the naming service to provide access to directory services like LDAP. JNDI also has a SPI (Service-Provider Interface) which is a programming model that vendors use to write JNDI plug-ins or implementations for their specific product. Each vendor's plug-in is called a service-provider.

A lookup example.

Java Code:
// Get the initial context as shown in a previous example.
...
// Look up the home interface using the JNDI name.
try {
   java.lang.Object ejbHome =
      initialContext.lookup(
         "java:comp/env/com/mycompany/accounting/AccountEJB");
   accountHome =
      (AccountHome)javax.rmi.PortableRemoteObject.narrow(ejbHome, AccountHome.class);
}
   catch (NamingException e) { // Error getting the home interface
   ...
}

Submit "Java Naming and Directory Interface" to Facebook Submit "Java Naming and Directory Interface" to Digg Submit "Java Naming and Directory Interface" to del.icio.us Submit "Java Naming and Directory Interface" to StumbleUpon Submit "Java Naming and Directory Interface" to Google

Tags: None Add / Edit Tags
Categories
JBoss

Comments