En esta página: URLs como Nombres Devueltos por Enumeraciones Podemos enumerar los contenidos de un contexto usando list(), listBindings(), o search(). Cuando llamamos a uno de estos métodos, obtenemos de vuelta una NamingEnumeration. Cada ítem de la enumeración es un ejemplar de NameClassPair o de una de sus subclases. Para obtener el nombre del ítem, es decir, el nombre del objeto en relación al contexto fuente (el contexto que hemos listado o buscado), usamos NameClassPair.getName(). El string del nombre devuelto por este método es un nombre mixto. Por ejemplo, deberíamos poder volver a alimentar con este nombre los métodos de Context del contexto fuente. Sin embargo, algunas veces el servicio o preveedor de servicio subyacente no puede devolver un nombre en relación al contexto fuente, por ejemplo, si el ítem fue recuperado siguiendo una referencia o un alias. Cuando no se puede devolver un nombre relativo, usamos esta string URL para pasarla a los métodos de InitialContext , como se desribió en la página anterior. Para dererminar si el nombre devuelto por getName() es relativo, usamos NameClassPair.isRelative(). Abajo tenemos un ejemplo que busca en un contexto entradas cuyo atributo "cn" empiece con la letra "S." Luego recupera el atributo "telephonenumber" del ítem usando DirContext.getAttributes(). Podríamos haber hecho esto más fácilmente usando el argumento SearchControls para la petición de atributos. Aquí, el atributo recuperado se ha separado para ilustrar el uso de isRelative(). Cuando el ejemplo obtiene un ítem que contiene un string URL como un nombre (es decir, isRelative() devuelve false), usa InitialContext para procesar el string URL. // Set up the environment for creating the initial context Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:489/o=JNDItutorial"); // Enable referrals so that we get some nonrelative names env.put(Context.REFERRAL, "follow"); // Create the initial context DirContext initCtx = new InitialDirContext(env); // Get the target context DirContext targetCtx = (DirContext)initCtx.lookup("ou=All"); SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); // Perform the search on the target context NamingEnumeration enum = targetCtx.search("", "(cn=S*)", constraints); Attributes attrs; NameClassPair item; String[] attrIds = new String[]{"telephonenumber"}; // For each answer found, get its "telephonenumber" attribute // If relative, resolve it relative to the target context // If not relative, resolve it relative to the initial context while (enum.hasMore()) { item = (NameClassPair)enum.next(); System.out.println(">>>>>" + item.getName() + " "); if (item.isRelative()) { attrs = targetCtx.getAttributes(item.getName(), attrIds); } else { attrs = initCtx.getAttributes(item.getName(), attrIds); } System.out.println(attrs); } Aquí tenemos la salida al ejecutar este programa. >>>>>ldap://localhost:389/cn=Scott Seligman, ou=People, o=JNDITutorial {telephonenumber=telephonenumber: +1 408 555 5252} >>>>>ldap://localhost:389/cn=Samuel Clemens, ou=People, o=JNDITutorial {telephonenumber=telephonenumber: +1 408 555 0186} >>>>>ldap://localhost:389/cn=Spuds Mackenzie, ou=People, o=JNDITutorial {telephonenumber=telephonenumber: +1 408 555 4420} >>>>>ldap://localhost:389/cn=S. User,ou=NewHires,o=JNDITutorial No attributes También puedes ir a la lección Referenciadores o a la sección Des-referenciar Alias para ver más ejemplos y descripciones.
|