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          DefaultConnectFutureure.html#DefaultConnectFuture">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 (RuntimeIoExceptionoException.html#RuntimeIoException">RuntimeIoException) new RuntimeIoException("Failed to get the session.").initCause((Throwable) v);
69          } else  {
70              return null;
71          }
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public Throwable getException() {
79          Object v = getValue();
80          
81          if (v instanceof Throwable) {
82              return (Throwable) v;
83          } else {
84              return null;
85          }
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public boolean isConnected() {
93          return getValue() instanceof IoSession;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public boolean isCanceled() {
101         return getValue() == CANCELED;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public void setSession(IoSession session) {
109         if (session == null) {
110             throw new IllegalArgumentException("session");
111         }
112         
113         setValue(session);
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public void setException(Throwable exception) {
121         if (exception == null) {
122             throw new IllegalArgumentException("exception");
123         }
124         
125         setValue(exception);
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public boolean cancel() {
133         return setValue(CANCELED);
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public ConnectFuture await() throws InterruptedException {
141         return (ConnectFuture) super.await();
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public ConnectFuture awaitUninterruptibly() {
149         return (ConnectFuture) super.awaitUninterruptibly();
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     public ConnectFuture addListener(IoFutureListener<?> listener) {
157         return (ConnectFuture) super.addListener(listener);
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public ConnectFuture removeListener(IoFutureListener<?> listener) {
165         return (ConnectFuture) super.removeListener(listener);
166     }
167 }