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