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