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 * A default implementation of {@link WriteFuture}.
29 *
30 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
31 */
32 public class DefaultReadFuture extends DefaultIoFuture implements ReadFuture {
33 /** A static object used when the session is closed */
34 private static final Object CLOSED = new Object();
35
36 /**
37 * Creates a new instance.
38 *
39 * @param session The associated session
40 */
41 public DefaultReadFuture(IoSession session) {
42 super(session);
43 }
44
45 /**
46 * {@inheritDoc}
47 */
48 public Object getMessage() {
49 if (isDone()) {
50 Object v = getValue();
51
52 if (v == CLOSED) {
53 return null;
54 }
55
56 if (v instanceof RuntimeException) {
57 throw (RuntimeException) v;
58 }
59
60 if (v instanceof Error) {
61 throw (Error) v;
62 }
63
64 if (v instanceof IOException || v instanceof Exception) {
65 throw new RuntimeIoException((Exception) v);
66 }
67
68 return v;
69 }
70
71 return null;
72 }
73
74 /**
75 * {@inheritDoc}
76 */
77 public boolean isRead() {
78 if (isDone()) {
79 Object v = getValue();
80
81 return (v != CLOSED && !(v instanceof Throwable));
82 }
83
84 return false;
85 }
86
87 /**
88 * {@inheritDoc}
89 */
90 public boolean isClosed() {
91 if (isDone()) {
92 return getValue() == CLOSED;
93 }
94
95 return false;
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 public Throwable getException() {
102 if (isDone()) {
103 Object v = getValue();
104
105 if (v instanceof Throwable) {
106 return (Throwable)v;
107 }
108 }
109
110 return null;
111 }
112
113 /**
114 * {@inheritDoc}
115 */
116 public void setClosed() {
117 setValue(CLOSED);
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 public void setRead(Object message) {
124 if (message == null) {
125 throw new IllegalArgumentException("message");
126 }
127
128 setValue(message);
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 public void setException(Throwable exception) {
135 if (exception == null) {
136 throw new IllegalArgumentException("exception");
137 }
138
139 setValue(exception);
140 }
141
142 /**
143 * {@inheritDoc}
144 */
145 @Override
146 public ReadFuture await() throws InterruptedException {
147 return (ReadFuture) super.await();
148 }
149
150 /**
151 * {@inheritDoc}
152 */
153 @Override
154 public ReadFuture awaitUninterruptibly() {
155 return (ReadFuture) super.awaitUninterruptibly();
156 }
157
158 /**
159 * {@inheritDoc}
160 */
161 @Override
162 public ReadFuture addListener(IoFutureListener<?> listener) {
163 return (ReadFuture) super.addListener(listener);
164 }
165
166 /**
167 * {@inheritDoc}
168 */
169 @Override
170 public ReadFuture removeListener(IoFutureListener<?> listener) {
171 return (ReadFuture) super.removeListener(listener);
172 }
173 }