/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt * or http://forgerock.org/license/CDDLv1.0.html. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at legal-notices/CDDLv1_0.txt. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: * Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END * * * Copyright 2009-2010 Sun Microsystems, Inc. * Portions copyright 2013-2014 ForgeRock AS. */ package com.forgerock.opendj.util; import static org.forgerock.opendj.ldap.ErrorResultException.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.locks.AbstractQueuedSynchronizer; import org.forgerock.opendj.ldap.CancelledResultException; import org.forgerock.opendj.ldap.ErrorResultException; import org.forgerock.opendj.ldap.FutureResult; import org.forgerock.opendj.ldap.ResultCode; import org.forgerock.opendj.ldap.ResultHandler; /** * This class provides a skeletal implementation of the {@code FutureResult} * interface, to minimize the effort required to implement this interface. *
* This {@code FutureResult} implementation provides the following features: *
* The default implementation returns the request ID passed in during * construction, or -1 if none was provided. */ @Override public int getRequestID() { return requestID; } /** * Sets the error result associated with this future. If ({@code isDone() == * true}) then the error result will be ignored, otherwise the result * handler will be invoked if one was provided and, on return, any threads * waiting on {@link #get} will be released and the provided error result * will be thrown. * * @param errorResult * The error result. */ @Override public final void handleErrorResult(final ErrorResultException errorResult) { sync.innerSetErrorResult(errorResult); } /** * Sets the result associated with this future. If ({@code isDone() == true} * ) then the result will be ignored, otherwise the result handler will be * invoked if one was provided and, on return, any threads waiting on * {@link #get} will be released and the provided result will be returned. * * @param result * The result. */ @Override public final void handleResult(final M result) { sync.innerSetResult(result); } /** * Attempts to set the error result associated with this future. If (i.e. * {@code isDone() == true}) then the error result will be ignored and * {@code false} will be returned, otherwise the result handler will be * invoked if one was provided and, on returning {@code true}, any threads * waiting on {@link #get} will be released and the provided error result * will be thrown. * * @param errorResult * The error result. * @return {@code false} if this future has already been completed, either * due to normal termination, an exception, or cancellation (i.e. * {@code isDone() == true}). */ public final boolean tryHandleErrorResult(final ErrorResultException errorResult) { return sync.innerSetErrorResult(errorResult); } /** * Attempts to set the result associated with this future. If (i.e. * {@code isDone() == true}) then the result will be ignored and * {@code false} will be returned, otherwise the result handler will be * invoked if one was provided and, on returning {@code true}, any threads * waiting on {@link #get} will be released and the provided result will be * returned. * * @param result * The result. * @return {@code false} if this future has already been completed, either * due to normal termination, an exception, or cancellation (i.e. * {@code isDone() == true}). */ public final boolean tryHandleResult(final M result) { return sync.innerSetResult(result); } /** * {@inheritDoc} */ @Override public final boolean isCancelled() { return sync.innerIsCancelled(); } /** * {@inheritDoc} */ @Override public final boolean isDone() { return sync.innerIsDone(); } /** * Invoked when {@link #cancel} is called and {@code isDone() == false} and * immediately before any threads waiting on {@link #get} are released. * Implementations may choose to return a custom error result if needed or * return {@code null} if the following default error result is acceptable: * *
* Result result = Responses.newResult(ResultCode.CLIENT_SIDE_USER_CANCELLED);
*
*
* In addition, implementations may perform other cleanup, for example, by
* issuing an LDAP abandon request. The default implementation is to do
* nothing.
*
* @param mayInterruptIfRunning
* {@code true} if the thread executing executing the response
* handler should be interrupted; otherwise, in-progress response
* handlers are allowed to complete.
* @return The custom error result, or {@code null} if the default is
* acceptable.
*/
protected ErrorResultException handleCancelRequest(final boolean mayInterruptIfRunning) {
// Do nothing by default.
return null;
}
/**
* Indicates whether this future result can be canceled.
*
* @return {@code true} if this future result is cancelable or {@code false}
* otherwise.
*/
protected boolean isCancelable() {
// Return true by default.
return true;
}
/**
* Appends a string representation of this future's state to the provided
* builder.
*
* @param sb
* The string builder.
*/
protected void toString(final StringBuilder sb) {
sb.append(" state = ");
sb.append(sync);
}
}