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 org.apache.mina.core.RuntimeIoException;
    23  import org.apache.mina.core.session.IoSession;
    24
    25  /**
    26   * A default implementation of {@link ConnectFuture}.
    27   *
    28   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    29   */
    30  public class DefaultConnectFuture extends DefaultIoFuture implements ConnectFuture {
    31
    32      private static final Object CANCELED = new Object();
    33
    34      /**
    35       * Returns a new {@link ConnectFuture} which is already marked as 'failed to connect'.
    36       */
    37      public static ConnectFuture newFailedFuture(Throwable exception) {
    38          DefaultConnectFuture failedFuture = new DefaultConnectFuture();
    39          failedFuture.setException(exception);
    40          return failedFuture;
    41      }
    42
    43      /**
    44       * Creates a new instance.
    45       */
    46      public DefaultConnectFuture() {
    47          super(null);
    48      }
    49
    50      @Override
    51      public IoSession getSession() {
    52          Object v = getValue();
    53          if (v instanceof RuntimeException) {
    54              throw (RuntimeException) v;
    55          } else if (v instanceof Error) {
    56              throw (Error) v;
    57          } else if (v instanceof Throwable) {
    58              throw (RuntimeIoException) new RuntimeIoException("Failed to get the session.").initCause((Throwable) v);
    59          } else if (v instanceof IoSession) {
    60              return (IoSession) v;
    61          } else {
    62              return null;
    63          }
    64      }
    65
    66      public Throwable getException() {
    67          Object v = getValue();
    68          if (v instanceof Throwable) {
    69              return (Throwable) v;
    70          } else {
    71              return null;
    72          }
    73      }
    74
    75      public boolean isConnected() {
    76          return getValue() instanceof IoSession;
    77      }
    78
    79      public boolean isCanceled() {
    80          return getValue() == CANCELED;
    81      }
    82
    83      public void setSession(IoSession session) {
    84          if (session == null) {
    85              throw new IllegalArgumentException("session");
    86          }
    87          setValue(session);
    88      }
    89
    90      public void setException(Throwable exception) {
    91          if (exception == null) {
    92              throw new IllegalArgumentException("exception");
    93          }
    94          setValue(exception);
    95      }
    96
    97      public void cancel() {
    98          setValue(CANCELED);
    99      }
    100
    101     @Override
    102     public ConnectFuture await() throws InterruptedException {
    103         return (ConnectFuture) super.await();
    104     }
    105
    106     @Override
    107     public ConnectFuture awaitUninterruptibly() {
    108         return (ConnectFuture) super.awaitUninterruptibly();
    109     }
    110
    111     @Override
    112     public ConnectFuture addListener(IoFutureListener<?> listener) {
    113         return (ConnectFuture) super.addListener(listener);
    114     }
    115
    116     @Override
    117     public ConnectFuture removeListener(IoFutureListener<?> listener) {
    118         return (ConnectFuture) super.removeListener(listener);
    119     }
    120 }