Search in sources :

Example 16 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class NetUtils method getFreeSocketPort.

/**
   * Return a free port number. There is no guarantee it will remain free, so
   * it should be used immediately.
   *
   * @returns A free port for binding a local socket
   */
public static int getFreeSocketPort() {
    int port = 0;
    try {
        ServerSocket s = new ServerSocket(0);
        port = s.getLocalPort();
        s.close();
        return port;
    } catch (IOException e) {
    // Could not get a free port. Return default port 0.
    }
    return port;
}
Also used : ServerSocket(java.net.ServerSocket) IOException(java.io.IOException)

Example 17 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestKMS method testKMSTimeout.

/**
   * Test the configurable timeout in the KMSClientProvider.  Open up a
   * socket, but don't accept connections for it.  This leads to a timeout
   * when the KMS client attempts to connect.
   * @throws Exception
   */
@Test
public void testKMSTimeout() throws Exception {
    File confDir = getTestDir();
    Configuration conf = createBaseKMSConf(confDir);
    conf.setInt(KMSClientProvider.TIMEOUT_ATTR, 1);
    writeConf(confDir, conf);
    ServerSocket sock;
    int port;
    try {
        sock = new ServerSocket(0, 50, InetAddress.getByName("localhost"));
        port = sock.getLocalPort();
    } catch (Exception e) {
        /* Problem creating socket?  Just bail. */
        return;
    }
    URL url = new URL("http://localhost:" + port + "/kms");
    URI uri = createKMSUri(url);
    boolean caughtTimeout = false;
    try {
        KeyProvider kp = createProvider(uri, conf);
        kp.getKeys();
    } catch (SocketTimeoutException e) {
        caughtTimeout = true;
    } catch (IOException e) {
        Assert.assertTrue("Caught unexpected exception" + e.toString(), false);
    }
    caughtTimeout = false;
    try {
        KeyProvider kp = createProvider(uri, conf);
        KeyProviderCryptoExtension.createKeyProviderCryptoExtension(kp).generateEncryptedKey("a");
    } catch (SocketTimeoutException e) {
        caughtTimeout = true;
    } catch (IOException e) {
        Assert.assertTrue("Caught unexpected exception" + e.toString(), false);
    }
    caughtTimeout = false;
    try {
        KeyProvider kp = createProvider(uri, conf);
        KeyProviderCryptoExtension.createKeyProviderCryptoExtension(kp).decryptEncryptedKey(new KMSClientProvider.KMSEncryptedKeyVersion("a", "a", new byte[] { 1, 2 }, "EEK", new byte[] { 1, 2 }));
    } catch (SocketTimeoutException e) {
        caughtTimeout = true;
    } catch (IOException e) {
        Assert.assertTrue("Caught unexpected exception" + e.toString(), false);
    }
    Assert.assertTrue(caughtTimeout);
    sock.close();
}
Also used : KeyProvider(org.apache.hadoop.crypto.key.KeyProvider) Configuration(org.apache.hadoop.conf.Configuration) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) URI(java.net.URI) KMSClientProvider(org.apache.hadoop.crypto.key.kms.KMSClientProvider) LoadBalancingKMSClientProvider(org.apache.hadoop.crypto.key.kms.LoadBalancingKMSClientProvider) AuthorizationException(org.apache.hadoop.security.authorize.AuthorizationException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) URL(java.net.URL) SocketTimeoutException(java.net.SocketTimeoutException) File(java.io.File) Test(org.junit.Test)

Example 18 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestJettyHelper method createJettyServer.

