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 */ 020package org.apache.mina.core.future; 021 022import java.io.IOException; 023 024import org.apache.mina.core.RuntimeIoException; 025import org.apache.mina.core.session.IoSession; 026 027/** 028 * A default implementation of {@link WriteFuture}. 029 * 030 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 031 */ 032public class DefaultReadFuture extends DefaultIoFuture implements ReadFuture { 033 /** A static object used when the session is closed */ 034 private static final Object CLOSED = new Object(); 035 036 /** 037 * Creates a new instance. 038 * 039 * @param session The associated session 040 */ 041 public DefaultReadFuture(IoSession session) { 042 super(session); 043 } 044 045 /** 046 * {@inheritDoc} 047 */ 048 public Object getMessage() { 049 if (isDone()) { 050 Object v = getValue(); 051 052 if (v == CLOSED) { 053 return null; 054 } 055 056 if (v instanceof RuntimeException) { 057 throw (RuntimeException) v; 058 } 059 060 if (v instanceof Error) { 061 throw (Error) v; 062 } 063 064 if (v instanceof IOException || v instanceof Exception) { 065 throw new RuntimeIoException((Exception) v); 066 } 067 068 return v; 069 } 070 071 return null; 072 } 073 074 /** 075 * {@inheritDoc} 076 */ 077 public boolean isRead() { 078 if (isDone()) { 079 Object v = getValue(); 080 081 return (v != CLOSED && !(v instanceof Throwable)); 082 } 083 084 return false; 085 } 086 087 /** 088 * {@inheritDoc} 089 */ 090 public boolean isClosed() { 091 if (isDone()) { 092 return getValue() == CLOSED; 093 } 094 095 return false; 096 } 097 098 /** 099 * {@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}