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 java.io.IOException;
23  
24  import org.apache.mina.core.RuntimeIoException;
25  import org.apache.mina.core.session.IoSession;
26  
27  
28  /**
29   * A default implementation of {@link WriteFuture}.
30   *
31   * @author The Apache MINA Project (dev@mina.apache.org)
32   * @version $Rev:671827 $, $Date:2008-06-26 09:49:48 +0100 (jeu., 26 juin 2008) $
33   */
34  public class DefaultReadFuture extends DefaultIoFuture implements ReadFuture {
35      
36      private static final Object CLOSED = new Object();
37      
38      /**
39       * Creates a new instance.
40       */
41      public DefaultReadFuture(IoSession session) {
42          super(session);
43      }
44      
45      public Object getMessage() {
46          if (isDone()) {
47              Object v = getValue();
48              if (v == CLOSED) {
49                  return null;
50              }
51              
52              if (v instanceof ExceptionHolder) {
53                  v = ((ExceptionHolder) v).exception;
54                  if (v instanceof RuntimeException) {
55                      throw (RuntimeException) v;
56                  }
57                  if (v instanceof Error) {
58                      throw (Error) v;
59                  }
60                  if (v instanceof IOException || v instanceof Exception) {
61                      throw new RuntimeIoException((Exception) v);
62                  }
63              }
64              
65              return v;
66          }
67  
68          return null;
69      }
70  
71  
72      public boolean isRead() {
73          if (isDone()) {
74              Object v = getValue();
75              return (v != CLOSED && !(v instanceof ExceptionHolder));
76          }
77          return false;
78      }
79      
80      public boolean isClosed() {
81          if (isDone()) {
82              return getValue() == CLOSED;
83          }
84          return false;
85      }
86  
87      public Throwable getException() {
88          if (isDone()) {
89              Object v = getValue();
90              if (v instanceof ExceptionHolder) {
91                  return ((ExceptionHolder) v).exception;
92              }
93          }
94          return null;
95      }
96  
97      public void setClosed() {
98          setValue(CLOSED);
99      }
100 
101     public void setRead(Object message) {
102         if (message == null) {
103             throw new NullPointerException("message");
104         }
105         setValue(message);
106     }
107 
108     public void setException(Throwable exception) {
109         if (exception == null) {
110             throw new NullPointerException("exception");
111         }
112         
113         setValue(new ExceptionHolder(exception));
114     }
115 
116     @Override
117     public ReadFuture await() throws InterruptedException {
118         return (ReadFuture) super.await();
119     }
120 
121     @Override
122     public ReadFuture awaitUninterruptibly() {
123         return (ReadFuture) super.awaitUninterruptibly();
124     }
125 
126     @Override
127     public ReadFuture addListener(IoFutureListener<?> listener) {
128         return (ReadFuture) super.addListener(listener);
129     }
130 
131     @Override
132     public ReadFuture removeListener(IoFutureListener<?> listener) {
133         return (ReadFuture) super.removeListener(listener);
134     }
135     
136     private static class ExceptionHolder {
137         private final Throwable exception;
138         
139         private ExceptionHolder(Throwable exception) {
140             this.exception = exception;
141         }
142     }
143 }