Example usage for java.net InetAddress getHostAddress

List of usage examples for java.net InetAddress getHostAddress

Introduction

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

Prototype

public String getHostAddress() 

Source Link

Document

Returns the IP address string in textual presentation.

Usage

From source file:org.openbaton.nfvo.system.SystemStartup.java

@Override
public void run(String... args) throws Exception {
    log.info("Initializing OpenBaton");

    log.debug(Arrays.asList(args).toString());

    propFileLocation = propFileLocation.replace("file:", "");
    log.debug("Property file: " + propFileLocation);

    InputStream is = new FileInputStream(propFileLocation);
    Properties properties = new Properties();
    properties.load(is);/*ww  w.j av a 2s  .  co  m*/

    log.debug("Config Values are: " + properties.values());

    Configuration c = new Configuration();

    c.setName("system");
    c.setConfigurationParameters(new HashSet<ConfigurationParameter>());

    /**
     * Adding properties from file
     */
    for (Entry<Object, Object> entry : properties.entrySet()) {
        ConfigurationParameter cp = new ConfigurationParameter();
        cp.setConfKey((String) entry.getKey());
        cp.setValue((String) entry.getValue());
        c.getConfigurationParameters().add(cp);
    }

    /**
     * Adding system properties
     */
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
        ConfigurationParameter cp = new ConfigurationParameter();
        log.trace("Display name: " + netint.getDisplayName());
        log.trace("Name: " + netint.getName());
        cp.setConfKey("ip-" + netint.getName().replaceAll("\\s", ""));
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            if (inetAddress.getHostAddress().contains(".")) {
                log.trace("InetAddress: " + inetAddress.getHostAddress());
                cp.setValue(inetAddress.getHostAddress());
            }
        }
        log.trace("");
        c.getConfigurationParameters().add(cp);
    }

    configurationRepository.save(c);

    if (installPlugin) {
        startPlugins(pluginDir);
    }
}

From source file:org.scassandra.http.client.CurrentClient.java

public ConnectionReport getConnections(InetAddress address) {
    return getConnectionsByUrl("/" + address.getHostAddress());
}

From source file:cloud.thrift.server.conf.ZooKeeperConfig.java

