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