1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.core.future;
21
22 import org.apache.mina.core.RuntimeIoException;
23 import org.apache.mina.core.session.IoSession;
24
25
26
27
28
29
30 public class DefaultConnectFuture extends DefaultIoFuture implements ConnectFuture {
31
32 private static final Object CANCELED = new Object();
33
34
35
36
37 public static ConnectFuture newFailedFuture(Throwable exception) {
38 DefaultConnectFuture failedFuture = new DefaultConnectFuture();
39 failedFuture.setException(exception);
40 return failedFuture;
41 }
42
43
44
45
46 public DefaultConnectFuture() {
47 super(null);
48 }
49
50 @Override
51 public IoSession getSession() {
52 Object v = getValue();
53 if (v instanceof RuntimeException) {
54 throw (RuntimeException) v;
55 } else if (v instanceof Error) {
56 throw (Error) v;
57 } else if (v instanceof Throwable) {
58 throw (RuntimeIoException) new RuntimeIoException("Failed to get the session.").initCause((Throwable) v);
59 } else if (v instanceof IoSession) {
60 return (IoSession) v;
61 } else {
62 return null;
63 }
64 }
65
66 public Throwable getException() {
67 Object v = getValue();
68 if (v instanceof Throwable) {
69 return (Throwable) v;
70 } else {
71 return null;
72 }
73 }
74
75 public boolean isConnected() {
76 return getValue() instanceof IoSession;
77 }
78
79 public boolean isCanceled() {
80 return getValue() == CANCELED;
81 }
82
83 public void setSession(IoSession session) {
84 if (session == null) {
85 throw new IllegalArgumentException("session");
86 }
87 setValue(session);
88 }
89
90 public void setException(Throwable exception) {
91 if (exception == null) {
92 throw new IllegalArgumentException("exception");
93 }
94 setValue(exception);
95 }
96
97 public void cancel() {
98 setValue(CANCELED);
99 }
100
101 @Override
102 public ConnectFuture await() throws InterruptedException {
103 return (ConnectFuture) super.await();
104 }
105
106 @Override
107 public ConnectFuture awaitUninterruptibly() {
108 return (ConnectFuture) super.awaitUninterruptibly();
109 }
110
111 @Override
112 public ConnectFuture addListener(IoFutureListener<?> listener) {
113 return (ConnectFuture) super.addListener(listener);
114 }
115
116 @Override
117 public ConnectFuture removeListener(IoFutureListener<?> listener) {
118 return (ConnectFuture) super.removeListener(listener);
119 }
120 }