Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

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

JNDI overview, Part 2: An introduction to directory services

Use JNDI directory services to better manage your distributed applications

  • Print
  • Feedback

Page 3 of 3

The modifyAttributes methods

modifyAttributes comes in two flavors as well:

void
modifyAttributes(
   String stringName,
   int nOperation,
   Attributes attributes
)


And:

void
modifyAttributes(
   String stringName,
   ModificationItem [] rgmodificationitem
)


The modifyAttributes methods modify the attributes associated with the specified entry. The permitted operations are ADD_ATTRIBUTE, REPLACE_ATTRIBUTE, and REMOVE_ATTRIBUTE. The first flavor of this method modifies several attributes in the same way, while the second performs a series of modifications on one or more attributes.

As with the Context class, each of the methods above also has a variant that takes a Name object rather than a String object.

Searching

In order to make use of directory service functionality, it is necessary to have some way to search the contents of a directory service. The DirContext class provides two general models by which searches may be conducted.

Searching by attribute name

There are two ways to conduct a search following this model:

NamingEnumeration
search(
  String stringName,
  Attributes attributesToMatch
)


And:

NamingEnumeration
search(
   String stringName,
   Attributes attributesToMatch,
   String [] rgstringAttributesToReturn
)


In this first model, the search occurs within a single context for entries that contain a specific set of attributes. For the entities that match, the search retrieves either the entire set of attributes (if the search is implemented using the first block of code above) or a select set of attributes (if the search is implemented using the second). Now, let's look at the second model.

Searching by RFC 2254 filter

Again, there are two ways to implement this model:

NamingEnumeration
search(
   Name stringName,
   String stringRFC2254Filter,
   SearchControls searchcontrols
)


And:

NamingEnumeration
search(
   Name             stringName,
   String           stringRFC2254Filter,
   Object []        stringRFC2254FilterArgs,
   SearchControls   searchcontrols
)


In our second model, the search occurs within a context for entries that satisfy a search filter. RFC 2254 (which describes a string representation for LDAP search filters -- see Resources for more information) defines the format of the filter.

An instance of the SearchControls class controls key aspects of the search:

SearchControls(
   int              nSearchScope,
   long             nEntryLimit,
   int              nTimeLimit,
   String []        rgstringAttributesToReturn,
   boolean          boolReturnObject,
   boolean          boolDereferenceLinks
)


The constructor above lists all of the aspects of a search that a SearchControls instance affects. Corresponding accessors (get and set methods) also exist. Below, I've listed each of these aspects and a short description of each:

  • nSearchScope: Sets the scope of the search to either the object (OBJECT_SCOPE), the object and the level immediately below it (ONELEVEL_SCOPE), or the object and its entire subtree (SUBTREE_SCOPE).

  • nEntryLimit: Sets the maximum number of entries that the search will return.

  • nTimeLimit: Sets the maximum number of milliseconds that the search will run.

  • rgstringAttributesToReturn: Determines which attributes should be returned along with the entries returned by the search.

  • boolReturnObject: Determines whether or not the objects bound to selected entries should to be returned along with the entries returned by the search.

  • boolDereferenceLinks: Determines whether or not links should be dereferenced links (or followed to their ultimate destination) during the search. A link references another directory entry and can span multiple naming systems. The underlying JNDI service provider may or may not provide support for links.


Once again, each of the methods above also has a variant that takes a Name object rather than a String.

Conclusion

If you've made it this far, you should now have a solid understanding of both naming and directory services, and a general understanding of JNDI. Next month, we'll build a handful of JNDI-based tools to work with. Since JNDI requires an underlying service provider in order to function, we'll also take a look at building and installing one of the freely available LDAP implementations.

About the author

Todd Sundsted has been writing programs since computers became available in convenient desktop models. Though originally interested in building distributed applications in C++, Todd moved on to the Java programming language when it became the obvious choice for that sort of thing. In addition to writing, Todd also serves as a Java architect with ComFrame Software.

Read more about Enterprise Java in JavaWorld's Enterprise Java section.

  • Print
  • Feedback

Resources