package org.glassfish.grizzly.nio.transport;
import org.glassfish.grizzly.AbstractBindingHandler;
import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.utils.Exceptions;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.util.concurrent.locks.Lock;
public class TCPNIOBindingHandler extends AbstractBindingHandler {
private final TCPNIOTransport tcpTransport;
TCPNIOBindingHandler(final TCPNIOTransport tcpTransport) {
super(tcpTransport);
this.tcpTransport = tcpTransport;
}
@Override
public TCPNIOServerConnection bind(SocketAddress socketAddress) throws IOException {
return bind(socketAddress, tcpTransport.getServerConnectionBackLog());
}
@Override
public TCPNIOServerConnection bind(SocketAddress socketAddress, int backlog) throws IOException {
return bindToChannelAndAddress(
tcpTransport.getSelectorProvider().openServerSocketChannel(),
socketAddress,
backlog);
}
@Override
public TCPNIOServerConnection bindToInherited() throws IOException {
return bindToChannelAndAddress(
this.<ServerSocketChannel>getSystemInheritedChannel(ServerSocketChannel.class),
null,
-1);
}
@Override
public void unbind(Connection connection) {
tcpTransport.unbind(connection);
}
public static Builder builder(final TCPNIOTransport transport) {
return new TCPNIOBindingHandler.Builder().transport(transport);
}
private TCPNIOServerConnection bindToChannelAndAddress(final ServerSocketChannel serverSocketChannel,
final SocketAddress socketAddress,
final int backlog)
throws IOException {
TCPNIOServerConnection serverConnection = null;
final Lock lock = tcpTransport.getState().getStateLocker().writeLock();
lock.lock();
try {
final ServerSocket serverSocket = serverSocketChannel.socket();
tcpTransport.getChannelConfigurator().preConfigure(transport,
serverSocketChannel);
if (socketAddress != null) {
serverSocket.bind(socketAddress, backlog);
}
tcpTransport.getChannelConfigurator().postConfigure(transport,
serverSocketChannel);
serverConnection = tcpTransport.obtainServerNIOConnection(serverSocketChannel);
serverConnection.setProcessor(getProcessor());
serverConnection.setProcessorSelector(getProcessorSelector());
tcpTransport.serverConnections.add(serverConnection);
serverConnection.resetProperties();
if (!tcpTransport.isStopped()) {
tcpTransport.listenServerConnection(serverConnection);
}
return serverConnection;
} catch (Exception e) {
if (serverConnection != null) {
tcpTransport.serverConnections.remove(serverConnection);
serverConnection.closeSilently();
} else {
try {
serverSocketChannel.close();
} catch (IOException ignored) {
}
}
throw Exceptions.makeIOException(e);
} finally {
lock.unlock();
}
}
public static class Builder extends AbstractBindingHandler.Builder<Builder> {
private TCPNIOTransport transport;
public Builder transport(TCPNIOTransport transport) {
this.transport = transport;
return this;
}
public TCPNIOBindingHandler build() {
return (TCPNIOBindingHandler) super.build();
}
@Override
protected AbstractBindingHandler create() {
if (transport == null) {
throw new IllegalStateException(
"Unable to create TCPNIOBindingHandler - transport is null");
}
return new TCPNIOBindingHandler(transport);
}
}
}