Example usage for com.google.common.net HostAndPort getHostText

List of usage examples for com.google.common.net HostAndPort getHostText

Introduction

In this page you can find the example usage for com.google.common.net HostAndPort getHostText.

Prototype

public String getHostText() 

Source Link

Document

Returns the portion of this HostAndPort instance that should represent the hostname or IPv4/IPv6 literal.

Usage

From source file:com.pingcap.tikv.RegionStoreClient.java

public static RegionStoreClient create(Region region, Store store, TiSession session) {
    RegionStoreClient client = null;/*from   w ww .  j  a v  a  2  s .c o m*/
    try {
        HostAndPort address = HostAndPort.fromString(store.getAddress());
        ManagedChannel channel = ManagedChannelBuilder.forAddress(address.getHostText(), address.getPort())
                .usePlaintext(true).build();
        TiKVBlockingStub blockingStub = TiKVGrpc.newBlockingStub(channel);
        TiKVStub asyncStub = TiKVGrpc.newStub(channel);
        client = new RegionStoreClient(region, session, channel, blockingStub, asyncStub);
    } catch (Exception e) {
        if (client != null) {
            try {
                client.close();
            } catch (Exception ignore) {
            }
        }
    }
    return client;
}

From source file:org.apache.beam.harness.channel.SocketAddressFactory.java

/**
 * Parse a {@link SocketAddress} from the given string.
 *///  w ww.  j  a v  a 2s. com
public static SocketAddress createFrom(String value) {
    if (value.startsWith(UNIX_DOMAIN_SOCKET_PREFIX)) {
        // Unix Domain Socket address.
        // Create the underlying file for the Unix Domain Socket.
        String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length());
        File file = new File(filePath);
        if (!file.isAbsolute()) {
            throw new IllegalArgumentException("File path must be absolute: " + filePath);
        }
        try {
            if (file.createNewFile()) {
                // If this application created the file, delete it when the application exits.
                file.deleteOnExit();
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        // Create the SocketAddress referencing the file.
        return new DomainSocketAddress(file);
    } else {
        // Standard TCP/IP address.
        HostAndPort hostAndPort = HostAndPort.fromString(value);
        checkArgument(hostAndPort.hasPort(),
                "Address must be a unix:// path or be in the form host:port. Got: %s", value);
        return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort());
    }
}

From source file:io.mandrel.transport.thrift.nifty.NiftyClient.java

private static InetSocketAddress toInetAddress(HostAndPort hostAndPort) {
    return (hostAndPort == null) ? null
            : new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort());
}

From source file:com.facebook.presto.cli.ClientOptions.java

