KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > db > SearchResultEnumeration


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.ldap.server.db;
18
19
20 import org.apache.ldap.common.message.LockableAttributesImpl;
21
22 import javax.naming.NamingEnumeration JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24 import javax.naming.directory.Attribute JavaDoc;
25 import javax.naming.directory.Attributes JavaDoc;
26
27
28 /**
29  * An enumeration that transforms another underlying enumeration over a set of
30  * IndexRecords into an enumeration over a set of SearchResults. Note that the
31  * SearchResult created may not be complete and other parts of the system may
32  * modify it before return. This enumeration simply creates a new copy of the
33  * entry to return stuffing it with the attributes that were specified. This is
34  * all that it does now but this may change later.
35  *
36  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
37  * @version $Rev: 169198 $
38  */

39 public class SearchResultEnumeration implements NamingEnumeration JavaDoc
40 {
41     /** Database used to lookup entries from */
42     private Database db = null;
43     /** base of search for relative names */
44     /** the attributes to return */
45     private final String JavaDoc [] attrIds;
46     /** underlying enumeration over IndexRecords */
47     private final NamingEnumeration JavaDoc underlying;
48
49
50     /**
51      * Creates an enumeration that returns entries packaged within SearchResults
52      * using the search parameters supplied to a search call.
53      *
54      * @param attrIds the returned attributes
55      * @param underlying the enumeration over IndexRecords
56      */

57     public SearchResultEnumeration( String JavaDoc [] attrIds,
58                                     NamingEnumeration JavaDoc underlying,
59                                     Database db )
60     {
61         this.db = db;
62         this.attrIds = attrIds;
63         this.underlying = underlying;
64     }
65     
66     
67     /**
68      * @see javax.naming.NamingEnumeration#close()
69      */

70     public void close() throws NamingException JavaDoc
71     {
72         underlying.close();
73     }
74
75     
76     /**
77      * @see javax.naming.NamingEnumeration#hasMore()
78      */

79     public boolean hasMore() throws NamingException JavaDoc
80     {
81         return underlying.hasMore();
82     }
83
84    
85     /**
86      * @see javax.naming.NamingEnumeration#next()
87      */

88     public Object JavaDoc next() throws NamingException JavaDoc
89     {
90         IndexRecord rec = ( IndexRecord ) underlying.next();
91         Attributes JavaDoc entry;
92         String JavaDoc name = db.getEntryUpdn( rec.getEntryId() );
93         
94         if ( null == rec.getAttributes() )
95         {
96             rec.setAttributes( db.lookup( rec.getEntryId() ) );
97         }
98
99         if ( attrIds == null )
100         {
101             entry = ( Attributes JavaDoc ) rec.getAttributes().clone();
102         }
103         else
104         {
105             entry = new LockableAttributesImpl();
106
107             for ( int ii = 0; ii < attrIds.length; ii++ )
108             {
109                 // there is no attribute by that name in the entry so we continue
110
if ( null == rec.getAttributes().get( attrIds[ii] ) )
111                 {
112                     continue;
113                 }
114
115                 // clone attribute to stuff into the new resultant entry
116
Attribute JavaDoc attr = ( Attribute JavaDoc ) rec.getAttributes().get( attrIds[ii] ).clone();
117                 entry.put( attr );
118             }
119         }
120
121         return new DbSearchResult( rec.getEntryId(), name, null, entry );
122     }
123
124     
125     /**
126      * @see java.util.Enumeration#hasMoreElements()
127      */

128     public boolean hasMoreElements()
129     {
130         return underlying.hasMoreElements();
131     }
132
133
134     /**
135      * @see java.util.Enumeration#nextElement()
136      */

137     public Object JavaDoc nextElement()
138     {
139         return underlying.nextElement();
140     }
141 }
142
Popular Tags