Java Code Examples for java.net.InetAddress.getClass()

The following are Jave code examples for showing how to use getClass() of the java.net.InetAddress class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: thirdcoast   File: TelemetryController.java   Source Code and License Vote up 6 votes
public List<String> getInventoryEndpoints() {
  List<String> endpoints = new ArrayList<>(2);
  try {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
      Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
      for (InetAddress addr : Collections.list(inetAddresses)) {
        if (!addr.isLinkLocalAddress() && addr.getClass() == Inet4Address.class) {
          endpoints.add(
              String.format("http://%s:%d/v1/grapher/inventory", addr.getHostAddress(), port));
        }
      }
    }
  } catch (SocketException e) {
    logger.error("Exception looking up network interfaces", e);
  }
  return endpoints;
}
 
Example 2
Project: RISE-V2G   File: MiscUtils.java   Source Code and License Vote up 5 votes
/**
 * Determines the link-local IPv6 address which is configured on the network interface provided
 * in the properties file.
 * @return The link-local address given as a String
 */
public static Inet6Address getLinkLocalAddress() {
	String networkInterfaceConfig = getPropertyValue("network.interface").toString();
	
	NetworkInterface nif = null;
	
	try {
		if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
			nif = NetworkInterface.getByIndex(Integer.parseInt(networkInterfaceConfig));
		} else {
			nif = NetworkInterface.getByName(networkInterfaceConfig);
		}
		Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();
		
		while (inetAddresses.hasMoreElements()) {
			InetAddress inetAddress = inetAddresses.nextElement();
			
			if (inetAddress.getClass() == Inet6Address.class && inetAddress.isLinkLocalAddress()) {
				return (Inet6Address) inetAddress;
			}
		}
		
		getLogger().fatal("No IPv6 link-local address found on the network interface '" +
				nif.getDisplayName() + "' configured in the properties file");
	} catch (SocketException e) {
		getLogger().fatal("SocketException while trying to get network interface for configured name " +
						  networkInterfaceConfig + "'", e); 
	} catch (NullPointerException | NumberFormatException e2) {
		getLogger().fatal("No network interface for configured network interface index '" + 
						  networkInterfaceConfig + "' found");
	}
	
	return null;
}