La lecci�n Los Componentes Esenciales mostr� como usar las utilidades JNDI para hacer extensible una implementaci�n de contexto. Cuando estemos construyendo una implementaci�n de contexto de directorio, deber�amos reemplazar aquellas utilidades con las descritas aqu�.
�Leer Objetos
Una implementaci�n de contexto de directorio deber�a usar DirectoryManager.getObjectInstance() antes de devolver un objeto al programa de usuario desde uno de los siguientes m�todos:
Aqu� tenemos como el ejemplo del directorio en forma de �rbol llama a getObjectInstance() en su m�todo 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;
}
De forma similar, cuando se devuelve una enumeraci�n generada por Context.listBindings() o las sobrecargas de DirContext.search(), deber�amos llamar a getObjectInstance() para el objeto de cada Binding o SearchResult de la enumeraci�n.
Aqu� est� la definici�n del m�todo next() de la enumeraci�n:
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);
}
�Almacenar Objetos
Una implementaci�n de contexto de directorio deber�a usar DirectoryManager.getStateToBind() antes de almacenar un objeto dado por el programa de usuario mediante uno de estos m�todos:
Aqu� tenemos c�mo el ejemplo del directorio en forma de �rbol llama a getStateToBind() en sus m�todos bind() y rebind():
// 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() devuelve un ejemplar de DirStateFactory.Result, que es un paquete que consiste en el objeto a unir y los atributos asociados con el objeto. Despu�s de recibir el resultado, la implementaci�n de contexto actualiza sus tablas de uniones y de atributos internas.