Previous | Next | Trail Map | Tips for LDAP Users | Searches

Other Context Methods

In addition to the search methods, there are other methods in the DirContext(in the API reference documentation) interface that read from the directory. These are:

Since the LDAP "search" operation is the primary way in which data is to be read from the directory, these other methods all use the LDAP "search" operation in one way or another. This section describes how each of these operations use the LDAP search operation. Examples of how to use each of these methods are available in the Basics (in the Basics trail) trail.

getAttributes()

This method retrieves attributes associated with the named entry. There are two forms of this method (ignoring the overloads that accept java.lang.String names instead of Name(in the API reference documentation)names): The first form is equivalent to the second form with null supplied as the retAttrs argument:
getAttributes(name, null);
The retAttrs argument contains the list of attributes to retrieve. If retAttrs contains an attribute with the special name "*", or if retAttrs is null, then all attributes of the named entry are retrieved. This method is equivalent to performing an LDAP "search" operation using the string filter "(objectclass=*)" and a search scope of SearchControls.OBJECT_SCOPE(in the API reference documentation) , and asking that the requested attributes be returned.

lookup() and lookupLink()

These methods return the object bound to the name. If a java.io.Serializable, java.rmi.Remote, Reference (in the API reference documentation) , or Referenceable (in the API reference documentation)object was previously bound to the name, using either bind()(in the API reference documentation) or rebind()(in the API reference documentation) , then the result of these methods will be an object constructed using the attributes used for storing Java objects. See the Representation (in the Java Objects and the Directory trail) lesson for details.

Otherwise, a DirContext object representing the named entry is returned.

These methods are implemented using an LDAP "search" operation with the string filter "(objectclass=*)" and a search scope of SearchControls.OBJECT_SCOPE(in the API reference documentation) , and asking for all of the entry's attributes. If the entry contains Java object-related attributes, they are used to reconstruct the object, as described in the Representation (in the Java Objects and the Directory trail) lesson. The result is then passed to the object factory mechanism, NamingManager.getObjectInstance()(in the API reference documentation) , before being returned to the caller. See Reading Objects from the Directory (in the Java Objects and the Directory trail) lesson for details.

list() and listBindings()

list() and listBindings() list the named context and return an enumeration of NameClassPair (in the API reference documentation) or Binding (in the API reference documentation) , respectively. These methods are implemented using an LDAP "search" operation with the string filter "(objectclass=*)" and a search scope of SearchControls.ONELEVEL_SCOPE(in the API reference documentation) . The list() method asks for the "objectClass" and "javaClassName" attributes so that the class name of each entry can be determined ( NameClassPair.getClassName() (in the API reference documentation) ). If the "javaClassName" attribute does not exist, the class name is "javax.naming.directory.DirContext". The name of each entry ( NameClassPair.getName() (in the API reference documentation) ) either is relative to the named context or is an LDAP URL. The latter is used if a referral or alias has been followed.

The listBindings() method is similar to the list() method, except it asks for all of the entry's attributes. It will attempt to create for each item in the enumeration an object (to be returned by Binding.getObject() (in the API reference documentation) ) similar to the way lookup() creates an object from the data read from the directory.


Previous | Next | Trail Map | Tips for LDAP Users | Searches