View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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   * An implementation of Callback that blocks until success or failure.
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       * Blocks until the Callback has succeeded or failed and
66       * after the return leave in the state to allow reuse.
67       * This is useful for code that wants to repeatable use a FutureCallback to convert
68       * an asynchronous API to a blocking API. 
69       * @throws IOException if exception was caught during blocking, or callback was cancelled 
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 }