Example usage for com.amazonaws.util EC2MetadataUtils getLocalHostName

List of usage examples for com.amazonaws.util EC2MetadataUtils getLocalHostName

Introduction

In this page you can find the example usage for com.amazonaws.util EC2MetadataUtils getLocalHostName.

Prototype

public static String getLocalHostName() 

Source Link

Document

Get the local hostname of the instance.

Usage

From source file:com.netflix.genie.web.configs.aws.AwsMvcConfig.java

License:Apache License

/**
 * Create an instance of {@link GenieHostInfo} using the EC2 metadata service as we're deployed in an AWS cloud
 * environment.//from   w  w w  .ja  v a2 s  . c o  m
 *
 * @return The {@link GenieHostInfo} instance
 * @throws UnknownHostException If all EC2 host instance calculation AND local resolution can't determine a hostname
 * @throws IllegalStateException If an instance can't be created
 */
@Bean
public GenieHostInfo genieHostInfo() throws UnknownHostException {
    final String ec2LocalHostName = EC2MetadataUtils.getLocalHostName();
    if (StringUtils.isNotBlank(ec2LocalHostName)) {
        return new GenieHostInfo(ec2LocalHostName);
    }

    final String ec2Ipv4Address = EC2MetadataUtils.getPrivateIpAddress();
    if (StringUtils.isNotBlank(ec2Ipv4Address)) {
        return new GenieHostInfo(ec2Ipv4Address);
    }

    final String localHostname = InetAddress.getLocalHost().getCanonicalHostName();
    if (StringUtils.isNotBlank(localHostname)) {
        return new GenieHostInfo(localHostname);
    }

    throw new IllegalStateException("Unable to resolve Genie host info");
}