001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.common;
021:
022: /**
023: * A default implementation of {@link ConnectFuture}.
024: *
025: * @author The Apache MINA Project (dev@mina.apache.org)
026: * @version $Rev: 599745 $, $Date: 2007-11-30 02:04:47 -0700 (Fri, 30 Nov 2007) $
027: */
028: public class DefaultConnectFuture extends DefaultIoFuture implements
029: ConnectFuture {
030:
031: private static final Object CANCELED = new Object();
032:
033: /**
034: * Returns a new {@link ConnectFuture} which is already marked as 'failed to connect'.
035: */
036: public static ConnectFuture newFailedFuture(Throwable exception) {
037: DefaultConnectFuture failedFuture = new DefaultConnectFuture();
038: failedFuture.setException(exception);
039: return failedFuture;
040: }
041:
042: /**
043: * Creates a new instance.
044: */
045: public DefaultConnectFuture() {
046: super (null);
047: }
048:
049: @Override
050: public IoSession getSession() {
051: Object v = getValue();
052: if (v instanceof RuntimeException) {
053: throw (RuntimeException) v;
054: } else if (v instanceof Error) {
055: throw (Error) v;
056: } else if (v instanceof Throwable) {
057: throw (RuntimeIoException) new RuntimeIoException(
058: "Failed to get the session.")
059: .initCause((Throwable) v);
060: } else if (v instanceof IoSession) {
061: return (IoSession) v;
062: } else {
063: return null;
064: }
065: }
066:
067: public Throwable getException() {
068: Object v = getValue();
069: if (v instanceof Throwable) {
070: return (Throwable) v;
071: } else {
072: return null;
073: }
074: }
075:
076: public boolean isConnected() {
077: return getValue() instanceof IoSession;
078: }
079:
080: public boolean isCanceled() {
081: return getValue() == CANCELED;
082: }
083:
084: public void setSession(IoSession session) {
085: if (session == null) {
086: throw new NullPointerException("session");
087: }
088: setValue(session);
089: }
090:
091: public void setException(Throwable exception) {
092: if (exception == null) {
093: throw new NullPointerException("exception");
094: }
095: setValue(exception);
096: }
097:
098: public void cancel() {
099: setValue(CANCELED);
100: }
101:
102: @Override
103: public ConnectFuture await() throws InterruptedException {
104: return (ConnectFuture) super .await();
105: }
106:
107: @Override
108: public ConnectFuture awaitUninterruptibly() {
109: return (ConnectFuture) super .awaitUninterruptibly();
110: }
111:
112: @Override
113: public ConnectFuture addListener(IoFutureListener<?> listener) {
114: return (ConnectFuture) super .addListener(listener);
115: }
116:
117: @Override
118: public ConnectFuture removeListener(IoFutureListener<?> listener) {
119: return (ConnectFuture) super.removeListener(listener);
120: }
121: }
|