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.net.SocketAddress;
023: import java.util.concurrent.TimeUnit;
024:
025: /**
026: * The default implementation of {@link WriteRequest}.
027: *
028: * @author The Apache MINA Project (dev@mina.apache.org)
029: * @version $Rev: 594773 $, $Date: 2007-11-13 22:27:14 -0700 (Tue, 13 Nov 2007) $
030: */
031: public class DefaultWriteRequest implements WriteRequest {
032: private static final WriteFuture UNUSED_FUTURE = new WriteFuture() {
033: public boolean isWritten() {
034: return false;
035: }
036:
037: public void setWritten() {
038: }
039:
040: public IoSession getSession() {
041: return null;
042: }
043:
044: public void join() {
045: }
046:
047: public boolean join(long timeoutInMillis) {
048: return true;
049: }
050:
051: public boolean isReady() {
052: return true;
053: }
054:
055: public WriteFuture addListener(IoFutureListener<?> listener) {
056: throw new IllegalStateException(
057: "You can't add a listener to a dummy future.");
058: }
059:
060: public WriteFuture removeListener(IoFutureListener<?> listener) {
061: throw new IllegalStateException(
062: "You can't add a listener to a dummy future.");
063: }
064:
065: public WriteFuture await() throws InterruptedException {
066: return this ;
067: }
068:
069: public boolean await(long timeout, TimeUnit unit)
070: throws InterruptedException {
071: return true;
072: }
073:
074: public boolean await(long timeoutMillis)
075: throws InterruptedException {
076: return true;
077: }
078:
079: public WriteFuture awaitUninterruptibly() {
080: return this ;
081: }
082:
083: public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
084: return true;
085: }
086:
087: public boolean awaitUninterruptibly(long timeoutMillis) {
088: return true;
089: }
090:
091: public Throwable getException() {
092: return null;
093: }
094:
095: public void setException(Throwable cause) {
096: }
097: };
098:
099: private final Object message;
100: private final WriteFuture future;
101: private final SocketAddress destination;
102:
103: /**
104: * Creates a new instance without {@link WriteFuture}. You'll get
105: * an instance of {@link WriteFuture} even if you called this constructor
106: * because {@link #getFuture()} will return a bogus future.
107: */
108: public DefaultWriteRequest(Object message) {
109: this (message, null, null);
110: }
111:
112: /**
113: * Creates a new instance with {@link WriteFuture}.
114: */
115: public DefaultWriteRequest(Object message, WriteFuture future) {
116: this (message, future, null);
117: }
118:
119: /**
120: * Creates a new instance.
121: *
122: * @param message a message to write
123: * @param future a future that needs to be notified when an operation is finished
124: * @param destination the destination of the message. This property will be
125: * ignored unless the transport supports it.
126: */
127: public DefaultWriteRequest(Object message, WriteFuture future,
128: SocketAddress destination) {
129: if (message == null) {
130: throw new NullPointerException("message");
131: }
132:
133: if (future == null) {
134: future = UNUSED_FUTURE;
135: }
136:
137: this .message = message;
138: this .future = future;
139: this .destination = destination;
140: }
141:
142: public WriteFuture getFuture() {
143: return future;
144: }
145:
146: public Object getMessage() {
147: return message;
148: }
149:
150: public WriteRequest getOriginalRequest() {
151: return this ;
152: }
153:
154: public SocketAddress getDestination() {
155: return destination;
156: }
157:
158: @Override
159: public String toString() {
160: return message.toString();
161: }
162: }
|