List of usage examples for java.net ServerSocket getLocalPort
public int getLocalPort()
From source file:org.apache.oozie.test.EmbeddedServletContainer.java
/** * Start the servlet container. The container starts on a free port. * * @throws Exception thrown if the container could not start. *//*from ww w .jav a 2s .c o m*/ public void start() throws Exception { if (StringUtils.isEmpty(host)) { host = InetAddress.getLocalHost().getHostName(); } ServerSocket ss = new ServerSocket(0); if (this.port == 0) { port = ss.getLocalPort(); } ss.close(); server.getConnectors()[0].setHost(host); server.getConnectors()[0].setPort(port); server.start(); System.out.println("Running embedded servlet container at: http://" + host + ":" + port); }
From source file:org.apache.flume.test.util.SyslogAgent.java
public void setRandomPort() throws IOException { ServerSocket s = new ServerSocket(0); port = s.getLocalPort(); s.close();/*w ww . j av a 2 s .c o m*/ }
From source file:com.kixeye.kixmpp.client.KixmppClientTest.java
@Before public void setUp() throws Exception { domain = UUID.randomUUID().toString().replace("-", ""); username = UUID.randomUUID().toString().replace("-", ""); password = UUID.randomUUID().toString().replace("-", ""); resource = UUID.randomUUID().toString().replace("-", ""); ServerSocket socketServer = new ServerSocket(); socketServer.bind(null);//ww w. j a va 2s . co m port = socketServer.getLocalPort(); socketServer.close(); StorageProviderRegistry providerRegistry = new MemoryStorageProviderRegistry(); final Entity adminJID = EntityImpl.parseUnchecked(username + "@" + domain); final AccountManagement accountManagement = (AccountManagement) providerRegistry .retrieve(AccountManagement.class); if (!accountManagement.verifyAccountExists(adminJID)) { accountManagement.addUser(adminJID, password); } TCPEndpoint tcpEndpoint = new TCPEndpoint(); tcpEndpoint.setPort(port); try (InputStream certStream = this.getClass().getResourceAsStream("/bogus_mina_tls.cert")) { server = new XMPPServer(domain); server.addEndpoint(tcpEndpoint); server.setStorageProviderRegistry(providerRegistry); server.setTLSCertificateInfo(certStream, "boguspw"); server.start(); server.addModule(new SoftwareVersionModule()); server.addModule(new EntityTimeModule()); server.addModule(new XmppPingModule()); server.addModule(new InBandRegistrationModule()); server.addModule(new AdhocCommandsModule()); final ServiceAdministrationModule serviceAdministrationModule = new ServiceAdministrationModule(); // unless admin user account with a secure password is added, this will be not become effective serviceAdministrationModule.setAddAdminJIDs(Arrays.asList(adminJID)); server.addModule(serviceAdministrationModule); } }
From source file:com.kixeye.relax.AsyncRestClientTest.java
@Before public void setUp() throws Exception { Server server = new ContainerServer(testContainer); connection = new SocketConnection(server); ServerSocket socketServer = new ServerSocket(0); port = socketServer.getLocalPort(); socketServer.close();/*from w ww .j av a 2 s.c om*/ connection.connect(new InetSocketAddress(port)); }
From source file:org.ngrinder.perftest.service.ConsoleManagerTest.java
@Test public void testAvailableSocket() throws IOException { ServerSocket serverSocket = null; try {//from www .ja v a2s .c om // When port is already used serverSocket = new ServerSocket(20111); int localPort = serverSocket.getLocalPort(); // It should be excluded in available ports List<Integer> availablePorts = NetworkUtils.getAvailablePorts("", 20, 20111, 40000); assertThat(availablePorts.contains(localPort), not(true)); assertThat(availablePorts.size(), is(20)); } finally { if (serverSocket != null) { serverSocket.close(); } } }
From source file:LCModels.MessageListener.java
public MessageListener(Session s) { int test = 0; try {/*from w ww. java 2 s. co m*/ ServerSocket ss1 = new ServerSocket(0); test = ss1.getLocalPort(); ss1.close(); } catch (IOException ex) { Logger.getLogger(MessageListener.class.getName()).log(Level.SEVERE, null, ex); } s.setPort(test); try { server = new ServerSocket(test); } catch (IOException ex) { System.out.println("ERROR KO!"); Logger.getLogger(MessageListener.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.barchart.netty.server.http.handlers.TestStaticResourceHandler.java
@Before public void setUp() throws Exception { final ServerSocket s = new ServerSocket(0); port = s.getLocalPort(); s.close();// ww w . jav a 2s.c om server = Servers.createHttpServer().group(new NioEventLoopGroup()) .requestHandler("/classpath", new StaticResourceHandler(this.getClass().getClassLoader(), "/files")) .requestHandler("/file", new StaticResourceHandler( new File(System.getProperty("user.dir") + "/src/test/resources/files"))); server.listen(port, "localhost"); client = new DefaultHttpClient(new PoolingClientConnectionManager()); }
From source file:org.sonatype.nexus.proxy.NonProxyHostsTest.java
private int getAFreePort() { int port = 0; ServerSocket serverSocket = null; try {/*from ww w . j ava2s . c o m*/ serverSocket = new ServerSocket(0); port = serverSocket.getLocalPort(); } catch (IOException e) { Assert.fail("Failed to find free port." + e.getMessage()); } finally { try { serverSocket.close(); } catch (IOException e) { Assert.fail("Failed to close port." + e.getMessage()); } } return port; }
From source file:ch.vorburger.mariadb4j.DBConfigurationBuilder.java
protected int detectFreePort() { try {/*w ww . jav a2 s.c o m*/ ServerSocket ss = new ServerSocket(0); port = ss.getLocalPort(); ss.setReuseAddress(true); ss.close(); return port; } catch (IOException e) { // This should never happen throw new RuntimeException(e); } }
From source file:org.apache.lens.server.LensJerseyTest.java
protected int getTestPort() { if (!isPortAlreadyFound()) { return port; }//from ww w. j a v a2s.c o m ServerSocket socket = null; try { socket = new ServerSocket(0); setPort(socket.getLocalPort()); } catch (IOException e) { LOG.info("Exception occured while creating socket. Use a default port number " + port); } finally { try { if (socket != null) { socket.close(); } } catch (IOException e) { LOG.info("Exception occured while closing the socket ", e); } } return port; }