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