KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > remoting > LocalChannel


1 package hudson.remoting;
2
3 import java.io.IOException JavaDoc;
4 import java.util.concurrent.ExecutionException JavaDoc;
5 import java.util.concurrent.ExecutorService JavaDoc;
6 import java.util.concurrent.TimeUnit JavaDoc;
7 import java.util.concurrent.TimeoutException JavaDoc;
8
9 /**
10  * {@link VirtualChannel} that performs computation on the local JVM.
11  *
12  * @author Kohsuke Kawaguchi
13  */

14 public class LocalChannel implements VirtualChannel {
15     private final ExecutorService JavaDoc executor;
16
17     public LocalChannel(ExecutorService JavaDoc executor) {
18         this.executor = executor;
19     }
20
21     public <V, T extends Throwable JavaDoc> V call(Callable<V,T> callable) throws T {
22         return callable.call();
23     }
24
25     public <V, T extends Throwable JavaDoc> Future<V> callAsync(final Callable<V,T> callable) throws IOException JavaDoc {
26         final java.util.concurrent.Future JavaDoc<V> f = executor.submit(new java.util.concurrent.Callable JavaDoc<V>() {
27             public V call() throws Exception JavaDoc {
28                 try {
29                     return callable.call();
30                 } catch (Exception JavaDoc t) {
31                     throw t;
32                 } catch (Error JavaDoc t) {
33                     throw t;
34                 } catch (Throwable JavaDoc t) {
35                     throw new ExecutionException JavaDoc(t);
36                 }
37             }
38         });
39
40         return new Future<V>() {
41             public boolean cancel(boolean mayInterruptIfRunning) {
42                 return f.cancel(mayInterruptIfRunning);
43             }
44
45             public boolean isCancelled() {
46                 return f.isCancelled();
47             }
48
49             public boolean isDone() {
50                 return f.isDone();
51             }
52
53             public V get() throws InterruptedException JavaDoc, ExecutionException JavaDoc {
54                 return f.get();
55             }
56
57             public V get(long timeout, TimeUnit JavaDoc unit) throws InterruptedException JavaDoc, ExecutionException JavaDoc, TimeoutException JavaDoc {
58                 return f.get(timeout,unit);
59             }
60         };
61     }
62
63     public void close() {
64         // noop
65
}
66
67     public <T> T export(Class JavaDoc<T> intf, T instance) {
68         return instance;
69     }
70 }
71
Popular Tags