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 */
020package org.apache.mina.core.future;
021
022import org.apache.mina.core.RuntimeIoException;
023import org.apache.mina.core.session.IoSession;
024
025/**
026 * A default implementation of {@link ConnectFuture}.
027 *
028 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
029 */
030public class DefaultConnectFuture extends DefaultIoFuture implements ConnectFuture {
031    /** A static object stored into the ConnectFuture when teh connection has been cancelled */
032    private static final Object CANCELED = new Object();
033
034    /**
035     * Creates a new instance.
036     */
037    public DefaultConnectFuture() {
038        super(null);
039    }
040
041    /**
042     * Creates a new instance of a Connection Failure, with the associated cause.
043     * 
044     * @param exception The exception that caused the failure
045     * @return a new {@link ConnectFuture} which is already marked as 'failed to connect'.
046     */
047    public static ConnectFuture newFailedFuture(Throwable exception) {
048        DefaultConnectFuture failedFuture = new DefaultConnectFuture();
049        failedFuture.setException(exception);
050        
051        return failedFuture;
052    }
053
054    /**
055     * {@inheritDoc}
056     */
057    @Override
058    public IoSession getSession() {
059        Object v = getValue();
060        
061        if (v instanceof IoSession) {
062            return (IoSession) v;
063        } else if (v instanceof RuntimeException) {
064            throw (RuntimeException) v;
065        } else if (v instanceof Error) {
066            throw (Error) v;
067        } else if (v instanceof Throwable) {
068            throw (RuntimeIoException) new RuntimeIoException("Failed to get the session.").initCause((Throwable) v);
069        } else  {
070            return null;
071        }
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    public Throwable getException() {
078        Object v = getValue();
079        
080        if (v instanceof Throwable) {
081            return (Throwable) v;
082        } else {
083            return null;
084        }
085    }
086
087    /**
088     * {@inheritDoc}
089     */
090    public boolean isConnected() {
091        return getValue() instanceof IoSession;
092    }
093
094    /**
095     * {@inheritDoc}
096     */
097    public boolean isCanceled() {
098        return getValue() == CANCELED;
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    public void setSession(IoSession session) {
105        if (session == null) {
106            throw new IllegalArgumentException("session");
107        }
108        
109        setValue(session);
110    }
111
112    /**
113     * {@inheritDoc}
114     */
115    public void setException(Throwable exception) {
116        if (exception == null) {
117            throw new IllegalArgumentException("exception");
118        }
119        
120        setValue(exception);
121    }
122
123    /**
124     * {@inheritDoc}
125     */
126    public boolean cancel() {
127        return setValue(CANCELED);
128    }
129
130    /**
131     * {@inheritDoc}
132     */
133    @Override
134    public ConnectFuture await() throws InterruptedException {
135        return (ConnectFuture) super.await();
136    }
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public ConnectFuture awaitUninterruptibly() {
143        return (ConnectFuture) super.awaitUninterruptibly();
144    }
145
146    /**
147     * {@inheritDoc}
148     */
149    @Override
150    public ConnectFuture addListener(IoFutureListener<?> listener) {
151        return (ConnectFuture) super.addListener(listener);
152    }
153
154    /**
155     * {@inheritDoc}
156     */
157    @Override
158    public ConnectFuture removeListener(IoFutureListener<?> listener) {
159        return (ConnectFuture) super.removeListener(listener);
160    }
161}