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.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
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 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
130
131 public boolean hasMoreElements()
132 {
133 return available;
134 }
135
136
137
138
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 }