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 The Apache MINA Project (dev@mina.apache.org)
33   */
34  public class DefaultWriteRequest implements WriteRequest {
35      private static final WriteFuture UNUSED_FUTURE = new WriteFuture() {
36          public boolean isWritten() {
37              return false;
38          }
39  
40          public void setWritten() {
41              // Do nothing
42          }
43  
44          public IoSession getSession() {
45              return null;
46          }
47  
48          public void join() {
49              // Do nothing
50          }
51  
52          public boolean join(long timeoutInMillis) {
53              return true;
54          }
55  
56          public boolean isDone() {
57              return true;
58          }
59  
60          public WriteFuture addListener(IoFutureListener<?> listener) {
61              throw new IllegalStateException(
62                      "You can't add a listener to a dummy future.");
63          }
64  
65          public WriteFuture removeListener(IoFutureListener<?> listener) {
66              throw new IllegalStateException(
67                      "You can't add a listener to a dummy future.");
68          }
69  
70          public WriteFuture await() throws InterruptedException {
71              return this;
72          }
73  
74          public boolean await(long timeout, TimeUnit unit)
75                  throws InterruptedException {
76              return true;
77          }
78  
79          public boolean await(long timeoutMillis) throws InterruptedException {
80              return true;
81          }
82  
83          public WriteFuture awaitUninterruptibly() {
84              return this;
85          }
86  
87          public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
88              return true;
89          }
90  
91          public boolean awaitUninterruptibly(long timeoutMillis) {
92              return true;
93          }
94  
95          public Throwable getException() {
96              return null;
97          }
98  
99          public void setException(Throwable cause) {
100             // Do nothing
101         }
102     };
103 
104     private final Object message;
105     private final WriteFuture future;
106     private final SocketAddress destination;
107 
108     /**
109      * Creates a new instance without {@link WriteFuture}.  You'll get
110      * an instance of {@link WriteFuture} even if you called this constructor
111      * because {@link #getFuture()} will return a bogus future.
112      */
113     public DefaultWriteRequest(Object message) {
114         this(message, null, null);
115     }
116 
117     /**
118      * Creates a new instance with {@link WriteFuture}.
119      */
120     public DefaultWriteRequest(Object message, WriteFuture future) {
121         this(message, future, null);
122     }
123 
124     /**
125      * Creates a new instance.
126      *
127      * @param message a message to write
128      * @param future a future that needs to be notified when an operation is finished
129      * @param destination the destination of the message.  This property will be
130      *                    ignored unless the transport supports it.
131      */
132     public DefaultWriteRequest(Object message, WriteFuture future,
133             SocketAddress destination) {
134         if (message == null) {
135             throw new NullPointerException("message");
136         }
137 
138         if (future == null) {
139             future = UNUSED_FUTURE;
140         }
141 
142         this.message = message;
143         this.future = future;
144         this.destination = destination;
145     }
146 
147     public WriteFuture getFuture() {
148         return future;
149     }
150 
151     public Object getMessage() {
152         return message;
153     }
154 
155     public WriteRequest getOriginalRequest() {
156         return this;
157     }
158 
159     public SocketAddress getDestination() {
160         return destination;
161     }
162 
163     @Override
164     public String toString() {
165         if (getDestination() == null) {
166             return message.toString();
167         }
168         
169         return message.toString() + " => " + getDestination();
170     }
171 }