| Adding Directory Support

## Building in Extensibility

> The Essential Components 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()` before returning an object to the user program from one of the following methods:
> - `Context.lookup()`
> - `Context.lookupLink()`
> - `Binding.getObject()`
> - `SearchResult.getObject()`
> 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()` or overloads of `DirContext.search()` , you should call `getObjectInstance()` for the object in each `Binding` or `SearchResult` in the enumeration.

> Here is the definition of the list enumeration's `next()` 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()` before storing an object given by the user program to one of the following methods:
> - `Context.bind()`
> - `DirContext.bind()`
> - `Context.rebind()`
> - `DirContext.rebind()`

> 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`, 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:

> - Continue on in this trail to learn how to add pieces to the service provider for supporting URLs.
> - Go to the Adding Federation Support lesson to learn how to add federation support to a service provider.
> - Go to the Miscellaneous lesson to learn how to add various enhancements to a service provider, such as adding support for referrals and bundling factories.

| Adding Directory Support