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      /** A static object stored into the ConnectFuture when teh connection has been cancelled */
32      private static final Object CANCELED = new Object();
33  
34      /**
35       * Creates a new instance.
36       */
37      public DefaultConnectFuture() {
38          super(null);
39      }
40  
41      /**
42       * Creates a new instance of a Connection Failure, with the associated cause.
43       * 
44       * @param exception The exception that caused the failure
45       * @return a new {@link ConnectFuture} which is already marked as 'failed to connect'.
46       */
47      public static ConnectFuture newFailedFuture(Throwable exception) {
48          DefaultConnectFuture failedFuture = new DefaultConnectFuture();
49          failedFuture.setException(exception);
50          
51          return failedFuture;
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public IoSession getSession() {
59          Object v = getValue();
60          
61          if (v instanceof IoSession) {
62              return (IoSession) v;
63          } else if (v instanceof RuntimeException) {
64              throw (RuntimeException) v;
65          } else if (v instanceof Error) {
66              throw (Error) v;
67          } else if (v instanceof Throwable) {
68              throw (RuntimeIoException) new RuntimeIoException("Failed to get the session.").initCause((Throwable) v);
69          } else  {
70              return null;
71          }
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      public Throwable getException() {
78          Object v = getValue();
79          
80          if (v instanceof Throwable) {
81              return (Throwable) v;
82          } else {
83              return null;
84          }
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      public boolean isConnected() {
91          return getValue() instanceof IoSession;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      public boolean isCanceled() {
98          return getValue() == CANCELED;
99      }
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 }