directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Lecharny <elecha...@apache.org>
Subject Re: Using a CoreSession in embeded mode to perform a search
Date Thu, 21 May 2009 21:58:59 GMT
Stefan Zoerner wrote:
> Hi all,
Hi Stefan,
>
> currently I implement an Apache Tomcat Realm which embeds ApacheDS and 
> uses it for authentication and authorization. The thing already works 
> and looks quite promising, I will post a first version at weekend on 
> this list in order to get some feedback.
>
> During implementation, I tried to avoid JNDI calls and used the 
> CoreSession interface which I can get from DirectoryService.
>
> Code (which searches a user entry by uid) goes like this:

>
> ...
> CoreSession adminSession = directoryService.getAdminSession();
>
> String base = "ou=system";
> LdapDN basedn = new LdapDN(base);
> SearchScope scope = SearchScope.SUBTREE;
> MessageFormat f = new MessageFormat(
>         "(&(objectClass=person)(uid={0}))");
> String sFilter = f.format(new Object[] { username });
>
> ExprNode filter = FilterParser.parse(sFilter);
> EntryFilteringCursor cursor = adminSession.search(basedn, scope,
>         filter, AliasDerefMode.NEVER_DEREF_ALIASES, null, 0, 0);
> CursorIterator iter = new CursorIterator(cursor);
> if (iter.hasNext()) {
>     ServerEntry entry = (ServerEntry) iter.next();
>     dn = entry.getDn();
> }
> adminSession.unbind();
> ...
>
> I am not happy with the use of CursorIterator (especially the cast). 
> Anybody a better idea how to perform a search and process the results 
> from an embedded server?

You don't have to use a CursorIterator. Once you get a cursor, you can 
do something like :

cursor.beforeFirst(); // To position the cursor at the beginning

while (cursor.next() )
{
    ServerEntry entry = cursor.get();
}

No need to cast, and the entry you get is a clone.


-- 
--
cordialement, regards,
Emmanuel L├ęcharny
www.iktek.com
directory.apache.org



Mime
View raw message