use of java.nio.channels.ServerSocketChannel in project hadoop by apache.
the class SecureDataNodeStarter method getSecureResources.
/**
* Acquire privileged resources (i.e., the privileged ports) for the data
* node. The privileged resources consist of the port of the RPC server and
* the port of HTTP (not HTTPS) server.
*/
@VisibleForTesting
public static SecureResources getSecureResources(Configuration conf) throws Exception {
HttpConfig.Policy policy = DFSUtil.getHttpPolicy(conf);
boolean isSecure = UserGroupInformation.isSecurityEnabled();
// Obtain secure port for data streaming to datanode
InetSocketAddress streamingAddr = DataNode.getStreamingAddr(conf);
int socketWriteTimeout = conf.getInt(DFSConfigKeys.DFS_DATANODE_SOCKET_WRITE_TIMEOUT_KEY, HdfsConstants.WRITE_TIMEOUT);
int backlogLength = conf.getInt(CommonConfigurationKeysPublic.IPC_SERVER_LISTEN_QUEUE_SIZE_KEY, CommonConfigurationKeysPublic.IPC_SERVER_LISTEN_QUEUE_SIZE_DEFAULT);
ServerSocket ss = (socketWriteTimeout > 0) ? ServerSocketChannel.open().socket() : new ServerSocket();
ss.bind(streamingAddr, backlogLength);
// Check that we got the port we need
if (ss.getLocalPort() != streamingAddr.getPort()) {
throw new RuntimeException("Unable to bind on specified streaming port in secure " + "context. Needed " + streamingAddr.getPort() + ", got " + ss.getLocalPort());
}
if (!SecurityUtil.isPrivilegedPort(ss.getLocalPort()) && isSecure) {
throw new RuntimeException("Cannot start secure datanode with unprivileged RPC ports");
}
System.err.println("Opened streaming server at " + streamingAddr);
// Bind a port for the web server. The code intends to bind HTTP server to
// privileged port only, as the client can authenticate the server using
// certificates if they are communicating through SSL.
final ServerSocketChannel httpChannel;
if (policy.isHttpEnabled()) {
httpChannel = ServerSocketChannel.open();
InetSocketAddress infoSocAddr = DataNode.getInfoAddr(conf);
httpChannel.socket().bind(infoSocAddr);
InetSocketAddress localAddr = (InetSocketAddress) httpChannel.socket().getLocalSocketAddress();
if (localAddr.getPort() != infoSocAddr.getPort()) {
throw new RuntimeException("Unable to bind on specified info port in secure " + "context. Needed " + streamingAddr.getPort() + ", got " + ss.getLocalPort());
}
System.err.println("Successfully obtained privileged resources (streaming port = " + ss + " ) (http listener port = " + localAddr.getPort() + ")");
if (localAddr.getPort() > 1023 && isSecure) {
throw new RuntimeException("Cannot start secure datanode with unprivileged HTTP ports");
}
System.err.println("Opened info server at " + infoSocAddr);
} else {
httpChannel = null;
}
return new SecureResources(ss, httpChannel);
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class ScannerTest method test_Constructor_LReadableByteChannel.
public void test_Constructor_LReadableByteChannel() throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(null);
SocketChannel sc = SocketChannel.open();
sc.connect(ssc.socket().getLocalSocketAddress());
sc.configureBlocking(false);
assertFalse(sc.isBlocking());
ssc.accept().close();
ssc.close();
assertFalse(sc.isBlocking());
Scanner s = new Scanner(sc);
try {
s.hasNextInt();
fail();
} catch (IllegalBlockingModeException expected) {
}
sc.close();
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class AbstractSelectorTest method test_register_LSelectorI_error.
/**
* @tests AbstractSelector#register(Selector,int)
*/
public void test_register_LSelectorI_error() throws IOException {
Selector acceptSelector = SelectorProvider.provider().openSelector();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
acceptSelector.close();
assertFalse(acceptSelector.isOpen());
try {
ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
fail("should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
assertFalse(ssc.isRegistered());
acceptSelector = Selector.open();
ssc.configureBlocking(true);
try {
ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
fail("should throw IllegalBlockingModeException");
} catch (IllegalBlockingModeException e) {
// expected
}
assertFalse(ssc.isRegistered());
ssc.configureBlocking(false);
SelectionKey acceptKey = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
assertNotNull(acceptKey);
assertTrue(acceptSelector.keys().contains(acceptKey));
assertTrue(ssc.isRegistered());
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class SocketChannelTest method test_write$LByteBuffer2.
/**
* @tests java.nio.channels.SocketChannel#write(ByteBuffer[])
*/
public void test_write$LByteBuffer2() throws IOException {
// Set-up
ServerSocketChannel server = ServerSocketChannel.open();
server.socket().bind(null);
SocketChannel client = SocketChannel.open();
client.connect(server.socket().getLocalSocketAddress());
SocketChannel worker = server.accept();
// Test overlapping buffers
byte[] data = "Hello world!".getBytes("UTF-8");
ByteBuffer[] buffers = new ByteBuffer[3];
buffers[0] = ByteBuffer.wrap(data, 0, 6);
buffers[1] = ByteBuffer.wrap(data, 6, data.length - 6);
buffers[2] = ByteBuffer.wrap(data);
// Write them out, read what we wrote and check it
client.write(buffers);
client.close();
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
while (EOF != worker.read(readBuffer)) {
}
;
readBuffer.flip();
Buffer expected = ByteBuffer.allocate(1024).put(data).put(data).flip();
assertEquals(expected, readBuffer);
// Tidy-up
worker.close();
server.close();
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class SocketChannelTest method test_socketChannel_write_close.
/**
* @tests SocketChannel#write(ByteBuffer) after close
*/
public void test_socketChannel_write_close() throws Exception {
// regression 4 for HARMONY-549
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(localAddr2);
SocketChannel sc = SocketChannel.open();
sc.connect(localAddr2);
SocketChannel sock = ssc.accept();
ByteBuffer buf = null;
ssc.close();
sc.close();
try {
sc.write(buf);
fail("should throw NPE");
} catch (NullPointerException e) {
// expected
}
sock.close();
}
Aggregations