Pages

Wednesday, August 25, 2010

JNDI Basics

Java Naming and Directory Interface (JNDI)

Naming and Directory Concepts:
Naming Concepts :
In computer system every program that we write, create files we gave them a name to identify. so giving name to a object helps to lookup into system.
so nameing service typically map object with user friendly name to its address into the system. These objects are used by a program in system.
for e.g. the Internet Domain Name System (DNS) maps machine names (such as www.sun.com) to IP addresses (such as 192.9.48.5). A file system maps a filename (for example, c:\bin\autoexec.bat) to a file handle that a program can use to access the contents of the file.
To look up an object in a naming system, you supply it the name of the object. The naming system determines the syntax that the name must follow. This syntax is sometimes called the naming system's naming convention.
For example, the UNIXTM file system's naming convention is that a file is named from its path relative to the root of the file system, with each component in the path separated from left to right using the forward slash character ("/"). The UNIX pathname, /usr/hello, for example, names a file hello in the file directory usr, which is located in the root of the file system.

Bindings:
The association of a name with an object is called a binding. For example, a file name is bound to a file.

The DNS contains bindings that map machine names to IP addresses. An LDAP name is bound to an LDAP entry. (Lightweight Directory Access Protocol)

References and Addresses:
Depending on the naming service, some objects are stored directly and some are not, objects which are stored directly are mapped to its address and those are not
are mapped as references i.e. pointer to its location. in naming service. a reference gives information about how to access an object.

Context:
A context is a set of name-to-object bindings. Every context has an associated naming convention. A context provides a lookup (resolution) operation that returns the object and may provide operations such as those for binding names, unbinding names, and listing bound names. A name in one context object can be bound to another context object (called a subcontext) that has the same naming convention.
like a DNS domain, such as COM, is a context. A DNS domain named relative to another DNS domain is a subcontext. For example, in the DNS domain Sun.COM, the DNS domain Sun is a subcontext of COM.

Naming Systems and Namespaces:
A naming system is a program that stores set of contexts of the same type (they have the same naming convention) and provides a common set of operations. \
for e.g. DNS provides naming service of mapping name to ip address, LDAP provides naming service of mapping name to Directories and files etc.
A namespace is the set of names in a naming system.



JNDI Overview
The Java Naming and Directory InterfaceTM (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the JavaTM programming language. It is defined to be independent of any specific directory service implementation. Thus a variety of directories--new, emerging, and already deployed--can be accessed in a common way.
The JNDI architecture consists of an API and a service provider interface (SPI). Java applications use the JNDI API to access a variety of naming and directory services. The SPI enables a variety of naming and directory services to be plugged in transparently, thereby allowing the Java application using the JNDI API to access their services
The JNDI is included in the Java 2 SDK, v1.3 and later releases.
To use the JNDI, you must have the JNDI classes and one or more service providers. Java 2 SDK, v1.3 and later releases aleredy includes following
service provider classes in it

  • Lightweight Directory Access Protocol (LDAP)
  • Common Object Request Broker Architecture (CORBA) Common Object Services (COS) name service
  • Java Remote Method Invocation (RMI) Registry

Other service providers can be downloaded from the JNDI Web site or obtained from other vendors

The JNDI is divided into five packages:

* javax.naming
* javax.naming.directory
* javax.naming.event
* javax.naming.ldap
* javax.naming.spi


References:

for more details and tutorial in depth please have a look at following link

sun tutorial for JNDI


sample Naming example from sun site

This example shows you how to write a program that looks up an object whose name is passed in as a command-line argument.


import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;

class Lookup {
public static void main(String[] args) {
// Check that user has supplied name of file to lookup
if (args.length != 1) {
System.err.println("usage: java Lookup ");
System.exit(-1);
}

String name = args[0];

// Identify service provider to use
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");

try {

// Create the initial context
Context ctx = new InitialContext(env);

// Look up an object
Object obj = ctx.lookup(name);

// Print it out
System.out.println(name + " is bound to: " + obj);

// Close the context when we're done
ctx.close();
} catch (NamingException e) {
System.err.println("Problem looking up " + name + ": " + e);
}
}
}

No comments:

Post a Comment