User:Ervinn/JNDI

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Introduction[edit | edit source]

During programming, many times we'd like to access a resource in a standard way, regardless how the resource is stored or implemented.

In a networking environment sometimes we'd like to share objects among the programs running in the network. In this case, we'd like to map names to objects. For this Java has JNDI, that stands for Java Naming and Directory Interface.

Maming Services
Name Services are for to help share a resource among programs. For example a File System is a Naming Service, mapping a file name to physical file. In JNDI, names are associated with objects and objects are found based on their names.
Directory Services
Many naming services are extended with a directory service. A directory service associates names with objects and also allows such objects to have attributes. Thus, you not only can look up an object by its name but also get the object's attributes or search for the object based on its attributes. JNDI as the name implies, is also a Directory Service.
Atributes
A directory object can have attributes. An attribute has an attribute identifier and a set of attribute values. An attribute identifier is a token that identifies an attribute independent of its values.
Directories
A directory is a connected set of directory objects. A directory service is a service that provides operations for creating, adding, removing, and modifying the attributes associated with objects in a directory.
Binding
The association of a name with an object is called a binding. For example, a file name is bound to a file. In JNDI, a name is bound to a Java object.
References and Addresses
Depending on the naming service, some objects cannot be stored directly; that is, a copy of the object cannot be placed inside the naming service. Instead, they must be stored by reference; that is, a pointer or reference to the object is placed inside the naming service. A reference is information about how to access an object.
Although in general a reference can contain any arbitrary information, it is useful to refer to its contents as addresses.
In JNDI, objects are stored in naming and directory services in different ways. A service that supports storing Java objects might support storing an object in its serialized form, it depends on the service provider. Some naming and directory services do not support the storing of Java objects.
The JNDI defines a 'Reference' class to represent a reference. A reference contains information on how to construct a copy of the object. JNDI will attempt to turn references looked up from the directory into the Java objects.
Context
Context is a set of Name to Object binding. It defines a set of naming conventions. It also provide a lookup operations, such as binding name, unbiding name, ... .
A name in one context object can be bound to another context object (called a subcontext) that has the same naming convention.
Initial Context
In the JNDI, all naming and directory operations are performed relative to a context. There are no absolute roots. Therefore the JNDI defines an initial context, InitialContext, which provides a starting point for naming and directory operations. Once you have an initial context, you can use it to look up other contexts and objects.
Naming Systems
A naming system provides a naming service to its customers for performing naming-related operations. A naming service is accessed through its own interface.
JNDI
Java Naming and Directory Interface.
The Java Naming and Directory Interface is an application programming interface (API) that provides naming and directory functionality to applications written using the Java 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 is included in the Java 2 SDK, v1.3 and later releases.

JNDI/Java Naming and Directory Interface[edit | edit source]

Architecture
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.
Initial Context
  	// 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);
   	}


java.util.Properties p = new java.util.Properties();
p.put(Context.PROVIDER_URL, "IIOP:///");
p.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
InitialContext initContext = new InitialContext(p);
// Look up a transaction
userTran = (UserTransaction)initContext.lookup("jta/usertransaction");
userTran.begin();
// Call the finder
// Assume that employeeHome has already been found, and has a method defined findAll()
Enumeration enum = employeeHome.findAll();
while (enum.hasMoreElements()) {
// Process the enumeration
}
userTran.commit();