|
@@ -0,0 +1,220 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2014 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.util;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.util.io.ConfigFile;
|
|
26
|
+import com.dmdirc.util.io.InvalidConfigFileException;
|
|
27
|
+
|
|
28
|
+import java.io.IOException;
|
|
29
|
+import java.lang.management.ManagementFactory;
|
|
30
|
+import java.util.Locale;
|
|
31
|
+
|
|
32
|
+import org.slf4j.Logger;
|
|
33
|
+import org.slf4j.LoggerFactory;
|
|
34
|
+
|
|
35
|
+/**
|
|
36
|
+ * Provides static utility methods to access information about the client and its environment.
|
|
37
|
+ */
|
|
38
|
+public final class ClientInfo {
|
|
39
|
+
|
|
40
|
+ private static final Logger LOG = LoggerFactory.getLogger(ClientInfo.class);
|
|
41
|
+
|
|
42
|
+ /** Domain to read version settings from. */
|
|
43
|
+ private static final String VERSION_DOMAIN = "version";
|
|
44
|
+ /** Key within the version domain that holds the actual version. */
|
|
45
|
+ private static final String VERSION_KEY = "version";
|
|
46
|
+ /** Domain to read updater settings from. */
|
|
47
|
+ private static final String UPDATER_DOMAIN = "updater";
|
|
48
|
+ /** Key within the updater domain that holds the channel. */
|
|
49
|
+ private static final String UPDATER_CHANNEL_KEY = "channel";
|
|
50
|
+
|
|
51
|
+ /** Fallback value to use if a system property isn't found. */
|
|
52
|
+ private static final String PROPERTY_FALLBACK = "unknown";
|
|
53
|
+
|
|
54
|
+ /** Lock used to guard access to {@link #versionConfig}. */
|
|
55
|
+ private static final Object VERSION_CONFIG_LOCK = new Object();
|
|
56
|
+
|
|
57
|
+ /** Cached config file containing the client's version information. */
|
|
58
|
+ private static ConfigFile versionConfig;
|
|
59
|
+
|
|
60
|
+ private ClientInfo() {
|
|
61
|
+ // Shouldn't be initialised.
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ /**
|
|
65
|
+ * @return The version of the client.
|
|
66
|
+ */
|
|
67
|
+ public static String getVersion() {
|
|
68
|
+ return getVersionConfigSetting(VERSION_DOMAIN, VERSION_KEY);
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ /**
|
|
72
|
+ * @return The major version of the client.
|
|
73
|
+ */
|
|
74
|
+ public static String getMajorVersion() {
|
|
75
|
+ return getVersion().replaceAll("(-|rc|a|b|m).*", "");
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ /**
|
|
79
|
+ * @return The channel that this client was built for.
|
|
80
|
+ */
|
|
81
|
+ public static String getUpdaterChannel() {
|
|
82
|
+ return getVersionConfigSetting(UPDATER_DOMAIN, UPDATER_CHANNEL_KEY);
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ /**
|
|
86
|
+ * @return The name of the operating system the client is running on.
|
|
87
|
+ */
|
|
88
|
+ public static String getOperatingSystemName() {
|
|
89
|
+ return System.getProperty("os.name", PROPERTY_FALLBACK);
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ /**
|
|
93
|
+ * @return The version of the operating system the client is running on.
|
|
94
|
+ */
|
|
95
|
+ public static String getOperatingSystemVersion() {
|
|
96
|
+ return System.getProperty("os.version", PROPERTY_FALLBACK);
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ /**
|
|
100
|
+ * @return The name of the architecture that the operating system is built for.
|
|
101
|
+ */
|
|
102
|
+ public static String getOperatingSystemArchitecture() {
|
|
103
|
+ return System.getProperty("os.arch", PROPERTY_FALLBACK);
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ /**
|
|
107
|
+ * @return The default file encoding used by the system.
|
|
108
|
+ */
|
|
109
|
+ public static String getSystemFileEncoding() {
|
|
110
|
+ return System.getProperty("file.encoding", PROPERTY_FALLBACK);
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ /**
|
|
114
|
+ * @return The default locale used by the system.
|
|
115
|
+ */
|
|
116
|
+ public static String getSystemDefaultLocale() {
|
|
117
|
+ return Locale.getDefault().toString();
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ /**
|
|
121
|
+ * @return The name of the JVM running the client.
|
|
122
|
+ */
|
|
123
|
+ public static String getJavaName() {
|
|
124
|
+ return System.getProperty("jvm.name", PROPERTY_FALLBACK);
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ /**
|
|
128
|
+ * @return The name of the vendor of the JVM running the client.
|
|
129
|
+ */
|
|
130
|
+ public static String getJavaVendor() {
|
|
131
|
+ return System.getProperty("jvm.vendor", PROPERTY_FALLBACK);
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ /**
|
|
135
|
+ * @return The version of the JVM running the client.
|
|
136
|
+ */
|
|
137
|
+ public static String getJavaVersion() {
|
|
138
|
+ return System.getProperty("jvm.version", PROPERTY_FALLBACK);
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ /**
|
|
142
|
+ * @return The major version of the JVM running the client.
|
|
143
|
+ */
|
|
144
|
+ public static String getJavaMajorVersion() {
|
|
145
|
+ return getJavaVersion().replaceAll("_.*", "");
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ /**
|
|
149
|
+ * @return The uptime for the client in milliseconds.
|
|
150
|
+ */
|
|
151
|
+ public static long getUptime() {
|
|
152
|
+ return ManagementFactory.getRuntimeMXBean().getUptime();
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ /**
|
|
156
|
+ * @return A string containing the DMDirc version and updater channel.
|
|
157
|
+ */
|
|
158
|
+ public static String getVersionInformation() {
|
|
159
|
+ return getVersion() + " (" + getUpdaterChannel() + ")";
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ /**
|
|
163
|
+ * @return A string containing the JVM name, version, and vendor.
|
|
164
|
+ */
|
|
165
|
+ public static String getJavaInformation() {
|
|
166
|
+ return getJavaName() + " " + getJavaVersion() + " [" + getJavaVendor() + "]";
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ /**
|
|
170
|
+ * @return A string containing the OS name, version and architecture, and the default file
|
|
171
|
+ * encoding and locale.
|
|
172
|
+ */
|
|
173
|
+ public static String getOperatingSystemInformation() {
|
|
174
|
+ return getOperatingSystemName() + " "
|
|
175
|
+ + getOperatingSystemVersion() + " "
|
|
176
|
+ + getOperatingSystemArchitecture() + "; "
|
|
177
|
+ + getSystemFileEncoding() + "; "
|
|
178
|
+ + getSystemDefaultLocale();
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ /**
|
|
182
|
+ * Retrieves a single setting from the version configuration file bundled with the client.
|
|
183
|
+ *
|
|
184
|
+ * @param domain The domain of the setting to retrieve.
|
|
185
|
+ * @param key The key of the setting to retrieve.
|
|
186
|
+ * @return The value of the key within the version config, or {@code null} if it doesn't exist.
|
|
187
|
+ */
|
|
188
|
+ public static String getVersionConfigSetting(final String domain, final String key) {
|
|
189
|
+ return getVersionConfig().getKeyDomain(domain).get(key);
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ /**
|
|
193
|
+ * Utility method to get the config containing the client's version information.
|
|
194
|
+ *
|
|
195
|
+ * <p>This will be cached where possible.
|
|
196
|
+ *
|
|
197
|
+ * @return The client's bundled version config file.
|
|
198
|
+ */
|
|
199
|
+ private static ConfigFile getVersionConfig() {
|
|
200
|
+ synchronized (VERSION_CONFIG_LOCK) {
|
|
201
|
+ if (versionConfig == null) {
|
|
202
|
+ LOG.debug("No previous version config cached, creating a new one...");
|
|
203
|
+
|
|
204
|
+ final ConfigFile config = new ConfigFile(
|
|
205
|
+ ClientInfo.class.getResourceAsStream("/com/dmdirc/version.config"));
|
|
206
|
+
|
|
207
|
+ try {
|
|
208
|
+ config.read();
|
|
209
|
+ } catch (IOException | InvalidConfigFileException ex) {
|
|
210
|
+ LOG.error("Unable to read version information", ex);
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ versionConfig = config;
|
|
214
|
+ }
|
|
215
|
+ }
|
|
216
|
+
|
|
217
|
+ return versionConfig;
|
|
218
|
+ }
|
|
219
|
+
|
|
220
|
+}
|