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
31
32 public class DefaultConnectFuture extends DefaultIoFuture implements
33 ConnectFuture {
34
35 private static final Object CANCELED = new Object();
36
37
38
39
40 public static ConnectFuture newFailedFuture(Throwable exception) {
41 DefaultConnectFuture failedFuture = new DefaultConnectFuture();
42 failedFuture.setException(exception);
43 return failedFuture;
44 }
45
46
47
48
49 public DefaultConnectFuture() {
50 super(null);
51 }
52
53 @Override
54 public IoSession getSession() {
55 Object v = getValue();
56 if (v instanceof RuntimeException) {
57 throw (RuntimeException) v;
58 } else if (v instanceof Error) {
59 throw (Error) v;
60 } else if (v instanceof Throwable) {
61 throw (RuntimeIoException) new RuntimeIoException(
62 "Failed to get the session.").initCause((Throwable) v);
63 } else if (v instanceof IoSession) {
64 return (IoSession) v;
65 } else {
66 return null;
67 }
68 }
69
70 public Throwable getException() {
71 Object v = getValue();
72 if (v instanceof Throwable) {
73 return (Throwable) v;
74 } else {
75 return null;
76 }
77 }
78
79 public boolean isConnected() {
80 return getValue() instanceof IoSession;
81 }
82
83 public boolean isCanceled() {
84 return getValue() == CANCELED;
85 }
86
87 public void setSession(IoSession session) {
88 if (session == null) {
89 throw new NullPointerException("session");
90 }
91 setValue(session);
92 }
93
94 public void setException(Throwable exception) {
95 if (exception == null) {
96 throw new NullPointerException("exception");
97 }
98 setValue(exception);
99 }
100
101 public void cancel() {
102 setValue(CANCELED);
103 }
104
105 @Override
106 public ConnectFuture await() throws InterruptedException {
107 return (ConnectFuture) super.await();
108 }
109
110 @Override
111 public ConnectFuture awaitUninterruptibly() {
112 return (ConnectFuture) super.awaitUninterruptibly();
113 }
114
115 @Override
116 public ConnectFuture addListener(IoFutureListener<?> listener) {
117 return (ConnectFuture) super.addListener(listener);
118 }
119
120 @Override
121 public ConnectFuture removeListener(IoFutureListener<?> listener) {
122 return (ConnectFuture) super.removeListener(listener);
123 }
124 }