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      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("You can't add a listener to a dummy future.");
    62          }
    63
    64          public WriteFuture removeListener(IoFutureListener<?> listener) {
    65              throw new IllegalStateException("You can't add a listener to a dummy future.");
    66          }
    67
    68          public WriteFuture await() throws InterruptedException {
    69              return this;
    70          }
    71
    72          public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
    73              return true;
    74          }
    75
    76          public boolean await(long timeoutMillis) throws InterruptedException {
    77              return true;
    78          }
    79
    80          public WriteFuture awaitUninterruptibly() {
    81              return this;
    82          }
    83
    84          public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
    85              return true;
    86          }
    87
    88          public boolean awaitUninterruptibly(long timeoutMillis) {
    89              return true;
    90          }
    91
    92          public Throwable getException() {
    93              return null;
    94          }
    95
    96          public void setException(Throwable cause) {
    97              // Do nothing
    98          }
    99      };
    100
    101     private final Object message;
    102
    103     private final WriteFuture future;
    104
    105     private final SocketAddress destination;
    106
    107     /**
    108      * Creates a new instance without {@link WriteFuture}.  You'll get
    109      * an instance of {@link WriteFuture} even if you called this constructor
    110      * because {@link #getFuture()} will return a bogus future.
    111      */
    112     public DefaultWriteRequest(Object message) {
    113         this(message, null, null);
    114     }
    115
    116     /**
    117      * Creates a new instance with {@link WriteFuture}.
    118      */
    119     public DefaultWriteRequest(Object message, WriteFuture future) {
    120         this(message, future, null);
    121     }
    122
    123     /**
    124      * Creates a new instance.
    125      *
    126      * @param message a message to write
    127      * @param future a future that needs to be notified when an operation is finished
    128      * @param destination the destination of the message.  This property will be
    129      *                    ignored unless the transport supports it.
    130      */
    131     public DefaultWriteRequest(Object message, WriteFuture future, SocketAddress destination) {
    132         if (message == null) {
    133             throw new IllegalArgumentException("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         StringBuilder sb = new StringBuilder();
    164
    165         sb.append("WriteRequest: ");
    166
    167         // Special case for the CLOSE_REQUEST writeRequest : it just
    168         // carries a native Object instance
    169         if (message.getClass().getName().equals(Object.class.getName())) {
    170             sb.append("CLOSE_REQUEST");
    171         } else {
    172             if (getDestination() == null) {
    173                 sb.append(message);
    174             } else {
    175                 sb.append(message);
    176                 sb.append(" => ");
    177                 sb.append(getDestination());
    178             }
    179         }
    180
    181         return sb.toString();
    182     }
    183
    184     public boolean isEncoded() {
    185         return false;
    186     }
    187 }