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

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

Introduction

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

Prototype

public static HostAndPort fromString(String hostPortString) 

Source Link

Document

Split a freeform string into a host and port, without strict validation.

Usage

From source file:com.facebook.nifty.client.HttpClientConnector.java

public HttpClientConnector(String hostNameAndPort, String servicePath) throws URISyntaxException {
    super(new InetSocketAddress(HostAndPort.fromString(hostNameAndPort).getHostText(),
            HostAndPort.fromString(hostNameAndPort).getPort()));

    this.endpointUri = new URI("http", hostNameAndPort, servicePath, null);
}

From source file:org.apache.beam.runners.dataflow.worker.fn.SocketAddressFactory.java

/** Parse a {@link SocketAddress} from the given string. */
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);
        }/* ww  w.j  a va  2  s . c o m*/
        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.getHost(), hostAndPort.getPort());
    }
}

From source file:com.spotify.helios.servicescommon.DockerHost.java

private DockerHost(final String endpoint) {
    final String stripped = endpoint.replaceAll(".*://", "");
    final HostAndPort hostAndPort = HostAndPort.fromString(stripped);
    final String hostText = hostAndPort.getHostText();
    this.port = hostAndPort.getPortOrDefault(defaultPort());
    this.address = Strings.isNullOrEmpty(hostText) ? DEFAULT_HOST : hostText;
    this.host = address + ":" + port;
    this.uri = URI.create("http://" + address + ":" + port);
}

From source file:com.github.jcustenborder.kafka.connect.utils.config.validators.ValidHostAndPort.java

void validate(final String setting, final String input) {
    HostAndPort hostAndPort = HostAndPort.fromString(input);
    if (this.requireBracketsForIPv6) {
        hostAndPort = hostAndPort.requireBracketsForIPv6();
    }/*from   w w  w. j  av a  2s.  c om*/
    if (null != this.defaultPort) {
        hostAndPort.withDefaultPort(this.defaultPort);
    }

    if (Strings.isNullOrEmpty(hostAndPort.getHostText())) {
        throw new ConfigException(String.format("'%s'(%s) host cannot be blank or null.", setting, input));
    }

    if (this.portRequired && !hostAndPort.hasPort()) {
        throw new ConfigException(String.format("'%s'(%s) must specify a port.", setting, input));
    }

}

From source file:org.apache.accumulo.core.client.impl.MasterClient.java

public static MasterClientService.Client getConnection(ClientContext context) {
    checkArgument(context != null, "context is null");

    List<String> locations = context.getInstance().getMasterLocations();

    if (locations.size() == 0) {
        log.debug("No masters...");
        return null;
    }/*from  w w  w .  ja v a 2s  .c o  m*/

    HostAndPort master = HostAndPort.fromString(locations.get(0));
    if (0 == master.getPort())
        return null;

    try {
        // Master requests can take a long time: don't ever time out
        MasterClientService.Client client = ThriftUtil
                .getClientNoTimeout(new MasterClientService.Client.Factory(), master, context);
        return client;
    } catch (TTransportException tte) {
        Throwable cause = tte.getCause();
        if (null != cause && cause instanceof UnknownHostException) {
            // do not expect to recover from this
            throw new RuntimeException(tte);
        }
        log.debug("Failed to connect to master=" + master + ", will retry... ", tte);
        return null;
    }
}

From source file:io.prestosql.plugin.thrift.location.ExtendedSimpleAddressSelector.java

@Override
public Optional<SimpleAddress> selectAddress(Optional<String> context, Set<SimpleAddress> attempted) {
    if (!context.isPresent()) {
        return delegate.selectAddress(context, attempted);
    }//from w w w .  j  av a2 s .c o  m

    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));
}

From source file:org.apache.slider.core.zk.ZookeeperUtils.java

/**
 * Split a quorum list into a list of hostnames and ports
 * @param hostPortQuorumList split to a list of hosts and ports
 * @return a list of values/*from  w ww  .  j a va2 s  . c om*/
 */
