org.opengts.util
Class OSTools

java.lang.Object
  extended by org.opengts.util.OSTools

public class OSTools
extends java.lang.Object


Nested Class Summary
static class OSTools.CpuUsage
           
static class OSTools.DiskUsage
           
static class OSTools.MemoryUsage
           
 
Field Summary
static long G_BYTES
           
static long K_BYTES
           
static long M_BYTES
           
static int OS_BSD
           
static int OS_BSD_FREEBSD
           
static int OS_LINUX
           
static int OS_LINUX_CENTOS
           
static int OS_LINUX_DEBIAN
           
static int OS_LINUX_FEDORA
           
static int OS_LINUX_UBUNTU
           
static int OS_MACOS
           
static int OS_MACOS_X
           
static int OS_SUBTYPE_MASK
           
static int OS_TYPE_MASK
           
static int OS_UNIX
           
static int OS_UNIX_AIX
           
static int OS_UNIX_DIGITAL
           
static int OS_UNIX_HPUX
           
static int OS_UNIX_IRIX
           
static int OS_UNIX_SOLARIS
           
static int OS_UNIX_SUNOS
           
static int OS_UNKNOWN
           
static int OS_WINDOWS
           
static int OS_WINDOWS_2000
           
static int OS_WINDOWS_2003
           
static int OS_WINDOWS_7
           
static int OS_WINDOWS_9X
           
static int OS_WINDOWS_CE
           
static int OS_WINDOWS_CYGWIN
           
static int OS_WINDOWS_NT
           
static int OS_WINDOWS_VISTA
           
static int OS_WINDOWS_XP
           
static java.lang.String PROPERTY_JAVA_HOME
           
static java.lang.String PROPERTY_JAVA_SPECIFICATION_VERSION
           
static java.lang.String PROPERTY_JAVA_VENDOR
           
 
Constructor Summary
OSTools()
           
 
Method Summary
static void checkMemoryUsage(boolean reset)
          Analyzes/Prints the current memory usage.
(This method only analyzes/prints memory usage if the current usage is less than the previous usage, implying that a garbage collection has recently occured)
Useful for determining IF there are memory leaks, and how much it is leaking, but useless for determining WHERE the leak is occurring.
static java.lang.String[] createJavaCommand(java.lang.String[] classpath, java.lang.String className, java.lang.String[] args)
          Returns a Java command set up to be executed by Runtime.getRuntime().exec(...)
static int exec(java.lang.String[] cmdArgs, java.lang.StringBuffer outSB, java.lang.StringBuffer errSB)
          Executes the specified command in a separate process
static java.lang.Class getCallerClass(int frame)
          Gets the class of the caller at the specified frame index
static OSTools.CpuUsage getCpuUsage()
          Gets the CPU usage information
static OSTools.CpuUsage getCpuUsage(OSTools.CpuUsage cpu, boolean displayWarnings)
          Gets the CPU usage information
static java.lang.String getCpuUsageString()
          Get the current CPU usage String
Note: may not be supported on all platforms
static java.lang.String getCpuUsageString(boolean longFmt)
          Get the current CPU usage String
Note: may not be supported on all platforms
static java.lang.String getHostIP()
          Gets the current host IP address
static java.lang.String getHostName()
          Gets the current host name
static java.lang.instrument.Instrumentation getInstrumentation()
          Gets the Instrumentation instance
static java.lang.String getJavaHome()
          Gets the Java home directory as a String
static java.lang.String getJavaVendor()
          Gets the Java vendor
static java.lang.String getJavaVersion()
          Gets the Java version
static java.lang.String getMemoryUnits(long D)
           
static OSTools.MemoryUsage getMemoryUsage(OSTools.MemoryUsage mu)
          Gets the current memory usage (in number of bytes) for the current process
static java.lang.String getMemoryUsageStringMb()
          Get the current memory usage String
static java.lang.String getMemoryUsageStringMb(boolean longFmt)
          Get the current memory usage String
static double getMemoryUtilization()
          Gets the current memory utilization (as a percent/100).
static long getObjectSize(java.lang.Object obj)
          Gets the byte size of the specified Object.
static int getOSType()
          Returns the known OS type as an integer bitmask
