From ed08a89377a333c10202ead88d355e16bcb3a0fd Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 27 Feb 2014 23:31:10 +0000
Subject: [PATCH] Backport fix for OPENDJ-1197: API is lacking functionality to specify TCP connect timeout

---
 opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/AsynchronousFutureResult.java |   87 +++++++++++++++++++++++++++++++------------
 1 files changed, 63 insertions(+), 24 deletions(-)

diff --git a/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/AsynchronousFutureResult.java b/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/AsynchronousFutureResult.java
index efa811d..77b0588 100644
--- a/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/AsynchronousFutureResult.java
+++ b/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/AsynchronousFutureResult.java
@@ -22,6 +22,7 @@
  *
  *
  *      Copyright 2009-2010 Sun Microsystems, Inc.
+ *      Portions copyright 2013-2014 ForgeRock AS.
  */
 
 package com.forgerock.opendj.util;
@@ -161,34 +162,36 @@
             return getState() > 1;
         }
 
-        void innerSetErrorResult(final ErrorResultException errorResult) {
-            if (setStatePending()) {
-                this.errorResult = errorResult;
-
-                try {
-                    // Invoke error result completion handler.
-                    if (handler != null) {
-                        handler.handleErrorResult(errorResult);
-                    }
-                } finally {
-                    releaseShared(FAIL); // Publishes errorResult.
-                }
+        boolean innerSetErrorResult(final ErrorResultException errorResult) {
+            if (!setStatePending()) {
+                return false;
             }
+            this.errorResult = errorResult;
+            try {
+                // Invoke error result completion handler.
+                if (handler != null) {
+                    handler.handleErrorResult(errorResult);
+                }
+            } finally {
+                releaseShared(FAIL); // Publishes errorResult.
+            }
+            return true;
         }
 
-        void innerSetResult(final M result) {
-            if (setStatePending()) {
-                this.result = result;
-
-                try {
-                    // Invoke result completion handler.
-                    if (handler != null) {
-                        handler.handleResult(result);
-                    }
-                } finally {
-                    releaseShared(SUCCESS); // Publishes result.
-                }
+        boolean innerSetResult(final M result) {
+            if (!setStatePending()) {
+                return false;
             }
+            this.result = result;
+            try {
+                // Invoke result completion handler.
+                if (handler != null) {
+                    handler.handleResult(result);
+                }
+            } finally {
+                releaseShared(SUCCESS); // Publishes result.
+            }
+            return true;
         }
 
         private M get0() throws ErrorResultException {
@@ -323,6 +326,42 @@
     }
 
     /**
+     * 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

--
Gitblit v1.10.0