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  /**
27   * A default implementation of {@link ConnectFuture}.
28   *
29   * @author The Apache MINA Project (dev@mina.apache.org)
30   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
31   */
32  public class DefaultConnectFuture extends DefaultIoFuture implements
33          ConnectFuture {
34  
35      private static final Object CANCELED = new Object();
36  
37      /**
38       * Returns a new {@link ConnectFuture} which is already marked as 'failed to connect'.
39       */
40      public static ConnectFuture newFailedFuture(Throwable exception) {
41          DefaultConnectFuture failedFuture = new DefaultConnectFuture();
42          failedFuture.setException(exception);
43          return failedFuture;
44      }
45  
46      /**
47       * Creates a new instance.
48       */
49      public DefaultConnectFuture() {
50          super(null);
51      }
52  
53      @Override
54      public IoSession getSession() {
55          Object v = getValue();
56          if (v instanceof RuntimeException) {
57              throw (RuntimeException) v;
58          } else if (v instanceof Error) {
59              throw (Error) v;
60          } else if (v instanceof Throwable) {
61              throw (RuntimeIoException) new RuntimeIoException(
62                      "Failed to get the session.").initCause((Throwable) v);
63          } else if (v instanceof IoSession) {
64              return (IoSession) v;
65          } else {
66              return null;
67          }
68      }
69  
70      public Throwable getException() {
71          Object v = getValue();
72          if (v instanceof Throwable) {
73              return (Throwable) v;
74          } else {
75              return null;
76          }
77      }
78  
79      public boolean isConnected() {
80          return getValue() instanceof IoSession;
81      }
82  
83      public boolean isCanceled() {
84          return getValue() == CANCELED;
85      }
86  
87      public void setSession(IoSession session) {
88          if (session == null) {
89              throw new NullPointerException("session");
90          }
91          setValue(session);
92      }
93  
94      public void setException(Throwable exception) {
95          if (exception == null) {
96              throw new NullPointerException("exception");
97          }
98          setValue(exception);
99      }
100 
101     public void cancel() {
102         setValue(CANCELED);
103     }
104 
105     @Override
106     public ConnectFuture await() throws InterruptedException {
107         return (ConnectFuture) super.await();
108     }
109 
110     @Override
111     public ConnectFuture awaitUninterruptibly() {
112         return (ConnectFuture) super.awaitUninterruptibly();
113     }
114 
115     @Override
116     public ConnectFuture addListener(IoFutureListener<?> listener) {
117         return (ConnectFuture) super.addListener(listener);
118     }
119 
120     @Override
121     public ConnectFuture removeListener(IoFutureListener<?> listener) {
122         return (ConnectFuture) super.removeListener(listener);
123     }
124 }