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 java.nio.channels;
019:
020: import java.io.IOException;
021: import java.net.ServerSocket;
022: import java.nio.channels.spi.AbstractSelectableChannel;
023: import java.nio.channels.spi.SelectorProvider;
024:
025: /**
026: * A ServerSocketChannel is a partly abstracted stream-oriented listening socket
027: * which is selectable. Binding and manipulation of socket options can only be
028: * done through the associated <code>ServerSocket</code> object, returned by
029: * calling socket method. ServerSocketChannels can not be constructed for a
030: * pre-existing server socket, nor can it be assigned a SocketImpl.
031: * <p>
032: * A Server-Socket channel is open but not bound when created by
033: * <code>open</code> method. (Calling <code>accept</code> before bound will
034: * cause a <code>NotYetBoundException</code>). It can be bound by calling the
035: * bind method of a related <code>ServerSocket</code> instance.
036: * </p>
037: */
038: public abstract class ServerSocketChannel extends
039: AbstractSelectableChannel {
040:
041: /**
042: * Construct a new instance for ServerSocketChannel
043: *
044: * @param selectorProvider
045: * An instance of SelectorProvider
046: */
047: protected ServerSocketChannel(SelectorProvider selectorProvider) {
048: super (selectorProvider);
049: }
050:
051: /**
052: * Create an open and unbound server-socket channel.
053: * <p>
054: * This channel is got by calling <code>openServerSocketChannel</code>
055: * method of the default <code>SelectorProvider </code> instance.
056: * </p>
057: *
058: * @return The new created channel which is open but unbound.
059: * @throws IOException
060: * If some IO problem occurs.
061: */
062: public static ServerSocketChannel open() throws IOException {
063: return SelectorProvider.provider().openServerSocketChannel();
064: }
065:
066: /**
067: * Get the valid operations of this channel. Server-socket channels support
068: * accepting operation.Currently the only supported operation is OP_ACCEPT.
069: * It always returns <code>SelectionKey.OP_ACCEPT</code>.
070: *
071: * @see java.nio.channels.SelectableChannel#validOps()
072: * @return Valid operations in bit-set.
073: */
074: @Override
075: public final int validOps() {
076: return SelectionKey.OP_ACCEPT;
077: }
078:
079: /**
080: * Return the related server-socket of this channel. All public methods
081: * declared in returned object should be declared in
082: * <code>ServerSocket</code>.
083: *
084: * @return The related ServerSocket instance.
085: */
086: public abstract ServerSocket socket();
087:
088: /**
089: * Accepts a connection to this socket.
090: * <p>
091: * It returns null when the channel is non-blocking and no connections
092: * available, otherwise it blocks indefinitely until a new connection is
093: * available or an I/O error occurs. The returned channel will be in
094: * blocking mode any way.
095: * </p>
096: *
097: * <p>
098: * This method just execute the same security checks as the accept method of
099: * the <code>ServerSocket</code> class.
100: * </p>
101: *
102: * @return The accepted SocketChannel instance, or null as the channel is
103: * non-blocking and no connections available.
104: * @throws ClosedChannelException
105: * If the channel is already closed.
106: * @throws AsynchronousCloseException
107: * If the channel is closed by another thread while this method
108: * is in operation.
109: * @throws ClosedByInterruptException
110: * If another thread interrupts the calling thread while the
111: * operation is in progress. The calling thread will have the
112: * interrupt state set, and the channel will be closed.
113: * @throws NotYetBoundException
114: * If the socket has not yet been bound.
115: * @throws SecurityException
116: * If there is a security manager, and the new connection is not
117: * permitted to access.
118: * @throws IOException
119: * Some other IO error occurred.
120: *
121: */
122: public abstract SocketChannel accept() throws IOException;
123: }
|