KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > remoteIterator > client > RemoteIteratorClient


1 /* ******************************************************************************** *
2  * Copyright (c) 2002 - 2004 Bright Side Factory. All rights reserved. *
3  * *
4  * Redistribution and use in source and binary forms, with or without modification, *
5  * are permitted provided that the following conditions are met: *
6  * *
7  * 1. Redistributions of source code must retain the above copyright notice, this *
8  * list of conditions and the following disclaimer. *
9  * *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, *
11  * this list of conditions and the following disclaimer in the documentation and/or *
12  * other materials provided with the distribution. *
13  * *
14  * 3. The end-user documentation included with the redistribution, if any, must *
15  * include the following acknowledgment: "This product includes software developed *
16  * by the Bright Side Factory (http://www.bs-factory.org/)." Alternately, this *
17  * acknowledgment may appear in the software itself, if and wherever such *
18  * third-party acknowledgments normally appear. *
19  * *
20  * 4. The names "Bright Side", "BS Framework" and "Bright Side Factory" must not be *
21  * used to endorse or promote products derived from this software without prior *
22  * written permission. For written permission, please contact info@bs-factory.org. *
23  * *
24  * 5. Products derived from this software may not be called "Bright Side", nor may *
25  * "Bright Side" appear in their name, without prior written permission of the *
26  * Bright Side Factory. *
27  * *
28  * THIS SOFTWARE IS PROVIDED ''AS IS'' BY THE COPYRIGHT OWNER AND ANY EXPRESSED OR *
29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT *
31  * SHALL THE COPYRIGHT OWNER OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, *
32  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
35  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE *
36  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED *
37  * OF THE POSSIBILITY OF SUCH DAMAGE. *
38  * *
39  * ================================================================================ *
40  * *
41  * This software consists of voluntary contributions made by many individuals on *
42  * behalf of the Bright Side Factory. For more information on the Bright Side *
43  * Factory, please see http://www.bs-factory.org. *
44  * *
45  * ******************************************************************************** */

46 package org.bsf.remoteIterator.client;
47
48 import org.bsf.remoteIterator.common.RemoteIteratorResultTable;
49 import org.bsf.remoteIterator.server.RemoteIterator;
50
51 import javax.ejb.RemoveException JavaDoc;
52 import java.io.Serializable JavaDoc;
53 import java.rmi.RemoteException JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.List JavaDoc;
56
57 /**
58  * Wrapper for a remoteIterator. To be used by the clients.
59  *
60  * @see org.bsf.remoteIterator.server.RemoteIteratorBean
61  * @see org.bsf.remoteIterator.common.RemoteIteratorResultTable
62  */

63 public class RemoteIteratorClient implements Serializable JavaDoc {
64     // *************************************************************************
65
// ****************************** Attributes *******************************
66
// *************************************************************************
67

68     private boolean _isLast = false;
69     private boolean _isFirst = true;
70
71     /**
72      * Total number of rows, -1 if not computed.
73      */

74     private int _rowCount = -1;
75
76     private RemoteIterator _remote = null;
77
78     // *************************************************************************
79
// ****************************** Constructor ******************************
80
// *************************************************************************
81

82     public RemoteIteratorClient( RemoteIterator p_remoteIteratorProxy ) throws IllegalArgumentException JavaDoc {
83         _remote = p_remoteIteratorProxy;
84     }
85
86     // *************************************************************************
87
// ****************************** Absolute *********************************
88
// *************************************************************************
89

90     public synchronized Iterator absolute( int p_position ) throws RemoteException JavaDoc {
91         if ( _remote == null ) return null;
92         return getResult( _remote.absolute( p_position ) ).iterator();
93     }
94
95     public synchronized Iterator absolute( int p_position, int p_nbRecords ) throws RemoteException JavaDoc {
96         if ( _remote == null ) return null;
97         return getResult( _remote.absolute( p_position, p_nbRecords ) ).iterator();
98     }
99     // *************************************************************************
100
// ******************************** Next ***********************************
101
// *************************************************************************
102

103     public synchronized Iterator next() throws RemoteException JavaDoc {
104         if ( _remote == null ) return null;
105         return getResult( _remote.next() ).iterator();
106     }
107
108     public synchronized Iterator next( int p_nbRecords ) throws RemoteException JavaDoc {
109         if ( _remote == null ) return null;
110         return getResult( _remote.next( p_nbRecords ) ).iterator();
111     }
112
113     // *************************************************************************
114
// ****************************** Previous *********************************
115
// *************************************************************************
116

117     public synchronized Iterator previous() throws RemoteException JavaDoc {
118         if ( _remote == null ) return null;
119         return getResult( _remote.previous() ).iterator();
120     }
121
122     public synchronized Iterator previous( int p_nbRecords ) throws RemoteException JavaDoc {
123         if ( _remote == null ) return null;
124         return getResult( _remote.previous( p_nbRecords ) ).iterator();
125     }
126
127     // *************************************************************************
128
// *************************** Utility Methods *****************************
129
// *************************************************************************
130

131     private synchronized List JavaDoc getResult( RemoteIteratorResultTable p_riResult ) {
132         _isLast = p_riResult.isLast();
133         _isFirst = p_riResult.isFirst();
134
135         return p_riResult.getRows();
136     }
137
138     /**
139      * Removes a RemoteItarator (releases the ressources).
140      *
141      * @see #hasRemoteReference
142      */

143     public synchronized void remove() throws RemoteException JavaDoc {
144         try {
145             if ( _remote != null ) {
146                 // We remove the RI
147
_remote.remove();
148             }
149         } catch( RemoveException JavaDoc e ) {
150             throw new RemoteException JavaDoc( e.getLocalizedMessage() );
151         } finally {
152             // No need to keep the ref to the remote
153
_remote = null;
154         }
155     }
156
157     /**
158      * @return true if this client has a remote reference, false otherwise.
159      * In other words, it returns true if the RemoteIterator has not been
160      * removed, false otherwise.
161      *
162      * @see #remove
163      */

164     public synchronized boolean hasRemoteReference() {
165         if ( _remote != null ) {
166             return true;
167         }
168
169         return false;
170     }
171
172     // *************************************************************************
173
// ******************************* Accessors *******************************
174
// *************************************************************************
175

176     /**
177      * @return a List of ColumnMetaData correpsonding to the executed query.
178      */

179     public synchronized List JavaDoc getColumnMetatData() throws RemoteException JavaDoc {
180         if ( _remote == null ) {
181             return null;
182         }
183
184         return _remote.getColumnMetaData();
185     }
186
187     /**
188      * @return the number of records returned by the request. -1 if no remote
189      * is set...
190      */

191     public synchronized int getRecordCount() throws RemoteException JavaDoc {
192         if ( _rowCount == -1 && _remote != null ) {
193             // We update the row count (the real one for sure)
194
_rowCount = _remote.getRowCount().intValue();
195         }
196
197         return _rowCount;
198     }
199
200     public synchronized boolean isLast() {
201         return _isLast;
202     }
203
204     public synchronized boolean isFirst() {
205         return _isFirst;
206     }
207 }
208
209
Popular Tags