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.jdbm;
18  
19  
20  import java.io.IOException;
21  
22  import javax.naming.NamingException;
23  
24  import org.apache.ldap.server.partition.impl.btree.Tuple;
25  import org.apache.ldap.server.partition.impl.btree.TupleBrowser;
26  
27  
28  /***
29   * TupleBrowser wrapper for Jdbm based TupleBrowsers.
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 264732 $
33   */
34  public class JdbmTupleBrowser implements TupleBrowser
35  {
36      /*** underlying wrapped jdbm.helper.TupleBrowser */
37      private jdbm.helper.TupleBrowser jdbmBrowser;
38      /*** safe temp jdbm.helper.Tuple used to store next/previous tuples */ 
39      private jdbm.helper.Tuple jdbmTuple = new jdbm.helper.Tuple();
40      
41      
42      /***
43       * Creates a JdbmTupleBrowser.
44       *
45       * @param jdbmBrowser JDBM browser to wrap.
46       */
47      public JdbmTupleBrowser( jdbm.helper.TupleBrowser jdbmBrowser )
48      {
49          this.jdbmBrowser = jdbmBrowser;
50      }
51      
52      
53      /***
54       * @see TupleBrowser#getNext(org.apache.ldap.server.partition.impl.btree.Tuple)
55       */
56      public boolean getNext( Tuple tuple ) throws NamingException
57      {
58          boolean isSuccess = false;
59          
60          synchronized ( jdbmTuple )
61          {
62              try
63              {
64                  isSuccess = jdbmBrowser.getNext( jdbmTuple );
65              }
66              catch ( IOException ioe )
67              {
68                  NamingException ne = new NamingException( 
69                      "Failed on call to jdbm TupleBrowser.getNext()" );
70                  ne.setRootCause( ioe );
71                  throw ne;
72              }
73              
74              if ( isSuccess )
75              {
76                  tuple.setKey( jdbmTuple.getKey() );
77                  tuple.setValue( jdbmTuple.getValue() );
78              }
79          }
80  
81          return isSuccess;
82      }
83      
84      
85      /***
86       * @see TupleBrowser#getPrevious(Tuple)
87       */
88      public boolean getPrevious( Tuple tuple ) throws NamingException
89      {
90          boolean isSuccess = false;
91          
92          synchronized ( jdbmTuple )
93          {
94              try
95              {
96                  isSuccess = jdbmBrowser.getPrevious( jdbmTuple );
97              }
98              catch ( IOException ioe )
99              {
100                 NamingException ne = new NamingException( 
101                     "Failed on call to jdbm TupleBrowser.getPrevious()" );
102                 ne.setRootCause( ioe );
103                 throw ne;
104             }
105             
106             if ( isSuccess )
107             {
108                 tuple.setKey( jdbmTuple.getKey() );
109                 tuple.setValue( jdbmTuple.getValue() );
110             }
111         }
112 
113         return isSuccess;
114     }
115 }