PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/mayastudios/systeminfo/SystemInfo.java

https://bitbucket.org/mayastudios/sysinfo
Java | 390 lines | 204 code | 60 blank | 126 comment | 36 complexity | ef1d6cce785d6eea00698b118433b723 MD5 | raw file
Possible License(s): Apache-2.0
  1. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright(C) 2009-2011 MayaStudios
  4. // All rights reserved. Use is subject to license terms.
  5. //
  6. // Part of the SystemInfo library
  7. //
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. //
  22. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  23. package com.mayastudios.systeminfo;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import com.mayastudios.systeminfo.PathInfo.SpecialDirType;
  27. /**
  28. * Base and main class for the system info package. The quickly determine the operating system use
  29. * {@link #OSTYPE}. To get more detailed information about the user's system access the methods of
  30. * {@link #INFO} (which is an instance of this class). To get even more information you can cast the
  31. * {@linkplain #INFO} instance to its correct subclass (according to {@link #OSTYPE}).
  32. *
  33. * @see "http://lopica.sourceforge.net/os.html"
  34. *
  35. * @author Sebastian Krysmanski
  36. */
  37. public abstract class SystemInfo {
  38. /**
  39. * The type of operating system the user is using.
  40. */
  41. public enum OperatationSystemType {
  42. Windows,
  43. Linux,
  44. MacOS,
  45. Solaris,
  46. Unknown;
  47. }
  48. /**
  49. * The CPU architecture of the operating system (like "x86" or "sparc").
  50. *
  51. * @see JavaArch
  52. */
  53. public static enum SystemArch {
  54. x86("32-bit architecture (x86-32)"),
  55. x64("64-bit architecture (x86-64)"),
  56. PowerPC("PowerPC processor 32-bit"),
  57. Sparc("SPARC processor 32-bit"),
  58. Unknown("Unknown architecture");
  59. private final String m_name;
  60. SystemArch(String name) {
  61. this.m_name = name;
  62. }
  63. @Override
  64. public String toString() {
  65. return this.m_name;
  66. }
  67. }
  68. /**
  69. * The CPU architecture the running JavaVM is built for (like "x86" or "sparc"). Note that this may differ
  70. * from "real" system architecture (see {@link SystemArch}). The most prominent case is that on a "x86-64"
  71. * system Java can be run as {@link #x86} or {@link #x64}.
  72. *
  73. * To obtain this value, use {@code SystemInfo.INFO.getJavaArch()}.
  74. */
  75. public static enum JavaArch {
  76. x86("32-bit architecture (x86-32)"),
  77. x64("64-bit architecture (x86-64)"),
  78. PowerPC("PowerPC processor 32-bit"),
  79. Sparc("SPARC processor 32-bit"),
  80. Unknown("Unknown architecture (" + System.getProperty("os.arch") + ")");
  81. private final String m_name;
  82. JavaArch(String name) {
  83. this.m_name = name;
  84. }
  85. @Override
  86. public String toString() {
  87. return this.m_name;
  88. }
  89. }
  90. /**
  91. * The version of this library.
  92. *
  93. * @see #LIBRARY_REVISION
  94. */
  95. public static final String LIBRARY_VERSION = VersionInfo.LIBRARY_VERSION;
  96. /**
  97. * VCS revision of this library. May be a number or a revision hash.
  98. */
  99. public static final String LIBRARY_REVISION = VersionInfo.LIBRARY_REVISION;
  100. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  101. /**
  102. * System dependent line break character(s). It's, for example, "\r\n" for Windows and "\n" for
  103. * Linx/Unix/MacOSX. (The line break character "\r" is only used in MacOS 9 and earlier.)
  104. */
  105. public static final String LINE_SEPERATOR = System.getProperty("line.separator");
  106. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  107. /**
  108. * The type of the operating system.
  109. */
  110. public static final OperatationSystemType OSTYPE = determineOsType();
  111. /**
  112. * Information about this operating system. Some information are available to all operating systems. If you
  113. * want more detailed or additional information you have to cast this instance to its specialized class
  114. * (e.g. to {@link WindowsInfo}, {@link LinuxInfo}, {@link MacOSInfo}, ...).
  115. *
  116. * @remarks To get a quick "dump" of the system information simply call {@link #toString()}.
  117. */
  118. public static final SystemInfo INFO = createSystemInfo();
  119. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  120. private final JavaArch m_arch;
  121. /**
  122. * Constructor for {@linkplain SystemInfo}.
  123. *
  124. * @param arch A {@link JavaArch} instance describing the used Java system.
  125. */
  126. protected SystemInfo(JavaArch arch) {
  127. this.m_arch = arch;
  128. }
  129. /**
  130. * Returns the {@link JavaArch} of the running JavaVM.
  131. *
  132. * @return The Java architecture.
  133. */
  134. public JavaArch getJavaArch() {
  135. return this.m_arch;
  136. }
  137. private static OperatationSystemType determineOsType() {
  138. String osName = System.getProperty("os.name").toLowerCase();
  139. if (osName.startsWith("windows")) {
  140. return OperatationSystemType.Windows;
  141. }
  142. if (osName.equals("linux")) {
  143. return OperatationSystemType.Linux;
  144. }
  145. if (osName.startsWith("mac os")) {
  146. return OperatationSystemType.MacOS;
  147. }
  148. if (osName.equals("solaris") || osName.equals("sunos")) {
  149. return OperatationSystemType.Solaris;
  150. }
  151. return OperatationSystemType.Unknown;
  152. }
  153. private static SystemInfo createSystemInfo() {
  154. switch (OSTYPE) {
  155. case Windows:
  156. return new WindowsInfo();
  157. case Linux:
  158. return new LinuxInfo();
  159. case MacOS:
  160. return new MacOSInfo();
  161. case Solaris:
  162. return new SolarisInfo();
  163. }
  164. return new UnknownSystemInfo();
  165. }
  166. /**
  167. * The name of the operating system as it's returned by {@link #toString()} (entry "Operating System:").
  168. *
  169. * @return The name of the operating system. Is never {@code null}.
  170. */
  171. public abstract String getOsName();
  172. /**
  173. * Returns the operating system's version number.
  174. *
  175. * @return The version number. Is never {@code null}.
  176. */
  177. public abstract String getSystemVersionNumber();
  178. /**
  179. * Returns the operating system's codename, if available.
  180. *
  181. * @return The codename. Returns {@code null} if the codename is unknown.
  182. */
  183. public abstract String getSystemCodename();
  184. /**
  185. * The system architecture. Note that this may differ from the Java architecture (returned by
  186. *{@link #getJavaArch()} - see {@link JavaArch} for more information).
  187. *
  188. * @return The system's processor architecture. Is never {@code null}. If this system architecture could not
  189. * be determined, {@link SystemArch#Unknown} will be returned.
  190. */
  191. public abstract SystemArch getSystemArch();
  192. /**
  193. * The name of the processor this software runs on. If there multiple processors installed, only the first
  194. * is returned.
  195. *
  196. * @return The name of the processor installed on the system. The info should include the processor's
  197. * vendor (e.g. "AMD Athlon(tm) 64 X2 Dual Core Processor 4400+"). Is {@code null} if the processor's name
  198. * could not be determined.
  199. */
  200. public abstract String getProcessorName();
  201. /**
  202. * The total amount of physical memory (RAM) installed on the system.
  203. *
  204. * @return The total amount in bytes. Is {@code 0} if it couldn't be determined.
  205. */
  206. public abstract long getTotalPhysicalMemorySize();
  207. /** {@inheritDoc} */
  208. @Override
  209. public String toString() {
  210. String totalMemory;
  211. if (getTotalPhysicalMemorySize() == 0) {
  212. totalMemory = "unknown";
  213. } else {
  214. totalMemory = (getTotalPhysicalMemorySize() / 1024 / 1024) + " MB";
  215. }
  216. return "Operating System: " + getOsName() + LINE_SEPERATOR
  217. + "Codename: " + (getSystemCodename() != null ? getSystemCodename() : "unknown")
  218. + LINE_SEPERATOR
  219. + "OS Version: " + getSystemVersionNumber() + LINE_SEPERATOR
  220. + "System Architecture: " + getSystemArch()
  221. + (getProcessorName() != null ? " (" + getProcessorName() + ")" : "")
  222. + LINE_SEPERATOR
  223. + "Total Memory: " + totalMemory + LINE_SEPERATOR
  224. + "Java Architecture: " + getJavaArch();
  225. }
  226. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  227. //
  228. // Main method - this way this library can be run on the console.
  229. //
  230. /**
  231. * The main method. This way this library can be "run" without to need to integrate it into a real Java
  232. * application.
  233. */
  234. public static void main(String[] args) {
  235. if (args.length == 0) {
  236. printInfo();
  237. } else if (args.length == 1) {
  238. if ("--props".equals(args[0])) {
  239. printProps();
  240. } else if ("--version".equals(args[0])) {
  241. printVersion();
  242. } else {
  243. printHelp();
  244. }
  245. } else {
  246. printHelp();
  247. }
  248. }
  249. private static void printInfo() {
  250. System.out.println(INFO);
  251. System.out.println();
  252. System.out.println(JavaInfo.getJavaInfo());
  253. System.out.println();
  254. System.out.println("Home Dir: " + PathInfo.getHomeDirPath());
  255. System.out.println("Temp Dir: " + PathInfo.getTempDirPath());
  256. System.out.println();
  257. System.out.println("Documents Dir: " + PathInfo.getSpecialDirPath(SpecialDirType.Documents));
  258. System.out.println("Desktop Dir: " + PathInfo.getSpecialDirPath(SpecialDirType.Desktop));
  259. System.out.println("Pictures Dir: " + PathInfo.getSpecialDirPath(SpecialDirType.Pictures));
  260. System.out.println("Music Dir: " + PathInfo.getSpecialDirPath(SpecialDirType.Music));
  261. System.out.println("Movies Dir: " + PathInfo.getSpecialDirPath(SpecialDirType.Videos));
  262. System.out.println();
  263. System.out.println("Programs Dir: " + PathInfo.getProgramsDirPath());
  264. System.out.println("Application Data Dir (Roaming): " + PathInfo.getApplicationDataDirPath("SystemInfo", true));
  265. System.out.println("Application Data Dir (Local): " + PathInfo.getApplicationDataDirPath("SystemInfo", false));
  266. }
  267. private static void printProps() {
  268. ArrayList<String> osProps = new ArrayList<String>(10);
  269. ArrayList<String> userProps = new ArrayList<String>(10);
  270. ArrayList<String> separatorProps = new ArrayList<String>(10);
  271. ArrayList<String> javaProps = new ArrayList<String>(20);
  272. ArrayList<String> otherProps = new ArrayList<String>(40);
  273. for (String key : System.getProperties().stringPropertyNames()) {
  274. String val = System.getProperty(key);
  275. if ("line.separator".equals(key)) {
  276. val = val.replace("\r", "\\r").replace("\n", "\\n");
  277. }
  278. val = key + ": " + val;
  279. if (key.endsWith(".separator")) {
  280. separatorProps.add(val);
  281. } else if (key.startsWith("os.")) {
  282. osProps.add(val);
  283. } else if (key.startsWith("user.")) {
  284. userProps.add(val);
  285. } else if (key.startsWith("java.")) {
  286. javaProps.add(val);
  287. } else {
  288. otherProps.add(val);
  289. }
  290. }
  291. Collections.sort(osProps);
  292. Collections.sort(userProps);
  293. Collections.sort(separatorProps);
  294. Collections.sort(javaProps);
  295. Collections.sort(otherProps);
  296. for (String line : osProps) {
  297. System.out.println(line);
  298. }
  299. System.out.println();
  300. for (String line : userProps) {
  301. System.out.println(line);
  302. }
  303. System.out.println();
  304. for (String line : separatorProps) {
  305. System.out.println(line);
  306. }
  307. System.out.println();
  308. for (String line : javaProps) {
  309. System.out.println(line);
  310. }
  311. System.out.println();
  312. for (String line : otherProps) {
  313. System.out.println(line);
  314. }
  315. }
  316. private static void printVersion() {
  317. System.out.println("Version: " + LIBRARY_VERSION);
  318. System.out.println("Revision: " + LIBRARY_REVISION);
  319. System.out.println("Build Date: " + VersionInfo.LIBRARY_BUILD_DATE);
  320. }
  321. private static void printHelp() {
  322. System.out.println("Usage: java -jar systeminfo.jar [options]");
  323. System.out.println();
  324. System.out.println("Options:");
  325. System.out.println(" --props : Print the values of all Java system properties ");
  326. System.out.println(" starting with\"os.\"");
  327. System.out.println(" --help : Print this page.");
  328. System.out.println(" --version : Print the version.");
  329. }
  330. }