View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.server.core.jndi;
21  
22  
23  import java.util.NoSuchElementException;
24  
25  import javax.naming.NamingEnumeration;
26  import javax.naming.NamingException;
27  import javax.naming.directory.SearchResult;
28  
29  import org.apache.directory.api.ldap.model.entry.Entry;
30  import org.apache.directory.api.ldap.util.JndiUtils;
31  import org.apache.directory.server.core.api.entry.ServerEntryUtils;
32  import org.apache.directory.server.core.api.filtering.EntryFilteringCursor;
33  
34  
35  /**
36   * Adapts a Cursor over entries into a NamingEnumeration.
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class NamingEnumerationAdapter implements NamingEnumeration<SearchResult>
41  {
42      private final EntryFilteringCursor cursor;
43      private boolean available = false;
44  
45  
46      public NamingEnumerationAdapter( EntryFilteringCursor cursor ) throws NamingException
47      {
48          this.cursor = cursor;
49          try
50          {
51              if ( !cursor.first() )
52              {
53                  cursor.close();
54                  available = false;
55              }
56              else
57              {
58                  available = true;
59              }
60          }
61          catch ( Exception e )
62          {
63              JndiUtils.wrap( e );
64          }
65      }
66  
67  
68      /* 
69       * @see NamingEnumeration#close()
70       */
71      public void close() throws NamingException
72      {
73          try
74          {
75              cursor.close();
76              available = false;
77          }
78          catch ( Exception e )
79          {
80              JndiUtils.wrap( e );
81          }
82      }
83  
84  
85      /* 
86       * @see NamingEnumeration#hasMore()
87       */
88      public boolean hasMore() throws NamingException
89      {
90          return available;
91      }
92  
93  
94      /* 
95       * @see NamingEnumeration#next()
96       */
97      public SearchResult next() throws NamingException
98      {
99          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 }