Class SystemManager

java.lang.Object
  extended by SystemManager
Direct Known Subclasses:
SystemManagerLinux, SystemManagerWin32

public abstract class SystemManager
extends Object

A unified interface to OS-specific implementation of a system manager.

SystemManager provides methods for sampling system and process status information.

Use getSystemManager method to create an OS specific implementation.

Access to process information is done through process identifiers (PIDs) and process handles. On some OSes (e.g. Windows) process handle makes sure that process information is available until the handle is closed, however one cannot rely on this as other implementations (e.g. Linux) may still fail even if handle has not been closed.

See Also:
getSystemManager(), ProcessStatus, SystemMemoryStatus, Runtime.exec(java.lang.String)

Constructor Summary
protected SystemManager()
           
 
Method Summary
abstract  void closeProcessHandle(int handle)
          Closes a process handle (which was opened by openProcessHandle).
abstract  int fetchProcessIDs(int[] pids)
          Fills the integer array with PIDs (process identifiers) of currently running processes.
abstract  boolean fetchProcessStatus(int handle, ProcessStatus ps)
          Fills the ps structure with process status information.
abstract  boolean fetchSystemMemoryStatus(SystemMemoryStatus sms)
          Fills the sms structure with system memory status information.
abstract  String getProcessName(int hProcess)
          Retrieves the name of executable referenced by process handle.
 ProcessStatus getProcessStatus(int handle)
          A-not-so-memory-efficient version of fetchProcessStatus.
static SystemManager getSystemManager()
          Returns an OS-specific implementation of a system manager.
 SystemMemoryStatus getSystemMemoryStatus()
          A-not-so-memory-efficient version of fetchSystemMemoryStatus.
protected static void loadTmpLibrary(String libsource, String prefix, String suffix)
          Copies the resource into temporary directory and loads it as a library.
abstract  int openProcessHandle(int processID)
          Opens a handle to a process with given identifier.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SystemManager

protected SystemManager()
Method Detail

fetchSystemMemoryStatus

public abstract boolean fetchSystemMemoryStatus(SystemMemoryStatus sms)
Fills the sms structure with system memory status information. The method call may fail due to sms being null, or for technical implementation reasons. sms structure will not reflect the system status if the call fails.

Parameters:
sms - the SystemMemoryStatus object, should not be null.
Returns:
true if successfull, otherwise false.
See Also:
SystemMemoryStatus

getSystemMemoryStatus

public SystemMemoryStatus getSystemMemoryStatus()
A-not-so-memory-efficient version of fetchSystemMemoryStatus.

Returns:
new instance of system memory status information if success or null otherwise.
See Also:
fetchSystemMemoryStatus(SystemMemoryStatus), SystemMemoryStatus

fetchProcessStatus

public abstract boolean fetchProcessStatus(int handle,
                                           ProcessStatus ps)
Fills the ps structure with process status information. After the succesfull call, ps structure reflects the status information about given process. The call may fail due to bad handle, ps being null or other technical implementation reasons (e.g. security).

Parameters:
handle - (OS specific) handle to a process, created from openProcessHandle.
ps - ProcessStatus object, should not be null.
Returns:
true if successfull, otherwise false.
See Also:
openProcessHandle(int), ProcessStatus

getProcessStatus

public ProcessStatus getProcessStatus(int handle)
A-not-so-memory-efficient version of fetchProcessStatus.

Returns:
new instance of process status information if success or null otherwise.
See Also:
fetchProcessStatus(int, ProcessStatus), openProcessHandle(int), ProcessStatus

openProcessHandle

public abstract int openProcessHandle(int processID)
Opens a handle to a process with given identifier. Some OSs (e.g. Windows) provide access to process information through handles, which is a nice feature since it keeps the status information available even if the process has terminated. The handle should be closed with closeProcessHandle when it's no longer needed.

Parameters:
processID - (host OS specific) process identifier.
Returns:
integer representing process handle upon success, 0 otherwise.
See Also:
closeProcessHandle(int)

closeProcessHandle

public abstract void closeProcessHandle(int handle)
Closes a process handle (which was opened by openProcessHandle).

Parameters:
handle - the open process handle.
See Also:
openProcessHandle(int)

fetchProcessIDs

public abstract int fetchProcessIDs(int[] pids)
Fills the integer array with PIDs (process identifiers) of currently running processes. The array should not be null but initialized with a length of estimated maximum number of processes. It is difficult to predict exact number of running processes (this is the official position of Windows API) so the array should be large enough to incorporate them all. The excess PIDs will be trimmed, so if the return value is the same as the length of the array, then consider enlarging the array and calling again.

Parameters:
pids - non-null array of integers to be filled with PIDs.
Returns:
the number of PIDs put into arrray.

getProcessName

public abstract String getProcessName(int hProcess)
Retrieves the name of executable referenced by process handle. The call may fail if invalid handle is given or there are technical problems (e.g. security obstacles) while retrieving the information.

Parameters:
hProcess - is a handle to a process created by openProcessHandle.
Returns:
the name of a process executable upon success, null otherwise.

loadTmpLibrary

protected static void loadTmpLibrary(String libsource,
                                     String prefix,
                                     String suffix)
                              throws IOException
Copies the resource into temporary directory and loads it as a library. The temporary library file is scheduled to be deleted when JVM exits.

Throws:
IOException

getSystemManager

public static SystemManager getSystemManager()
Returns an OS-specific implementation of a system manager. The method maintains a singletone object of a system manager, i.e. there is only one instance if at all. Currently the following OSes are supported: Linux, Windows (32-bit).

Returns:
a reference to system manager singletone or null if no implementation is available or there are technical problems to create one.


© 2007 Marius Mikucionis