List of usage examples for java.net ServerSocket close
public void close() throws IOException
From source file:org.auraframework.integration.test.archetype.AuraArchetypeSimpleTestMANUAL.java
@Test public void testProjectCreation() throws Throwable { Process jettyProcess = null;/*from w ww . j ava 2 s. com*/ workspace = new File(IOUtil.newTempDir("archetype")); try { // create a workspace to place the project files in workspace.mkdirs(); // generate a project from the archetype Process genProcess = startProcess(workspace, ImmutableList.of("mvn", "archetype:generate", "-DarchetypeRepository=" + archRepo, "-DarchetypeCatalog=" + archCatalog, "-DarchetypeGroupId=" + archetype.groupId, "-DarchetypeArtifactId=" + archetype.artifactId, "-DarchetypeVersion=" + archetype.version, "-DgroupId=" + project.groupId, "-DartifactId=" + project.artifactId, "-Dversion=" + project.version, "-Dpackage=" + projectPackage, "-DinteractiveMode=false")); goldMavenOutput(genProcess, "-creation.txt", "Failed to generate artifact!"); File projectDir = new File(workspace, project.artifactId); assertDirectory(projectDir); verifyGeneratedResources(projectDir); // build the new project Process buildProcess = startProcess(projectDir, ImmutableList.of("mvn", "install")); goldMavenOutput(buildProcess, "-install.txt", "Failed to build new project!"); // get a free port for jetty ServerSocket socket = new ServerSocket(0); int jettyPort = socket.getLocalPort(); socket.close(); // start up jetty jettyProcess = startProcess(projectDir, ImmutableList.of("mvn", "jetty:run", "-Djetty.port=" + jettyPort)); int status = 0; for (int i = 0; i < 30; i++) { try { HttpGet get = obtainGetMethod("/"); HttpResponse response = perform(get); status = getStatusCode(response); get.releaseConnection(); break; } catch (ConnectException ce) { // expected, before server is listening Thread.sleep(1000); } } assertEquals("Failed to connect to server", HttpStatus.SC_OK, status); verifyDefaultDocument(); verifySampleComponents(); } catch (Throwable t) { // if any errors in Jetty requests, let's print out the Jetty // console output for diag before killing the // test if (jettyProcess != null) { InputStream is = jettyProcess.getInputStream(); int len = is.available(); byte[] buf = new byte[len]; is.read(buf); System.err.println(new String(buf)); } throw t; } finally { // kill Jetty if (jettyProcess != null) { try { jettyProcess.exitValue(); } catch (IllegalThreadStateException e) { jettyProcess.destroy(); } } // cleanup generated workspace IOUtil.delete(workspace); } }
From source file:org.auraframework.archetype.AuraArchetypeSimpleTestMANUAL.java
public void testProjectCreation() throws Throwable { Process jettyProcess = null;//from ww w. ja va 2 s .c om workspace = new File( System.getProperty("java.io.tmpdir") + File.separator + getName() + System.currentTimeMillis()); try { // create a workspace to place the project files in workspace.mkdirs(); // generate a project from the archetype Process genProcess = startProcess(workspace, ImmutableList.of("mvn", "archetype:generate", "-DarchetypeRepository=" + archRepo, "-DarchetypeCatalog=" + archCatalog, "-DarchetypeGroupId=" + archetype.groupId, "-DarchetypeArtifactId=" + archetype.artifactId, "-DarchetypeVersion=" + archetype.version, "-DgroupId=" + project.groupId, "-DartifactId=" + project.artifactId, "-Dversion=" + project.version, "-Dpackage=" + projectPackage, "-DinteractiveMode=false")); goldMavenOutput(genProcess, "-creation.txt", "Failed to generate artifact!"); File projectDir = new File(workspace, project.artifactId); assertDirectory(projectDir); verifyGeneratedResources(projectDir); // build the new project Process buildProcess = startProcess(projectDir, ImmutableList.of("mvn", "install")); goldMavenOutput(buildProcess, "-install.txt", "Failed to build new project!"); // get a free port for jetty ServerSocket socket = new ServerSocket(0); int jettyPort = socket.getLocalPort(); socket.close(); // start up jetty jettyProcess = startProcess(projectDir, ImmutableList.of("mvn", "jetty:run", "-Djetty.port=" + jettyPort)); int status = 0; for (int i = 0; i < 30; i++) { try { HttpGet get = obtainGetMethod("/"); HttpResponse response = perform(get); status = getStatusCode(response); get.releaseConnection(); break; } catch (ConnectException ce) { // expected, before server is listening Thread.sleep(1000); } } assertEquals("Failed to connect to server", HttpStatus.SC_OK, status); verifyDefaultDocument(); verifySampleComponents(); } catch (Throwable t) { // if any errors in Jetty requests, let's print out the Jetty // console output for diag before killing the // test if (jettyProcess != null) { InputStream is = jettyProcess.getInputStream(); int len = is.available(); byte[] buf = new byte[len]; is.read(buf); System.err.println(new String(buf)); } throw t; } finally { // kill Jetty if (jettyProcess != null) { try { jettyProcess.exitValue(); } catch (IllegalThreadStateException e) { jettyProcess.destroy(); } } // cleanup generated workspace IOUtil.delete(workspace); } }
From source file:org.mule.tck.junit4.rule.FreePortFinder.java
/** * Check and log is a given port is available * * @param port the port number to check//from ww w. j av a2 s. com * @return true if the port is available, false otherwise */ public boolean isPortFree(int port) { boolean portIsFree = true; ServerSocket server = null; try { server = new ServerSocket(port); server.setReuseAddress(true); } catch (IOException e) { portIsFree = false; } finally { if (server != null) { try { server.close(); } catch (IOException e) { // ignore } } } return portIsFree; }
From source file:edu.umn.msi.tropix.transfer.http.server.embedded.JettyServer.java
private boolean isPortFree(final int port) { boolean isPortFree = true; ServerSocket socket = null; try {//w ww. java 2s . c o m socket = new ServerSocket(port); } catch (final IOException e) { isPortFree = false; } finally { // Close socket quitely. if (socket != null) { try { socket.close(); } catch (final IOException io) { LOG.debug("Failed to close socket " + socket); } } } return isPortFree; }
From source file:org.opendaylight.groupbasedpolicy.renderer.opflex.lib.OpflexConnectionServiceTest.java
private int getAvailableServerPort() { try {//from w w w . j a va 2 s. c om int freePort; ServerSocket s = create(new int[] { 6670, 6671, 6672, 6673, 6674, 6675, 6676, 6677, 6678 }); freePort = s.getLocalPort(); s.close(); return freePort; } catch (IOException ex) { return 0; } }
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. *///from w w w. j av a 2 s .c o m 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:org.apache.hadoop.ipc.TestServer.java
@Test public void testBindSimple() throws Exception { ServerSocket socket = new ServerSocket(); InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0); Server.bind(socket, address, 10);/*from w ww .j a va 2 s .c om*/ try { assertTrue(socket.isBound()); } finally { socket.close(); } }
From source file:org.wso2.developerstudio.eclipse.embedded.tomcat.server.EmbeddedTomcatServer.java
/** * Finds a port which is currently not in use. * /*from w w w . j av a 2s. c om*/ * @return Available Port. * * @throws IOException */ private Integer getAvailablePort() throws IOException { ServerSocket socket = new ServerSocket(0); Integer port = socket.getLocalPort(); socket.close(); return port; }
From source file:ru.calypso.ogar.server.OgarServer.java
private void checkPort() { boolean binded = false; while (!binded) try {//w w w .ja va 2 s . co m ServerSocket ss = new ServerSocket(Config.Server.PORT); ss.close(); binded = true; } catch (Exception e) { _log.warn("Port " + Config.Server.PORT + " is allready binded. Please free it!"); binded = false; try { Thread.sleep(1000); } catch (InterruptedException e2) { } } }
From source file:com.barchart.http.server.TestHttpServer.java
@Before public void setUp() throws Exception { server = new HttpServer(); basic = new TestRequestHandler("basic", false, 0, 0, false, false); async = new TestRequestHandler("async", true, 0, 0, false, false); asyncDelayed = new TestRequestHandler("async-delayed", true, 50, 0, false, false); clientDisconnect = new TestRequestHandler("", true, 500, 500, false, false); error = new TestRequestHandler("error", false, 0, 0, true, false); channelError = new TestRequestHandler("channel-error", false, 0, 0, false, true); infoHandler = new TestRequestHandler("info", false, 0, 0, false, false); serviceHandler = new TestRequestHandler("service", false, 0, 0, false, false); final ServerSocket s = new ServerSocket(0); port = s.getLocalPort();// ww w .ja va 2s. co m s.close(); final HttpServerConfig config = new HttpServerConfig().requestHandler("/basic", basic) .address(new InetSocketAddress("localhost", port)).parentGroup(new NioEventLoopGroup(1)) .childGroup(new NioEventLoopGroup(1)).requestHandler("/async", async) .requestHandler("/async-delayed", asyncDelayed) .requestHandler("/client-disconnect", clientDisconnect) .requestHandler("/channel-error", channelError).requestHandler("/error", error) .requestHandler("/service/info", infoHandler).requestHandler("/service", serviceHandler) .maxConnections(1); server.configure(config).listen().sync(); client = new DefaultHttpClient(new PoolingClientConnectionManager()); }