private Server createJettyServer() {
    try {
        InetAddress localhost = InetAddress.getByName("localhost");
        String host = "localhost";
        ServerSocket ss = new ServerSocket(0, 50, localhost);
        int port = ss.getLocalPort();
        ss.close();
        Server server = new Server();
        ServerConnector conn = new ServerConnector(server);
        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setRequestHeaderSize(JettyUtils.HEADER_SIZE);
        http_config.setResponseHeaderSize(JettyUtils.HEADER_SIZE);
        http_config.setSecureScheme("https");
        http_config.addCustomizer(new SecureRequestCustomizer());
        ConnectionFactory connFactory = new HttpConnectionFactory(http_config);
        conn.addConnectionFactory(connFactory);
        conn.setHost(host);
        conn.setPort(port);
        if (ssl) {
            SslContextFactory sslContextFactory = new SslContextFactory();
            sslContextFactory.setNeedClientAuth(false);
            sslContextFactory.setKeyStorePath(keyStore);
            sslContextFactory.setKeyStoreType(keyStoreType);
            sslContextFactory.setKeyStorePassword(keyStorePassword);
            conn.addFirstConnectionFactory(new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()));
        }
        server.addConnector(conn);
        return server;
    } catch (Exception ex) {
        throw new RuntimeException("Could not start embedded servlet container, " + ex.getMessage(), ex);
    }
}
Also used : SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServerSocket(java.net.ServerSocket) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ConnectionFactory(org.eclipse.jetty.server.ConnectionFactory) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) InetAddress(java.net.InetAddress)

Example 19 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestWebHdfsTimeouts method setUp.

@Before
public void setUp() throws Exception {
    Configuration conf = WebHdfsTestUtil.createConf();
    serverSocket = new ServerSocket(0, CONNECTION_BACKLOG);
    nnHttpAddress = new InetSocketAddress("localhost", serverSocket.getLocalPort());
    conf.set(DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY, "localhost:" + serverSocket.getLocalPort());
    if (timeoutSource == TimeoutSource.Configuration) {
        String v = Integer.toString(SHORT_SOCKET_TIMEOUT) + "ms";
        conf.set(HdfsClientConfigKeys.DFS_WEBHDFS_SOCKET_CONNECT_TIMEOUT_KEY, v);
        conf.set(HdfsClientConfigKeys.DFS_WEBHDFS_SOCKET_READ_TIMEOUT_KEY, v);
    }
    fs = WebHdfsTestUtil.getWebHdfsFileSystem(conf, WebHdfsConstants.WEBHDFS_SCHEME);
    if (timeoutSource == TimeoutSource.ConnectionFactory) {
        fs.connectionFactory = connectionFactory;
    }
    clients = new ArrayList<SocketChannel>();
    serverThread = null;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Before(org.junit.Before)

Example 20 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestServer method testBind.

@Test
public void testBind() throws Exception {
    Configuration conf = new Configuration();
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    socket.bind(address);
    try {
        int min = socket.getLocalPort();
        int max = min + 100;
        conf.set("TestRange", min + "-" + max);
        ServerSocket socket2 = new ServerSocket();
        InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
        Server.bind(socket2, address2, 10, conf, "TestRange");
        try {
            assertTrue(socket2.isBound());
            assertTrue(socket2.getLocalPort() > min);
            assertTrue(socket2.getLocalPort() <= max);
        } finally {
            socket2.close();
        }
    } finally {
        socket.close();
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

ServerSocket (java.net.ServerSocket)655 IOException (java.io.IOException)306 Socket (java.net.Socket)254 InetSocketAddress (java.net.InetSocketAddress)126 Test (org.junit.Test)94 SocketException (java.net.SocketException)54 InputStream (java.io.InputStream)48 SocketTimeoutException (java.net.SocketTimeoutException)42 OutputStream (java.io.OutputStream)40 InetAddress (java.net.InetAddress)39 BindException (java.net.BindException)27 URL (java.net.URL)26 InputStreamReader (java.io.InputStreamReader)23 File (java.io.File)22 UnknownHostException (java.net.UnknownHostException)22 SSLSocket (javax.net.ssl.SSLSocket)21 BufferedReader (java.io.BufferedReader)20 SSLServerSocket (javax.net.ssl.SSLServerSocket)20 ServerSocketChannel (java.nio.channels.ServerSocketChannel)15 SocketChannel (java.nio.channels.SocketChannel)14