Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dfb4faea3064f541a58b88ecd261d45d97e9845b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.util;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

/* ------------------------------------------------------------ */
/**
 * TODO
 */
public class BlockingCallback implements Callback
{
    private static Throwable SUCCEEDED=new Throwable()
    {
        @Override
        public String toString() { return "SUCCEEDED"; }
    };
    
    private final CountDownLatch _latch = new CountDownLatch(1);
    private final AtomicReference<Throwable> _state = new AtomicReference<>();
    
    public BlockingCallback()
    {}

    @Override
    public void succeeded()
    {
        if (_state.compareAndSet(null,SUCCEEDED))
            _latch.countDown();
    }

    @Override
    public void failed(Throwable cause)
    {
        if (_state.compareAndSet(null,cause))
            _latch.countDown();
    }

    /** Block until the Callback has succeeded or failed and 
     * after the return leave in the state to allow reuse.
     * This is useful for code that wants to repeatable use a FutureCallback to convert
     * an asynchronous API to a blocking API. 
     * @throws IOException if exception was caught during blocking, or callback was cancelled 
     */
    public void block() throws IOException
    {
        try
        {
            _latch.await();
            Throwable state=_state.get();
            if (state==SUCCEEDED)
                return;
            if (state instanceof IOException)
                throw (IOException) state;
            if (state instanceof CancellationException)
                throw (CancellationException) state;
            throw new IOException(state);
        }
        catch (final InterruptedException e)
        {
            throw new InterruptedIOException(){{initCause(e);}};
        }
        finally
        {
            _state.set(null);
        }
    }
    
    
    @Override
    public String toString()
    {
        return String.format("%s@%x{%s}",BlockingCallback.class.getSimpleName(),hashCode(),_state.get());
    }

}

Back to the top