Selaa lähdekoodia

Introduce a new util class for getting client info

This replaces the old ui.core.util.Info class, and allows access
to the version information without going through the config
system.

Change-Id: I4b372b62f6fd5adcfa21f0793ec73df7bf2633b6
Reviewed-on: http://gerrit.dmdirc.com/3120
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.8rc1
Chris Smith 10 vuotta sitten
vanhempi
commit
38356e1182

+ 12
- 18
src/com/dmdirc/logger/ProgramError.java Näytä tiedosto

@@ -23,7 +23,7 @@
23 23
 package com.dmdirc.logger;
24 24
 
25 25
 import com.dmdirc.config.IdentityManager;
26
-import com.dmdirc.ui.core.util.Info;
26
+import com.dmdirc.util.ClientInfo;
27 27
 import com.dmdirc.util.io.Downloader;
28 28
 
29 29
 import java.io.File;
@@ -40,7 +40,6 @@ import java.util.Arrays;
40 40
 import java.util.Date;
41 41
 import java.util.HashMap;
42 42
 import java.util.List;
43
-import java.util.Locale;
44 43
 import java.util.Map;
45 44
 import java.util.Objects;
46 45
 import java.util.concurrent.Semaphore;
@@ -335,17 +334,17 @@ public final class ProgramError implements Serializable {
335 334
                 .setLevel(getSentryLevel())
336 335
                 .setServerName("")
337 336
                 .setTimestamp(firstDate)
338
-                .addTag("version", getVersion())
339
-                .addTag("version.major", getVersion().replaceAll("-.*", ""))
340
-                .addTag("os.name", System.getProperty("os.name", "unknown"))
341
-                .addTag("os.version", System.getProperty("os.version", "unknown"))
342
-                .addTag("os.arch", System.getProperty("os.arch", "unknown"))
343
-                .addTag("encoding", System.getProperty("file.encoding", "unknown"))
344
-                .addTag("locale", Locale.getDefault().toString())
345
-                .addTag("jvm.name", System.getProperty("java.vm.name", "unknown"))
346
-                .addTag("jvm.vendor", System.getProperty("java.vm.vendor", "unknown"))
347
-                .addTag("jvm.version", System.getProperty("java.version", "unknown"))
348
-                .addTag("jvm.version.major", System.getProperty("java.version", "unknown").replaceAll("_.*", ""));
337
+                .addTag("version", ClientInfo.getVersion())
338
+                .addTag("version.major", ClientInfo.getMajorVersion())
339
+                .addTag("os.name", ClientInfo.getOperatingSystemName())
340
+                .addTag("os.version", ClientInfo.getOperatingSystemVersion())
341
+                .addTag("os.arch", ClientInfo.getOperatingSystemArchitecture())
342
+                .addTag("encoding", ClientInfo.getSystemFileEncoding())
343
+                .addTag("locale", ClientInfo.getSystemDefaultLocale())
344
+                .addTag("jvm.name", ClientInfo.getJavaName())
345
+                .addTag("jvm.vendor", ClientInfo.getJavaVendor())
346
+                .addTag("jvm.version", ClientInfo.getJavaVersion())
347
+                .addTag("jvm.version.major", ClientInfo.getJavaMajorVersion());
349 348
 
350 349
         if (exception != null) {
351 350
             eventBuilder.addSentryInterface(new ExceptionInterface(exception));
@@ -605,9 +604,4 @@ public final class ProgramError implements Serializable {
605 604
         }
606 605
     }
607 606
 
608
-    private String getVersion() {
609
-        return IdentityManager.getIdentityManager().getGlobalConfiguration()
610
-                .getOption("version", "version");
611
-    }
612
-
613 607
 }

+ 4
- 0
src/com/dmdirc/ui/core/util/Info.java Näytä tiedosto

@@ -23,13 +23,17 @@
23 23
 package com.dmdirc.ui.core.util;
24 24
 
25 25
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
26
+import com.dmdirc.util.ClientInfo;
26 27
 
27 28
 import java.lang.management.ManagementFactory;
28 29
 import java.util.Locale;
29 30
 
30 31
 /**
31 32
  * Retrives various pieces of information about DMDirc.
33
+ *
34
+ * @deprecated Use methods in {@link ClientInfo} instead.
32 35
  */
36
+@Deprecated
33 37
 public class Info {
34 38
 
35 39
     /** Not intended to be instantiated. */

+ 220
- 0
src/com/dmdirc/util/ClientInfo.java Näytä tiedosto

@@ -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
+}

Loading…
Peruuta
Tallenna