001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.directory.server.core.jndi;
021
022
023import java.util.NoSuchElementException;
024
025import javax.naming.NamingEnumeration;
026import javax.naming.NamingException;
027import javax.naming.directory.SearchResult;
028
029import org.apache.directory.api.ldap.model.entry.Entry;
030import org.apache.directory.api.ldap.util.JndiUtils;
031import org.apache.directory.server.core.api.entry.ServerEntryUtils;
032import org.apache.directory.server.core.api.filtering.EntryFilteringCursor;
033
034
035/**
036 * Adapts a Cursor over entries into a NamingEnumeration.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class NamingEnumerationAdapter implements NamingEnumeration<SearchResult>
041{
042    private final EntryFilteringCursor cursor;
043    private boolean available = false;
044
045
046    public NamingEnumerationAdapter( EntryFilteringCursor cursor ) throws NamingException
047    {
048        this.cursor = cursor;
049        try
050        {
051            if ( !cursor.first() )
052            {
053                cursor.close();
054                available = false;
055            }
056            else
057            {
058                available = true;
059            }
060        }
061        catch ( Exception e )
062        {
063            JndiUtils.wrap( e );
064        }
065    }
066
067
068    /* 
069     * @see NamingEnumeration#close()
070     */
071    public void close() throws NamingException
072    {
073        try
074        {
075            cursor.close();
076            available = false;
077        }
078        catch ( Exception e )
079        {
080            JndiUtils.wrap( e );
081        }
082    }
083
084
085    /* 
086     * @see NamingEnumeration#hasMore()
087     */
088    public boolean hasMore() throws NamingException
089    {
090        return available;
091    }
092
093
094    /* 
095     * @see NamingEnumeration#next()
096     */
097    public SearchResult next() throws NamingException
098    {
099        Entry entry = null;
100
101        try
102        {
103            entry = cursor.get();
104
105            if ( cursor.next() )
106            {
107                available = true;
108            }
109            else
110            {
111                available = false;
112                cursor.close();
113            }
114        }
115        catch ( Exception e )
116        {
117            JndiUtils.wrap( e );
118        }
119
120        SearchResult result = new SearchResult( entry.getDn().getName(), null,
121            ServerEntryUtils.toBasicAttributes( entry ) );
122        result.setRelative( false );
123
124        return result;
125    }
126
127
128    /* 
129     * @see Enumeration#hasMoreElements()
130     */
131    public boolean hasMoreElements()
132    {
133        return available;
134    }
135
136
137    /* 
138     * @see Enumeration#nextElement()
139     */
140    public SearchResult nextElement()
141    {
142        try
143        {
144            return next();
145        }
146        catch ( NamingException e )
147        {
148            throw new NoSuchElementException( e.getLocalizedMessage() );
149        }
150    }
151}