1 7 package org.jboss.test.remoting; 8 9 import java.io.IOException ; 10 import java.net.ServerSocket ; 11 import java.security.SecureRandom ; 12 import org.jboss.logging.Logger; 13 14 17 public class TestUtil 18 { 19 private static final Logger log = Logger.getLogger(TestUtil.class); 20 21 public static int getRandomPort() 22 { 23 Integer port = null; 24 while(port == null) 25 { 26 port = getFreePort(); 27 } 28 return port.intValue(); 29 } 30 31 private static Integer getFreePort() 32 { 33 Integer port = null; 34 ServerSocket socket = null; 35 36 int p = Math.abs(new SecureRandom (String.valueOf(System.currentTimeMillis()).getBytes()).nextInt(2000)); 37 38 try 39 { 40 socket = new ServerSocket (p); 41 port = new Integer (p); 42 } 43 catch(IOException e) 44 { 45 log.debug("port " + p + " already in use. Will try another."); 46 } 47 finally 48 { 49 if(socket != null) 50 { 51 try 52 { 53 socket.close(); 54 } 55 catch(IOException e) 56 { 57 58 } 59 } 60 } 61 return port; 62 } 63 } | Popular Tags |