Example usage for java.net ServerSocket close

List of usage examples for java.net ServerSocket close

Introduction

In this page you can find the example usage for java.net ServerSocket close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this socket.

Usage

From source file:gobblin.service.FlowConfigTest.java

private static int chooseRandomPort() throws IOException {
    ServerSocket socket = null;
    try {//from  www .j  a  va  2s  .c  om
        socket = new ServerSocket(0);
        return socket.getLocalPort();
    } finally {
        if (socket != null) {
            socket.close();
        }
    }
}

From source file:org.apache.hadoop.mapred.MiniMRHACluster.java

private static int findEphemeralPort() throws IOException {
    ServerSocket socket = null;
    try {//from   w  ww .  ja v  a  2  s. c om
        socket = new ServerSocket(0);
        return socket.getLocalPort();
    } finally {
        if (socket != null) {
            socket.close();
        }
    }
}

From source file:org.apache.ranger.authorization.kafka.authorizer.KafkaRangerAuthorizerTest.java

@org.junit.BeforeClass
public static void setup() throws Exception {
    // Create keys
    String serviceDN = "CN=Service,O=Apache,L=Dublin,ST=Leinster,C=IE";
    String clientDN = "CN=Client,O=Apache,L=Dublin,ST=Leinster,C=IE";

    // Create a truststore
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    keystore.load(null, "security".toCharArray());

    serviceKeystorePath = KafkaTestUtils.createAndStoreKey(serviceDN, serviceDN, BigInteger.valueOf(30),
            "sspass", "myservicekey", "skpass", keystore);
    clientKeystorePath = KafkaTestUtils.createAndStoreKey(clientDN, clientDN, BigInteger.valueOf(31), "cspass",
            "myclientkey", "ckpass", keystore);

    File truststoreFile = File.createTempFile("kafkatruststore", ".jks");
    try (OutputStream output = new FileOutputStream(truststoreFile)) {
        keystore.store(output, "security".toCharArray());
    }/*from w  w  w . j  av a 2s. c  o  m*/
    truststorePath = truststoreFile.getPath();

    zkServer = new TestingServer();

    // Get a random port
    ServerSocket serverSocket = new ServerSocket(0);
    port = serverSocket.getLocalPort();
    serverSocket.close();

    tempDir = Files.createTempDirectory("kafka");

    final Properties props = new Properties();
    props.put("broker.id", 1);
    props.put("host.name", "localhost");
    props.put("port", port);
    props.put("log.dir", tempDir.toString());
    props.put("zookeeper.connect", zkServer.getConnectString());
    props.put("replica.socket.timeout.ms", "1500");
    props.put("controlled.shutdown.enable", Boolean.TRUE.toString());
    // Enable SSL
    props.put("listeners", "SSL://localhost:" + port);
    props.put("ssl.keystore.location", serviceKeystorePath);
    props.put("ssl.keystore.password", "sspass");
    props.put("ssl.key.password", "skpass");
    props.put("ssl.truststore.location", truststorePath);
    props.put("ssl.truststore.password", "security");
    props.put("security.inter.broker.protocol", "SSL");
    props.put("ssl.client.auth", "required");

    // Plug in Apache Ranger authorizer
    props.put("authorizer.class.name",
            "org.apache.ranger.authorization.kafka.authorizer.RangerKafkaAuthorizer");

    // Create users for testing
    UserGroupInformation.createUserForTesting(clientDN, new String[] { "public" });
    UserGroupInformation.createUserForTesting(serviceDN, new String[] { "IT" });

    KafkaConfig config = new KafkaConfig(props);
    kafkaServer = new KafkaServerStartable(config);
    kafkaServer.startup();

    // Create some topics
    ZkClient zkClient = new ZkClient(zkServer.getConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$);

    final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkServer.getConnectString()), false);
    AdminUtils.createTopic(zkUtils, "test", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
    AdminUtils.createTopic(zkUtils, "dev", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
}

From source file:org.glowroot.tests.WebDriverSetup.java

private static int getAvailablePort() throws Exception {
    if (SauceLabs.useSauceLabs()) {
        // glowroot must listen on one of the ports that sauce connect proxies
        // see https://saucelabs.com/docs/connect#localhost
        return 4000;
    } else {//from w ww  . j  av a 2  s  .co  m
        ServerSocket serverSocket = new ServerSocket(0);
        int port = serverSocket.getLocalPort();
        serverSocket.close();
        return port;
    }
}

From source file:org.openehealth.ipf.commons.ihe.ws.server.ServletServer.java

private static boolean isPortFree(int port) {
    ServerSocket socket = null;
    try {/* www  .  ja v  a  2s.  c  o m*/
        socket = new ServerSocket(port);
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                return false;
            }
        }
    }
}

From source file:org.apache.hadoop.gateway.GatewayServer.java

private static void checkAddressAvailability(InetSocketAddress address) throws IOException {
    ServerSocket socket = new ServerSocket();
    socket.bind(address);/*from   ww w .  jav a2 s  .c  o  m*/
    socket.close();
}

From source file:org.neo4j.server.helpers.ServerHelper.java

private static void checkServerCanStart(String host, int port) throws IOException {
    ServerSocket serverSocket = null;
    try {//from ww w  .j  av  a 2 s .c om
        serverSocket = new ServerSocket(port, 1, InetAddress.getByName(host));
    } catch (IOException ex) {
        throw new RuntimeException("Unable to start server on " + host + ":" + port, ex);
    } finally {
        if (serverSocket != null) {
            serverSocket.close();
        }
    }
}

From source file:au.com.jwatmuff.eventmanager.Main.java

/**
 * Binds to a TCP port to ensure we are the only instance of Event Manager
 * running on the computer. Returns false if the TCP port is already bound
 * and the force parameter is set to false.
 *///from  www . j a  va 2s. co m
private static boolean obtainRunLock(boolean force) {
    if (force)
        return true;
    try {
        final ServerSocket lockSocket = new ServerSocket(LOCK_PORT);
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    lockSocket.close();
                } catch (IOException e) {
                    log.warn("Failed to close lock socket", e);
                }
            }
        });
    } catch (IOException e) {
        log.info("Run lock is present");
        return false;
    }

    return true;
}

From source file:com.nesscomputing.service.discovery.server.TestStaticAnnounce.java

private static final int findUnusedPort() {
    int port;//  w  ww .  j a v  a2s .com

    ServerSocket socket = null;
    try {
        socket = new ServerSocket();
        socket.bind(new InetSocketAddress(0));
        port = socket.getLocalPort();
    } catch (final IOException ioe) {
        throw Throwables.propagate(ioe);
    } finally {
        try {
            socket.close();
        } catch (final IOException ioe) {
            // GNDN
        }
    }

    return port;
}

From source file:org.datavyu.models.db.MongoDatastore.java

public static int findFreePort(int port) throws IOException {
    int free_port = 0;
    while (free_port == 0) {
        try {/*from ww w . j a  v a 2s . com*/
            ServerSocket server = new ServerSocket(port);
            server.close();

            free_port = port;
        } catch (IOException io) {
            port += 1;
        }
    }
    return free_port;
}