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.write;
21
22 import java.net.SocketAddress;
23 import java.util.concurrent.TimeUnit;
24
25 import org.apache.mina.core.future.IoFutureListener;
26 import org.apache.mina.core.future.WriteFuture;
27 import org.apache.mina.core.session.IoSession;
28
29
30
31
32
33
34 public class DefaultWriteRequest implements WriteRequest {
35
36 public static final byte[] EMPTY_MESSAGE = new byte[] {};
37
38
39 private static final WriteFuture UNUSED_FUTURE = new WriteFuture() {
40 public boolean isWritten() {
41 return false;
42 }
43
44 public void setWritten() {
45
46 }
47
48 public IoSession getSession() {
49 return null;
50 }
51
52 public void join() {
53
54 }
55
56 public boolean join(long timeoutInMillis) {
57 return true;
58 }
59
60 public boolean isDone() {
61 return true;
62 }
63
64 public WriteFuture addListener(IoFutureListener<?> listener) {
65 throw new IllegalStateException("You can't add a listener to a dummy future.");
66 }
67
68 public WriteFuture removeListener(IoFutureListener<?> listener) {
69 throw new IllegalStateException("You can't add a listener to a dummy future.");
70 }
71
72 public WriteFuture await() throws InterruptedException {
73 return this;
74 }
75
76 public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
77 return true;
78 }
79
80 public boolean await(long timeoutMillis) throws InterruptedException {
81 return true;
82 }
83
84 public WriteFuture awaitUninterruptibly() {
85 return this;
86 }
87
88 public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
89 return true;
90 }
91
92 public boolean awaitUninterruptibly(long timeoutMillis) {
93 return true;
94 }
95
96 public Throwable getException() {
97 return null;
98 }
99
100 public void setException(Throwable cause) {
101
102 }
103 };
104
105 private final Object message;
106
107 private final WriteFuture future;
108
109 private final SocketAddress destination;
110
111
112
113
114
115
116
117
118 public DefaultWriteRequest(Object message) {
119 this(message, null, null);
120 }
121
122
123
124
125
126
127
128 public DefaultWriteRequest(Object message, WriteFuture future) {
129 this(message, future, null);
130 }
131
132
133
134
135
136
137
138
139
140 public DefaultWriteRequest(Object message, WriteFuture future, SocketAddress destination) {
141 if (message == null) {
142 throw new IllegalArgumentException("message");
143 }
144
145 if (future == null) {
146 future = UNUSED_FUTURE;
147 }
148
149 this.message = message;
150 this.future = future;
151 this.destination = destination;
152 }
153
154 public WriteFuture getFuture() {
155 return future;
156 }
157
158 public Object getMessage() {
159 return message;
160 }
161
162 public WriteRequest getOriginalRequest() {
163 return this;
164 }
165
166 public SocketAddress getDestination() {
167 return destination;
168 }
169
170 @Override
171 public String toString() {
172 StringBuilder sb = new StringBuilder();
173
174 sb.append("WriteRequest: ");
175
176
177
178 if (message.getClass().getName().equals(Object.class.getName())) {
179 sb.append("CLOSE_REQUEST");
180 } else {
181 if (getDestination() == null) {
182 sb.append(message);
183 } else {
184 sb.append(message);
185 sb.append(" => ");
186 sb.append(getDestination());
187 }
188 }
189
190 return sb.toString();
191 }
192
193 public boolean isEncoded() {
194 return false;
195 }
196 }