Example usage for java.net ServerSocket getLocalPort

List of usage examples for java.net ServerSocket getLocalPort

Introduction

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

Prototype

public int getLocalPort() 

Source Link

Document

Returns the port number on which this socket is listening.

Usage

From source file:org.apache.ranger.authorization.hbase.HBaseRangerAuthorizationTest.java

private static int getFreePort() throws IOException {
    ServerSocket serverSocket = new ServerSocket(0);
    int port = serverSocket.getLocalPort();
    serverSocket.close();//  w  w  w .  j a  v a  2s. c om
    return port;
}

From source file:com.streamsets.datacollector.credential.cyberark.TestWebServicesFetcher.java

public static int getFreePort() throws IOException {
    ServerSocket serverSocket = new ServerSocket(0);
    int port = serverSocket.getLocalPort();
    serverSocket.close();/*from w ww. jav  a 2s . c  o  m*/
    return port;
}

From source file:atg.tools.dynunit.nucleus.NucleusUtils.java

/**
 * This method returns a free port number on the current machine. There is
 * some chance that the port number could be taken by the time the caller
 * actually gets around to using it./*from  www . j  a  va 2s  . c  o  m*/
 * <p/>
 * This method returns -9999 if it's not able to find a port.
 */
public static int findFreePort() {
    ServerSocket socket = null;
    int freePort = -9999;
    try {
        socket = new ServerSocket(0);
        freePort = socket.getLocalPort();
    } catch (IOException e) {
        logger.catching(e);
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
            logger.catching(e);
        }
    }
    return freePort;
}

From source file:org.paxml.el.UtilFunctions.java

/**
 * Get a random available port./* ww  w.  ja  va2  s .  com*/
 * @return the port, or minus if no available port found
 */
public static int getRandomPort() {
    ServerSocket s = null;

    try {
        s = new ServerSocket(0);
        return s.getLocalPort();
    } catch (IOException e) {
        log.warn("Cannot get random port", e);
        return -1;
    } finally {
        try {
            if (s != null) {
                s.close();
            }
        } catch (Exception e) {
            log.warn("Cannot close server socket of port: " + s.getLocalPort(), e);
        }
    }

}

From source file:org.sonar.process.BaseProcessTest.java

@Before
public void setup() throws IOException {
    ServerSocket socket = new ServerSocket(0);
    freePort = socket.getLocalPort();
    socket.close();//  ww w  . j a  v  a2  s  . co  m

    dummyAppJar = FileUtils.toFile(getClass().getResource("/sonar-dummy-app.jar"));
}

From source file:sample.config.RedisConfig.java

private int getPort() throws IOException {
    if (availablePort == null) {
        ServerSocket socket = new ServerSocket(0);
        availablePort = socket.getLocalPort();
        socket.close();/*from www.  j  av a  2 s . co  m*/
    }
    return availablePort;
}

From source file:org.apache.sling.commons.messaging.mail.MailTestSupport.java

protected synchronized int findFreePort() {
    try {//w  w w  . jav a 2  s.c o  m
        final ServerSocket serverSocket = new ServerSocket(0);
        final int port = serverSocket.getLocalPort();
        serverSocket.close();
        return port;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.jboss.aerogear.unifiedpush.migrator.EmbeddedMysqlDatabase.java

private int getFreePort() {
    ServerSocket socket = null;
    try {// ww w .j ava 2  s  .c om
        socket = new ServerSocket(0);
        return socket.getLocalPort();
    } catch (IOException e) {
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
            }
        }
    }
    return -1;
}

From source file:com.ning.billing.dbi.MysqlTestingHelper.java

public void startMysql() throws IOException {
    // New socket on any free port
    final ServerSocket socket = new ServerSocket(0);
    port = socket.getLocalPort();
    socket.close();//from  ww  w.  j a  v  a 2 s. co m

    dbDir = File.createTempFile("mysql", "");
    dbDir.delete();
    dbDir.mkdir();
    mysqldResource = new MysqldResource(dbDir);

    final Map<String, String> dbOpts = new HashMap<String, String>();
    dbOpts.put(MysqldResourceI.PORT, Integer.toString(port));
    dbOpts.put(MysqldResourceI.INITIALIZE_USER, "true");
    dbOpts.put(MysqldResourceI.INITIALIZE_USER_NAME, USERNAME);
    dbOpts.put(MysqldResourceI.INITIALIZE_PASSWORD, PASSWORD);

    mysqldResource.start("test-mysqld-thread", dbOpts);
    if (!mysqldResource.isRunning()) {
        throw new IllegalStateException("MySQL did not start.");
    } else {
        log.info("MySQL running on port " + mysqldResource.getPort());
    }
}

From source file:org.sonar.server.database.EmbeddedDatabaseTest.java

private int findFreeServerPort() throws IOException, InterruptedException {
    ServerSocket srv = new ServerSocket(0);
    int port = srv.getLocalPort();
    srv.close();//w w  w  .  j ava 2 s  . c o m
    Thread.sleep(1500);
    return port;
}