View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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   * The default implementation of {@link WriteRequest}.
31   *
32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33   */
34  public class DefaultWriteRequest implements WriteRequest {
35      /** An empty message */
36      public static final byte[] EMPTY_MESSAGE = new byte[] {};
37  
38      /** An empty FUTURE */
39      private static final WriteFuture UNUSED_FUTURE = new WriteFuture() {
40          /**
41           * {@inheritDoc}
42           */
43          @Override
44          public boolean isWritten() {
45              return false;
46          }
47  
48          /**
49           * {@inheritDoc}
50           */
51          @Override
52          public void setWritten() {
53              // Do nothing
54          }
55  
56          /**
57           * {@inheritDoc}
58           */
59          @Override
60          public IoSession getSession() {
61              return null;
62          }
63  
64          /**
65           * {@inheritDoc}
66           */
67          @Override
68          public void join() {
69              // Do nothing
70          }
71  
72          /**
73           * {@inheritDoc}
74           */
75          @Override
76          public boolean join(long timeoutInMillis) {
77              return true;
78          }
79  
80          /**
81           * {@inheritDoc}
82           */
83          @Override
84          public boolean isDone() {
85              return true;
86          }
87  
88          /**
89           * {@inheritDoc}
90           */
91          @Override
92          public WriteFuture addListener(IoFutureListener<?> listener) {
93              throw new IllegalStateException("You can't add a listener to a dummy future.");
94          }
95  
96          /**
97           * {@inheritDoc}
98           */
99          @Override
100         public WriteFuture removeListener(IoFutureListener<?> listener) {
101             throw new IllegalStateException("You can't add a listener to a dummy future.");
102         }
103 
104         /**
105          * {@inheritDoc}
106          */
107         @Override
108         public WriteFuture await() throws InterruptedException {
109             return this;
110         }
111 
112         /**
113          * {@inheritDoc}
114          */
115         @Override
116         public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
117             return true;
118         }
119 
120         /**
121          * {@inheritDoc}
122          */
123         @Override
124         public boolean await(long timeoutMillis) throws InterruptedException {
125             return true;
126         }
127 
128         /**
129          * {@inheritDoc}
130          */
131         @Override
132         public WriteFuture awaitUninterruptibly() {
133             return this;
134         }
135 
136         /**
137          * {@inheritDoc}
138          */
139         @Override
140         public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
141             return true;
142         }
143 
144         /**
145          * {@inheritDoc}
146          */
147         @Override
148         public boolean awaitUninterruptibly(long timeoutMillis) {
149             return true;
150         }
151 
152         /**
153          * {@inheritDoc}
154          */
155         @Override
156         public Throwable getException() {
157             return null;
158         }
159 
160         /**
161          * {@inheritDoc}
162          */
163         @Override
164         public void setException(Throwable cause) {
165             // Do nothing
166         }
167     };
168 
169     private final Object message;
170 
171     private final WriteFuture future;
172 
173     private final SocketAddress destination;
174 
175     /**
176      * Creates a new instance without {@link WriteFuture}.  You'll get
177      * an instance of {@link WriteFuture} even if you called this constructor
178      * because {@link #getFuture()} will return a bogus future.
179      * 
180      * @param message The message that will be written
181      */
182     public DefaultWriteRequest(Object message) {
183         this(message, null, null);
184     }
185 
186     /**
187      * Creates a new instance with {@link WriteFuture}.
188      * 
189      * @param message The message that will be written
190      * @param future The associated {@link WriteFuture}
191      */
192     public DefaultWriteRequest(Object message, WriteFuture future) {
193         this(message, future, null);
194     }
195 
196     /**
197      * Creates a new instance.
198      *
199      * @param message a message to write
200      * @param future a future that needs to be notified when an operation is finished
201      * @param destination the destination of the message.  This property will be
202      *                    ignored unless the transport supports it.
203      */
204     public DefaultWriteRequest(Object message, WriteFuture future, SocketAddress destination) {
205         if (message == null) {
206             throw new IllegalArgumentException("message");
207         }
208 
209         if (future == null) {
210             future = UNUSED_FUTURE;
211         }
212 
213         this.message = message;
214         this.future = future;
215         this.destination = destination;
216     }
217 
218     /**
219      * {@inheritDoc}
220      */
221     @Override
222     public WriteFuture getFuture() {
223         return future;
224     }
225 
226     /**
227      * {@inheritDoc}
228      */
229     @Override
230     public Object getMessage() {
231         return message;
232     }
233 
234     /**
235      * {@inheritDoc}
236      */
237     @Override
238     public WriteRequest getOriginalRequest() {
239         return this;
240     }
241 
242     /**
243      * {@inheritDoc}
244      */
245     @Override
246     public SocketAddress getDestination() {
247         return destination;
248     }
249 
250     @Override
251     public String toString() {
252         StringBuilder sb = new StringBuilder();
253 
254         sb.append("WriteRequest: ");
255 
256         // Special case for the CLOSE_REQUEST writeRequest : it just
257         // carries a native Object instance
258         if (message.getClass().getName().equals(Object.class.getName())) {
259             sb.append("CLOSE_REQUEST");
260         } else {
261             if (getDestination() == null) {
262                 sb.append(message);
263             } else {
264                 sb.append(message);
265                 sb.append(" => ");
266                 sb.append(getDestination());
267             }
268         }
269 
270         return sb.toString();
271     }
272 
273     /**
274      * {@inheritDoc}
275      */
276     @Override
277     public boolean isEncoded() {
278         return false;
279     }
280 }