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:org.thingsplode.agent.infrastructure.DeviceController.java

@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextRefreshedEvent) {
        ContextRefreshedEvent crevt = ((ContextRefreshedEvent) event);
        device = Device.create(deviceId, EnabledState.DISABLED, StatusInfo.OFFLINE);
        device.addComponents(systemComponentProvider.collect());
        try {/*from   w w w  .ja  v  a2s  .c  o m*/
            device.putIpAddress(InetAddress.getLocalHost());
            device.putStartupDate(Calendar.getInstance());
        } catch (UnknownHostException ex) {
            Logger.getLogger(DeviceController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:org.eclipse.epp.internal.logging.aeri.ui.utils.Proxies.java

/**
 * Returns the host name of this workstation (localhost)
 *//* w w w  . j av  a2  s . c o  m*/
public static Optional<String> getWorkstation() {
    try {
        return of(InetAddress.getLocalHost().getHostName());
    } catch (UnknownHostException e) {
        return absent();
    }
}

From source file:com.networknt.utility.Util.java

/**
 * Get InetAddress//from  www  . jav  a  2s  .  c  om
 * @return The InetAddress object
 * @deprecated
 */
public static InetAddress getInetAddress() {
    InetAddress inetAddress = null;
    try {
        inetAddress = InetAddress.getLocalHost();
    } catch (IOException ioe) {
        logger.error("Error in getting InetAddress", ioe);
    }
    return inetAddress;
}

From source file:com.cloudera.sqoop.util.TestDirectImportUtils.java

public void testLocalhost() throws UnknownHostException {
    InetAddress localHostAddr = InetAddress.getLocalHost();
    LOG.info("Advertised localhost address: " + localHostAddr);

    InetAddress requestAddr = InetAddress.getByName("localhost");
    LOG.info("Requested localhost address: " + requestAddr);

    assertTrue(/*from w ww  .  j a  va 2 s. c  om*/
            "Requested addr does not identify as localhost. "
                    + "Check /etc/hosts (see TestDirectImportUtils comments)",
            localHostAddr.equals(requestAddr));

    assertTrue(
            "Couldn't match 'localhost' to localhost; "
                    + "check /etc/hosts (see TestDirectImportUtils comments)",
            DirectImportUtils.isLocalhost("localhost"));
}

From source file:com.example.HoneycombFilter.java

public void init(FilterConfig filterConfig) throws ServletException {
    libhoney = new LibHoney.Builder().writeKey(filterConfig.getInitParameter("honeycomb.writeKey"))
            .dataSet(filterConfig.getInitParameter("honeycomb.dataSet")).build();
    try {/*from   ww  w. j  av a2s .c o  m*/
        libhoney.addField("server", InetAddress.getLocalHost().getHostName());
    } catch (UnknownHostException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:io.hawkcd.agent.AgentConfiguration.java

private static void getUserEnteredSettings() {
    Properties configFileProperties = fetchConfigFileProperties();

    String agentId = configFileProperties.getProperty("agentId");
    agentInfo.setId((agentId != null && !agentId.isEmpty()) ? agentId : generateAgentId(configFileProperties));

    String agentName = configFileProperties.getProperty("agentName");
    agentInfo.setName((agentName != null && !agentName.isEmpty()) ? agentName : ConfigConstants.AGENT_NAME);
    agentInfo.setConnected(true);/*from w  w  w  .  j a va  2s.c om*/
    agentInfo.setEnabled(false);
    agentInfo.setRootPath(ConfigConstants.AGENT_SANDBOX);
    agentInfo.setLastReportedTime(null);
    try {
        agentInfo.setHostName(InetAddress.getLocalHost().getHostName());
        agentInfo.setIpAddress(InetAddress.getLocalHost().getHostAddress());
        agentInfo.setOperatingSystem(System.getProperty("os.name"));

    } catch (UnknownHostException e) {
        agentInfo.setHostName("unknown");
        agentInfo.setIpAddress("unknown");
        agentInfo.setOperatingSystem("not available");
    }

    String agentPipelinesDir = configFileProperties.getProperty("agentPipelinesDir");
    installInfo.setAgentPipelinesDir(
            (agentPipelinesDir != null && !agentPipelinesDir.isEmpty()) ? agentPipelinesDir
                    : ConfigConstants.AGENT_PIPELINES_DIR);

    String serverName = configFileProperties.getProperty("serverName");
    installInfo.setServerName(
            (serverName != null && !serverName.isEmpty()) ? serverName : ConfigConstants.SERVER_NAME);

    String serverPort = configFileProperties.getProperty("serverPort");
    installInfo.setServerPort((serverPort != null && !serverPort.isEmpty()) ? Integer.parseInt(serverPort)
            : ConfigConstants.SERVER_PORT);

    installInfo.setServerAddress(
            String.format("http://%s:%s", installInfo.getServerName(), installInfo.getServerPort()));

    installInfo.setReportJobApiAddress(String.format("%s/%s", installInfo.getServerAddress(),
            String.format(ConfigConstants.SERVER_REPORT_JOB_API_ADDRESS, getAgentInfo().getId())));

    installInfo.setReportAgentApiAddress(String.format("%s/%s/%s/%s", installInfo.getServerAddress(),
            ConfigConstants.SERVER_REPORT_AGENT_API_ADDRESS, getAgentInfo().getId(), "report"));

    installInfo.setCheckForWorkApiAddress(String.format("%s/%s", installInfo.getServerAddress(),
            String.format(ConfigConstants.SERVER_CHECK_FOR_WORK_API_ADDRESS, getAgentInfo().getId())));

    installInfo.setCreateArtifactApiAddress(String.format("%s/%s", installInfo.getServerAddress(),
            ConfigConstants.SERVER_CREATE_ARTIFACT_API_ADDRESS));

    installInfo.setFetchArtifactApiAddress(String.format("%s/%s", installInfo.getServerAddress(),
            ConfigConstants.SERVER_FETCH_ARTIFACT_API_ADDRESS));

    installInfo.setAgentSandbox(Paths.get(ConfigConstants.AGENT_SANDBOX).toString());

    installInfo.setAgentTempDirectoryPath(
            Paths.get(ConfigConstants.AGENT_SANDBOX, ConfigConstants.AGENT_TEMP_DIR).toString());

    installInfo.setAgentArtifactsDirectoryPath(
            Paths.get(ConfigConstants.AGENT_SANDBOX, ConfigConstants.ARTIFACTS_DIRECTORY).toString());
}

From source file:com.alibaba.napoli.metamorphosis.network.RemotingUtils.java

public static String getLocalAddress() throws Exception {
    // ????ip?//  w  w  w  .jav  a2 s .c o  m
    final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
    InetAddress ipv6Address = null;
    while (enumeration.hasMoreElements()) {
        final NetworkInterface networkInterface = enumeration.nextElement();
        final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
        while (en.hasMoreElements()) {
            final InetAddress address = en.nextElement();
            if (!address.isLoopbackAddress()) {
                if (address instanceof Inet6Address) {
                    ipv6Address = address;
                } else {
                    // ipv4
                    return normalizeHostAddress(address);
                }
            }

        }

    }
    // ipv4?ipv6
    if (ipv6Address != null) {
        return normalizeHostAddress(ipv6Address);
    }
    final InetAddress localHost = InetAddress.getLocalHost();
    return normalizeHostAddress(localHost);
}

From source file:com.floragunn.searchguard.config.EvaluatorTest.java

@Test
public void testEval() throws IOException, MalformedConfigurationException {
    final TokenEvaluator te = new TokenEvaluator(new BytesArray(loadFile("ac_rules_6.json")));
    try {/*from w  w  w  .  j a  va2  s . c  o m*/
        te.getEvaluator(Lists.<String>newArrayList(), Lists.<String>newArrayList(),
                Lists.<String>newArrayList(), InetAddress.getLocalHost(), new User("test"));
        Assert.fail();
    } catch (final MalformedConfigurationException e) {
        //expected
        Assert.assertTrue(e.getMessage().contains("filters at all"));
    }

}

From source file:com.flipkart.foxtrot.core.querystore.impl.HazelcastConnection.java

public HazelcastConnection(ClusterConfig clusterConfig, ObjectMapper mapper) throws Exception {
    this.clusterConfig = clusterConfig;
    this.mapper = mapper;
    final String hostName = InetAddress.getLocalHost().getCanonicalHostName();
    Config hzConfig = new Config();
    hzConfig.getGroupConfig().setName(clusterConfig.getName());
    //        ManagementCenterConfig managementCenterConfig = new ManagementCenterConfig();
    //        managementCenterConfig.setEnabled(true);
    //        logger.info("Enabling management center for Hazelcast");
    //        managementCenterConfig.setUrl(clusterConfig.getWebServerUrl());
    //        logger.info("Setting management center url for Hazelcast to : " + clusterConfig.getWebServerUrl());
    //        hzConfig.setManagementCenterConfig(managementCenterConfig);
    hzConfig.setInstanceName(String.format("foxtrot-%s-%d", hostName, System.currentTimeMillis()));
    if (clusterConfig.isDisableMulticast()) {
        hzConfig.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
        for (String member : clusterConfig.getMembers()) {
            hzConfig.getNetworkConfig().getJoin().getTcpIpConfig().addMember(member);
        }// www.  j  a  va  2 s  .  co  m
        hzConfig.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
    }
    this.hazelcastConfig = hzConfig;
}

From source file:com.github.lynxdb.server.common.HostnameProvider.java

public HostnameProvider() {
    String tempHostName;//from   w  ww.  ja va  2s  .  c  om

    try {
        tempHostName = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException ex) {
        LOGGER.error("Hostname could not be determined using InetAddress method", ex);
        tempHostName = null;
    }

    if (tempHostName != null) {
        hostname = tempHostName;
        return;
    }

    try {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(Runtime.getRuntime().exec("cat /etc/hostname").getInputStream()));
        tempHostName = reader.readLine();
    } catch (IOException ex) {
        LOGGER.error("Hostname could not be determined using /etc/hostname method", ex);
        tempHostName = null;
    }

    if (tempHostName != null) {
        hostname = tempHostName;
        return;
    }

    throw new RuntimeException("Could not determine hostname");
}