static java.lang.String getOSTypeName(int type, boolean inclSubtype)
          Returns the String representation of the specified OS type
static int getProcessID()
          Gets the Process-ID of this JVM invocation.
IMPORTANT: This implementation relies on a "convention", rather that a documented method of obtaining the process-id of this JVM within the OS.
static java.lang.String getSerialNumber()
          Attempts to get the computer serial number by executing specific commands to obtain the value from the operating system.
static OSTools.DiskUsage getSystemDiskUsage(java.io.File dir, OSTools.DiskUsage du)
          Gets the System Disk usage information
static OSTools.MemoryUsage getSystemMemoryUsage(OSTools.MemoryUsage mu)
          Gets the System Memory usage information
static java.lang.String getUniqueSystemID()
          Attempts to get a unique system ID.
This ID may be suitable for a GPS tracking "UniqueID", however absolute uniqueness may not be guaranteed.
Currently, the returned ID is based on the localhost MAC address, if available.
static boolean hasGetCallerClass()
          Returns true if 'sun.reflect.Reflection' is present in the runtime libraries.
(will return true when running with the Sun Microsystems version of Java)
static boolean instanceOf(java.lang.Object obj, java.lang.String className)
          Returns true if the specified object is an instance of (or equal to) the specified class name.
static boolean isBrokenToFront()
          Returns true if this implementation has a broken 'toFront' Swing implementation.
(may only be applicable on Java v1.4.2)
static boolean isLinux()
          Returns true if the OS is Unix/Linux
static boolean isMacOS()
          Returns true if the OS is Apple Mac OS
static boolean isMacOSX()
          Returns true if the OS is Apple Mac OS X
static boolean isOSType(int type)
          Returns true if the OS is the specified type
static boolean isOSType(int type, int subType)
          Returns true if the OS is the specified type
static boolean isSunJava()
          Returns true if executed from a Sun Microsystems JVM.
static boolean isUnknown()
          Returns true if the OS is unknown
static boolean isWindows()
          Returns true if the OS is a flavor of Windows
static boolean isWindows9X()
          Returns true if the OS is Windows 95/98
static boolean isWindowsXP()
          Returns true if the OS is Windows XP
static void main(java.lang.String[] argv)
          Main entry point for testing/debugging
static void premain(java.lang.String agentArgs, java.lang.instrument.Instrumentation inst)
          "premain" entry point used to obtain an Instramentation instance
static void printCallerClasses()
          Prints the class of the caller (debug purposes only)
static void printMemoryUsage()
          Prints the current memory usage to the log file
static void printMemoryUsageMXBean()
          Prints the current memory usage to the log file
static int readPidFile(java.io.File pidFile)
          Reads the PID previously written to the specified file
static void setInstrumentation(java.lang.instrument.Instrumentation inst)
          Sets the Instrumentation instance.
static boolean sleepMS(long ms)
          Sleeps for the specified number of milliseconds
static boolean sleepSec(long sec)
          Sleeps for the specified number of seconds
static boolean writePidFile(java.io.File pidFile, boolean overwrite)
          Writes the current PID into the specified file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OS_TYPE_MASK

public static final int OS_TYPE_MASK
See Also:
Constant Field Values

OS_SUBTYPE_MASK

public static final int OS_SUBTYPE_MASK
See Also:
Constant Field Values

OS_UNKNOWN

public static final int OS_UNKNOWN
See Also:
Constant Field Values

OS_LINUX

public static final int OS_LINUX
See Also:
Constant Field Values

OS_LINUX_FEDORA

public static final int OS_LINUX_FEDORA
See Also:
Constant Field Values

OS_LINUX_CENTOS

public static final int OS_LINUX_CENTOS
See Also:
Constant Field Values

OS_LINUX_UBUNTU

public static final int OS_LINUX_UBUNTU
See Also:
Constant Field Values

OS_LINUX_DEBIAN

public static final int OS_LINUX_DEBIAN
See Also:
Constant Field Values

OS_UNIX

public static final int OS_UNIX
See Also:
Constant Field Values

OS_UNIX_SOLARIS

public static final int OS_UNIX_SOLARIS
See Also:
Constant Field Values

OS_UNIX_SUNOS

