1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
38
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
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
87
88 public boolean hasMore() throws NamingException
89 {
90 return available;
91 }
92
93
94
95
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
128
129 public boolean hasMoreElements()
130 {
131 return available;
132 }
133
134
135
136
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 }