1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package com.its.vds.config;
- import lombok.Getter;
- import lombok.Setter;
- import lombok.ToString;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.env.Environment;
- import javax.annotation.PostConstruct;
- import java.net.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Enumeration;
- import java.util.List;
- @Slf4j
- @Getter
- @Setter
- @ToString
- @Configuration
- public class SystemInfo {
- private final Environment env;
- private String activeProfiles;
- private String osName;
- private int cpuCores;
- private String hostName;
- private String ipAddress;
- public SystemInfo(Environment env) {
- this.env = env;
- }
- @PostConstruct
- private void init() {
- setActiveProfiles(Arrays.toString((Object[])this.env.getActiveProfiles()));
- setOsName(System.getProperty("os.name"));
- setCpuCores(Integer.valueOf(Runtime.getRuntime().availableProcessors()));
- setHostName(getLocalHostName());
- setIpAddress(getLocalIpAddress());
- log.info("{}", this);
- }
- private String getLocalHostName() {
- String hostName = null;
- try {
- hostName = InetAddress.getLocalHost().getHostName();
- } catch (UnknownHostException e) {
- log.error("getLocalHostName: UnknownHostException");
- }
- return hostName;
- }
- private String getLocalIpAddress() {
- List<String> ipAddresses = new ArrayList<String>();
- Enumeration<NetworkInterface> e = null;
- try {
- e = NetworkInterface.getNetworkInterfaces();
- } catch (SocketException ex) {
- log.error("getLocalIpAddress: SocketException");
- return "127.0.0.1";
- }
- while (e.hasMoreElements()) {
- for (InterfaceAddress ifAddr : ((NetworkInterface)e.nextElement()).getInterfaceAddresses()) {
- if (!(ifAddr.getAddress() instanceof java.net.Inet6Address))
- ipAddresses.add(ifAddr.getAddress().getHostAddress());
- }
- }
- return ipAddresses.toString();
- }
- public List<String> getIpAddressesList() {
- List<String> ipAddresses = new ArrayList<>();
- Enumeration<NetworkInterface> e = null;
- try {
- e = NetworkInterface.getNetworkInterfaces();
- } catch (SocketException ex) {
- log.error("getIpAddressesList: SocketException");
- return ipAddresses;
- }
- while (e.hasMoreElements()) {
- for (InterfaceAddress ifAddr : ((NetworkInterface)e.nextElement()).getInterfaceAddresses()) {
- if (!(ifAddr.getAddress() instanceof java.net.Inet6Address))
- ipAddresses.add(ifAddr.getAddress().getHostAddress());
- }
- }
- return ipAddresses;
- }
- }
|