001: //========================================================================
002: // Parts Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: //------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: //========================================================================
014:
015: package org.mortbay.jetty.grizzly;
016:
017: import com.sun.enterprise.web.connector.grizzly.OutputWriter;
018: import com.sun.enterprise.web.connector.grizzly.SelectorFactory;
019: import java.io.IOException;
020: import java.nio.ByteBuffer;
021: import java.nio.channels.ByteChannel;
022: import java.nio.channels.SelectionKey;
023: import java.nio.channels.Selector;
024: import java.nio.channels.SocketChannel;
025:
026: /**
027: *
028: * @author Jeanfrancois Arcand
029: */
030: public class GrizzlySocketChannel implements ByteChannel {
031:
032: private SocketChannel socketChannel;
033:
034: private SelectionKey key;
035:
036: private long readTimeout = 30 * 1000;
037:
038: private long writeTimeout = 30 * 1000;
039:
040: public GrizzlySocketChannel() {
041: }
042:
043: public int read(ByteBuffer dst) throws IOException {
044: //System.err.println("GrizzlySocketChannel.read");
045:
046: if (key == null)
047: return -1;
048:
049: int count = 1;
050: int byteRead = 0;
051: Selector readSelector = null;
052: SelectionKey tmpKey = null;
053:
054: try {
055: SocketChannel socketChannel = (SocketChannel) key.channel();
056: while (count > 0) {
057: count = socketChannel.read(dst);
058: if (count > -1)
059: byteRead += count;
060: else
061: byteRead = count;
062: }
063:
064: if (byteRead == 0) {
065: readSelector = SelectorFactory.getSelector();
066:
067: if (readSelector == null) {
068: return 0;
069: }
070: count = 1;
071: tmpKey = socketChannel.register(readSelector,
072: SelectionKey.OP_READ);
073: tmpKey.interestOps(tmpKey.interestOps()
074: | SelectionKey.OP_READ);
075: int code = readSelector.select(readTimeout);
076: tmpKey.interestOps(tmpKey.interestOps()
077: & (~SelectionKey.OP_READ));
078:
079: if (code == 0) {
080: throw new IOException("timing out");
081: }
082:
083: while (count > 0) {
084: count = socketChannel.read(dst);
085: if (count > -1)
086: byteRead += count;
087: else
088: byteRead = count;
089: }
090: }
091: } finally {
092: if (tmpKey != null)
093: tmpKey.cancel();
094:
095: if (readSelector != null) {
096: // Bug 6403933
097: try {
098: readSelector.selectNow();
099: } catch (IOException ex) {
100: ;
101: }
102: SelectorFactory.returnSelector(readSelector);
103: }
104: }
105: return byteRead;
106: }
107:
108: public boolean isOpen() {
109: return (socketChannel != null ? socketChannel.isOpen() : false);
110: }
111:
112: public void close() throws IOException {
113: socketChannel.close();
114: }
115:
116: public int write(ByteBuffer src) throws IOException {
117: return (int) OutputWriter.flushChannel(socketChannel, src,
118: writeTimeout);
119: }
120:
121: public SocketChannel getSocketChannel() {
122: return socketChannel;
123: }
124:
125: public void setSocketChannel(SocketChannel socketChannel) {
126: this .socketChannel = socketChannel;
127: }
128:
129: public SelectionKey getSelectionKey() {
130: return key;
131: }
132:
133: public void setSelectionKey(SelectionKey key) {
134: this .key = key;
135: }
136:
137: public long getReadTimeout() {
138: return readTimeout;
139: }
140:
141: public void setReadTimeout(long readTimeout) {
142: this .readTimeout = readTimeout;
143: }
144:
145: public long getWriteTimeout() {
146: return writeTimeout;
147: }
148:
149: public void setWriteTimeout(long writeTimeout) {
150: this.writeTimeout = writeTimeout;
151: }
152:
153: }
|