public static Integer getMcastPort() { if (mcastPort == null) { mcastPort = new Integer(AvailablePort .getRandomAvailablePort(AvailablePort.JGROUPS)); } return mcastPort; }
public static Keeper getRandomAvailablePortKeeper(int protocol) { return getRandomAvailablePortKeeper(protocol, getAddress(protocol)); }
/** * Returns a randomly selected available port in the range 5001 to * 32767 that satisfies a modulus * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #MULTICAST}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getRandomAvailablePortWithMod(int protocol,int mod) { return getRandomAvailablePortWithMod(protocol, getAddress(protocol),mod); }
public static Keeper getRandomAvailablePortKeeper(int protocol, InetAddress addr) { while (true) { int port = getRandomWildcardBindPortNumber(); Keeper result = isPortKeepable(port, protocol, addr); if (result != null) { return result; } } }
/** * Returns a randomly selected available port in the provided range. * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #MULTICAST}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getAvailablePortInRange(int rangeBase, int rangeTop, int protocol) { return getAvailablePortInRange(protocol, getAddress(protocol), rangeBase, rangeTop); }
/** * Returns a randomly selected available port in the provided range. * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #JGROUPS}). * @param addr the bind-address or mcast address to use * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getAvailablePortInRange(int protocol, InetAddress addr, int rangeBase, int rangeTop) { for (int port = rangeBase; port <= rangeTop; port++) { if (isPortAvailable(port, protocol, addr)) { return port; } } return -1; }
public void testIsPortAvailable() throws IOException { ServerSocket socket = new ServerSocket(); int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); socket.bind(new InetSocketAddress(InetAddressUtil.LOOPBACK, port)); try { Assert.assertFalse(AvailablePort.isPortAvailable(port, AvailablePort.SOCKET, InetAddress.getByName(InetAddressUtil.LOOPBACK_ADDRESS))); //Get local host will return the hostname for the server, so this should succeed, since we're bound to the loopback address only. Assert.assertTrue(AvailablePort.isPortAvailable(port, AvailablePort.SOCKET, InetAddress.getLocalHost())); //This should test all interfaces. Assert.assertFalse(AvailablePort.isPortAvailable(port, AvailablePort.SOCKET)); } finally { socket.close(); } }
usage("Spurious command line: " + args[i]); usage("Missing protocol"); return; usage("Unknown protocol: " + protocolString); return; usage("Malformed port: " + portString); return; (isPortAvailable(port, protocol, addr) ? "" : "not ") + "available for a " + protocolString + " connection\n"); + getRandomAvailablePort(protocol, addr) + "\n");
/** * Returns whether or not the given port on the local host is * available (that is, unused). * * @param port * The port to check * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #JGROUPS}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static boolean isPortAvailable(final int port, int protocol) { return isPortAvailable(port, protocol, getAddress(protocol)); }
/** * Returns a randomly selected available port in the range 5001 to * 32767 that satisfies a modulus and the provided protocol * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #MULTICAST}). * @param addr the bind-address or mcast address to use * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getRandomAvailablePortWithMod(int protocol, InetAddress addr,int mod) { while (true) { int port = getRandomWildcardBindPortNumber(); if (isPortAvailable(port, protocol, addr) && (port % mod)==0) { return port; } } }
/** * Returns a randomly selected available port in the range 5001 to * 32767. * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #JGROUPS}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getRandomAvailablePort(int protocol) { return getRandomAvailablePort(protocol, getAddress(protocol)); }
/** * Returns array of unique randomly available tcp ports of specified count. */ public static int getRandomAvailablePortForDUnitSite() { int site = 1; String hostName = System.getProperty("hostName"); if(hostName != null && hostName.startsWith("host")) { if (hostName.length() > 4) { site = Integer.parseInt(hostName.substring(4)); } } int port = AvailablePort.getRandomAvailablePortWithMod(AvailablePort.SOCKET,site); return port; }
public static List<Keeper> getRandomAvailableTCPPortKeepers(int count) { List<Keeper> result = new ArrayList<Keeper>(); while (result.size() < count) { result.add(AvailablePort.getRandomAvailablePortKeeper(AvailablePort.SOCKET)); } return result; }
public static Keeper isPortKeepable(final int port, int protocol, InetAddress addr) { if (protocol == SOCKET) { // Try to create a ServerSocket if(addr == null) { return keepAllInterfaces(port); } else { return keepOneInterface(addr, port); } } else if (protocol == MULTICAST) { throw new IllegalArgumentException("You can not keep the JGROUPS protocol"); } else { throw new IllegalArgumentException(LocalizedStrings.AvailablePort_UNKNOWN_PROTOCOL_0.toLocalizedString(Integer.valueOf(protocol))); } }
private static Keeper keepAllInterfaces(int port) { if(!testOneInterface(null, port)) { return null; while(en2.hasMoreElements()) { InetAddress addr = (InetAddress) en2.nextElement(); boolean available = testOneInterface(addr, port); if(!available) { return null; return keepOneInterface(null, port);
private static boolean testOneInterface(InetAddress addr, int port) { Keeper k = keepOneInterface(addr, port); if (k != null) { k.release(); return true; } else { return false; } } private static Keeper keepOneInterface(InetAddress addr, int port) {
/** * Test to see if a given port is available port on all interfaces on this host. * @param port * @return true of if the port is free on all interfaces */ private static boolean testAllInterfaces(int port) { Keeper k = keepAllInterfaces(port); if (k != null) { k.release(); return true; } else { return false; } } private static Keeper keepAllInterfaces(int port) {
/** * Returns a randomly selected available port in the provided range. * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #MULTICAST}). * @param addr the bind-address or mcast address to use * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getAvailablePortInRange(int protocol, InetAddress addr, int rangeBase, int rangeTop) { for (int port = rangeBase; port <= rangeTop; port++) { if (isPortAvailable(port, protocol, addr)) { return port; } } return -1; }
public void testWildcardAddressBound() throws IOException { String osName = System.getProperty("os.name"); if(osName != null && osName.startsWith("Windows")) { //AvailablePort is useless on windows because it uses //setReuseAddr. Do nothing on windows. See bug #39368 return; } ServerSocket socket = new ServerSocket(); int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); socket.bind(new InetSocketAddress((InetAddress)null, port)); try { Assert.assertFalse(AvailablePort.isPortAvailable(port, AvailablePort.SOCKET)); } finally { socket.close(); } } }
usage("Spurious command line: " + args[i]); usage("Missing protocol"); return; usage("Unknown protocol: " + protocolString); return; usage("Malformed port: " + portString); return; (isPortAvailable(port, protocol, addr) ? "" : "not ") + "available for a " + protocolString + " connection\n"); + getRandomAvailablePort(protocol, addr) + "\n");