/** * Build a HostAndPort instance from a host only. * * <p>Note: Non-bracketed IPv6 literals are allowed. Use {@link #requireBracketsForIPv6()} to * prohibit these. * * @param host the host-only string to parse. Must not contain a port number. * @return if parsing was successful, a populated HostAndPort object. * @throws IllegalArgumentException if {@code host} contains a port number. * @since 17.0 */ public static HostAndPort fromHost(String host) { HostAndPort parsedHost = fromString(host); checkArgument(!parsedHost.hasPort(), "Host has a port: %s", host); return parsedHost; }
private static int extractPort(@Nullable final String hostAndPort) { if (hostAndPort == null) { return -1; } return HostAndPort.fromString(hostAndPort).getPortOrDefault(-1); }
@Nullable private static String extractHost(@Nullable final String hostAndPort) { if (hostAndPort == null) { return null; } return HostAndPort.fromString(hostAndPort).getHostText(); }
@Override public Optional<SimpleAddress> selectAddress(Optional<String> context, Set<SimpleAddress> attempted) { if (!context.isPresent()) { return delegate.selectAddress(context, attempted); } List<String> list = Splitter.on(',').splitToList(context.get()); String value = list.get(ThreadLocalRandom.current().nextInt(list.size())); HostAndPort address = HostAndPort.fromString(value); return Optional.of(new SimpleAddress(address)); } }
public static URI parseServer(String server) { server = server.toLowerCase(ENGLISH); if (server.startsWith("http://") || server.startsWith("https://")) { return URI.create(server); } HostAndPort host = HostAndPort.fromString(server); try { return new URI("http", null, host.getHost(), host.getPortOrDefault(80), null, null, null); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } }
private static URI parseServer(String server) { server = server.toLowerCase(ENGLISH); if (server.startsWith("http://") || server.startsWith("https://")) { return URI.create(server); } HostAndPort host = HostAndPort.fromString(server); try { return new URI("http", null, host.getHost(), host.getPortOrDefault(80), null, null, null); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } }
/** * Build a HostAndPort instance from a host only. * * <p>Note: Non-bracketed IPv6 literals are allowed. Use {@link #requireBracketsForIPv6()} to * prohibit these. * * @param host the host-only string to parse. Must not contain a port number. * @return if parsing was successful, a populated HostAndPort object. * @throws IllegalArgumentException if {@code host} contains a port number. * @since 17.0 */ public static HostAndPort fromHost(String host) { HostAndPort parsedHost = fromString(host); checkArgument(!parsedHost.hasPort(), "Host has a port: %s", host); return parsedHost; }
private List<InetSocketAddress> parseServerIpAddresses(String dnsServerIps) { // Parse and prepare DNS server IP addresses for Netty. return StreamSupport // Split comma-separated sever IP:port combos. .stream(Splitter.on(",").trimResults().omitEmptyStrings().split(dnsServerIps).spliterator(), false) // Parse as HostAndPort objects (allows convenient handling of port provided after colon). .map(hostAndPort -> HostAndPort.fromString(hostAndPort).withDefaultPort(DnsClient.DEFAULT_DNS_PORT)) // Convert HostAndPort > InetSocketAddress as required by Netty. .map(hostAndPort -> new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort())) .collect(Collectors.toList()); }
/** * Build a HostAndPort instance from separate host and port values. * * <p>Note: Non-bracketed IPv6 literals are allowed. Use {@link #requireBracketsForIPv6()} to * prohibit these. * * @param host the host string to parse. Must not contain a port number. * @param port a port number from [0..65535] * @return if parsing was successful, a populated HostAndPort object. * @throws IllegalArgumentException if {@code host} contains a port number, or {@code port} is out * of range. */ public static HostAndPort fromParts(String host, int port) { checkArgument(isValidPort(port), "Port out of range: %s", port); HostAndPort parsedHost = fromString(host); checkArgument(!parsedHost.hasPort(), "Host has a port: %s", host); return new HostAndPort(parsedHost.host, port, parsedHost.hasBracketlessColons); }
/** * Returns host and port together as something that can be used as part of a URI. */ public String getHostAndPort() { if (enablePlaintextPort) { if (plaintextPort < 0) { return HostAndPort.fromString(host).toString(); } else { return HostAndPort.fromParts(host, plaintextPort).toString(); } } return null; }
public static HostAndPortWithScheme fromString(String scheme, String hostPortString) { return new HostAndPortWithScheme(checkAndGetScheme(scheme), HostAndPort.fromString(hostPortString)); }
public void testSerialization() { SerializableTester.reserializeAndAssert(HostAndPort.fromParts("host", 80)); SerializableTester.reserializeAndAssert(HostAndPort.fromString("host")); SerializableTester.reserializeAndAssert(HostAndPort.fromString("host:80")); SerializableTester.reserializeAndAssert(HostAndPort.fromString("[::1]:104")); SerializableTester.reserializeAndAssert(HostAndPort.fromParts("1::2", 103)); } }
public void testHashCodeAndEquals() { HostAndPort hpNoPort1 = HostAndPort.fromString("foo::123"); HostAndPort hpNoPort2 = HostAndPort.fromString("foo::123"); HostAndPort hpNoPort3 = HostAndPort.fromString("[foo::123]"); HostAndPort hpNoPort4 = HostAndPort.fromHost("[foo::123]"); HostAndPort hpNoPort5 = HostAndPort.fromHost("foo::123"); HostAndPort hpWithPort1 = HostAndPort.fromParts("[foo::123]", 80); HostAndPort hpWithPort2 = HostAndPort.fromParts("foo::123", 80); HostAndPort hpWithPort3 = HostAndPort.fromString("[foo::123]:80"); new EqualsTester() .addEqualityGroup(hpNoPort1, hpNoPort2, hpNoPort3, hpNoPort4, hpNoPort5) .addEqualityGroup(hpWithPort1, hpWithPort2, hpWithPort3) .testEquals(); }
private void addLocations(FileBlockInfo fileBlockInfo) { Set<String> locations = new HashSet<>(); // add alluxio locations for (BlockLocation location : fileBlockInfo.getBlockInfo().getLocations()) { locations.add(location.getWorkerAddress().getHost()); } // add underFS locations for (String location : fileBlockInfo.getUfsLocations()) { locations.add(HostAndPort.fromString(location).getHost()); } mLocations.addAll(locations); }
private void reconnect(Connection connection, String hostAndPort) { HostAndPort h_p = HostAndPort.fromString(hostAndPort); connections.get(hostAndPort).remove(connection); connection.close(); addConnection(h_p.getHost(), h_p.getPort(), false); }
public void testGetPortOrDefault() { assertEquals(80, HostAndPort.fromString("host:80").getPortOrDefault(123)); assertEquals(123, HostAndPort.fromString("host").getPortOrDefault(123)); }
public void testToString() { // With ports. assertEquals("foo:101", "" + HostAndPort.fromString("foo:101")); assertEquals(":102", HostAndPort.fromString(":102").toString()); assertEquals("[1::2]:103", HostAndPort.fromParts("1::2", 103).toString()); assertEquals("[::1]:104", HostAndPort.fromString("[::1]:104").toString()); // Without ports. assertEquals("foo", "" + HostAndPort.fromString("foo")); assertEquals("", HostAndPort.fromString("").toString()); assertEquals("[1::2]", HostAndPort.fromString("1::2").toString()); assertEquals("[::1]", HostAndPort.fromString("[::1]").toString()); // Garbage in, garbage out. assertEquals("[::]]:107", HostAndPort.fromParts("::]", 107).toString()); assertEquals("[[:]]:108", HostAndPort.fromString("[[:]]:108").toString()); }
public void testRequireBracketsForIPv6() { // Bracketed IPv6 works fine. assertEquals("::1", HostAndPort.fromString("[::1]").requireBracketsForIPv6().getHost()); assertEquals("::1", HostAndPort.fromString("[::1]:80").requireBracketsForIPv6().getHost()); // Non-bracketed non-IPv6 works fine. assertEquals("x", HostAndPort.fromString("x").requireBracketsForIPv6().getHost()); assertEquals("x", HostAndPort.fromString("x:80").requireBracketsForIPv6().getHost()); // Non-bracketed IPv6 fails. try { HostAndPort.fromString("::1").requireBracketsForIPv6(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
final HostAndPort parsedHost = HostAndPort.fromString(specifier); Preconditions.checkArgument(!parsedHost.hasPort()); final String host = parsedHost.getHost();
protected final void setup(String host, int port, String databaseName, String timeZone) { HiveClientConfig hiveClientConfig = getHiveClientConfig(); hiveClientConfig.setTimeZone(timeZone); String proxy = System.getProperty("hive.metastore.thrift.client.socks-proxy"); if (proxy != null) { hiveClientConfig.setMetastoreSocksProxy(HostAndPort.fromString(proxy)); } HiveCluster hiveCluster = new TestingHiveCluster(hiveClientConfig, host, port); ExtendedHiveMetastore metastore = new CachingHiveMetastore( new BridgingHiveMetastore(new ThriftHiveMetastore(hiveCluster)), executor, Duration.valueOf("1m"), Duration.valueOf("15s"), 10000); setup(databaseName, hiveClientConfig, metastore); }