001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.net.server.test; 015 016import java.io.IOException; 017import java.net.BindException; 018import java.net.ServerSocket; 019 020import javax.net.ServerSocketFactory; 021 022/** 023 * Static utility methods for obtaining a {@link ServerSocket} bound to 024 * a random unused port. 025 * 026 * @author Carl Harris 027 */ 028public class ServerSocketUtil { 029 030 /** 031 * Creates a new {@link ServerSocket} bound to a random unused port. 032 * <p> 033 * This method is a convenience overload for 034 * {@link #createServerSocket(ServerSocketFactory)} using the platform's 035 * default {@link ServerSocketFactory}. 036 * @return socket 037 * @throws IOException 038 */ 039 public static ServerSocket createServerSocket() throws IOException { 040 return createServerSocket(ServerSocketFactory.getDefault()); 041 } 042 043 /** 044 * Creates a new {@link ServerSocket} bound to a random unused port. 045 * @param socketFactory socket factory that will be used to create the 046 * socket 047 * @return socket 048 * @throws IOException 049 */ 050 public static ServerSocket createServerSocket(ServerSocketFactory socketFactory) throws IOException { 051 ServerSocket socket = null; 052 int retries = 10; 053 while (retries-- > 0 && socket == null) { 054 int port = (int) ((65536 - 1024) * Math.random()) + 1024; 055 try { 056 socket = socketFactory.createServerSocket(port); 057 } catch (BindException ex) { 058 // try again with different port 059 } 060 } 061 if (socket == null) { 062 throw new BindException("cannot find an unused port to bind"); 063 } 064 return socket; 065 } 066 067}