Example usage for java.net InetAddress getLocalHost

List of usage examples for java.net InetAddress getLocalHost

Introduction

In this page you can find the example usage for java.net InetAddress getLocalHost.

Prototype

public static InetAddress getLocalHost() throws UnknownHostException 

Source Link

Document

Returns the address of the local host.

Usage

From source file:edu.duke.cabig.c3pr.webservice.integration.SecurityWebServiceTest.java

@Override
protected void setUp() throws Exception {
    if (noEmbeddedTomcat) {
        endpointURL = new URL("https://localhost:8443/c3pr/services/services/SubjectManagement");
        initDataSourceFile();/* ww w  .  j a  va 2s. c  om*/
    } else {
        super.setUp();
        endpointURL = new URL("https://" + InetAddress.getLocalHost().getHostName() + ":" + sslPort
                + C3PR_CONTEXT + WS_ENDPOINT_SERVLET_PATH);
    }

    wsdlLocation = new URL(endpointURL.toString() + "?wsdl");

    logger.info("endpointURL: " + endpointURL);
    logger.info("wsdlLocation: " + wsdlLocation);
}

From source file:com.netflix.genie.web.configs.MvcConfigUnitTests.java

/**
 * Make sure we get the correct host.//from  w w  w . j a v a  2  s .  c  o m
 *
 * @throws UnknownHostException When the host can't be calculated
 */
@Test
public void canGetHostname() throws UnknownHostException {
    final String expectedHostname = InetAddress.getLocalHost().getCanonicalHostName();
    Assert.assertThat(this.mvcConfig.hostName(), Matchers.is(expectedHostname));
}

From source file:eu.stratosphere.core.fs.local.LocalFileSystem.java

/**
 * Constructs a new <code>LocalFileSystem</code> object.
 *///from  w w  w  . ja  v a 2s. co m
public LocalFileSystem() {
    this.workingDir = new Path(System.getProperty("user.dir")).makeQualified(this);

    String tmp = "unknownHost";

    try {
        tmp = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        LOG.error(e);
    }

    this.hostName = tmp;
}

From source file:com.redblackit.web.server.HostNetUtils.java

/**
 * Get local hostname/*from   w w w  . j a  v  a2s .  com*/
 * 
 * @return hostname
 */
public static String getLocalHostname() {
    String localhost;
    try {
        localhost = InetAddress.getLocalHost().getHostName();
        logger.debug("localhost=" + localhost);
    } catch (UnknownHostException ex) {
        localhost = "localhost";
        logger.warn("couldn't get localhost name, defaulting to localhost");
    }

    logger.debug("resulting localhost=" + localhost);
    return localhost;
}

From source file:de.codecentric.boot.admin.services.SpringBootAdminRegistratorTask.java

/**
 * @see java.lang.Runnable#run()/*from w ww  .j a v  a 2s  . c om*/
 */
@Override
public void run() {
    try {
        String id = env.getProperty("info.id");
        int port = env.getProperty("server.port", Integer.class);
        String adminUrl = env.getProperty("spring.boot.admin.url");
        RestTemplate template = new RestTemplate();
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        ApplicationList list = template.getForObject(adminUrl + "/api/applications", ApplicationList.class);
        for (Application app : list) {
            if (id.equals(app.getId())) {
                // the application is already registered at the admin tool
                LOGGER.debug("Application already registered with ID '{}'", id);
                return;
            }
        }
        // register the application with the used URL and port
        String url = new URL("http", InetAddress.getLocalHost().getCanonicalHostName(), port, "").toString();
        Application app = new Application();
        app.setId(id);
        app.setUrl(url);
        template.postForObject(adminUrl + "/api/applications", app, String.class);
        LOGGER.info("Application registered itself at the admin application with ID '{}' and URL '{}'", id,
                url);
    } catch (Exception e) {
        LOGGER.warn("Failed to register application at spring-boot-admin, message={}", e.getMessage());
    }
}

From source file:com.mgmtp.perfload.perfmon.util.PerfMonUtils.java

/**
 * Gets all IP addresses of the localhost.
 * /*from   w w  w .j a v  a 2 s . c o  m*/
 * @return an array of {@link InetAddress} objects
 */
public static InetAddress[] getNetworkIPAddresses() throws UnknownHostException {
    InetAddress localhost = InetAddress.getLocalHost();
    return InetAddress.getAllByName(localhost.getCanonicalHostName());
}

From source file:io.watchcat.node.Watchcat.java

@Bean
public String hostname() {
    ShellCommand getHostname = new ShellCommand("cat /etc/hostname");
    String hostname = getHostname.execute().replace("\n", "");

    if (hostname == null || hostname.length() == 0) {
        try {/*from   ww w . j  av a 2 s  . c  o m*/
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return hostname;
}

From source file:com.nridge.core.base.std.Platform.java

/**
 * Convenience method that returns the host name of the current machine.
 *
 * @return Host name.//  w w w  .j  ava2 s  . c om
 */
public static String getHostName() {
    String hostName;

    try {
        InetAddress inetAddress = InetAddress.getLocalHost();
        hostName = inetAddress.getHostName();
    } catch (UnknownHostException e) {
        hostName = "localhost";
    }

    return hostName;
}

From source file:SystemUtils.java

public static String GetHostname() {
    String hostname = null;//from   w w w.  j ava2  s.com

    try {
        InetAddress lh = InetAddress.getLocalHost();
        hostname = lh.getHostName();
    } catch (Exception e) {
    }

    return hostname;
}

From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java

/**
 * Test that by default we have protocol version 1.4.
 * //w w w . j av  a2  s.  c om
 * @throws Exception
 *             When the test failed.
 */
@Test
public void testDefault() throws Exception {
    final Agent agent = new ZabbixAgent();
    // give the agent some time to open the port
    Thread.sleep(100);
    final Socket socket = new Socket(InetAddress.getLocalHost(), ZabbixAgent.DEFAULT_PORT);

    final Writer out = new OutputStreamWriter(socket.getOutputStream());
    out.write("system.property[java.version]\n");
    out.flush();

    final InputStream in = socket.getInputStream();
    final byte[] buffer = new byte[1024];
    in.read(buffer);

    assertEquals('Z', buffer[0]);
    assertEquals('B', buffer[1]);
    assertEquals('X', buffer[2]);
    assertEquals('D', buffer[3]);
    // we'll take the rest for granted...

    socket.close();
    agent.stop();
}