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

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

Introduction

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

Prototype

public static HostAndPort fromHost(String host) 

Source Link

Document

Build a HostAndPort instance from a host only.

Usage

From source file:io.bazel.rules.closure.webfiles.server.NetworkUtils.java

/** Turns {@code address} into a more human readable form. */
static HostAndPort createUrlAddress(HostAndPort address) {
    if (address.getHost().equals("::") || address.getHost().equals("0.0.0.0")) {
        return address.getPortOrDefault(80) == 80 ? HostAndPort.fromHost(getCanonicalHostName())
                : HostAndPort.fromParts(getCanonicalHostName(), address.getPort());
    } else {/*from w  w w  .  j av  a 2 s .c  o  m*/
        return address.getPortOrDefault(80) == 80 ? HostAndPort.fromHost(address.getHost()) : address;
    }
}

From source file:de.uniulm.omi.cloudiator.visor.sensors.bandwith.IperfSensor.java

@Override
protected void initialize(MonitorContext monitorContext, SensorConfiguration sensorConfiguration)
        throws SensorInitializationException {
    super.initialize(monitorContext, sensorConfiguration);

    host = HostAndPort.fromHost(sensorConfiguration.getValue(HOST_FIELD).orElse(HOST_DEFAULT_VALUE));
    final Iperf3Installer iperf3Installer = new Iperf3Installer();
    if (!iperf3Installer.installed()) {
        try {//from  www .  j  a v a 2s.c  o m
            iperf3Installer.install();
        } catch (IOException e) {
            throw new SensorInitializationException(e);
        }
    }
    try {
        new IperfServer().start();
    } catch (IOException e) {
        throw new SensorInitializationException(e);
    }
}

From source file:org.apache.brooklyn.location.jclouds.DefaultConnectivityResolver.java

/**
 * Combines the given resolve options with the customiser's configuration to determine the
 * best address and credential pair for management. In particular, if the resolve options
 * allow it will check that the credential is actually valid for the address.
 *///ww  w.java2  s  .c om
@Override
public ManagementAddressResolveResult resolve(JcloudsLocation location, NodeMetadata node, ConfigBag config,
        ConnectivityResolverOptions options) {
    LOG.debug("{} resolving management parameters for {}, node={}, config={}, options={}",
            new Object[] { this, location, node, config, options });
    final Stopwatch timer = Stopwatch.createStarted();
    // Should only be null in tests.
    final Entity contextEntity = getContextEntity(config);
    if (shouldPublishNetworks() && !options.isRebinding() && contextEntity != null) {
        publishNetworks(node, contextEntity);
    }
    HostAndPort hapChoice = null;
    LoginCredentials credChoice = null;

    final Iterable<HostAndPort> managementCandidates = getManagementCandidates(location, node, config, options);
    Iterable<LoginCredentials> credentialCandidates = Collections.emptyList();
    if (!Iterables.isEmpty(managementCandidates)) {
        credentialCandidates = getCredentialCandidates(location, node, options, config);

        // Try each pair of address and credential until one succeeds.
        if (shouldCheckCredentials() && options.pollForReachableAddresses()) {
            for (HostAndPort hap : managementCandidates) {
                for (LoginCredentials cred : credentialCandidates) {
                    LOG.trace("Testing host={} with credential={}", hap, cred);
                    if (checkCredential(location, hap, cred, config, options.isWindows())) {
                        hapChoice = hap;
                        credChoice = cred;
                        break;
                    }
                }
                if (hapChoice != null)
                    break;
            }
        } else if (shouldCheckCredentials()) {
            LOG.debug("{} set on {} but pollForFirstReachableAddress={}",
                    new Object[] { CHECK_CREDENTIALS.getName(), this, options.pollForReachableAddresses() });
        }
    }

    if (hapChoice == null) {
        LOG.trace("Choosing first management candidate given node={} and mode={}", node, getNetworkMode());
        hapChoice = Iterables.getFirst(managementCandidates, null);
    }
    if (hapChoice == null) {
        LOG.trace("Choosing first address of node={} in mode={}", node, getNetworkMode());
        final Iterator<String> hit = getResolvableAddressesWithMode(node).iterator();
        if (hit.hasNext())
            HostAndPort.fromHost(hit.next());
    }

    if (hapChoice == null) {
        LOG.error("None of the addresses of node {} are reachable in mode {}",
                new Object[] { node, getNetworkMode() });
        throw new IllegalStateException(
                "Could not determine management address for node: " + node + " in mode: " + getNetworkMode());
    }

    if (credChoice == null) {
        credChoice = Iterables.getFirst(credentialCandidates, null);
        if (credChoice == null) {
            throw new IllegalStateException("No credentials configured for " + location);
        }
    }

    if (contextEntity != null) {
        contextEntity.sensors().set(Attributes.ADDRESS, hapChoice.getHostText());
    }

    // Treat AWS as a special case because the DNS fully qualified hostname in AWS is
    // (normally?!) a good way to refer to the VM from both inside and outside of the region.
    if (!isNetworkModeSet() && !options.isWindows()) {
        final boolean lookupAwsHostname = Boolean.TRUE
                .equals(config.get(JcloudsLocationConfig.LOOKUP_AWS_HOSTNAME));
        String provider = config.get(JcloudsLocationConfig.CLOUD_PROVIDER);
        if (provider == null) {
            provider = location.getProvider();
        }
        if (options.waitForConnectable() && "aws-ec2".equals(provider) && lookupAwsHostname) {
            // getHostnameAws sshes to the machine and curls 169.254.169.254/latest/meta-data/public-hostname.
            try {
                LOG.debug("Resolving AWS hostname of {}", location);
                String result = location.getHostnameAws(hapChoice, credChoice, config);
                hapChoice = HostAndPort.fromParts(result, hapChoice.getPort());
                LOG.debug("Resolved AWS hostname of {}: {}", location, result);
            } catch (Exception e) {
                LOG.debug("Failed to resolve AWS hostname of " + location, e);
            }
        }
    }

    ManagementAddressResolveResult result = new ManagementAddressResolveResult(hapChoice, credChoice);
    LOG.debug("{} resolved management parameters for {} in {}: {}",
            new Object[] { this, location, Duration.of(timer), result });
    return result;
}