public int getPort() { return hostAndPort.getPort(); }
@Override public Integer getPort() { return syncSource.getPort(); }
private static InetSocketAddress toUnresolvedAddress(HostAndPort address) { return InetSocketAddress.createUnresolved(address.getHost(), address.getPort()); }
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()); }
private static TTransport createRaw(HostAndPort address, Optional<SSLContext> sslContext, Optional<HostAndPort> socksProxy, int timeoutMillis) throws TTransportException { Proxy proxy = socksProxy .map(socksAddress -> new Proxy(SOCKS, InetSocketAddress.createUnresolved(socksAddress.getHost(), socksAddress.getPort()))) .orElse(Proxy.NO_PROXY); Socket socket = new Socket(proxy); try { socket.connect(new InetSocketAddress(address.getHost(), address.getPort()), timeoutMillis); socket.setSoTimeout(timeoutMillis); if (sslContext.isPresent()) { // SSL will connect to the SOCKS address when present HostAndPort sslConnectAddress = socksProxy.orElse(address); socket = sslContext.get().getSocketFactory().createSocket(socket, sslConnectAddress.getHost(), sslConnectAddress.getPort(), true); } return new TSocket(socket); } catch (Throwable t) { // something went wrong, close the socket and rethrow try { socket.close(); } catch (IOException e) { t.addSuppressed(e); } throw new TTransportException(t); } }
private static Iterable<InetSocketAddress> getZkAddresses(URI uri) { String zkServers = getZKServersFromDLUri(uri); String[] zkServerList = StringUtils.split(zkServers, ','); ImmutableList.Builder<InetSocketAddress> builder = ImmutableList.builder(); for (String zkServer : zkServerList) { HostAndPort hostAndPort = HostAndPort.fromString(zkServer).withDefaultPort(2181); builder.add(InetSocketAddress.createUnresolved( hostAndPort.getHostText(), hostAndPort.getPort())); } return builder.build(); }
private Connection createConnection(Map<String, String> additionalProperties) throws SQLException { String url = format("jdbc:presto://localhost:%s", server.getHttpsAddress().getPort()); Properties properties = new Properties(); properties.setProperty("user", "test"); properties.setProperty("SSL", "true"); properties.setProperty("SSLTrustStorePath", getResource("localhost.truststore").getPath()); properties.setProperty("SSLTrustStorePassword", "changeit"); properties.putAll(additionalProperties); return DriverManager.getConnection(url, properties); } }
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); }
/** * Overload of {@link #create(MetricRegistry, Optional, String, int)} which allows for setting * additional attributes in each reported metric. * * <p>The additional attributes are modeled as a Supplier to allow for attributes that might * change values at runtime. */ public static FastForwardReporter create( MetricRegistry registry, Optional<HostAndPort> address, String metricKey, int intervalSeconds, Supplier<Map<String, String>> additionalAttributes) throws SocketException, UnknownHostException { final FastForward ff; if (address.isPresent()) { ff = FastForward.setup(address.get().getHost(), address.get().getPort()); } else { ff = FastForward.setup(); } final ThreadFactory threadFactory = new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat("fast-forward-reporter-%d") .build(); final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(threadFactory); return new FastForwardReporter(ff, registry, executorService, metricKey, intervalSeconds, TimeUnit.SECONDS, additionalAttributes); }
/** * Parse the authority part of a URI. The authority part may have one of the following formats: * <ul> * <li>{@code "group:<groupName>"} for a group endpoint</li> * <li>{@code "<host>:<port>"} for a host endpoint</li> * <li>{@code "<host>"} for a host endpoint with no port number specified</li> * </ul> * An IPv4 or IPv6 address can be specified in lieu of a host name, e.g. {@code "127.0.0.1:8080"} and * {@code "[::1]:8080"}. */ public static Endpoint parse(String authority) { requireNonNull(authority, "authority"); if (authority.startsWith("group:")) { return ofGroup(authority.substring(6)); } final HostAndPort parsed = HostAndPort.fromString(authority).withDefaultPort(0); return create(parsed.getHost(), parsed.getPort()); }
null, bindAddress.getHost(), bindAddress.getPort(), isNullOrEmpty(contextPath) ? "/" : contextPath, null,
private SimpleConsumer createConsumer(TopicPartition topicPartition) { HostAndPort leader = findLeader(topicPartition); if (leader == null) { LOG.warn("no leader for topic {} partition {}", topicPartition.getTopic(), topicPartition.getPartition()); return null; } LOG.debug("leader for topic {} partition {} is {}", topicPartition.getTopic(), topicPartition.getPartition(), leader); final String clientName = getClientName(topicPartition); return createConsumer(leader.getHostText(), leader.getPort(), clientName); }
public void testFromParts() { HostAndPort hp = HostAndPort.fromParts("gmail.com", 81); assertEquals("gmail.com", hp.getHost()); assertTrue(hp.hasPort()); assertEquals(81, hp.getPort()); try { HostAndPort.fromParts("gmail.com:80", 81); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } try { HostAndPort.fromParts("gmail.com", -1); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
DruidServerHolder(DruidServer druidServer) { this.druidServer = druidServer; try { HostAndPort hostAndPort = HostAndPort.fromString(druidServer.getHost()); this.syncer = new ChangeRequestHttpSyncer<>( smileMapper, httpClient, executor, new URL(druidServer.getScheme(), hostAndPort.getHostText(), hostAndPort.getPort(), "/"), "/druid-internal/v1/segments", SEGMENT_LIST_RESP_TYPE_REF, config.getServerTimeout(), config.getServerUnstabilityTimeout(), createSyncListener() ); } catch (MalformedURLException ex) { throw new IAE(ex, "Failed to construct server URL."); } }
hostAndPort = HostAndPort.fromString(host); host = hostAndPort.getHostText(); portFromHostConfig = hostAndPort.hasPort() ? hostAndPort.getPort() : null; if (plainTextPort != null && portFromHostConfig != null && !plainTextPort.equals(portFromHostConfig)) { throw new IAE("Conflicting host:port [%s] and port [%d] settings", host, plainTextPort);
public ProgressMonitor(SecorConfig config) throws Exception { mConfig = config; mZookeeperConnector = new ZookeeperConnector(mConfig); try { Class timestampClass = Class.forName(mConfig.getKafkaClientClass()); this.mKafkaClient = (KafkaClient) timestampClass.newInstance(); this.mKafkaClient.init(config); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } mMessageParser = (MessageParser) ReflectionUtil.createMessageParser( mConfig.getMessageParserClass(), mConfig); mPrefix = mConfig.getMonitoringPrefix(); if (Strings.isNullOrEmpty(mPrefix)) { mPrefix = "secor"; } if (mConfig.getStatsDHostPort() != null && !mConfig.getStatsDHostPort().isEmpty()) { HostAndPort hostPort = HostAndPort.fromString(mConfig.getStatsDHostPort()); mStatsDClient = new NonBlockingStatsDClient(null, hostPort.getHostText(), hostPort.getPort(), mConfig.getStatsDDogstatsdConstantTags()); } }
/** * Removes a port from a host+port if the string contains the specified port. If the host+port does not contain * a port, or contains another port, the string is returned unaltered. For example, if hostWithPort is the * string {@code www.website.com:443}, this method will return {@code www.website.com}. * * <b>Note:</b> The hostWithPort string is not a URI and should not contain a scheme or resource. This method does * not attempt to validate the specified host; it <i>might</i> throw IllegalArgumentException if there was a problem * parsing the hostname, but makes no guarantees. In general, it should be validated externally, if necessary. * * @param hostWithPort string containing a hostname and optional port * @param portNumber port to remove from the string * @return string with the specified port removed, or the original string if it did not contain the portNumber */ public static String removeMatchingPort(String hostWithPort, int portNumber) { HostAndPort parsedHostAndPort = HostAndPort.fromString(hostWithPort); if (parsedHostAndPort.hasPort() && parsedHostAndPort.getPort() == portNumber) { // HostAndPort.getHostText() strips brackets from ipv6 addresses, so reparse using fromHost return HostAndPort.fromHost(parsedHostAndPort.getHost()).toString(); } else { return hostWithPort; } }
try { log.info("Finding new leader from Kafka brokers, try broker [%s]", broker.toString()); consumer = new SimpleConsumer(broker.getHostText(), broker.getPort(), SO_TIMEOUT_MILLIS, BUFFER_SIZE, leaderLookupClientId); TopicMetadataResponse resp = consumer.send(new TopicMetadataRequest(Collections.singletonList(topic)));