Programación en castellano
Inicio > Tutoriales > J2EE > Sistema de Nombrado en Java (JNDI) [Parte I]
-Tutoriales

Sistema de Nombrado en Java (JNDI) [Parte I]


Búsqueda Básica

La forma más sencilla de búsqueda requiere que especifiquemos un conjunto de atributos que la entrada debe tener y el nombre del contexto fuente en el que realizar la búsqueda.

El siguiente código crea un conjunto de atrivutos matchAttrs, que tiene dos atributos "telephonenumber" y "mail".

Especifica que las entradas cualificadas deben tener un atributo apellido ("sn") con un valor de "Geisel" y un atributo "mail" con cualquier valor. Luego llama a  DirContext.search() para buscar las entradas en el contexto "ou=People" que tienen los atributos especificados por matchAttrs.

// Specify the attributes to match
// Ask for objects that has a surname ("sn") attribute with 
// the value "Geisel" and the "mail" attribute
Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case
matchAttrs.put(new BasicAttribute("sn", "Geisel"));
matchAttrs.put(new BasicAttribute("mail"));

// Search for objects that have those matching attributes
NamingEnumeration answer = ctx.search("ou=People", matchAttrs);

Podemos imprimir los resultados de esta forma.

while (answer.hasMore()) {
    SearchResult sr = (SearchResult)answer.next();
    System.out.println(">>>" + sr.getName());
    printAttrs(sr.getAttributes());
}

printAttrs() es similar al código del ejemplo getAttributes()  que imprime un conjunto de atributos.

Ejecutar este ejemplo produce la siguiente salida.

# java SearchRetAll
>>>cn=Ted Geisel
attribute: sn
value: Geisel
attribute: objectclass
value: top
value: person
value: organizationalPerson
value: inetOrgPerson
attribute: jpegphoto
value: [B@1dacd78b
attribute: mail
value: Ted.Geisel@JNDITutorial.com
attribute: facsimiletelephonenumber
value: +1 408 555 2329
attribute: cn
value: Ted Geisel
attribute: telephonenumber
value: +1 408 555 5252

. Devolver los Atributos Seleccionados

El ejemplo anterior devuelve todos los atributos asociados con las entradas que satisfacen la consulta especificada. Podemos seleccionar los atributos devueltos pasando a search() un array de identificadores de atributos que queremos incluir en los resultados. Después de crear un matchAttrs como se vió anteriormente, también necesitamos crear el array de identificadores de atributos, como se muestra aquí.

// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "telephonenumber", "golfhandicap", "mail"};

// Search for objects that have those matching attributes
NamingEnumeration answer = ctx.search("ou=People", matchAttrs, attrIDs);

Este ejemplo devuelve los atributos "sn", "telephonenumber", "golfhandicap", y "mail" de las entradas que tienen un atributo "mail" y tienen un atributo "sn" con el valor "Geisel".

Este ejemplo produce la siguiente salida. (La entrada no tiene un atributo "golfhandicap", por eso no se devuelve.)

# java Search 
>>>cn=Ted Geisel
attribute: sn
value: Geisel
attribute: mail
value: Ted.Geisel@JNDITutorial.com
attribute: telephonenumber
value: +1 408 555 5252
 
Patrocinados
 

Copyright © 1999-2007 Programación en castellano. Todos los derechos reservados.
Formulario de Contacto - Datos legales - Publicidad

Hospedaje web y servidores dedicados linux por Ferca Network

red internet: musica mp3 | logos y melodias | hospedaje web linux | registro de dominios | servidores dedicados
más internet: comprar | recursos gratis | posicionamiento en buscadores | tienda virtual | gifs animados