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