public static final int OS_UNIX_SUNOS
See Also:
Constant Field Values

OS_UNIX_AIX

public static final int OS_UNIX_AIX
See Also:
Constant Field Values

OS_UNIX_DIGITAL

public static final int OS_UNIX_DIGITAL
See Also:
Constant Field Values

OS_UNIX_HPUX

public static final int OS_UNIX_HPUX
See Also:
Constant Field Values

OS_UNIX_IRIX

public static final int OS_UNIX_IRIX
See Also:
Constant Field Values

OS_BSD

public static final int OS_BSD
See Also:
Constant Field Values

OS_BSD_FREEBSD

public static final int OS_BSD_FREEBSD
See Also:
Constant Field Values

OS_MACOS

public static final int OS_MACOS
See Also:
Constant Field Values

OS_MACOS_X

public static final int OS_MACOS_X
See Also:
Constant Field Values

OS_WINDOWS

public static final int OS_WINDOWS
See Also:
Constant Field Values

OS_WINDOWS_9X

public static final int OS_WINDOWS_9X
See Also:
Constant Field Values

OS_WINDOWS_XP

public static final int OS_WINDOWS_XP
See Also:
Constant Field Values

OS_WINDOWS_VISTA

public static final int OS_WINDOWS_VISTA
See Also:
Constant Field Values

OS_WINDOWS_7

public static final int OS_WINDOWS_7
See Also:
Constant Field Values

OS_WINDOWS_CE

public static final int OS_WINDOWS_CE
See Also:
Constant Field Values

OS_WINDOWS_NT

public static final int OS_WINDOWS_NT
See Also:
Constant Field Values

OS_WINDOWS_2000

public static final int OS_WINDOWS_2000
See Also:
Constant Field Values

OS_WINDOWS_2003

public static final int OS_WINDOWS_2003
See Also:
Constant Field Values

OS_WINDOWS_CYGWIN

public static final int OS_WINDOWS_CYGWIN
See Also:
Constant Field Values

K_BYTES

public static final long K_BYTES
See Also:
Constant Field Values

M_BYTES

public static final long M_BYTES
See Also:
Constant Field Values

G_BYTES

public static final long G_BYTES
See Also:
Constant Field Values

PROPERTY_JAVA_HOME

public static final java.lang.String PROPERTY_JAVA_HOME
See Also:
Constant Field Values

PROPERTY_JAVA_VENDOR

public static final java.lang.String PROPERTY_JAVA_VENDOR
See Also:
Constant Field Values

PROPERTY_JAVA_SPECIFICATION_VERSION

public static final java.lang.String PROPERTY_JAVA_SPECIFICATION_VERSION
See Also:
Constant Field Values
Constructor Detail

OSTools

public OSTools()
Method Detail

getOSType

public static int getOSType()
Returns the known OS type as an integer bitmask

Returns:
The OS type

getOSTypeName

public static java.lang.String getOSTypeName(int type,
                                             boolean inclSubtype)
Returns the String representation of the specified OS type

Parameters:
type - The OS type
Returns:
The OS type name (never null)

isOSType

public static boolean isOSType(int type)
Returns true if the OS is the specified type

Returns:
True if the OS is the specified type

isOSType

public static boolean isOSType(int type,
                               int subType)
Returns true if the OS is the specified type

Returns:
True if the OS is the specified type

isUnknown

public static boolean isUnknown()
Returns true if the OS is unknown

Returns:
True if the OS is unknown

isWindows

public static boolean isWindows()
Returns true if the OS is a flavor of Windows

Returns:
True if the OS is a flavor of Windows

isWindowsXP

public static boolean isWindowsXP()
Returns true if the OS is Windows XP

Returns:
True if the OS is Windows XP

isWindows9X

public static boolean isWindows9X()
Returns true if the OS is Windows 95/98

Returns:
True if the OS is Windows 95/98

isLinux

public static boolean isLinux()
Returns true if the OS is Unix/Linux

Returns:
True if the OS is Unix/Linux

isMacOS

public static boolean isMacOS()
Returns true if the OS is Apple Mac OS

Returns:
True if the OS is Apple Mac OS

isMacOSX

