Previous | Next | Trail Map | Building a Service Provider | Adding Directory Support

Attribute Retrieval

The DirContext(in the API reference documentation) interface contains the following methods (plus their java.lang.String overloads) for retrieving the attributes of an object in a directory:

Retrieving object attributes is the most basic of directory operations in the DirContext interface. Most directory context implementation support these methods.

Note that getAttributes(name) is just a shorthand form of writing

getAttributes(name, null);
Therefore its implementation should only call the longhand form, as follows.
public Attributes getAttributes(Name name) throws NamingException {
    return getAttributes(name, null);  // Same as attrIds == null

The implementation of getAttributes() depends on the attribute model of the underlying directory service. An actual implementation might need to do no more than pass the name and request to the underlying directory service for processing.

The hierarchical directory example uses a similar pattern as that for the Context(in the API reference documentation) operations in the hierarchical namespace example. Basically, it first determines whether the request is for this context or for a context or object named relative to this context. If it is for this context, then a copy of the context's attributes is returned. If it is for an immediate child object, then a copy of the object's attributes is returned. Otherwise, the next atomic name is resolved to a DirContext and the request is passed on to that context.

public Attributes getAttributes(Name name, String[] attrIds) 
    throws NamingException {
    if (name.isEmpty()) {
        // Ask for the attributes of this context
        return deepClone(myAttrs);
    // Extract the components that belong to this namespace
    Name nm = getMyComponents(name);
    String atom = nm.get(0);
    Object inter = bindings.get(atom);

    if (nm.size() == 1) {
        // Atomic name; find object in the internal data structure
	if (inter == null) {
	    throw new NameNotFoundException(name + " not found");

	if (inter instanceof DirContext) {
	    return ((DirContext)inter).getAttributes("", attrIds);
	} else {
	    // Fetch the object's attributes from this context
	    Attributes attrs = (Attributes) bindingAttrs.get(atom);
	    if (attrs == null) {
		return new BasicAttributes();
	    } else {
		return deepClone(attrs);
    } else {
	// Intermediate name; consume the name in this context 
	// and then continue
	if (!(inter instanceof DirContext)) {
	    throw new NotContextException(atom + " does not name a dircontext");
	return ((DirContext)inter).getAttributes(nm.getSuffix(1), attrIds);

Previous | Next | Trail Map | Building a Service Provider | Adding Directory Support