2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.io.IOException;
22 import java.util.concurrent.CancellationException;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Future;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 import java.util.concurrent.atomic.AtomicBoolean;
30 public class FutureCallback implements Future<Void>,Callback
32 private static Throwable COMPLETED=new Throwable();
33 private final AtomicBoolean _done=new AtomicBoolean(false);
34 private final CountDownLatch _latch=new CountDownLatch(1);
35 private Throwable _cause;
37 public FutureCallback()
40 public FutureCallback(boolean completed)
50 public FutureCallback(Throwable failed)
58 public void succeeded()
60 if (_done.compareAndSet(false,true))
68 public void failed(Throwable cause)
70 if (_done.compareAndSet(false,true))
78 public boolean cancel(boolean mayInterruptIfRunning)
80 if (_done.compareAndSet(false,true))
82 _cause=new CancellationException();
90 public boolean isCancelled()
98 catch (InterruptedException e)
100 throw new RuntimeException(e);
102 return _cause instanceof CancellationException;
108 public boolean isDone()
110 return _done.get() && _latch.getCount()==0;
114 public Void get() throws InterruptedException, ExecutionException
117 if (_cause==COMPLETED)
119 if (_cause instanceof CancellationException)
120 throw (CancellationException) new CancellationException().initCause(_cause);
121 throw new ExecutionException(_cause);
125 public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
127 if (!_latch.await(timeout,unit))
128 throw new TimeoutException();
130 if (_cause==COMPLETED)
132 if (_cause instanceof TimeoutException)
133 throw (TimeoutException)_cause;
134 if (_cause instanceof CancellationException)
135 throw (CancellationException) new CancellationException().initCause(_cause);
136 throw new ExecutionException(_cause);
139 public static void rethrow(ExecutionException e) throws IOException
141 Throwable cause=e.getCause();
142 if (cause instanceof IOException)
143 throw (IOException)cause;
144 if (cause instanceof Error)
146 if (cause instanceof RuntimeException)
147 throw (RuntimeException)cause;
148 throw new RuntimeException(cause);
152 public String toString()
154 return String.format("FutureCallback@%x{%b,%b}",hashCode(),_done,_cause==COMPLETED);