001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2009-2010 Sun Microsystems, Inc. 015 * Portions Copyright 2011-2016 ForgeRock AS. 016 */ 017package org.forgerock.opendj.ldap; 018 019import org.forgerock.opendj.ldap.requests.AddRequest; 020import org.forgerock.opendj.ldap.requests.BindRequest; 021import org.forgerock.opendj.ldap.requests.CompareRequest; 022import org.forgerock.opendj.ldap.requests.DeleteRequest; 023import org.forgerock.opendj.ldap.requests.ExtendedRequest; 024import org.forgerock.opendj.ldap.requests.ModifyDNRequest; 025import org.forgerock.opendj.ldap.requests.ModifyRequest; 026import org.forgerock.opendj.ldap.requests.SearchRequest; 027import org.forgerock.opendj.ldap.responses.BindResult; 028import org.forgerock.opendj.ldap.responses.CompareResult; 029import org.forgerock.opendj.ldap.responses.ExtendedResult; 030import org.forgerock.opendj.ldap.responses.Result; 031 032import static org.forgerock.opendj.ldap.LdapException.*; 033 034/** 035 * An abstract connection whose synchronous methods are implemented in terms of 036 * asynchronous methods. 037 */ 038public abstract class AbstractAsynchronousConnection extends AbstractConnection { 039 /** Creates a new abstract asynchronous connection. */ 040 protected AbstractAsynchronousConnection() { 041 // No implementation required. 042 } 043 044 @Override 045 public Result add(final AddRequest request) throws LdapException { 046 return blockingGetOrThrow(addAsync(request)); 047 } 048 049 @Override 050 public BindResult bind(final BindRequest request) throws LdapException { 051 return blockingGetOrThrow(bindAsync(request)); 052 } 053 054 @Override 055 public CompareResult compare(final CompareRequest request) throws LdapException { 056 return blockingGetOrThrow(compareAsync(request)); 057 } 058 059 @Override 060 public Result delete(final DeleteRequest request) throws LdapException { 061 return blockingGetOrThrow(deleteAsync(request)); 062 } 063 064 @Override 065 public <R extends ExtendedResult> R extendedRequest(final ExtendedRequest<R> request, 066 final IntermediateResponseHandler handler) throws LdapException { 067 return blockingGetOrThrow(extendedRequestAsync(request, handler)); 068 } 069 070 @Override 071 public Result modify(final ModifyRequest request) throws LdapException { 072 return blockingGetOrThrow(modifyAsync(request)); 073 } 074 075 @Override 076 public Result modifyDN(final ModifyDNRequest request) throws LdapException { 077 return blockingGetOrThrow(modifyDNAsync(request)); 078 } 079 080 @Override 081 public Result search(final SearchRequest request, final SearchResultHandler handler) throws LdapException { 082 return blockingGetOrThrow(searchAsync(request, handler)); 083 } 084 085 private <T extends Result> T blockingGetOrThrow(LdapPromise<T> promise) throws LdapException { 086 try { 087 return promise.getOrThrow(); 088 } catch (InterruptedException e) { 089 throw interrupted(e); 090 } 091 } 092 093 /** Handle thread interruption. */ 094 private LdapException interrupted(InterruptedException e) { 095 return newLdapException(ResultCode.CLIENT_SIDE_USER_CANCELLED, e); 096 } 097}