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.torodb.packaging.util.MongoClientConfigurationFactory.java

public static MongoClientConfiguration getMongoClientConfiguration(AbstractShardReplication replication) {
    HostAndPort syncSource = HostAndPort.fromString(replication.getSyncSource().value()).withDefaultPort(27017);

    MongoClientConfiguration.Builder mongoClientConfigurationBuilder = new MongoClientConfiguration.Builder(
            syncSource);//from  ww w .  j av a2  s.  c  o m

    Ssl ssl = replication.getSsl();
    mongoClientConfigurationBuilder.setSslEnabled(ssl.getEnabled().value());
    if (ssl.getEnabled().value()) {
        try {
            mongoClientConfigurationBuilder.setSslAllowInvalidHostnames(ssl.getAllowInvalidHostnames().value());

            TrustManager[] tms = getTrustManagers(ssl);

            KeyManager[] kms = getKeyManagers(ssl);

            SSLContext sslContext;
            if (ssl.getFipsMode().value()) {
                sslContext = SSLContext.getInstance("TLS", "SunPKCS11-NSS");
            } else {
                sslContext = SSLContext.getInstance("TLS");
            }
            sslContext.init(kms, tms, null);
            mongoClientConfigurationBuilder.setSocketFactory(sslContext.getSocketFactory());
        } catch (CertificateException | KeyManagementException | KeyStoreException | UnrecoverableKeyException
                | NoSuchProviderException | NoSuchAlgorithmException | IOException exception) {
            throw new SystemException(exception);
        }
    }

    Auth auth = replication.getAuth();
    if (auth.getMode().value().isEnabled()) {
        MongoAuthenticationConfiguration mongoAuthenticationConfiguration = getMongoAuthenticationConfiguration(
                auth, ssl);
        mongoClientConfigurationBuilder.addAuthenticationConfiguration(mongoAuthenticationConfiguration);
    }

    return mongoClientConfigurationBuilder.build();
}

From source file:dk.dma.ais.reader.AisReaders.java

/**
 * Creates a {@link AisTcpReader} from a list of one or more hosts. On the form: host1:port1,...,hostN:portN
 * /*ww  w . ja va2 s  .c  o m*/
 * @param commaHostPort
 */
public static AisTcpReader createReader(String commaHostPort) {
    AisTcpReader r = new AisTcpReader();
    String[] hostPorts = StringUtils.split(commaHostPort, ",");
    for (String hp : hostPorts) {
        r.addHostPort(HostAndPort.fromString(hp));
    }
    return r;
}

From source file:google.registry.flows.TlsCredentials.java

static InetAddress parseInetAddress(String asciiAddr) {
    try {/*from w ww .j  a va 2  s .c  o m*/
        return InetAddresses.forString(HostAndPort.fromString(asciiAddr).getHostText());
    } catch (IllegalArgumentException e) {
        return null;
    }
}

From source file:com.facebook.presto.hive.DiscoveryLocatedHiveCluster.java

@Override
public HiveMetastoreClient createMetastoreClient() {
    List<ServiceDescriptor> descriptors = selector.selectAllServices().stream()
            .filter(input -> input.getState() != ServiceState.STOPPED).collect(toList());
    if (descriptors.isEmpty()) {
        throw new DiscoveryException("No metastore servers available for pool: " + selector.getPool());
    }/* w w w . jav  a 2  s.c o m*/

    Collections.shuffle(descriptors);
    TTransportException lastException = null;
    for (ServiceDescriptor descriptor : descriptors) {
        String thrift = descriptor.getProperties().get("thrift");
        if (thrift != null) {
            try {
                HostAndPort metastore = HostAndPort.fromString(thrift);
                checkArgument(metastore.hasPort());
                return clientFactory.create(metastore.getHostText(), metastore.getPort());
            } catch (IllegalArgumentException ignored) {
                // Ignore entries with parse issues
            } catch (TTransportException e) {
                lastException = e;
            }
        }
    }

    throw new DiscoveryException("Unable to connect to any metastore servers in pool: " + selector.getPool(),
            lastException);
}

