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          public boolean isWritten() {
41              return false;
42          }
43  
44          public void setWritten() {
45              // Do nothing
46          }
47  
48          public IoSession getSession() {
49              return null;
50          }
51  
52          public void join() {
53              // Do nothing
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             // Do nothing
102         }
103     };
104 
105     private final Object message;
106 
107     private final WriteFuture future;
108 
109     private final SocketAddress destination;
110 
111     /**
112      * Creates a new instance without {@link WriteFuture}.  You'll get
113      * an instance of {@link WriteFuture} even if you called this constructor
114      * because {@link #getFuture()} will return a bogus future.
115      */
116     public DefaultWriteRequest(Object message) {
117         this(message, null, null);
118     }
119 
120     /**
121      * Creates a new instance with {@link WriteFuture}.
122      */
123     public DefaultWriteRequest(Object message, WriteFuture future) {
124         this(message, future, null);
125     }
126 
127     /**
128      * Creates a new instance.
129      *
130      * @param message a message to write
131      * @param future a future that needs to be notified when an operation is finished
132      * @param destination the destination of the message.  This property will be
133      *                    ignored unless the transport supports it.
134      */
135     public DefaultWriteRequest(Object message, WriteFuture future, SocketAddress destination) {
136         if (message == null) {
137             throw new IllegalArgumentException("message");
138         }
139 
140         if (future == null) {
141             future = UNUSED_FUTURE;
142         }
143 
144         this.message = message;
145         this.future = future;
146         this.destination = destination;
147     }
148 
149     public WriteFuture getFuture() {
150         return future;
151     }
152 
153     public Object getMessage() {
154         return message;
155     }
156 
157     public WriteRequest getOriginalRequest() {
158         return this;
159     }
160 
161     public SocketAddress getDestination() {
162         return destination;
163     }
164 
165     @Override
166     public String toString() {
167         StringBuilder sb = new StringBuilder();
168 
169         sb.append("WriteRequest: ");
170 
171         // Special case for the CLOSE_REQUEST writeRequest : it just
172         // carries a native Object instance
173         if (message.getClass().getName().equals(Object.class.getName())) {
174             sb.append("CLOSE_REQUEST");
175         } else {
176             if (getDestination() == null) {
177                 sb.append(message);
178             } else {
179                 sb.append(message);
180                 sb.append(" => ");
181                 sb.append(getDestination());
182             }
183         }
184 
185         return sb.toString();
186     }
187 
188     public boolean isEncoded() {
189         return false;
190     }
191 }