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

Building in Extensibility

The Essential Components (in the Building a Service Provider trail) lesson showed how to use JNDI utilities to make a context implementation extensible. When you are building a directory context implementation, you should replace those utilities with the ones described here.

Reading Objects

A directory context implementation should use DirectoryManager.getObjectInstance() (in the API reference documentation) before returning an object to the user program from one of the following methods: Here is how the hierarchical directory example calls getObjectInstance() in its lookup().
// Code that determine "inter" is the object bound to the
// atomic name "atom"
    ...

// Get the object's attributes
Attributes attrs;
if (inter instanceof DirContext) {
    attrs = ((DirContext)inter).getAttributes("");
} else {
    // Fetch the object's attributes from this context
    attrs = (Attributes) bindingAttrs.get(atom);
}

// Call getObjectInstance() for using any object factories
try {
    return DirectoryManager.getObjectInstance(inter, 
        new CompositeName().add(atom), this, myEnv, attrs);
} catch (Exception e) {
    NamingException ne = new NamingException("getObjectInstance failed");
    ne.setRootCause(e);
    throw ne;
}

Similarly, when returning the enumeration generated by Context.listBindings() (in the API reference documentation) or overloads of DirContext.search() (in the API reference documentation), you should call getObjectInstance() for the object in each Binding (in the API reference documentation) or SearchResult (in the API reference documentation) in the enumeration.

Here is the definition of the list enumeration's next() (in the API reference documentation) method.

public Object next() throws NamingException {
    String name = (String)names.nextElement();
    Object obj = bindings.get(name);

    try {
	// Get the attributes
	Attributes attrs;
	if (obj instanceof DirContext) {
	    attrs = ((DirContext)obj).getAttributes("");
	} else {
	    // Fetch the object's attributes from this context
	    attrs = (Attributes) bindingAttrs.get(name);
	}

	obj = DirectoryManager.getObjectInstance(
	    obj, new CompositeName().add(name), HierDirCtx.this, 
	    HierDirCtx.this.myEnv, attrs);
    } catch (Exception e) {
	NamingException ne = new NamingException("getObjectInstance failed");
	ne.setRootCause(e);
	throw ne;
    }
    return new Binding(name, obj);
}

Storing Objects

A directory context implementation should use DirectoryManager.getStateToBind() (in the API reference documentation) before storing an object given by the user program to one of the following methods:

Here is how the hierarchical directory example calls getStateToBind() in its bind() and rebind() methods.

// Code that determined that this is the context in which
// to bind the atomic name "atom" to the object "obj"
    ...

// Call getStateToBind for using any state factories
DirStateFactory.Result res = DirectoryManager.getStateToBind(
    obj, new CompositeName().add(atom), this, myEnv, attrs);

// Add the object to the internal data structure
bindings.put(atom, res.getObject());

// Add the attributes
if (res.getAttributes() != null) {
    bindingAttrs.put(atom, deepClone(res.getAttributes()));
}
DirectoryManager.getStateToBind() returns an instance of DirStateFactory.Result(in the API reference documentation), which is a tuple consisting of the object to bind and the attributes to associate with the object. Upon receiving the result, the context implementation updates its internal bindings table and attributes table.

Adding Directory Support: End of Lesson

What's next? Now you can:


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