From source file:org.apache.accumulo.monitor.servlets.ScanServlet.java

@Override
protected void pageBody(HttpServletRequest req, HttpServletResponse response, StringBuilder sb)
        throws IOException {
    Map<HostAndPort, ScanStats> scans = Monitor.getScans();
    Table scanTable = new Table("scanStatus", "Scan&nbsp;Status");
    scanTable.addSortableColumn("Server", new TServerLinkType(), null);
    scanTable.addSortableColumn("#", new PreciseNumberType(0, 20, 0, 100), "Number of scans presently running");
    scanTable.addSortableColumn("Oldest&nbsp;Age", new DurationType(0l, 5 * 60 * 1000l),
            "The age of the oldest scan on this server.");
    for (TabletServerStatus tserverInfo : Monitor.getMmi().getTServerInfo()) {
        ScanStats stats = scans.get(HostAndPort.fromString(tserverInfo.name));
        if (stats != null) {
            TableRow row = scanTable.prepareRow();
            row.add(tserverInfo);/*from  w  w w. j  av a  2 s . c om*/
            row.add(stats.scanCount);
            row.add(stats.oldestScan);
            scanTable.addRow(row);
        }
    }
    scanTable.generate(req, sb);
}

From source file:org.apache.accumulo.server.master.state.TServerInstance.java

public TServerInstance(String formattedString) {
    int pos = formattedString.indexOf("[");
    if (pos < 0 || !formattedString.endsWith("]")) {
        throw new IllegalArgumentException(formattedString);
    }//from www . j  a va2 s  .c om
    this.location = HostAndPort.fromString(formattedString.substring(0, pos));
    this.session = formattedString.substring(pos + 1, formattedString.length() - 1);
    this.cachedStringRepresentation = hostPort() + "[" + session + "]";
}

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));/* ww  w  .j  av 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;
}

From source file:me.j360.trace.storage.elasticsearch.LazyClient.java

@Override
protected Client compute() {
    Settings settings = Settings.builder().put("cluster.name", clusterName)
            .put("lazyClient.transport.sniff", true).build();

    TransportClient client = TransportClient.builder().settings(settings).build();
    for (String host : hosts) {
        HostAndPort hostAndPort = HostAndPort.fromString(host).withDefaultPort(9300);
        try {/* w ww.jav  a 2s  .c  o  m*/
            client.addTransportAddress(new InetSocketTransportAddress(
                    InetAddress.getByName(hostAndPort.getHostText()), hostAndPort.getPort()));
        } catch (UnknownHostException e) {
            // Hosts may be down transiently, we should still try to connect. If all of them happen
            // to be down we will fail later when trying to use the client when checking the index
            // template.
            continue;
        }
    }
    checkForIndexTemplate(client, indexTemplate);
    return client;
}

From source file:com.spotify.docker.client.DockerHost.java

private DockerHost(final String endpoint, final String certPath) {
    if (endpoint.startsWith("unix://")) {
        this.port = 0;
        this.address = DEFAULT_ADDRESS;
        this.host = endpoint;
        this.uri = URI.create(endpoint);
        this.bindUri = URI.create(endpoint);
    } else {/*from  www  . j  a v a2  s  .co  m*/
        final String stripped = endpoint.replaceAll(".*://", "");
        final HostAndPort hostAndPort = HostAndPort.fromString(stripped);
        final String hostText = hostAndPort.getHostText();
        final String scheme = isNullOrEmpty(certPath) ? "http" : "https";

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

    this.certPath = certPath;
}

From source file:alluxio.web.UIFileBlockInfo.java

private void addLocations(FileBlockInfo fileBlockInfo) {
    Set<String> locations = new HashSet<>();
    // add alluxio locations
    for (BlockLocation location : fileBlockInfo.getBlockInfo().getLocations()) {
        locations.add(location.getWorkerAddress().getHost());
    }/*w  w  w  . j a  va 2s . c o m*/
    // add underFS locations
    for (String location : fileBlockInfo.getUfsLocations()) {
        locations.add(HostAndPort.fromString(location).getHostText());
    }
    mLocations.addAll(locations);
}