SystemInfo.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.its.vds.config;
  2. import lombok.Getter;
  3. import lombok.Setter;
  4. import lombok.ToString;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.core.env.Environment;
  8. import javax.annotation.PostConstruct;
  9. import java.net.*;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.Enumeration;
  13. import java.util.List;
  14. @Slf4j
  15. @Getter
  16. @Setter
  17. @ToString
  18. @Configuration
  19. public class SystemInfo {
  20. private final Environment env;
  21. private String activeProfiles;
  22. private String osName;
  23. private int cpuCores;
  24. private String hostName;
  25. private String ipAddress;
  26. public SystemInfo(Environment env) {
  27. this.env = env;
  28. }
  29. @PostConstruct
  30. private void init() {
  31. setActiveProfiles(Arrays.toString((Object[])this.env.getActiveProfiles()));
  32. setOsName(System.getProperty("os.name"));
  33. setCpuCores(Integer.valueOf(Runtime.getRuntime().availableProcessors()));
  34. setHostName(getLocalHostName());
  35. setIpAddress(getLocalIpAddress());
  36. log.info("{}", this);
  37. }
  38. private String getLocalHostName() {
  39. String hostName = null;
  40. try {
  41. hostName = InetAddress.getLocalHost().getHostName();
  42. } catch (UnknownHostException e) {
  43. log.error("getLocalHostName: UnknownHostException");
  44. }
  45. return hostName;
  46. }
  47. private String getLocalIpAddress() {
  48. List<String> ipAddresses = new ArrayList<String>();
  49. Enumeration<NetworkInterface> e = null;
  50. try {
  51. e = NetworkInterface.getNetworkInterfaces();
  52. } catch (SocketException ex) {
  53. log.error("getLocalIpAddress: SocketException");
  54. return "127.0.0.1";
  55. }
  56. while (e.hasMoreElements()) {
  57. for (InterfaceAddress ifAddr : ((NetworkInterface)e.nextElement()).getInterfaceAddresses()) {
  58. if (!(ifAddr.getAddress() instanceof java.net.Inet6Address))
  59. ipAddresses.add(ifAddr.getAddress().getHostAddress());
  60. }
  61. }
  62. return ipAddresses.toString();
  63. }
  64. public List<String> getIpAddressesList() {
  65. List<String> ipAddresses = new ArrayList<>();
  66. Enumeration<NetworkInterface> e = null;
  67. try {
  68. e = NetworkInterface.getNetworkInterfaces();
  69. } catch (SocketException ex) {
  70. log.error("getIpAddressesList: SocketException");
  71. return ipAddresses;
  72. }
  73. while (e.hasMoreElements()) {
  74. for (InterfaceAddress ifAddr : ((NetworkInterface)e.nextElement()).getInterfaceAddresses()) {
  75. if (!(ifAddr.getAddress() instanceof java.net.Inet6Address))
  76. ipAddresses.add(ifAddr.getAddress().getHostAddress());
  77. }
  78. }
  79. return ipAddresses;
  80. }
  81. }