/src/com/mayastudios/systeminfo/SystemInfo.java
Java | 390 lines | 204 code | 60 blank | 126 comment | 36 complexity | ef1d6cce785d6eea00698b118433b723 MD5 | raw file
Possible License(s): Apache-2.0
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Copyright(C) 2009-2011 MayaStudios
- // All rights reserved. Use is subject to license terms.
- //
- // Part of the SystemInfo library
- //
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////
- package com.mayastudios.systeminfo;
- import java.util.ArrayList;
- import java.util.Collections;
- import com.mayastudios.systeminfo.PathInfo.SpecialDirType;
- /**
- * Base and main class for the system info package. The quickly determine the operating system use
- * {@link #OSTYPE}. To get more detailed information about the user's system access the methods of
- * {@link #INFO} (which is an instance of this class). To get even more information you can cast the
- * {@linkplain #INFO} instance to its correct subclass (according to {@link #OSTYPE}).
- *
- * @see "http://lopica.sourceforge.net/os.html"
- *
- * @author Sebastian Krysmanski
- */
- public abstract class SystemInfo {
- /**
- * The type of operating system the user is using.
- */
- public enum OperatationSystemType {
- Windows,
- Linux,
- MacOS,
- Solaris,
- Unknown;
- }
- /**
- * The CPU architecture of the operating system (like "x86" or "sparc").
- *
- * @see JavaArch
- */
- public static enum SystemArch {
- x86("32-bit architecture (x86-32)"),
- x64("64-bit architecture (x86-64)"),
- PowerPC("PowerPC processor 32-bit"),
- Sparc("SPARC processor 32-bit"),
- Unknown("Unknown architecture");
- private final String m_name;
- SystemArch(String name) {
- this.m_name = name;
- }
- @Override
- public String toString() {
- return this.m_name;
- }
- }
- /**
- * The CPU architecture the running JavaVM is built for (like "x86" or "sparc"). Note that this may differ
- * from "real" system architecture (see {@link SystemArch}). The most prominent case is that on a "x86-64"
- * system Java can be run as {@link #x86} or {@link #x64}.
- *
- * To obtain this value, use {@code SystemInfo.INFO.getJavaArch()}.
- */
- public static enum JavaArch {
- x86("32-bit architecture (x86-32)"),
- x64("64-bit architecture (x86-64)"),
- PowerPC("PowerPC processor 32-bit"),
- Sparc("SPARC processor 32-bit"),
- Unknown("Unknown architecture (" + System.getProperty("os.arch") + ")");
- private final String m_name;
- JavaArch(String name) {
- this.m_name = name;
- }
- @Override
- public String toString() {
- return this.m_name;
- }
- }
- /**
- * The version of this library.
- *
- * @see #LIBRARY_REVISION
- */
- public static final String LIBRARY_VERSION = VersionInfo.LIBRARY_VERSION;
- /**
- * VCS revision of this library. May be a number or a revision hash.
- */
- public static final String LIBRARY_REVISION = VersionInfo.LIBRARY_REVISION;
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * System dependent line break character(s). It's, for example, "\r\n" for Windows and "\n" for
- * Linx/Unix/MacOSX. (The line break character "\r" is only used in MacOS 9 and earlier.)
- */
- public static final String LINE_SEPERATOR = System.getProperty("line.separator");
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * The type of the operating system.
- */
- public static final OperatationSystemType OSTYPE = determineOsType();
- /**
- * Information about this operating system. Some information are available to all operating systems. If you
- * want more detailed or additional information you have to cast this instance to its specialized class
- * (e.g. to {@link WindowsInfo}, {@link LinuxInfo}, {@link MacOSInfo}, ...).
- *
- * @remarks To get a quick "dump" of the system information simply call {@link #toString()}.
- */
- public static final SystemInfo INFO = createSystemInfo();
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- private final JavaArch m_arch;
- /**
- * Constructor for {@linkplain SystemInfo}.
- *
- * @param arch A {@link JavaArch} instance describing the used Java system.
- */
- protected SystemInfo(JavaArch arch) {
- this.m_arch = arch;
- }
- /**
- * Returns the {@link JavaArch} of the running JavaVM.
- *
- * @return The Java architecture.
- */
- public JavaArch getJavaArch() {
- return this.m_arch;
- }
- private static OperatationSystemType determineOsType() {
- String osName = System.getProperty("os.name").toLowerCase();
- if (osName.startsWith("windows")) {
- return OperatationSystemType.Windows;
- }
- if (osName.equals("linux")) {
- return OperatationSystemType.Linux;
- }
- if (osName.startsWith("mac os")) {
- return OperatationSystemType.MacOS;
- }
- if (osName.equals("solaris") || osName.equals("sunos")) {
- return OperatationSystemType.Solaris;
- }
- return OperatationSystemType.Unknown;
- }
- private static SystemInfo createSystemInfo() {
- switch (OSTYPE) {
- case Windows:
- return new WindowsInfo();
- case Linux:
- return new LinuxInfo();
- case MacOS:
- return new MacOSInfo();
- case Solaris:
- return new SolarisInfo();
- }
- return new UnknownSystemInfo();
- }
- /**
- * The name of the operating system as it's returned by {@link #toString()} (entry "Operating System:").
- *
- * @return The name of the operating system. Is never {@code null}.
- */
- public abstract String getOsName();
- /**
- * Returns the operating system's version number.
- *
- * @return The version number. Is never {@code null}.
- */
- public abstract String getSystemVersionNumber();
- /**
- * Returns the operating system's codename, if available.
- *
- * @return The codename. Returns {@code null} if the codename is unknown.
- */
- public abstract String getSystemCodename();
- /**
- * The system architecture. Note that this may differ from the Java architecture (returned by
- *{@link #getJavaArch()} - see {@link JavaArch} for more information).
- *
- * @return The system's processor architecture. Is never {@code null}. If this system architecture could not
- * be determined, {@link SystemArch#Unknown} will be returned.
- */
- public abstract SystemArch getSystemArch();
- /**
- * The name of the processor this software runs on. If there multiple processors installed, only the first
- * is returned.
- *
- * @return The name of the processor installed on the system. The info should include the processor's
- * vendor (e.g. "AMD Athlon(tm) 64 X2 Dual Core Processor 4400+"). Is {@code null} if the processor's name
- * could not be determined.
- */
- public abstract String getProcessorName();
- /**
- * The total amount of physical memory (RAM) installed on the system.
- *
- * @return The total amount in bytes. Is {@code 0} if it couldn't be determined.
- */
- public abstract long getTotalPhysicalMemorySize();
- /** {@inheritDoc} */
- @Override
- public String toString() {
- String totalMemory;
- if (getTotalPhysicalMemorySize() == 0) {
- totalMemory = "unknown";
- } else {
- totalMemory = (getTotalPhysicalMemorySize() / 1024 / 1024) + " MB";
- }
- return "Operating System: " + getOsName() + LINE_SEPERATOR
- + "Codename: " + (getSystemCodename() != null ? getSystemCodename() : "unknown")
- + LINE_SEPERATOR
- + "OS Version: " + getSystemVersionNumber() + LINE_SEPERATOR
- + "System Architecture: " + getSystemArch()
- + (getProcessorName() != null ? " (" + getProcessorName() + ")" : "")
- + LINE_SEPERATOR
- + "Total Memory: " + totalMemory + LINE_SEPERATOR
- + "Java Architecture: " + getJavaArch();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Main method - this way this library can be run on the console.
- //
- /**
- * The main method. This way this library can be "run" without to need to integrate it into a real Java
- * application.
- */
- public static void main(String[] args) {
- if (args.length == 0) {
- printInfo();
- } else if (args.length == 1) {
- if ("--props".equals(args[0])) {
- printProps();
- } else if ("--version".equals(args[0])) {
- printVersion();
- } else {
- printHelp();
- }
- } else {
- printHelp();
- }
- }
- private static void printInfo() {
- System.out.println(INFO);
- System.out.println();
- System.out.println(JavaInfo.getJavaInfo());
- System.out.println();
- System.out.println("Home Dir: " + PathInfo.getHomeDirPath());
- System.out.println("Temp Dir: " + PathInfo.getTempDirPath());
- System.out.println();
- System.out.println("Documents Dir: " + PathInfo.getSpecialDirPath(SpecialDirType.Documents));
- System.out.println("Desktop Dir: " + PathInfo.getSpecialDirPath(SpecialDirType.Desktop));
- System.out.println("Pictures Dir: " + PathInfo.getSpecialDirPath(SpecialDirType.Pictures));
- System.out.println("Music Dir: " + PathInfo.getSpecialDirPath(SpecialDirType.Music));
- System.out.println("Movies Dir: " + PathInfo.getSpecialDirPath(SpecialDirType.Videos));
- System.out.println();
- System.out.println("Programs Dir: " + PathInfo.getProgramsDirPath());
- System.out.println("Application Data Dir (Roaming): " + PathInfo.getApplicationDataDirPath("SystemInfo", true));
- System.out.println("Application Data Dir (Local): " + PathInfo.getApplicationDataDirPath("SystemInfo", false));
- }
- private static void printProps() {
- ArrayList<String> osProps = new ArrayList<String>(10);
- ArrayList<String> userProps = new ArrayList<String>(10);
- ArrayList<String> separatorProps = new ArrayList<String>(10);
- ArrayList<String> javaProps = new ArrayList<String>(20);
- ArrayList<String> otherProps = new ArrayList<String>(40);
- for (String key : System.getProperties().stringPropertyNames()) {
- String val = System.getProperty(key);
- if ("line.separator".equals(key)) {
- val = val.replace("\r", "\\r").replace("\n", "\\n");
- }
- val = key + ": " + val;
- if (key.endsWith(".separator")) {
- separatorProps.add(val);
- } else if (key.startsWith("os.")) {
- osProps.add(val);
- } else if (key.startsWith("user.")) {
- userProps.add(val);
- } else if (key.startsWith("java.")) {
- javaProps.add(val);
- } else {
- otherProps.add(val);
- }
- }
- Collections.sort(osProps);
- Collections.sort(userProps);
- Collections.sort(separatorProps);
- Collections.sort(javaProps);
- Collections.sort(otherProps);
- for (String line : osProps) {
- System.out.println(line);
- }
- System.out.println();
- for (String line : userProps) {
- System.out.println(line);
- }
- System.out.println();
- for (String line : separatorProps) {
- System.out.println(line);
- }
- System.out.println();
- for (String line : javaProps) {
- System.out.println(line);
- }
- System.out.println();
- for (String line : otherProps) {
- System.out.println(line);
- }
- }
- private static void printVersion() {
- System.out.println("Version: " + LIBRARY_VERSION);
- System.out.println("Revision: " + LIBRARY_REVISION);
- System.out.println("Build Date: " + VersionInfo.LIBRARY_BUILD_DATE);
- }
- private static void printHelp() {
- System.out.println("Usage: java -jar systeminfo.jar [options]");
- System.out.println();
- System.out.println("Options:");
- System.out.println(" --props : Print the values of all Java system properties ");
- System.out.println(" starting with\"os.\"");
- System.out.println(" --help : Print this page.");
- System.out.println(" --version : Print the version.");
- }
- }