View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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.websocket.common;
20  
21  import java.util.concurrent.Callable;
22  import java.util.concurrent.Future;
23  import java.util.concurrent.FutureTask;
24  
25  import javax.net.websocket.SendHandler;
26  import javax.net.websocket.SendResult;
27  
28  public class FailedFuture extends FutureTask<SendResult> implements Future<SendResult>
29  {
30      private static class FailedRunner implements Callable<SendResult>
31      {
32          private SendHandler completion;
33          private Throwable error;
34  
35          public FailedRunner(SendHandler completion, Throwable error)
36          {
37              this.completion = completion;
38              this.error = error;
39          }
40  
41          @Override
42          public SendResult call() throws Exception
43          {
44              SendResult result = new SendResult(this.error);
45              if (completion != null)
46              {
47                  completion.setResult(result);
48              }
49              return result;
50          }
51      }
52  
53      public FailedFuture(SendHandler completion, Throwable error)
54      {
55          super(new FailedRunner(completion,error));
56          run();
57      }
58  }