List of usage examples for java.net ServerSocket getLocalPort
public int getLocalPort()
From source file:com.meschbach.psi.tomcat6.Tomcat6Builder.java
/** * The following method is ugly. Horribly ugly. But its a necessity * unfortunately due to Tomcat sprinkling copies of data everywhere. */// w w w .j av a 2 s. c om public int findOpenPort() throws PSIException { ServerSocket s = null; try { s = new ServerSocket(0); return s.getLocalPort(); } catch (IOException e) { throw new PSIException(e); } finally { if (s != null) { try { s.close(); } catch (IOException ioe) { throw new PSIException(ioe); } } } }
From source file:de.stklcode.jvault.connector.HTTPVaultConnectorTest.java
/** * Find and return a free TCP port.//from w w w. j a v a2 s . c o m * * @return port number */ private static Integer getFreePort() { ServerSocket socket = null; try { socket = new ServerSocket(0); socket.setReuseAddress(true); int port = socket.getLocalPort(); try { socket.close(); } catch (IOException e) { // Ignore IOException on close() } return port; } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } throw new IllegalStateException("Unable to find a free TCP port."); }
From source file:org.neo4j.mytests.SSShortestPathTest.java
@Before public void before() throws IOException { ServerSocket serverSocket = new ServerSocket(0); server = CommunityServerBuilder.server().onPort(serverSocket.getLocalPort()) .withThirdPartyJaxRsPackage("org.neo4j.hintplugin.utils", "/hintplugin/utils").build(); server.start();//from ww w. j a v a2s . c o m db = server.getDatabase().getGraph(); }
From source file:org.sonatype.nexus.index.DownloadRemoteIndexerManagerTest.java
@Override protected void setUp() throws Exception { super.setUp(); fakeCentral = new File(getBasedir(), "target/repos/fake-central"); fakeCentral.mkdirs();/* w w w.ja v a 2 s .c om*/ // create proxy server ServerSocket s = new ServerSocket(0); int port = s.getLocalPort(); s.close(); server = new Server(port); ResourceHandler resource_handler = new ResourceHandler() { @Override public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { System.out.print("JETTY: " + target); super.handle(target, request, response, dispatch); System.out.println(" :: " + ((Response) response).getStatus()); } }; resource_handler.setResourceBase(fakeCentral.getAbsolutePath()); HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() }); server.setHandler(handlers); System.out.print("JETTY Started on port: " + port); server.start(); // update central to use proxy server central.setDownloadRemoteIndexes(true); central.setRemoteUrl("http://localhost:" + port); central.setRepositoryPolicy(RepositoryPolicy.SNAPSHOT); nexusConfiguration.saveConfiguration(); Thread.sleep(100); wairForAsyncEventsToCalmDown(); waitForTasksToStop(); }
From source file:org.apache.hadoop.mapred.TestHAWebUI.java
private Server createJettyServer() throws Exception { InetAddress localhost = InetAddress.getByName("localhost"); String host = "localhost"; ServerSocket ss = new ServerSocket(0, 50, localhost); int port = ss.getLocalPort(); ss.close();/*from w ww.j ava2s . c o m*/ Server server = new Server(0); server.getConnectors()[0].setHost(host); server.getConnectors()[0].setPort(port); return server; }
From source file:com.netflix.hystrix.contrib.metrics.controller.HystricsMetricsControllerTest.java
@Override protected Application configure() { int port = 0; try {/*from w ww . j a v a 2 s . co m*/ final ServerSocket socket = new ServerSocket(0); port = socket.getLocalPort(); socket.close(); } catch (IOException e1) { throw new RuntimeException("Failed to find port to start test server"); } set(TestProperties.CONTAINER_PORT, port); try { SystemConfiguration.setSystemProperties("test.properties"); } catch (Exception e) { throw new RuntimeException("Failed to load config file"); } return new ResourceConfig(HystricsMetricsControllerTest.class, HystrixStreamFeature.class); }
From source file:org.apache.vysper.xmpp.extension.xep0124.inttests.IntegrationTestTemplate.java
private int findFreePort() throws IOException { ServerSocket ss = null; try {/* ww w . j a v a 2 s . c o m*/ ss = new ServerSocket(0); ss.setReuseAddress(true); return ss.getLocalPort(); } finally { if (ss != null) { ss.close(); } } }
From source file:org.sakaiproject.nakamura.testutils.http.DummyServer.java
/** * Create the dummy server on the first available port. There will be 100 tries before * we stop trying./*from w w w .ja v a 2 s. c o m*/ */ public DummyServer() { int attempts = 0; while (server == null) { try { // new ServerSocket(0) will automatically try to find a free port. ServerSocket socket = new ServerSocket(0); port = socket.getLocalPort(); socket.close(); server = new Server(port); server.setHandler(this); server.start(); break; } catch (Exception e) { if (server != null) { try { server.stop(); server.destroy(); } catch (Exception ex2) { } } server = null; } attempts++; if (attempts == 100) { throw new RuntimeException( "Unable to find a free port the range 8888 - 8988, aborting http server startup "); } } }
From source file:org.apache.apex.malhar.kafka.EmbeddedKafka.java
public void start() throws IOException { // Find port// w ww . java2s.c o m try { ServerSocket serverSocket = new ServerSocket(0); BROKERPORT = Integer.toString(serverSocket.getLocalPort()); serverSocket.close(); } catch (IOException e) { throw Throwables.propagate(e); } // Setup Zookeeper zkServer = new EmbeddedZookeeper(); String zkConnect = BROKERHOST + ":" + zkServer.port(); zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$); zkUtils = ZkUtils.apply(zkClient, false); // Setup brokers cleanupDir(); Properties props = new Properties(); props.setProperty("zookeeper.connect", zkConnect); props.setProperty("broker.id", "0"); props.setProperty("log.dirs", KAFKA_PATH); props.setProperty("listeners", "PLAINTEXT://" + BROKERHOST + ":" + BROKERPORT); KafkaConfig config = new KafkaConfig(props); Time mock = new MockTime(); kafkaServer = TestUtils.createServer(config, mock); }
From source file:com.ning.metrics.goodwill.access.CachingGoodwillAccessorTest.java
private int findFreePort() throws IOException { ServerSocket socket = null; try {/*w w w . j a v a 2 s . c o m*/ socket = new ServerSocket(0); return socket.getLocalPort(); } finally { if (socket != null) { socket.close(); } } }