CustomUtil.java 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.sckj.opc.utils;
  2. import com.google.common.collect.Sets;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
  5. import org.springframework.util.StringUtils;
  6. import java.net.*;
  7. import java.util.*;
  8. /**
  9. * @author kangaroo hy
  10. * @version 0.0.1
  11. * @desc
  12. * @since 2020/4/13
  13. */
  14. @Slf4j
  15. public class CustomUtil {
  16. private static final String OPC_UA_NOT_CONFIG = "请配置OPC UA地址信息";
  17. private CustomUtil() {
  18. }
  19. public static String getHostname() {
  20. try {
  21. return InetAddress.getLocalHost().getHostName();
  22. } catch (UnknownHostException var1) {
  23. return "localhost";
  24. }
  25. }
  26. public static Set<String> getHostnames(String address) {
  27. return getHostnames(address, true);
  28. }
  29. public static Set<String> getHostnames(String address, boolean includeLoopback) {
  30. HashSet<String> hostnames = Sets.newHashSet();
  31. try {
  32. InetAddress inetAddress = InetAddress.getByName(address);
  33. if (inetAddress.isAnyLocalAddress()) {
  34. try {
  35. Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
  36. for (NetworkInterface ni : Collections.list(nis)) {
  37. Collections.list(ni.getInetAddresses()).forEach((ia) -> {
  38. if (ia instanceof Inet4Address) {
  39. boolean loopback = ia.isLoopbackAddress();
  40. if (!loopback || includeLoopback) {
  41. hostnames.add(ia.getHostName());
  42. hostnames.add(ia.getHostAddress());
  43. hostnames.add(ia.getCanonicalHostName());
  44. }
  45. }
  46. });
  47. }
  48. } catch (SocketException var7) {
  49. log.warn("Failed to NetworkInterfaces for bind address: {}", address, var7);
  50. }
  51. } else {
  52. boolean loopback = inetAddress.isLoopbackAddress();
  53. if (!loopback || includeLoopback) {
  54. hostnames.add(inetAddress.getHostName());
  55. hostnames.add(inetAddress.getHostAddress());
  56. hostnames.add(inetAddress.getCanonicalHostName());
  57. }
  58. }
  59. } catch (UnknownHostException var8) {
  60. log.warn("Failed to get InetAddress for bind address: {}", address, var8);
  61. }
  62. return hostnames;
  63. }
  64. }