001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.tomcat.util.net;
019:
020: import java.io.IOException;
021: import java.nio.ByteBuffer;
022: import java.nio.channels.ByteChannel;
023: import java.nio.channels.SocketChannel;
024:
025: import org.apache.tomcat.util.net.NioEndpoint.Poller;
026: import org.apache.tomcat.util.net.SecureNioChannel.ApplicationBufferHandler;
027: import java.nio.channels.Selector;
028: import java.nio.channels.SelectionKey;
029:
030: /**
031: *
032: * Base class for a SocketChannel wrapper used by the endpoint.
033: * This way, logic for a SSL socket channel remains the same as for
034: * a non SSL, making sure we don't need to code for any exception cases.
035: *
036: * @author Filip Hanik
037: * @version 1.0
038: */
039: public class NioChannel implements ByteChannel {
040:
041: protected static ByteBuffer emptyBuf = ByteBuffer.allocate(0);
042:
043: protected SocketChannel sc = null;
044:
045: protected ApplicationBufferHandler bufHandler;
046:
047: protected Poller poller;
048:
049: public NioChannel(SocketChannel channel,
050: ApplicationBufferHandler bufHandler) throws IOException {
051: this .sc = channel;
052: this .bufHandler = bufHandler;
053: }
054:
055: public void reset() throws IOException {
056: bufHandler.getReadBuffer().clear();
057: bufHandler.getWriteBuffer().clear();
058: }
059:
060: public int getBufferSize() {
061: if (bufHandler == null)
062: return 0;
063: int size = 0;
064: size += bufHandler.getReadBuffer() != null ? bufHandler
065: .getReadBuffer().capacity() : 0;
066: size += bufHandler.getWriteBuffer() != null ? bufHandler
067: .getWriteBuffer().capacity() : 0;
068: return size;
069: }
070:
071: /**
072: * returns true if the network buffer has
073: * been flushed out and is empty
074: * @return boolean
075: */
076: public boolean flush(boolean block, Selector s, long timeout)
077: throws IOException {
078: return true; //no network buffer in the regular channel
079: }
080:
081: /**
082: * Closes this channel.
083: *
084: * @throws IOException If an I/O error occurs
085: * @todo Implement this java.nio.channels.Channel method
086: */
087: public void close() throws IOException {
088: getIOChannel().socket().close();
089: getIOChannel().close();
090: }
091:
092: public void close(boolean force) throws IOException {
093: if (isOpen() || force)
094: close();
095: }
096:
097: /**
098: * Tells whether or not this channel is open.
099: *
100: * @return <tt>true</tt> if, and only if, this channel is open
101: * @todo Implement this java.nio.channels.Channel method
102: */
103: public boolean isOpen() {
104: return sc.isOpen();
105: }
106:
107: /**
108: * Writes a sequence of bytes to this channel from the given buffer.
109: *
110: * @param src The buffer from which bytes are to be retrieved
111: * @return The number of bytes written, possibly zero
112: * @throws IOException If some other I/O error occurs
113: * @todo Implement this java.nio.channels.WritableByteChannel method
114: */
115: public int write(ByteBuffer src) throws IOException {
116: return sc.write(src);
117: }
118:
119: /**
120: * Reads a sequence of bytes from this channel into the given buffer.
121: *
122: * @param dst The buffer into which bytes are to be transferred
123: * @return The number of bytes read, possibly zero, or <tt>-1</tt> if the channel has reached end-of-stream
124: * @throws IOException If some other I/O error occurs
125: * @todo Implement this java.nio.channels.ReadableByteChannel method
126: */
127: public int read(ByteBuffer dst) throws IOException {
128: return sc.read(dst);
129: }
130:
131: public Object getAttachment(boolean remove) {
132: Poller pol = getPoller();
133: Selector sel = pol != null ? pol.getSelector() : null;
134: SelectionKey key = sel != null ? getIOChannel().keyFor(sel)
135: : null;
136: Object att = key != null ? key.attachment() : null;
137: if (key != null && att != null && remove)
138: key.attach(null);
139: return att;
140: }
141:
142: /**
143: * getBufHandler
144: *
145: * @return ApplicationBufferHandler
146: * @todo Implement this org.apache.tomcat.util.net.SecureNioChannel method
147: */
148: public ApplicationBufferHandler getBufHandler() {
149: return bufHandler;
150: }
151:
152: public Poller getPoller() {
153: return poller;
154: }
155:
156: /**
157: * getIOChannel
158: *
159: * @return SocketChannel
160: * @todo Implement this org.apache.tomcat.util.net.SecureNioChannel method
161: */
162: public SocketChannel getIOChannel() {
163: return sc;
164: }
165:
166: /**
167: * isClosing
168: *
169: * @return boolean
170: * @todo Implement this org.apache.tomcat.util.net.SecureNioChannel method
171: */
172: public boolean isClosing() {
173: return false;
174: }
175:
176: /**
177: * isInitHandshakeComplete
178: *
179: * @return boolean
180: * @todo Implement this org.apache.tomcat.util.net.SecureNioChannel method
181: */
182: public boolean isInitHandshakeComplete() {
183: return true;
184: }
185:
186: public int handshake(boolean read, boolean write)
187: throws IOException {
188: return 0;
189: }
190:
191: public void setPoller(Poller poller) {
192: this .poller = poller;
193: }
194:
195: public void setIOChannel(SocketChannel IOChannel) {
196: this .sc = IOChannel;
197: }
198:
199: public String toString() {
200: return super .toString() + ":" + this.sc.toString();
201: }
202:
203: }
|