public static URI parseServer(String server) {
    server = server.toLowerCase(ENGLISH);
    if (server.startsWith("http://") || server.startsWith("https://")) {
        return URI.create(server);
    }/*w w  w  .j  av a  2s  . c om*/

    HostAndPort host = HostAndPort.fromString(server);
    try {
        return new URI("http", null, host.getHostText(), host.getPortOrDefault(80), null, null, null);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.linecorp.armeria.client.Endpoint.java

/**
 * Parse the authority part of a URI. The authority part may have one of the following formats:
 * <ul>/* ww  w . j av  a  2s .co  m*/
 *   <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>
 */
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 new Endpoint(parsed.getHostText(), parsed.getPort(), 1000);
}

From source file:com.groupon.mesos.util.UPID.java

public static UPID fromParts(final String id, final HostAndPort hostAndPort) throws IOException {
    checkNotNull(id, "id is null");
    checkNotNull(hostAndPort, "hostAndPort is null");

    return new UPID(id, hostAndPort, resolveIp(hostAndPort.getHostText()));
}

From source file:com.streamsets.pipeline.kafka.impl.KafkaValidationUtil08.java

private static TopicMetadata getTopicMetadata(List<HostAndPort> kafkaBrokers, String topic, int maxRetries,
        long backOffms) throws IOException {
    TopicMetadata topicMetadata = null;/*  w  ww  .ja v a 2s .co  m*/
    boolean connectionError = true;
    boolean retry = true;
    int retryCount = 0;
    while (retry && retryCount <= maxRetries) {
        for (HostAndPort broker : kafkaBrokers) {
            SimpleConsumer simpleConsumer = null;
            try {
                simpleConsumer = new SimpleConsumer(broker.getHostText(), broker.getPort(),
                        METADATA_READER_TIME_OUT, BUFFER_SIZE, METADATA_READER_CLIENT);

                List<String> topics = Collections.singletonList(topic);
                TopicMetadataRequest req = new TopicMetadataRequest(topics);
                kafka.javaapi.TopicMetadataResponse resp = simpleConsumer.send(req);

                // No exception => no connection error
                connectionError = false;

                List<TopicMetadata> topicMetadataList = resp.topicsMetadata();
                if (topicMetadataList == null || topicMetadataList.isEmpty()) {
                    //This broker did not have any metadata. May not be in sync?
                    continue;
                }
                topicMetadata = topicMetadataList.iterator().next();
                if (topicMetadata != null && topicMetadata.errorCode() == 0) {
                    retry = false;
                }
            } catch (Exception e) {
                //could not connect to this broker, try others
            } finally {
                if (simpleConsumer != null) {
                    simpleConsumer.close();
                }
            }
        }
        if (retry) {
            LOG.warn(
                    "Unable to connect or cannot fetch topic metadata. Waiting for '{}' seconds before retrying",
                    backOffms / 1000);
            retryCount++;
            if (!ThreadUtil.sleep(backOffms)) {
                break;
            }
        }
    }
    if (connectionError) {
        //could not connect any broker even after retries. Fail with exception
        throw new IOException(Utils.format(KafkaErrors.KAFKA_67.getMessage(), getKafkaBrokers(kafkaBrokers)));
    }
    return topicMetadata;
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

/**
 * Creates a {@link MongoDBClientSupport} instance in standalone mode.
 *//*from   www. j  av  a2s.c o m*/
public static MongoDBClientSupport forServer(AbstractMongoDBServer standalone) throws UnknownHostException {
    HostAndPort hostAndPort = BrooklynAccessUtils.getBrooklynAccessibleAddress(standalone,
            standalone.getAttribute(MongoDBServer.PORT));
    ServerAddress address = new ServerAddress(hostAndPort.getHostText(), hostAndPort.getPort());
    return new MongoDBClientSupport(address);
}

From source file:com.google.cloud.GrpcTransportOptions.java

/**
 * Returns a channel provider./*from   ww  w .  java2  s.c o m*/
 */
public static ChannelProvider getChannelProvider(ServiceOptions<?, ?> serviceOptions) {
    HostAndPort hostAndPort = HostAndPort.fromString(serviceOptions.getHost());
    InstantiatingChannelProvider.Builder builder = InstantiatingChannelProvider.newBuilder()
            .setServiceAddress(hostAndPort.getHostText()).setPort(hostAndPort.getPort())
            .setClientLibHeader(serviceOptions.getGoogApiClientLibName(),
                    firstNonNull(serviceOptions.getLibraryVersion(), ""));
    Credentials scopedCredentials = serviceOptions.getScopedCredentials();
    if (scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()) {
        builder.setCredentialsProvider(FixedCredentialsProvider.create(scopedCredentials));
    }
    return builder.build();
}

From source file:com.streamsets.pipeline.destination.aerospike.AerospikeBeanConfig.java

public static List<Host> getAerospikeHosts(List<Target.ConfigIssue> issues, List<String> connectionString,
        String configGroupName, String configName, Target.Context context) {
    List<Host> clusterNodesList = new ArrayList<>();
    if (connectionString == null || connectionString.isEmpty()) {
        issues.add(context.createConfigIssue(configGroupName, configName, AerospikeErrors.AEROSPIKE_01,
                configName));/*from  w w  w  .  jav a 2s  . c o  m*/
    } else {
        for (String node : connectionString) {
            try {
                HostAndPort hostAndPort = HostAndPort.fromString(node);
                if (!hostAndPort.hasPort() || hostAndPort.getPort() < 0) {
                    issues.add(context.createConfigIssue(configGroupName, configName,
                            AerospikeErrors.AEROSPIKE_02, connectionString));
                } else {
                    clusterNodesList.add(new Host(hostAndPort.getHostText(), hostAndPort.getPort()));
                }
            } catch (IllegalArgumentException e) {
                issues.add(context.createConfigIssue(configGroupName, configName, AerospikeErrors.AEROSPIKE_02,
                        connectionString));
            }
        }
    }
    return clusterNodesList;
}