Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 33656 invoked from network); 2 Mar 2010 14:24:15 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 2 Mar 2010 14:24:15 -0000 Received: (qmail 54135 invoked by uid 500); 2 Mar 2010 14:24:11 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 54077 invoked by uid 500); 2 Mar 2010 14:24:11 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 54070 invoked by uid 99); 2 Mar 2010 14:24:11 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Mar 2010 14:24:11 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Mar 2010 14:24:09 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E55EA23889BF; Tue, 2 Mar 2010 14:23:47 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r918050 - /directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/SearchCursor.java Date: Tue, 02 Mar 2010 14:23:47 -0000 To: commits@directory.apache.org From: kayyagari@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100302142347.E55EA23889BF@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kayyagari Date: Tue Mar 2 14:23:47 2010 New Revision: 918050 URL: http://svn.apache.org/viewvc?rev=918050&view=rev Log: a forward only cursor implementation to serve the SearchRespons ojects from the client-api Added: directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/SearchCursor.java Added: directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/SearchCursor.java URL: http://svn.apache.org/viewvc/directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/SearchCursor.java?rev=918050&view=auto ============================================================================== --- directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/SearchCursor.java (added) +++ directory/clients/ldap/trunk/ldap-client-api/src/main/java/org/apache/directory/ldap/client/api/SearchCursor.java Tue Mar 2 14:23:47 2010 @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.directory.ldap.client.api; + + +import java.util.concurrent.TimeUnit; + +import org.apache.directory.ldap.client.api.exception.LdapException; +import org.apache.directory.ldap.client.api.future.SearchFuture; +import org.apache.directory.ldap.client.api.message.SearchResponse; +import org.apache.directory.ldap.client.api.message.SearchResultDone; +import org.apache.directory.shared.ldap.cursor.AbstractCursor; +import org.apache.directory.shared.ldap.cursor.InvalidCursorPositionException; + + +/** + * An implementation of Cursor based on the underlying SearchFuture instance. + * + * Note: This is a forward only cursor hence the only valid operations are next(), get() and close() + * + * @author Apache Directory Project + * @version $Rev$, $Date$ + */ +public class SearchCursor extends AbstractCursor +{ + + /** the search future */ + private SearchFuture future; + + /** wait time while polling for a SearchResponse */ + private long timeout; + + /** time units of timeout value */ + private TimeUnit timeUnit; + + /** a reference to hold the retrieved SearchResponse object from SearchFuture */ + private SearchResponse response; + + + public SearchCursor( SearchFuture future, long timeout, TimeUnit timeUnit ) + { + this.future = future; + this.timeout = timeout; + this.timeUnit = timeUnit; + } + + + public boolean next() throws Exception + { + try + { + response = future.get( timeout, timeUnit ); + } + catch ( Exception e ) + { + LdapException ldapException = new LdapException( LdapConnection.NO_RESPONSE_ERROR ); + ldapException.initCause( e ); + + // Send an abandon request + if ( !future.isCancelled() ) + { + future.cancel( true ); + } + + // close the cursor + close( ldapException ); + + throw ldapException; + } + + if ( response == null ) + { + future.cancel( true ); + + throw new LdapException( LdapConnection.TIME_OUT_ERROR ); + } + + boolean done = ( response instanceof SearchResultDone ); + + if ( done ) + { + response = null; + } + + return !done; + } + + + public SearchResponse get() throws Exception + { + if ( !available() ) + { + throw new InvalidCursorPositionException(); + } + + return response; + } + + + public boolean isElementReused() + { + return true; + } + + + public boolean available() + { + return response != null; + } + + + @Override + public void close() throws Exception + { + close( null ); + } + + + @Override + public void close( Exception cause ) throws Exception + { + if ( !future.isCancelled() ) + { + future.cancel( true ); + } + + if ( cause != null ) + { + super.close( cause ); + } + else + { + super.close(); + } + } + + + // rest of all operations will throw UnsupportedOperationException + public void after( Object element ) throws Exception + { + throw new UnsupportedOperationException(); + } + + + public void afterLast() throws Exception + { + throw new UnsupportedOperationException(); + } + + + public void before( Object element ) throws Exception + { + throw new UnsupportedOperationException(); + } + + + public void beforeFirst() throws Exception + { + throw new UnsupportedOperationException(); + } + + + public boolean first() throws Exception + { + throw new UnsupportedOperationException(); + } + + + public boolean last() throws Exception + { + throw new UnsupportedOperationException(); + } + + + public boolean previous() throws Exception + { + throw new UnsupportedOperationException(); + } + +}