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.future;
    21
    22  import java.io.IOException;
    23
    24  import org.apache.mina.core.RuntimeIoException;
    25  import org.apache.mina.core.session.IoSession;
    26
    27  /**
    28   * A default implementation of {@link WriteFuture}.
    29   *
    30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    31   */
    32  public class DefaultReadFuture extends DefaultIoFuture implements ReadFuture {
    33
    34      private static final Object CLOSED = new Object();
    35
    36      /**
    37       * Creates a new instance.
    38       */
    39      public DefaultReadFuture(IoSession session) {
    40          super(session);
    41      }
    42
    43      public Object getMessage() {
    44          if (isDone()) {
    45              Object v = getValue();
    46              if (v == CLOSED) {
    47                  return null;
    48              }
    49
    50              if (v instanceof ExceptionHolder) {
    51                  v = ((ExceptionHolder) v).exception;
    52                  if (v instanceof RuntimeException) {
    53                      throw (RuntimeException) v;
    54                  }
    55                  if (v instanceof Error) {
    56                      throw (Error) v;
    57                  }
    58                  if (v instanceof IOException || v instanceof Exception) {
    59                      throw new RuntimeIoException((Exception) v);
    60                  }
    61              }
    62
    63              return v;
    64          }
    65
    66          return null;
    67      }
    68
    69      public boolean isRead() {
    70          if (isDone()) {
    71              Object v = getValue();
    72              return (v != CLOSED && !(v instanceof ExceptionHolder));
    73          }
    74          return false;
    75      }
    76
    77      public boolean isClosed() {
    78          if (isDone()) {
    79              return getValue() == CLOSED;
    80          }
    81          return false;
    82      }
    83
    84      public Throwable getException() {
    85          if (isDone()) {
    86              Object v = getValue();
    87              if (v instanceof ExceptionHolder) {
    88                  return ((ExceptionHolder) v).exception;
    89              }
    90          }
    91          return null;
    92      }
    93
    94      public void setClosed() {
    95          setValue(CLOSED);
    96      }
    97
    98      public void setRead(Object message) {
    99          if (message == null) {
    100             throw new IllegalArgumentException("message");
    101         }
    102         setValue(message);
    103     }
    104
    105     public void setException(Throwable exception) {
    106         if (exception == null) {
    107             throw new IllegalArgumentException("exception");
    108         }
    109
    110         setValue(new ExceptionHolder(exception));
    111     }
    112
    113     @Override
    114     public ReadFuture await() throws InterruptedException {
    115         return (ReadFuture) super.await();
    116     }
    117
    118     @Override
    119     public ReadFuture awaitUninterruptibly() {
    120         return (ReadFuture) super.awaitUninterruptibly();
    121     }
    122
    123     @Override
    124     public ReadFuture addListener(IoFutureListener<?> listener) {
    125         return (ReadFuture) super.addListener(listener);
    126     }
    127
    128     @Override
    129     public ReadFuture removeListener(IoFutureListener<?> listener) {
    130         return (ReadFuture) super.removeListener(listener);
    131     }
    132
    133     private static class ExceptionHolder {
    134         private final Throwable exception;
    135
    136         private ExceptionHolder(Throwable exception) {
    137             this.exception = exception;
    138         }
    139     }
    140 }