Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 3
Naming services and directory services are logical partners. In fact, most existing products provide both sets of functionality. Naming services provide name-to-object mapping, and directory services provide information about the objects and tools for searching for them.
There are a number of existing directory service products; LDAP (the Lightweight Directory Access Protocol) is the most common. LDAP provides both naming and directory functionality. Numerous commercial implementations exist, as well as some freely available implementations (OpenLDAP being the most common -- see the Resources section below for more information).
Descriptions of various directory services are available in January's How-To Java column.
JNDI directory-service support is both comprehensive and powerful. It adds advanced functions, like storing and retrieving serialized class instances, to searching and other components of the basic suite of direct functions. This month, we will pass on the advanced features and focus on understanding basic JNDI directory functionality.
With this thought in mind, let's examine the DirContext class -- the heart of JNDI directory services.
The DirContext class is a subclass of the Context class described last month. It provides all of the standard naming service functionality, and can also work with attributes
and search for directory entries.
Let's take a look at the methods of DirContext that extend those methods provided by the Context class.
void bind( String stringName, Object object, Attributes attributes )
The bind() method binds a name to an object and stores the specified attributes with that entry. This operation generally tries to preserve
existing attributes in cases in which that makes sense. Specifically, if attributes is null and object is an instance of the DirContext class, the resulting binding will retain the attributes originally associated with object.
void rebind( String stringName, Object object, Attributes attributes )
The rebind() method binds a name to an object and stores the specified attributes with that entry. The previous binding is replaced. As
is the case with bind(), this operation tries to preserve existing attributes in cases in which that would make sense.
DirContext createSubcontext( String stringName, Attributes attributes )
The createSubcontext() method creates a new subcontext and binds a name to it, and then stores the specified attributes with that entry. If attributes is null, this method works in exactly the same fashion as the like-named method on the Context class.
Let's next consider those of DirContext's methods that do not extend methods provided by the Context class, providing the tools for working with attributes instead.
The class provides two flavors of this method:
Attributes getAttributes( String stringName )
And:
Attributes getAttributes( String stringName, String [] rgstringAttributeNames )
The getAttributes methods return the attributes associated with the specified entry. The Attributes class represents a collection of attributes; it contains instances of the Attribute class, which by itself represents a single attribute. The first flavor of this method returns all attributes, and the second returns the attributes named in the supplied array of attribute names.