1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.util;
20
21 import java.io.IOException;
22 import java.io.InterruptedIOException;
23 import java.util.concurrent.CancellationException;
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.atomic.AtomicReference;
26
27 import org.eclipse.jetty.util.log.Log;
28 import org.eclipse.jetty.util.log.Logger;
29
30
31
32
33 @Deprecated
34 public class BlockingCallback implements Callback.NonBlocking
35 {
36 private static final Logger LOG = Log.getLogger(BlockingCallback.class);
37 private static Throwable SUCCEEDED = new Throwable()
38 {
39 @Override
40 public String toString() { return "SUCCEEDED"; }
41 };
42
43 private final CountDownLatch _latch = new CountDownLatch(1);
44 private final AtomicReference<Throwable> _state = new AtomicReference<>();
45
46 public BlockingCallback()
47 {
48 }
49
50 @Override
51 public void succeeded()
52 {
53 if (_state.compareAndSet(null,SUCCEEDED))
54 _latch.countDown();
55 }
56
57 @Override
58 public void failed(Throwable cause)
59 {
60 if (_state.compareAndSet(null,cause))
61 _latch.countDown();
62 }
63
64
65
66
67
68
69
70
71 public void block() throws IOException
72 {
73 try
74 {
75 _latch.await();
76 Throwable state=_state.get();
77 if (state==SUCCEEDED)
78 return;
79 if (state instanceof IOException)
80 throw (IOException) state;
81 if (state instanceof CancellationException)
82 throw (CancellationException) state;
83 throw new IOException(state);
84 }
85 catch (final InterruptedException e)
86 {
87 throw new InterruptedIOException(){{initCause(e);}};
88 }
89 finally
90 {
91 _state.set(null);
92 }
93 }
94
95 @Override
96 public String toString()
97 {
98 return String.format("%s@%x{%s}",BlockingCallback.class.getSimpleName(),hashCode(),_state.get());
99 }
100 }