|
Spec-Zone .ru
спецификации, руководства, описания, API
|
SearchControls значения по умолчанию определяет, что поиск должен быть выполнен в именованном контексте (). Это значение по умолчанию используется в примерах в
В дополнение к этому значению по умолчанию можно определить, что поиск выполняется во всем поддереве или только в именованном объекте.
Поиск всего поддерева ищет именованный объект и всех его потомков. Чтобы заставить поиск вести себя таким образом, передайте SearchControls.SUBTREE_SCOPE к следующим образом.
// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "telephonenumber", "golfhandicap", "mail"};
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Specify the search filter to match
// Ask for objects that have the attribute "sn" == "Geisel"
// and the "mail" attribute
String filter = "(&(sn=Geisel)(mail=*))";
// Search the subtree for objects by using the filter
NamingEnumeration answer = ctx.search("", filter, ctls);
# java SearchSubtree >>>cn=Ted Geisel, ou=People attribute: sn value: Geisel attribute: mail value: Ted.Geisel@JNDITutorial.example.com attribute: telephonenumber value: +1 408 555 5252
Можно также искать именованный объект. Это полезно, например, чтобы протестировать, удовлетворяет ли именованный объект фильтр поиска. Чтобы искать именованный объект, передайте SearchControls.OBJECT_SCOPE к setSearchScope().
// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "telephonenumber", "golfhandicap", "mail"};
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
// Specify the search filter to match
// Ask for objects that have the attribute "sn" == "Geisel"
// and the "mail" attribute
String filter = "(&(sn=Geisel)(mail=*))";
// Search the subtree for objects by using the filter
NamingEnumeration answer =
ctx.search("cn=Ted Geisel, ou=People", filter, ctls);
# java SearchObject >>> attribute: sn value: Geisel attribute: mail value: Ted.Geisel@JNDITutorial.example.com attribute: telephonenumber value: +1 408 555 5252
Пример, найденный одним ответом и напечатанный это. Заметьте, что имя результата является пустой строкой. Это - то, потому что имя объекта всегда называют относительно контекста поиска (в этом случае, "cn=Ted Geisel, ou=People").