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