You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClientInfo.java 7.2KB

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