public static List<HostAndPort> splitToHostsAndPorts(String hostPortQuorumList) {
    // split an address hot
    String[] strings = StringUtils.getStrings(hostPortQuorumList);
    int len = 0;
    if (strings != null) {
        len = strings.length;
    }
    List<HostAndPort> list = new ArrayList<HostAndPort>(len);
    if (strings != null) {
        for (String s : strings) {
            list.add(HostAndPort.fromString(s.trim()).withDefaultPort(DEFAULT_PORT));
        }
    }
    return list;
}

From source file:com.spotify.helios.testing.DockerHost.java

private DockerHost(final String endpoint, final String dockerCertPath) {
    if (endpoint.startsWith("unix://")) {
        this.port = 0;
        this.address = DEFAULT_HOST;
        this.host = endpoint;
        this.uri = URI.create(endpoint);
        this.bindURI = URI.create(endpoint);
    } else {/*from  w  ww. j  av a  2 s .  c om*/
        final String stripped = endpoint.replaceAll(".*://", "");
        final HostAndPort hostAndPort = HostAndPort.fromString(stripped);
        final String hostText = hostAndPort.getHostText();
        final String scheme = isNullOrEmpty(dockerCertPath) ? "http" : "https";

        this.port = hostAndPort.getPortOrDefault(defaultPort());
        this.address = isNullOrEmpty(hostText) ? DEFAULT_HOST : hostText;
        this.host = address + ":" + port;
        this.uri = URI.create(scheme + "://" + address + ":" + port);
        this.bindURI = URI.create("tcp://" + address + ":" + port);
    }

    this.dockerCertPath = dockerCertPath;
}

From source file:ezbake.security.service.admins.AdminSyncer.java

public void sendUpdates(Set<String> admins) throws Exception {
    logger.info("{} preparing to send updated list of EzAdmins to other instances",
            AdminSyncer.class.getSimpleName());

    List<String> instances = discovery.getEndpoints(ezsecurityConstants.SERVICE_NAME);
    if (instances.isEmpty()) {
        logger.info("Not sending EzAdmin updates. No other instances found");
    }//from  ww w .ja v  a2 s  .  c om

    for (String instance : instances) {
        HostAndPort hp = HostAndPort.fromString(instance);

        EzSecurity.Client client = null;
        try {

            client = ThriftUtils.getClient(EzSecurity.Client.class, hp, properties);
            logger.info("Sending update to {}", hp);
            client.updateEzAdmins(admins);

        } catch (Exception e) {
            logger.warn("Unable to send admins update to peer: {}", hp);
        } finally {
            if (client != null) {
                client.getInputProtocol().getTransport().close();
                client.getOutputProtocol().getTransport().close();
            }
        }
    }
}

From source file:com.streamsets.pipeline.lib.kafka.BaseKafkaValidationUtil.java

@Override
public List<HostAndPort> validateKafkaBrokerConnectionString(List<Stage.ConfigIssue> issues,
        String connectionString, String configGroupName, String configName, Stage.Context context) {
    List<HostAndPort> kafkaBrokers = new ArrayList<>();
    if (connectionString == null || connectionString.isEmpty()) {
        issues.add(context.createConfigIssue(configGroupName, configName, KafkaErrors.KAFKA_06, configName));
    } else {/* w ww.j  av  a  2 s . c  om*/
        String[] brokers = connectionString.split(",");
        for (String broker : brokers) {
            try {
                HostAndPort hostAndPort = HostAndPort.fromString(broker);
                if (!hostAndPort.hasPort() || hostAndPort.getPort() < 0) {
                    issues.add(context.createConfigIssue(configGroupName, configName, KafkaErrors.KAFKA_07,
                            connectionString));
                } else {
                    kafkaBrokers.add(hostAndPort);
                }
            } catch (IllegalArgumentException e) {
                issues.add(context.createConfigIssue(configGroupName, configName, KafkaErrors.KAFKA_07,
                        connectionString));
            }
        }
    }
    return kafkaBrokers;
}