public static boolean isMacOSX()
Returns true if the OS is Apple Mac OS X

Returns:
True if the OS is Apple Mac OS X

getHostName

public static java.lang.String getHostName()
Gets the current host name

Returns:
The current hostname

getHostIP

public static java.lang.String getHostIP()
Gets the current host IP address

Returns:
The current IP address

getSerialNumber

public static java.lang.String getSerialNumber()
Attempts to get the computer serial number by executing specific commands to obtain the value from the operating system. Returns null if unable to read the system serial number.


getUniqueSystemID

public static java.lang.String getUniqueSystemID()
Attempts to get a unique system ID.
This ID may be suitable for a GPS tracking "UniqueID", however absolute uniqueness may not be guaranteed.
Currently, the returned ID is based on the localhost MAC address, if available. Otherwise it will return the local hostname (sans any space/'.' characters).

Returns:
A unique system ID

getCpuUsage

public static OSTools.CpuUsage getCpuUsage()
Gets the CPU usage information

Returns:
A new instance of CpuUsage containing CPU usage information, or null if unable to obtain the CPU usage information.

getCpuUsage

public static OSTools.CpuUsage getCpuUsage(OSTools.CpuUsage cpu,
                                           boolean displayWarnings)
Gets the CPU usage information

Parameters:
cpu - The CpuUsage instance into which the CPU information is written.
displayWarnings - True to display warnings encountered when attempting read CPU information.
Returns:
The specified CpuUsage instance, or a new instance if no CpuUsage instance is provided. Returns null if unable to obtain the CPU usage.

getCpuUsageString

public static java.lang.String getCpuUsageString(boolean longFmt)
Get the current CPU usage String
Note: may not be supported on all platforms


getCpuUsageString

public static java.lang.String getCpuUsageString()
Get the current CPU usage String
Note: may not be supported on all platforms


getMemoryUnits

public static java.lang.String getMemoryUnits(long D)

getSystemMemoryUsage

public static OSTools.MemoryUsage getSystemMemoryUsage(OSTools.MemoryUsage mu)
Gets the System Memory usage information

Parameters:
mu - The MemoryUsage instance into which the memory information is written
Returns:
The specified MemoryUsage instance, or a new instance if no MemoryUsage instance is provided. Returns null if unable to obtain the memory usage.

getSystemDiskUsage

public static OSTools.DiskUsage getSystemDiskUsage(java.io.File dir,
                                                   OSTools.DiskUsage du)
Gets the System Disk usage information

Parameters:
dir - A directory of the partition for which the disk usage will be returned.
du - The DiskUsage instance into which the disk information is written
Returns:
The specified DiskUsage instance, or a new instance if no DiskUsage instance is provided. Returns null if unable to obtain the disk usage.

getMemoryUtilization

public static double getMemoryUtilization()
Gets the current memory utilization (as a percent/100). Returns a negative value if the memory utilization cannot be obtained.


getMemoryUsage

public static OSTools.MemoryUsage getMemoryUsage(OSTools.MemoryUsage mu)
Gets the current memory usage (in number of bytes) for the current process

Parameters:
mu - Am OSTools.MemoryUsage instance that will be updates with the memory usage values. If 'null', a new MemoryUsage instance will be created.
Returns:
A MemoryUsage instance containing the memory usage values, or null if unable to obtain the memory usage values.

getMemoryUsageStringMb

public static java.lang.String getMemoryUsageStringMb()
Get the current memory usage String


getMemoryUsageStringMb

public static java.lang.String getMemoryUsageStringMb(boolean longFmt)
Get the current memory usage String


printMemoryUsage

public static void printMemoryUsage()
Prints the current memory usage to the log file


printMemoryUsageMXBean

public static void printMemoryUsageMXBean()
Prints the current memory usage to the log file


checkMemoryUsage

public static void checkMemoryUsage(boolean reset)
Analyzes/Prints the current memory usage.
(This method only analyzes/prints memory usage if the current usage is less than the previous usage, implying that a garbage collection has recently occured)
Useful for determining IF there are memory leaks, and how much it is leaking, but useless for determining WHERE the leak is occurring.

Parameters:
reset - True to reset the memory growth-rate checks.

getJavaVersion

