I want to look up data in LDAP server. When I use the following code, it wants me to have NamingEnumeration (not List, HashMap) and also it forces me to use SearchResult type.
NamingEnumeration<SearchResult> values =
dirContext.search("cn=Loggers,cn=config", "(objectClass=*)", searchCtls);
When I try to use it, since it is NamingEnumeration type, I don't know how to change that to String. Is there a way to cast it to String? I want to use split() but it's not a String so doesn't seem to work.
for (NamingEnumeration<SearchResult> ne : searchResult) {
String a = searchResult.split(""); // I want to split.
if(a.length-1].equals("Logger")){
String logType = a[a.lenth-2];
try {
// and then , I will do something with logType
As you know, my Java basic is really weak. I'd appreciate any advice on how to change NamingEnumeration type to String? If there are many ways, I want to know.
The usual way to iterate a NamingEnumeration
is with hasMore()
and next()
.
NamingEnumeration<SearchResult> results =
dirContext.search("cn=Loggers,cn=config", "(objectClass=*)", searchCtls);
while (results.hasMore()) {
SearchResult result = results.next();
Attributes attributes = result.getAttributes();
Attribute cn = attributes.get("cn");
//get/iterate the values of the attribute
}
The "enhanced for statement (sometimes called the "for-each loop" statement)" cannot be used for them because they implement Enumeration
instead of the Iterable
-interface. The reason for this is mostly historic, NamingEnumeration
exists since Java 1.3, Iterable
since Java 1.5.