Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ClientInfo.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. package com.dmdirc.util;
  23. import com.dmdirc.util.io.ConfigFile;
  24. import com.dmdirc.util.io.InvalidConfigFileException;
  25. import java.io.IOException;
  26. import java.lang.management.ManagementFactory;
  27. import java.util.Locale;
  28. import javax.inject.Inject;
  29. import javax.inject.Singleton;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. /**
  33. * Provides static utility methods to access information about the client and its environment.
  34. */
  35. @Singleton
  36. public class ClientInfo {
  37. private static final Logger LOG = LoggerFactory.getLogger(ClientInfo.class);
  38. /** Domain to read version settings from. */
  39. private static final String VERSION_DOMAIN = "version";
  40. /** Key within the version domain that holds the actual version. */
  41. private static final String VERSION_KEY = "version";
  42. /** Domain to read updater settings from. */
  43. private static final String UPDATER_DOMAIN = "updater";
  44. /** Key within the updater domain that holds the channel. */
  45. private static final String UPDATER_CHANNEL_KEY = "channel";
  46. /** Fallback value to use if a system property isn't found. */
  47. private static final String PROPERTY_FALLBACK = "unknown";
  48. /** Lock used to guard access to {@link #versionConfig}. */
  49. private final Object VERSION_CONFIG_LOCK = new Object();
  50. /** Cached config file containing the client's version information. */
  51. private ConfigFile versionConfig;
  52. @Inject
  53. public ClientInfo() {
  54. // Shouldn't be initialised.
  55. }
  56. /**
  57. * @return The version of the client.
  58. */
  59. public String getVersion() {
  60. return getVersionConfigSetting(VERSION_DOMAIN, VERSION_KEY);
  61. }
  62. /**
  63. * @return The major version of the client.
  64. */
  65. public String getMajorVersion() {
  66. return getVersion().replaceAll("(-|rc|a|b|m).*", "");
  67. }
  68. /**
  69. * @return The channel that this client was built for.
  70. */
  71. public String getUpdaterChannel() {
  72. return getVersionConfigSetting(UPDATER_DOMAIN, UPDATER_CHANNEL_KEY);
  73. }
  74. /**
  75. * @return The name of the operating system the client is running on.
  76. */
  77. public String getOperatingSystemName() {
  78. return System.getProperty("os.name", PROPERTY_FALLBACK);
  79. }
  80. /**
  81. * @return The version of the operating system the client is running on.
  82. */
  83. public String getOperatingSystemVersion() {
  84. return System.getProperty("os.version", PROPERTY_FALLBACK);
  85. }
  86. /**
  87. * @return The name of the architecture that the operating system is built for.
  88. */
  89. public String getOperatingSystemArchitecture() {
  90. return System.getProperty("os.arch", PROPERTY_FALLBACK);
  91. }
  92. /**
  93. * @return The default file encoding used by the system.
  94. */
  95. public String getSystemFileEncoding() {
  96. return System.getProperty("file.encoding", PROPERTY_FALLBACK);
  97. }
  98. /**
  99. * @return The default locale used by the system.
  100. */
  101. public String getSystemDefaultLocale() {
  102. return Locale.getDefault().toString();
  103. }
  104. /**
  105. * @return The name of the JVM running the client.
  106. */
  107. public String getJavaName() {
  108. return System.getProperty("java.vm.name", PROPERTY_FALLBACK);
  109. }
  110. /**
  111. * @return The name of the vendor of the JVM running the client.
  112. */
  113. public String getJavaVendor() {
  114. return System.getProperty("java.vm.vendor", PROPERTY_FALLBACK);
  115. }
  116. /**
  117. * @return The version of the JVM running the client.
  118. */
  119. public String getJavaVersion() {
  120. return System.getProperty("java.version", PROPERTY_FALLBACK);
  121. }
  122. /**
  123. * @return The major version of the JVM running the client.
  124. */
  125. public String getJavaMajorVersion() {
  126. return getJavaVersion().replaceAll("_.*", "");
  127. }
  128. /**
  129. * @return The uptime for the client in milliseconds.
  130. */
  131. public long getUptime() {
  132. return ManagementFactory.getRuntimeMXBean().getUptime();
  133. }
  134. /**
  135. * @return A string containing the DMDirc version and updater channel.
  136. */
  137. public String getVersionInformation() {
  138. return getVersion() + " (" + getUpdaterChannel() + ')';
  139. }
  140. /**
  141. * @return A string containing the JVM name, version, and vendor.
  142. */
  143. public String getJavaInformation() {
  144. return getJavaName() + ' ' + getJavaVersion() + " [" + getJavaVendor() + ']';
  145. }
  146. /**
  147. * @return A string containing the OS name, version and architecture, and the default file
  148. * encoding and locale.
  149. */
  150. public String getOperatingSystemInformation() {
  151. return getOperatingSystemName() + ' '
  152. + getOperatingSystemVersion() + ' '
  153. + getOperatingSystemArchitecture() + "; "
  154. + getSystemFileEncoding() + "; "
  155. + getSystemDefaultLocale();
  156. }
  157. /**
  158. * Retrieves a single setting from the version configuration file bundled with the client.
  159. *
  160. * @param domain The domain of the setting to retrieve.
  161. * @param key The key of the setting to retrieve.
  162. *
  163. * @return The value of the key within the version config, or {@code null} if it doesn't exist.
  164. */
  165. public String getVersionConfigSetting(final String domain, final String key) {
  166. return getVersionConfig().getKeyDomain(domain).get(key);
  167. }
  168. /**
  169. * Utility method to get the config containing the client's version information.
  170. *
  171. * <p>
  172. * This will be cached where possible.
  173. *
  174. * @return The client's bundled version config file.
  175. */
  176. private ConfigFile getVersionConfig() {
  177. synchronized (VERSION_CONFIG_LOCK) {
  178. if (versionConfig == null) {
  179. LOG.debug("No previous version config cached, creating a new one...");
  180. final ConfigFile config = new ConfigFile(
  181. ClientInfo.class.getResourceAsStream("/com/dmdirc/version.config"));
  182. try {
  183. config.read();
  184. } catch (IOException | InvalidConfigFileException ex) {
  185. LOG.error("Unable to read version information", ex);
  186. }
  187. versionConfig = config;
  188. }
  189. }
  190. return versionConfig;
  191. }
  192. }