1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.concurrent; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.util.concurrent.ExecutionException; |
24 | import java.util.concurrent.Executor; |
25 | import java.util.concurrent.TimeUnit; |
26 | import java.util.concurrent.TimeoutException; |
27 | |
28 | import com.google.common.base.Function; |
29 | import com.google.common.util.concurrent.ListenableFuture; |
30 | |
31 | /** |
32 | * Transforms the exceptions in a future upon get |
33 | * |
34 | * Temporarily here until the following is resolved: <a |
35 | * href="http://code.google.com/p/guava-libraries/issues/detail?id=310"> guava issue 310</a> |
36 | * |
37 | * @author Adrian Cole |
38 | */ |
39 | public class ExceptionParsingListenableFuture<T> implements ListenableFuture<T> { |
40 | |
41 | private final ListenableFuture<T> future; |
42 | private final Function<Exception, T> function; |
43 | |
44 | public static <T> ExceptionParsingListenableFuture<T> create(ListenableFuture<T> future, |
45 | Function<Exception, T> function) { |
46 | return new ExceptionParsingListenableFuture<T>(future, function); |
47 | } |
48 | |
49 | public ExceptionParsingListenableFuture(ListenableFuture<T> future, Function<Exception, T> function) { |
50 | this.future = checkNotNull(future); |
51 | this.function = checkNotNull(function); |
52 | } |
53 | |
54 | public boolean cancel(boolean mayInterruptIfRunning) { |
55 | return future.cancel(mayInterruptIfRunning); |
56 | } |
57 | |
58 | public T get() throws InterruptedException, ExecutionException { |
59 | try { |
60 | return future.get(); |
61 | } catch (Exception e) { |
62 | return attemptConvert(e); |
63 | } |
64 | } |
65 | |
66 | private T attemptConvert(Exception e) { |
67 | if (e instanceof ExecutionException && e.getCause() instanceof Exception) |
68 | return function.apply((Exception) e.getCause()); |
69 | return function.apply(e); |
70 | } |
71 | |
72 | public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
73 | try { |
74 | return future.get(timeout, unit); |
75 | } catch (Exception e) { |
76 | return attemptConvert(e); |
77 | } |
78 | } |
79 | |
80 | public boolean isCancelled() { |
81 | return future.isCancelled(); |
82 | } |
83 | |
84 | public boolean isDone() { |
85 | return future.isDone(); |
86 | } |
87 | |
88 | @Override |
89 | public void addListener(Runnable listener, Executor exec) { |
90 | future.addListener(listener, exec); |
91 | } |
92 | } |