public static java.lang.String getJavaVersion()
Gets the Java version


getJavaVendor

public static java.lang.String getJavaVendor()
Gets the Java vendor


getJavaHome

public static java.lang.String getJavaHome()
Gets the Java home directory as a String


setInstrumentation

public static void setInstrumentation(java.lang.instrument.Instrumentation inst)
Sets the Instrumentation instance. The Instrumentation instance is typically obtained from a call to "premain".


getInstrumentation

public static java.lang.instrument.Instrumentation getInstrumentation()
Gets the Instrumentation instance


getObjectSize

public static long getObjectSize(java.lang.Object obj)
Gets the byte size of the specified Object. Note: Requires a valid Instramentation instance.


premain

public static void premain(java.lang.String agentArgs,
                           java.lang.instrument.Instrumentation inst)
"premain" entry point used to obtain an Instramentation instance


isBrokenToFront

public static boolean isBrokenToFront()
Returns true if this implementation has a broken 'toFront' Swing implementation.
(may only be applicable on Java v1.4.2)

Returns:
True if this implementation has a broken 'toFront' Swing implementation.

isSunJava

public static boolean isSunJava()
Returns true if executed from a Sun Microsystems JVM.

Returns:
True is executed from a Sun Microsystems JVM.

getCallerClass

public static java.lang.Class getCallerClass(int frame)
Gets the class of the caller at the specified frame index

Parameters:
frame - The frame index
Returns:
The calling class

hasGetCallerClass

public static boolean hasGetCallerClass()
Returns true if 'sun.reflect.Reflection' is present in the runtime libraries.
(will return true when running with the Sun Microsystems version of Java)

Returns:
True if 'getCallerClass' is available.

printCallerClasses

public static void printCallerClasses()
Prints the class of the caller (debug purposes only)


getProcessID

public static int getProcessID()
Gets the Process-ID of this JVM invocation.
IMPORTANT: This implementation relies on a "convention", rather that a documented method of obtaining the process-id of this JVM within the OS. Caveat Emptor!
(On Windows, this returns the 'WINPID' which is probably useless anyway)

Returns:
The Process-ID

writePidFile

public static boolean writePidFile(java.io.File pidFile,
                                   boolean overwrite)
Writes the current PID into the specified file

Parameters:
pidFile - The pid file to write
overwrite - True to overwrite if file already exists
Returns:
True if the file was successfully written

readPidFile

public static int readPidFile(java.io.File pidFile)
Reads the PID previously written to the specified file

Parameters:
pidFile - The file from which the PID is read (must have ".pid" extension).
Returns:
The PID in the specified file, or -1 if unable to read the PID from the file. Does not guarantee that the PID is actually still valid.

createJavaCommand

public static java.lang.String[] createJavaCommand(java.lang.String[] classpath,
                                                   java.lang.String className,
                                                   java.lang.String[] args)
Returns a Java command set up to be executed by Runtime.getRuntime().exec(...)

Parameters:
classpath - The classpath
className - The main Java class name (or .jar file)
args - The command line arguments
Returns:
A command to call and it's arguments

exec

public static int exec(java.lang.String[] cmdArgs,
                       java.lang.StringBuffer outSB,
                       java.lang.StringBuffer errSB)
Executes the specified command in a separate process

Parameters:
cmdArgs - The command and arguments to execute
outSB - The StringBuffer into which the stdOut output is placed
errSB - The StringBuffer into which the stdErr output is placed
Returns:
The process exit status code

instanceOf

public static boolean instanceOf(java.lang.Object obj,
                                 java.lang.String className)
Returns true if the specified object is an instance of (or equal to) the specified class name.


sleepMS

public static boolean sleepMS(long ms)
Sleeps for the specified number of milliseconds

Parameters:
ms - Number of milliseconds to sleep
Returns:
True if sleep was performed without interruption, false otherwise

sleepSec

public static boolean sleepSec(long sec)
Sleeps for the specified number of seconds

Parameters:
sec - Number of milliseconds to sleep
Returns:
True if sleep was performed without interruption, false otherwise

main

public static void main(java.lang.String[] argv)
Main entry point for testing/debugging

Parameters:
argv - Comand-line arguments