View Javadoc

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.partition.impl.btree;
18  
19  
20  import javax.naming.NamingEnumeration;
21  import javax.naming.NamingException;
22  import javax.naming.directory.Attribute;
23  import javax.naming.directory.Attributes;
24  
25  import org.apache.ldap.common.message.LockableAttributesImpl;
26  import org.apache.ldap.server.enumeration.SearchResultEnumeration;
27  
28  
29  /***
30   * An enumeration that transforms another underlying enumeration over a set of 
31   * IndexRecords into an enumeration over a set of SearchResults.  Note that the
32   * SearchResult created may not be complete and other parts of the system may
33   * modify it before return.  This enumeration simply creates a new copy of the 
34   * entry to return stuffing it with the attributes that were specified.  This is
35   * all that it does now but this may change later.
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev: 226451 $
39   */
40  public class BTreeSearchResultEnumeration implements SearchResultEnumeration
41  {
42      /*** Database used to lookup entries from */
43      private BTreeContextPartition partition = null;
44      /*** base of search for relative names */
45      /*** the attributes to return */
46      private final String [] attrIds;
47      /*** underlying enumeration over IndexRecords */
48      private final NamingEnumeration underlying;
49  
50  
51      /***
52       * Creates an enumeration that returns entries packaged within SearchResults
53       * using the search parameters supplied to a search call.
54       * 
55       * @param attrIds the returned attributes
56       * @param underlying the enumeration over IndexRecords
57       */
58      public BTreeSearchResultEnumeration( String [] attrIds, 
59                                      NamingEnumeration underlying,
60                                      BTreeContextPartition db )
61      {
62          this.partition = db;
63          this.attrIds = attrIds;
64          this.underlying = underlying;
65      }
66      
67      
68      /***
69       * @see javax.naming.NamingEnumeration#close()
70       */
71      public void close() throws NamingException
72      {
73          underlying.close();
74      }
75  
76      
77      /***
78       * @see javax.naming.NamingEnumeration#hasMore()
79       */
80      public boolean hasMore() throws NamingException
81      {
82          return underlying.hasMore();
83      }
84  
85     
86      /***
87       * @see javax.naming.NamingEnumeration#next()
88       */
89      public Object next() throws NamingException
90      {
91          IndexRecord rec = ( IndexRecord ) underlying.next();
92          Attributes entry;
93          String name = partition.getEntryUpdn( rec.getEntryId() );
94          
95          if ( null == rec.getAttributes() )
96          {
97              rec.setAttributes( partition.lookup( rec.getEntryId() ) );
98          }
99  
100         if ( attrIds == null )
101         {
102             entry = ( Attributes ) rec.getAttributes().clone();
103         }
104         else
105         {
106             entry = new LockableAttributesImpl();
107 
108             for ( int ii = 0; ii < attrIds.length; ii++ )
109             {
110                 // there is no attribute by that name in the entry so we continue
111                 if ( null == rec.getAttributes().get( attrIds[ii] ) )
112                 {
113                     continue;
114                 }
115 
116                 // clone attribute to stuff into the new resultant entry
117                 Attribute attr = ( Attribute ) rec.getAttributes().get( attrIds[ii] ).clone();
118                 entry.put( attr );
119             }
120         }
121 
122         return new BTreeSearchResult( rec.getEntryId(), name, null, entry );
123     }
124 
125     
126     /***
127      * @see java.util.Enumeration#hasMoreElements()
128      */
129     public boolean hasMoreElements()
130     {
131         return underlying.hasMoreElements();
132     }
133 
134 
135     /***
136      * @see java.util.Enumeration#nextElement()
137      */
138     public Object nextElement()
139     {
140         return underlying.nextElement();
141     }
142 }