KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > db > jdbm > JdbmTupleBrowser


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.jdbm;
18
19
20 import org.apache.ldap.server.db.Tuple;
21 import org.apache.ldap.server.db.TupleBrowser;
22
23 import javax.naming.NamingException JavaDoc;
24 import java.io.IOException JavaDoc;
25
26
27 /**
28  * TupleBrowser wrapper for Jdbm based TupleBrowsers.
29  *
30  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
31  * @version $Rev: 169198 $
32  */

33 public class JdbmTupleBrowser implements TupleBrowser
34 {
35     /** underlying wrapped jdbm.helper.TupleBrowser */
36     private jdbm.helper.TupleBrowser jdbmBrowser;
37     /** safe temp jdbm.helper.Tuple used to store next/previous tuples */
38     private jdbm.helper.Tuple jdbmTuple = new jdbm.helper.Tuple();
39     
40     
41     /**
42      * Creates a JdbmTupleBrowser.
43      *
44      * @param jdbmBrowser JDBM browser to wrap.
45      */

46     public JdbmTupleBrowser( jdbm.helper.TupleBrowser jdbmBrowser )
47     {
48         this.jdbmBrowser = jdbmBrowser;
49     }
50     
51     
52     /**
53      * @see TupleBrowser#getNext(org.apache.ldap.server.db.Tuple)
54      */

55     public boolean getNext( Tuple tuple ) throws NamingException JavaDoc
56     {
57         boolean isSuccess = false;
58         
59         synchronized ( jdbmTuple )
60         {
61             try
62             {
63                 isSuccess = jdbmBrowser.getNext( jdbmTuple );
64             }
65             catch ( IOException JavaDoc ioe )
66             {
67                 NamingException JavaDoc ne = new NamingException JavaDoc(
68                     "Failed on call to jdbm TupleBrowser.getNext()" );
69                 ne.setRootCause( ioe );
70                 throw ne;
71             }
72             
73             if ( isSuccess )
74             {
75                 tuple.setKey( jdbmTuple.getKey() );
76                 tuple.setValue( jdbmTuple.getValue() );
77             }
78         }
79
80         return isSuccess;
81     }
82     
83     
84     /**
85      * @see TupleBrowser#getPrevious(Tuple)
86      */

87     public boolean getPrevious( Tuple tuple ) throws NamingException JavaDoc
88     {
89         boolean isSuccess = false;
90         
91         synchronized ( jdbmTuple )
92         {
93             try
94             {
95                 isSuccess = jdbmBrowser.getPrevious( jdbmTuple );
96             }
97             catch ( IOException JavaDoc ioe )
98             {
99                 NamingException JavaDoc ne = new NamingException JavaDoc(
100                     "Failed on call to jdbm TupleBrowser.getPrevious()" );
101                 ne.setRootCause( ioe );
102                 throw ne;
103             }
104             
105             if ( isSuccess )
106             {
107                 tuple.setKey( jdbmTuple.getKey() );
108                 tuple.setValue( jdbmTuple.getValue() );
109             }
110         }
111
112         return isSuccess;
113     }
114 }
115
Popular Tags