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.server.core.entry.ClonedServerEntry;
30  import org.apache.directory.server.core.entry.ServerEntryUtils;
31  import org.apache.directory.server.core.filtering.EntryFilteringCursor;
32  
33  
34  /**
35   * Adapts a Cursor over entries into a NamingEnumeration.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev$, $Date$
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          ClonedServerEntry entry = null;
100         
101         try
102         {
103             entry = cursor.get();
104             if ( cursor.next() )
105             {
106                 available = true;
107             }
108             else
109             {
110                 available = false;
111                 cursor.close();
112             }
113         }
114         catch ( Exception e )
115         {
116             JndiUtils.wrap( e );
117         }
118         
119         SearchResult result = new SearchResult( entry.getDn().getUpName(), null, 
120             ServerEntryUtils.toBasicAttributes( entry ) );
121         result.setRelative( false );
122         return result;
123     }
124 
125 
126     /* 
127      * @see Enumeration#hasMoreElements()
128      */
129     public boolean hasMoreElements()
130     {
131         return available;
132     }
133 
134 
135     /* 
136      * @see Enumeration#nextElement()
137      */
138     public SearchResult nextElement()
139     {
140         try
141         {
142             return next();
143         }
144         catch ( NamingException e )
145         {
146             throw new NoSuchElementException( e.getMessage() );
147         }
148     }
149 }