public ZkClient registService() {
    String servicePath = "/" + serviceName;// 
    ZkClient zkClient = new ZkClient(serverList);
    boolean rootExists = zkClient.exists(servicePath);
    if (!rootExists) {
        zkClient.createPersistent(servicePath);
    }//from ww w  .j a  v  a  2s . c  o m
    InetAddress addr = null;
    try {
        addr = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    String ip = addr.getHostAddress().toString();
    String serviceInstance = System.nanoTime() + "-" + ip;
    // ??
    zkClient.createEphemeral(servicePath + "/" + serviceInstance);
    System.out.println("???" + servicePath + "/" + serviceInstance);
    return zkClient;
}

From source file:com.edmunds.etm.management.api.HostAddress.java

/**
 * Gets the host ip address.//  ww  w.  ja v a  2s . co  m
 *
 * @return host ip address
 */
public String getIpAddress() {
    if (ipAddress == null) {
        try {
            InetAddress addr = InetAddress.getByName(host);
            ipAddress = addr.getHostAddress();
        } catch (UnknownHostException e) {
            ipAddress = null;
        }
    }

    return ipAddress;
}

From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java

@SuppressWarnings("deprecation")
private String readWifiIP4Address() {
    String ip4Address = "0.0.0.0";
    byte[] ipAddress = BigInteger.valueOf(mWifiManager.getConnectionInfo().getIpAddress()).toByteArray();
    try {/*w w w  .j a  va  2s.c  om*/
        InetAddress address = InetAddress.getByAddress(ipAddress);
        String concreteAddressString = address.getHostAddress().toUpperCase(Locale.getDefault());
        if (InetAddressUtils.isIPv4Address(concreteAddressString)) {
            ip4Address = Formatter.formatIpAddress(mWifiManager.getConnectionInfo().getIpAddress());
        }
    } catch (UnknownHostException e) {
        return ip4Address;
    }
    return ip4Address;
}

From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java

private String readIP4AddressOfEmulator() throws SocketException {
    String inet4Address = "0.0.0.0";

    for (Enumeration<NetworkInterface> iter = NetworkInterface.getNetworkInterfaces(); iter
            .hasMoreElements();) {/*from   w w  w.  j  av  a  2s  . com*/
        NetworkInterface nic = iter.nextElement();

        if (nic.getName().startsWith("eth")) {
            Enumeration<InetAddress> addresses = nic.getInetAddresses();
            addresses.nextElement(); // skip first
            if (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();

                String concreteAddressString = address.getHostAddress().toUpperCase(Locale.getDefault());
                if (InetAddressUtils.isIPv4Address(concreteAddressString)) {
                    inet4Address = concreteAddressString;
                }
            }
        }
    }
    return inet4Address;
}

From source file:org.peterbaldwin.vlcremote.sweep.Worker.java

@Override
public void run() {
    // Note: InetAddress#isReachable(int) always returns false for Windows
    // hosts because applications do not have permission to perform ICMP
    // echo requests.
    for (;;) {//from  w  w  w . j  a  v  a 2 s  .c o  m
        byte[] ipAddress = mManager.pollIpAddress();
        if (ipAddress == null) {
            break;
        }
        try {
            InetAddress address = InetAddress.getByAddress(ipAddress);
            String hostAddress = address.getHostAddress();
            URL url = createUrl("http", hostAddress, mPort, mPath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(1000);
            try {
                int responseCode = connection.getResponseCode();
                String responseMessage = connection.getResponseMessage();
                InputStream inputStream = (responseCode == HttpURLConnection.HTTP_OK)
                        ? connection.getInputStream()
                        : connection.getErrorStream();
                if (inputStream != null) {
                    try {
                        int length = connection.getContentLength();
                        InputStreamEntity entity = new InputStreamEntity(inputStream, length);
                        entity.setContentType(connection.getContentType());

                        // Copy the entire response body into memory
                        // before the HTTP connection is closed:
                        String body = EntityUtils.toString(entity);

                        ProtocolVersion version = new ProtocolVersion("HTTP", 1, 1);
                        String hostname = address.getHostName();
                        StatusLine statusLine = new BasicStatusLine(version, responseCode, responseMessage);
                        HttpResponse response = new BasicHttpResponse(statusLine);
                        response.setHeader(HTTP.TARGET_HOST, hostname + ":" + mPort);
                        response.setEntity(new StringEntity(body));
                        mCallback.onReachable(ipAddress, response);
                    } finally {
                        inputStream.close();
                    }
                } else {
                    Log.w(TAG, "InputStream is null");
                }
            } finally {
                connection.disconnect();
            }
        } catch (IOException e) {
            mCallback.onUnreachable(ipAddress, e);
        }
    }
}

From source file:net.shopxx.controller.admin.VirAccountController.java

/**
 * /*  w ww . j av a  2 s. co  m*/
 */
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(Long id, String amount, String content, ModelMap model,
        RedirectAttributes redirectAttributes, String recruitMap) {

    VirAccount virAccount = virAccountService.find(id);
    Admin admin = adminService.getCurrent();

    InetAddress addr;
    try {
        addr = InetAddress.getLocalHost();
        virAccount.setLastRechargeIp(addr.getHostAddress());
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    BigDecimal rebateAmounts = virAccount.getRebateAmounts().add(new BigDecimal(amount));
    virAccount.setRebateAmounts(rebateAmounts);
    virAccount.setLastRechargeDate(new Date());
    virAccountService.update(virAccount);

    VirTradeLog virTradeLog = new VirTradeLog();
    virTradeLog.setVirAccount(virAccount);
    virTradeLog.setType(VirTradeLog.Type.rebate);
    virTradeLog.setOperator(admin.getName());
    if (content == null) {
        virTradeLog.setContent(admin.getName() + "? " + virAccount.getSerialNumber() + ""
                + new BigDecimal(amount));
    } else {
        virTradeLog.setContent(content);
    }
    virTradeLog.setRebateAmount(new BigDecimal(amount));
    virTradeLog.setRechargeAmount(new BigDecimal(0));
    virTradeLogService.save(virTradeLog);
    model.addAttribute("vitual", virAccount);
    addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);

    return "redirect:list.jhtml";
}

From source file:com.dropbox.presto.kafka.KafkaSplit.java

@JsonCreator
public KafkaSplit(@JsonProperty("clientId") String clientId, @JsonProperty("topicName") String topicName,
        @JsonProperty("partitionId") int partitionId, @JsonProperty("brokerHost") String brokerHost,
        @JsonProperty("brokerPort") int brokerPort, @JsonProperty("sampleRate") int sampleRate,
        @JsonProperty("startTs") long startTs, @JsonProperty("endTs") long endTs,
        @JsonProperty("zookeeper") String zookeeper, @JsonProperty("zkSessionTimeout") int zkSessionTimeout,
        @JsonProperty("zkConnectTimeout") int zkConnectTimeout) {
    checkNotNull(clientId, "clientId is null");
    checkNotNull(topicName, "topicName is null");
    checkNotNull(partitionId, "partitionId is null");
    checkNotNull(startTs, "startTs is null");
    checkNotNull(endTs, "endTs is null");
    this.clientId = clientId;
    this.topicName = topicName;
    this.partitionId = partitionId;
    this.brokerHost = brokerHost;
    this.brokerPort = brokerPort;
    this.sampleRate = sampleRate;
    this.startTs = startTs;
    this.endTs = endTs;
    this.zookeeper = zookeeper;
    this.zkSessionTimeout = zkSessionTimeout;
    this.zkConnectTimeout = zkConnectTimeout;
    try {/* www . j a v  a2 s .co m*/
        InetAddress address = InetAddress.getByName(brokerHost);
        this.address = HostAddress.fromParts(address.getHostAddress(), 8080);
    } catch (UnknownHostException ex) {
        throw new RuntimeException(ex.toString());
    }
}

From source file:com.chicm.cmraft.core.ClusterMemberManager.java

private void initLocalAddresses() {
    try {/*from  ww  w .j  a  v a  2  s  . c om*/
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface inet = (NetworkInterface) interfaces.nextElement();
            Enumeration<InetAddress> addrs = inet.getInetAddresses();
            while (addrs.hasMoreElements()) {
                InetAddress addr = (InetAddress) addrs.nextElement();
                LOG.debug("local address:" + addr.getHostAddress());
                LOG.debug("local address:" + addr.getHostName());
                if (!addr.getHostAddress().isEmpty()) {
                    localAddresses.add(addr.getHostAddress());
                }
                if (!addr.getHostName().isEmpty()) {
                    localAddresses.add(addr.getHostName());
                }
            }
        }

        InetAddress inetAddress = InetAddress.getLocalHost();
        localAddresses.add(inetAddress.getHostAddress());
        localAddresses.add(inetAddress.getCanonicalHostName());
        localAddresses.add(inetAddress.getHostName());

        inetAddress = InetAddress.getLoopbackAddress();
        localAddresses.add(inetAddress.getHostAddress());
        localAddresses.add(inetAddress.getCanonicalHostName());
        localAddresses.add(inetAddress.getHostName());
    } catch (SocketException | UnknownHostException e) {
        LOG.error("", e);
    }
}