- JNDI Basics
- Directory Operations
- Using LDAP with JNDI
- LDAP Classes and Attributes
- Troubleshooting
Directory Operations
JNDI has directory-specific extensions for performing directory operations as opposed to the simple name-value operations in most naming services. The DirContext interface and InitialDirContext classes provide additional methods for dealing with directories. The directory-specific classes are all contained within the javax.naming.directory package.
If you need to perform directory operations, create an InitialDirContext instead of an InitialContext. For example
DirContext dirCtx = new InitialDirContext();
All the same rules apply to InitialDirContext as far as the property names for choosing an initial context factory.
The Attribute interface and the BasicAttribute class represent an attribute of an object stored in a directory. An attribute might have more than one value, but it only has a single name. For example, a directory entry representing a person might have an attribute called children that could contain any number of names. A person might also have an age attribute containing a single number.
Most of the methods in the Attribute interface are for dealing with multi-valued attributes. There are add methods and remove methods, as well as get and set methods:
public void add(int index, Object value) public boolean add(Object value) public Object remove(int index) public boolean remove(Object value) public Object get() public Object get(int index) public Object set(index, Object value)
The Attributes interface and the BasicAttributes class encapsulate all the attributes. It's easier to manage single-valued attributes from the Attributes class than it is to first get an Attribute and then perform the manipulation. The main methods you use are get, put, and remove:
public Attribute get(String attrName) public Attribute put(Attribute attr) public Attribute put(String attrName, Object attrValue) public Attribute remove(String attrName)
The Attribute returned by put is the attribute being replaced by the new attribute-that is, the attribute previously stored under the same name as the new attribute. If there was no attribute with that name, put returns null. The remove method returns the Attribute that is being removed, or null if no such attribute exists.
The bind and rebind methods in the DirContext interface let you bind an object with a specific set of attributes:
public void bind(String name, Object ob, Attributes attrs) public void bind(Name name, Object ob, Attributes attrs) public void rebind(String name, Object ob, Attributesattrs) public void rebind(Name name, Object ob, Attributes attrs)
The DirContext interface also provides several variations of a search method. One of the things that distinguishes a directory service from a naming service is that you can search for items based on a set of attributes and not a specific name. For instance, find all people with age greater than 18. The various search methods are
public NamingEnumeration search(Name name, Attributes[] matchAttributes) public NamingEnumeration search(Name name, Attributes[] matchAttributes, String[] attributesToReturn) public NamingEnumeration search(Name name, String searchFilter, SearchControls controls) public NamingEnumeration search(Name name, String searchFilter, Object[] filterArgs, SearchControls controls) public NamingEnumeration search(String name, Attributes[] matchAttributes) public NamingEnumeration search(String name, Attributes[] matchAttributes, String[] attributesToReturn) public NamingEnumeration search(String name, String searchFilter, SearchControls controls) public NamingEnumeration search(String name, String searchFilter, Object[] filterArgs, SearchControls controls)