001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.common;
021:
022: import java.io.IOException;
023:
024: /**
025: * A default implementation of {@link WriteFuture}.
026: *
027: * @author The Apache MINA Project (dev@mina.apache.org)
028: * @version $Rev: 599745 $, $Date: 2007-11-30 02:04:47 -0700 (Fri, 30 Nov 2007) $
029: */
030: public class DefaultReadFuture extends DefaultIoFuture implements
031: ReadFuture {
032:
033: private static final Object CLOSED = new Object();
034:
035: /**
036: * Creates a new instance.
037: */
038: public DefaultReadFuture(IoSession session) {
039: super (session);
040: }
041:
042: public Object getMessage() {
043: if (isReady()) {
044: Object v = getValue();
045: if (v == CLOSED) {
046: return null;
047: }
048:
049: if (v instanceof ExceptionHolder) {
050: v = ((ExceptionHolder) v).exception;
051: if (v instanceof RuntimeException) {
052: throw (RuntimeException) v;
053: }
054: if (v instanceof Error) {
055: throw (Error) v;
056: }
057: if (v instanceof IOException || v instanceof Exception) {
058: throw new RuntimeIoException((Exception) v);
059: }
060: }
061:
062: return v;
063: }
064:
065: return null;
066: }
067:
068: public boolean isRead() {
069: if (isReady()) {
070: Object v = getValue();
071: return (v != CLOSED && !(v instanceof ExceptionHolder));
072: }
073: return false;
074: }
075:
076: public boolean isClosed() {
077: if (isReady()) {
078: return getValue() == CLOSED;
079: }
080: return false;
081: }
082:
083: public Throwable getException() {
084: if (isReady()) {
085: Object v = getValue();
086: if (v instanceof ExceptionHolder) {
087: return ((ExceptionHolder) v).exception;
088: }
089: }
090: return null;
091: }
092:
093: public void setClosed() {
094: setValue(CLOSED);
095: }
096:
097: public void setRead(Object message) {
098: if (message == null) {
099: throw new NullPointerException("message");
100: }
101: setValue(message);
102: }
103:
104: public void setException(Throwable exception) {
105: if (exception == null) {
106: throw new NullPointerException("exception");
107: }
108:
109: setValue(new ExceptionHolder(exception));
110: }
111:
112: @Override
113: public ReadFuture await() throws InterruptedException {
114: return (ReadFuture) super .await();
115: }
116:
117: @Override
118: public ReadFuture awaitUninterruptibly() {
119: return (ReadFuture) super .awaitUninterruptibly();
120: }
121:
122: @Override
123: public ReadFuture addListener(IoFutureListener<?> listener) {
124: return (ReadFuture) super .addListener(listener);
125: }
126:
127: @Override
128: public ReadFuture removeListener(IoFutureListener<?> listener) {
129: return (ReadFuture) super .removeListener(listener);
130: }
131:
132: private static class ExceptionHolder {
133: private final Throwable exception;
134:
135: private ExceptionHolder(Throwable exception) {
136: this.exception = exception;
137: }
138: }
139: }
|