1
2
3
4
5 package oshi.software.os;
6
7 import java.time.Instant;
8 import java.time.LocalDateTime;
9 import java.time.ZoneId;
10 import java.time.format.DateTimeFormatter;
11 import java.util.Locale;
12
13 import oshi.annotation.concurrent.Immutable;
14
15
16
17
18 @Immutable
19 public class OSSession {
20 private static final DateTimeFormatter LOGIN_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm", Locale.ROOT);
21
22 private final String userName;
23 private final String terminalDevice;
24 private final long loginTime;
25 private final String host;
26
27 public OSSession(String userName, String terminalDevice, long loginTime, String host) {
28 this.userName = userName;
29 this.terminalDevice = terminalDevice;
30 this.loginTime = loginTime;
31 this.host = host;
32 }
33
34
35
36
37
38
39 public String getUserName() {
40 return userName;
41 }
42
43
44
45
46
47
48 public String getTerminalDevice() {
49 return terminalDevice;
50 }
51
52
53
54
55
56
57 public long getLoginTime() {
58 return loginTime;
59 }
60
61
62
63
64
65
66
67 public String getHost() {
68 return host;
69 }
70
71 @Override
72 public String toString() {
73 String loginStr = loginTime == 0 ? "No login"
74 : LocalDateTime.ofInstant(Instant.ofEpochMilli(loginTime), ZoneId.systemDefault()).format(LOGIN_FORMAT);
75 String hostStr = "";
76 if (!host.isEmpty() && !host.equals("::") && !host.equals("0.0.0.0")) {
77 hostStr = ", (" + host + ")";
78 }
79 return String.format(Locale.ROOT, "%s, %s, %s%s", userName, terminalDevice, loginStr, hostStr);
80 }
81 }