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.common;
21  
22  
23  /**
24   * A default implementation of {@link ConnectFuture}.
25   *
26   * @author The Apache MINA Project (dev@mina.apache.org)
27   * @version $Rev: 599745 $, $Date: 2007-11-30 02:04:47 -0700 (Fri, 30 Nov 2007) $
28   */
29  public class DefaultConnectFuture extends DefaultIoFuture implements
30          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(
59                      "Failed to get the session.").initCause((Throwable) v);
60          } else if (v instanceof IoSession) {
61              return (IoSession) v;
62          } else {
63              return null;
64          }
65      }
66  
67      public Throwable getException() {
68          Object v = getValue();
69          if (v instanceof Throwable) {
70              return (Throwable) v;
71          } else {
72              return null;
73          }
74      }
75  
76      public boolean isConnected() {
77          return getValue() instanceof IoSession;
78      }
79  
80      public boolean isCanceled() {
81          return getValue() == CANCELED;
82      }
83  
84      public void setSession(IoSession session) {
85          if (session == null) {
86              throw new NullPointerException("session");
87          }
88          setValue(session);
89      }
90  
91      public void setException(Throwable exception) {
92          if (exception == null) {
93              throw new NullPointerException("exception");
94          }
95          setValue(exception);
96      }
97  
98      public void cancel() {
99          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 }