ariba.util.core
Class SystemUtil

java.lang.Object
  extended by ariba.util.core.SystemUtil

public final class SystemUtil
extends java.lang.Object

System Utilities. These are helper functions for dealing with system functions.


Nested Class Summary
static class SystemUtil.ValidCaller
          A simple bean to hold the attributes of a valid caller for use by validateCaller method.
 
Method Summary
static void checkCalledFromUnitTest(java.lang.String className, java.lang.String methodName)
          Checks to make sure we are the specified class and method are invoked from unit tests.
static int compare(boolean first, boolean second)
          Convenience function that calls compare(boolean,boolean,boolean) with falseLessThanTrue == true.
static int compare(boolean first, boolean second, boolean falseLessThanTrue)
          Compares first and second and returns the result.
static int compare(java.lang.Comparable first, java.lang.Comparable second)
          Compares first to second and returns a negative, zero or postive integer as first is less than, equal to or greater than second, respectively.
static int compare(java.lang.Comparable first, java.lang.Comparable second, boolean nullComparesGreaterThanNonNull)
          Compares first to second and returns a negative, zero or postive integer as first is less than, equal to or greater than second, respectively.
static int compare(java.lang.Object first, java.lang.Object second, java.util.Comparator comparator)
          Compares first to second using the supplied comparator and returns a negative, zero or postive integer as first is less than, equal to or greater than second, respectively.
static int compare(java.lang.Object first, java.lang.Object second, java.util.Comparator comparator, boolean nullComparesGreaterThanNonNull)
          Compares first to second using the supplied comparator and returns a negative, zero or postive integer as first is less than, equal to or greater than second, respectively.
static void consumeException(java.lang.String reason, java.lang.Throwable t)
          Prevent a compiler warning when you don't want to do something in a catch block.
static void consumeInterruptedException(java.lang.InterruptedException e)
          Provide proper handling of InterruptedException from wait() and sleep() methods.
static boolean equal(java.lang.Object one, java.lang.Object two)
          Check if two objects are equal in a null safe manner.
static java.io.PrintWriter err()
          Get a Writer version of System.err
static void exit(int code)
          Terminate the java process.
static java.lang.String fixRelativePath(java.lang.String path)
          Modify a relative file path to use custom config and directory locations
static void flushOutput()
          Flush system out and system error.
static java.lang.String getArchitecture()
          Return the OS architecture of this VM
static java.lang.String getBootClassPath()
          Returns the boot classpath of this VM.
static java.lang.String getClassPath()
          Return the class path of this VM.
static java.lang.String getCompleteClassPath()
          Returns the complete classpath used by this VM.
static java.io.File getConfigDirectory()
          Returns the config directory.
static java.lang.String getConfigDirectoryString()
           
static java.net.URL getConfigURL()
          Returns the config URL.
static java.lang.String getCwd()
          Get the current working directory as a String.
static java.io.File getCwdFile()
          Get the current working directory as a File
static java.util.Map getenv()
          For internal use only
static java.lang.String getenv(java.lang.String envVar)
          For internal use only
static boolean getExitException()
          Used for unit tests to check if an exception will be thrown when SystemUtil.exit() is calld.
static java.lang.String getFileEncoding()
          Return the default file encoding of this VM
static java.net.InetAddress getHost()
          Get the InetAddress for this machine.
static java.lang.String getHostname()
          Get the hostname of this machine.
static java.io.File getInstallDirectory()
          Returns the installation directory.
static java.io.File getInternalDirectory()
          Returns the internal directory.
static java.lang.String getJavaVendor()
          Return this VM's vendor
static java.io.File getLocalTempDirectory()
          Returns the temporary directory for the local server This directory can be used to store temporary files which do not need to be shared accross multiple processes
static java.lang.String getOperatingSystem()
          Return the OS name of this VM
static java.lang.String getOperatingSystemType()
          Return the OS type of this VM.
static java.lang.String getPathSeparator()
          Return the path separator of this VM.
static char getPathSeparatorChar()
          Return the path separator of this VM.
static java.lang.String getPwd()
          Find the current working directory.
static java.lang.Throwable getRootCause(java.lang.Throwable t)
          Returns the root cause that cause that causes this exception.
static java.io.File getSharedTempDirectory()
          Returns the temporary directory which can be shared accross multiple processes This directory can be used to store temporary files which require to be accessible by different nodes
static java.io.File getSystemDirectory()
          Returns the sytem directory.
static java.lang.String getUserName()
          Return the current user name
static int hashCode(java.lang.Object object)
          Convenience method that returns the hashCode() of object.
static int hostAsInt()
          helper function to convert hostname into int for database storage
static java.io.BufferedReader in()
          Get a Reader version of System.in
static boolean isAIX()
          Determine if the system is a AIX based system.
static boolean isDevelopment()
          Are we in development, not production mode? This is a low level check needed for internal testing.
static boolean isHP()
          Determine if the system is a HP based system.
static boolean isLinux()
          Determine if the system is a Linux based system.
static boolean isSunOS()
          Determine if the system is a Sun based system.
static boolean isWin32()
          Determine if the system is a Windows based system.
static boolean memoryCompare(byte[] a, int aIndex, byte[] b, int bIndex, int len)
          Compares len bytes in array a starting with byte aIndex with len bytes in array b starting with byte bIndex.
static java.io.PrintWriter out()
          Get a Writer version of System.out
static void setConfigDirectory(java.lang.String configDir)
          Defines the location of the config directory.
static void setErr(java.io.PrintWriter pw)
          Set the output stream returned by SystemUtil.out();
static void setExitException(boolean value)
          Used for unit tests to cause SystemUtil.exit() to thrown an exception rather than bring down the process.
static void setJ2EEServerInUse()
           
static void setJ2EEServerInUse(boolean flag)
           
static void setLocalTempDirectory(java.lang.String tempDir)
          Defines the location of the local temporary directory.
static void setOut(java.io.PrintWriter pw)
          Set the output stream returned by SystemUtil.out();
static void setSharedTempDirectory(java.lang.String tempDir)
          Defines the location of the shared temporary directory.
static void setSystemDirectory(java.lang.String systemDir)
          Defines the location of the system directory.
static void sleep(long sleepTime)
           
static java.lang.String stackTrace()
          Get a string which represents the current call stack.
static java.lang.String stackTrace(java.lang.Throwable t)
          Get a string which represents the Throwable's call stack.
static java.lang.String stackTraceCodePath()
          Returns String stackTrace for current thread, reduced to focus on most relevant stack frames for analyzing application execution.
static java.lang.String stackTraceCodePath(java.lang.String st)
          Returns String stackTrace reduced to focus on most relevant stack frames for analyzing application execution, given a String stackTrace.
static java.lang.String stackTraceCodePath(java.lang.Throwable t)
          Returns String stackTrace for given Throwable, reduced to focus on most relevant stack frames for analyzing application execution.
static boolean usingJ2EEServer()
           
static void validateCaller(SystemUtil.ValidCaller[] validCallers, boolean fatalAssert)
          Validates that a method is allowed to call another method.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

sleep

public static void sleep(long sleepTime)

equal

public static boolean equal(java.lang.Object one,
                            java.lang.Object two)
Check if two objects are equal in a null safe manner.

Parameters:
one - the first of two objects to compare
two - the second of two objects to compare
Returns:
true if the two objects one and two are equals according to the equals() method of one or if they're both null. false is returned otherwise.

hashCode

public static int hashCode(java.lang.Object object)
Convenience method that returns the hashCode() of object. The case of a null object is handled; zero is returned.

This method is complementary to the method equal(java.lang.Object, java.lang.Object) which tests two objects for equality in a null safe manner.

Parameters:
object - the object for which to return the hashcode
Returns:
the hashcode value or 0 if object is null

compare

public static int compare(java.lang.Comparable first,
                          java.lang.Comparable second)
Compares first to second and returns a negative, zero or postive integer as first is less than, equal to or greater than second, respectively.

first and second may be null. A null object is always considered to be less than any non-null object.

Parameters:
first - the first Comparable to compare, may be null
second - the first Comparable to compare, may be null
Returns:
the result of the comparison of first and second

compare

public static int compare(java.lang.Comparable first,
                          java.lang.Comparable second,
                          boolean nullComparesGreaterThanNonNull)
Compares first to second and returns a negative, zero or postive integer as first is less than, equal to or greater than second, respectively.

first and second may be null. A null object is always considered to be less than any non-null object.

Parameters:
first - the first Comparable to compare, may be null
second - the first Comparable to compare, may be null
Returns:
the result of the comparison of first and second

compare

public static int compare(java.lang.Object first,
                          java.lang.Object second,
                          java.util.Comparator comparator)
Compares first to second using the supplied comparator and returns a negative, zero or postive integer as first is less than, equal to or greater than second, respectively.

first and second may be null. A null object is always considered to be less than any non-null object.

Parameters:
first - the first Comparable to compare, may be null
second - the first Comparable to compare, may be null
comparator - the Comparator to use when comparing, may not be null
Returns:
the result of the comparison of first and second

compare

public static int compare(java.lang.Object first,
                          java.lang.Object second,
                          java.util.Comparator comparator,
                          boolean nullComparesGreaterThanNonNull)
Compares first to second using the supplied comparator and returns a negative, zero or postive integer as first is less than, equal to or greater than second, respectively.

first and second may be null. A null object is always considered to be less than any non-null object.

Parameters:
first - the first Comparable to compare, may be null
second - the first Comparable to compare, may be null
comparator - the Comparator to use when comparing
Returns:
the result of the comparison of first and second

compare

public static int compare(boolean first,
                          boolean second,
                          boolean falseLessThanTrue)
Compares first and second and returns the result.

If falseLessThanTrue == true then -1 is returned if !first && second and +1 is returned if first && !second and 0 when first == second.

If falseLessThanTrue == false the result is the opposite.

Parameters:
first - the first of the two values to compare
second - the second of the two values to compare
falseLessThanTrue - whether or not false should be considered less than true
Returns:
the result

compare

public static int compare(boolean first,
                          boolean second)
Convenience function that calls compare(boolean,boolean,boolean) with falseLessThanTrue == true.


setExitException

public static void setExitException(boolean value)
Used for unit tests to cause SystemUtil.exit() to thrown an exception rather than bring down the process.


getExitException

public static boolean getExitException()
Used for unit tests to check if an exception will be thrown when SystemUtil.exit() is calld.


checkCalledFromUnitTest

public static final void checkCalledFromUnitTest(java.lang.String className,
                                                 java.lang.String methodName)
Checks to make sure we are the specified class and method are invoked from unit tests. If not, will Assert. This method is useful if there are methods that should be called by our unit tests. It makes sure no other code can call the given class/method.

Parameters:
className - the class name
methodName - the method name

exit

public static void exit(int code)
Terminate the java process. All shutdown hooks which are registered by components in the server will be run.

Parameters:
code - the exit code to pass to System.exit()

memoryCompare

public static boolean memoryCompare(byte[] a,
                                    int aIndex,
                                    byte[] b,
                                    int bIndex,
                                    int len)
Compares len bytes in array a starting with byte aIndex with len bytes in array b starting with byte bIndex.

Parameters:
a - array of memory to compare with b
aIndex - where to start compare in array a
b - array of memory to compare with a
bIndex - where to start compare in array b
len - the number of bytes to compare.
Returns:
true if each byte compared is equal, false otherwise

getHostname

public static java.lang.String getHostname()
Get the hostname of this machine. If you are in the server, you should use Server.hostname() which consults the parameters in case they override the name the OS returns

Returns:
the hostname for this machine
See Also:
ariba.rpc.server.Server#hostname()

getHost

public static java.net.InetAddress getHost()
Get the InetAddress for this machine.

Returns:
the InetAddress for this machine

hostAsInt

public static int hostAsInt()
helper function to convert hostname into int for database storage


getPwd

public static java.lang.String getPwd()
Find the current working directory.

Returns:
the present working directory of this VM

getCwd

public static java.lang.String getCwd()
Get the current working directory as a String.

Returns:
current working directory as a String

getCwdFile

public static java.io.File getCwdFile()
Get the current working directory as a File

Returns:
current working directory as a File

getFileEncoding

public static java.lang.String getFileEncoding()
Return the default file encoding of this VM

Returns:
the default file encoding of this VM

getClassPath

public static java.lang.String getClassPath()
Return the class path of this VM. See also bootClassPath()

Returns:
the class path of this VM

getBootClassPath

public static java.lang.String getBootClassPath()
Returns the boot classpath of this VM. This is only really relevant for the Sun VM. There is a switch you can use to start the VM that sets the bootClassPath instead of the classpath.

Returns:
the boot classpath of this VM

getCompleteClassPath

public static java.lang.String getCompleteClassPath()
Returns the complete classpath used by this VM. It contains the boot classpath as well as the class path.


getPathSeparator

public static java.lang.String getPathSeparator()
Return the path separator of this VM. That is : for Unix and ; for Windows.

Returns:
the path separator of this VM. That is : for Unix and ; for Windows.

getPathSeparatorChar

public static char getPathSeparatorChar()
Return the path separator of this VM. That is : for Unix and ; for Windows.

Returns:
the path separator of this VM. That is : for Unix and ; for Windows.

getArchitecture

public static java.lang.String getArchitecture()
Return the OS architecture of this VM

Returns:
the OS architecture of this VM

getOperatingSystem

public static java.lang.String getOperatingSystem()
Return the OS name of this VM

Returns:
the OS name of this VM

getOperatingSystemType

public static java.lang.String getOperatingSystemType()
Return the OS type of this VM. It will return either Win32, SunOS, AIX, HP-UX, or whatever getOperatingSystem returns if unknown.

Returns:
the OS type of this VM

getJavaVendor

public static java.lang.String getJavaVendor()
Return this VM's vendor

Returns:
the vendor of this VM

getUserName

public static java.lang.String getUserName()
Return the current user name

Returns:
the current user name

isWin32

public static final boolean isWin32()
Determine if the system is a Windows based system.

Returns:
true if the operating system is Windows 95 or NT or 2000, false otherwise

isSunOS

public static final boolean isSunOS()
Determine if the system is a Sun based system.

Returns:
true if the operating system is Sun OS false otherwise

isHP

public static final boolean isHP()
Determine if the system is a HP based system.

Returns:
true if the operating system is HP-UX false otherwise

isAIX

public static final boolean isAIX()
Determine if the system is a AIX based system.

Returns:
true if the operating system is AIX false otherwise

isLinux

public static final boolean isLinux()
Determine if the system is a Linux based system.

Returns:
true if the operating system is Linux false otherwise

in

public static java.io.BufferedReader in()
Get a Reader version of System.in

Returns:
a Reader version of System.in

out

public static java.io.PrintWriter out()
Get a Writer version of System.out

Returns:
a Writer version of System.out

err

public static java.io.PrintWriter err()
Get a Writer version of System.err

Returns:
a Writer version of System.err

setOut

public static void setOut(java.io.PrintWriter pw)
Set the output stream returned by SystemUtil.out();


setErr

public static void setErr(java.io.PrintWriter pw)
Set the output stream returned by SystemUtil.out();


flushOutput

public static void flushOutput()
Flush system out and system error.


consumeException

public static final void consumeException(java.lang.String reason,
                                          java.lang.Throwable t)
Prevent a compiler warning when you don't want to do something in a catch block. The arguments are passed so that at a later point in time we could add some kind of logging to see where in our code we use this call.

Parameters:
reason - text reason for why you don't want to do anything.
t - throwable (usally exception) that was thrown.

consumeInterruptedException

public static final void consumeInterruptedException(java.lang.InterruptedException e)
Provide proper handling of InterruptedException from wait() and sleep() methods.

Parameters:
e - exception that was thrown.

stackTrace

public static java.lang.String stackTrace()
Get a string which represents the current call stack.

Returns:
a string which represents the current call stack

stackTraceCodePath

public static java.lang.String stackTraceCodePath()
Returns String stackTrace for current thread, reduced to focus on most relevant stack frames for analyzing application execution. It removes the tail of the stackTrace starting with the ariba.ui.servletadaptor frame, because the details of servlet dispatch are boring and not helpful. It elides (replaces with ...) all ariba.ui frames (except AWKeyPathBinding), because they are boring and voluminous, and almost never helpful for understanding application performance. It elides the details of reflexive method invokation under FieldValue_Object.getFieldValue. Etc.


stackTraceCodePath

public static java.lang.String stackTraceCodePath(java.lang.Throwable t)
Returns String stackTrace for given Throwable, reduced to focus on most relevant stack frames for analyzing application execution. It removes the tail of the stackTrace starting with the ariba.ui.servletadaptor frame, because the details of servlet dispatch are boring and not helpful. It elides (replaces with ...) all ariba.ui frames (except AWKeyPathBinding), because they are boring and voluminous, and almost never helpful for understanding application performance. It elides the details of reflexive method invokation under FieldValue_Object.getFieldValue. Etc.


stackTraceCodePath

public static java.lang.String stackTraceCodePath(java.lang.String st)
Returns String stackTrace reduced to focus on most relevant stack frames for analyzing application execution, given a String stackTrace. It removes the tail of the stackTrace starting with the ariba.ui.servletadaptor frame, because the details of servlet dispatch are boring and not helpful. It elides (replaces with ...) all ariba.ui frames (except AWKeyPathBinding), because they are boring and voluminous, and almost never helpful for understanding application performance. It elides the details of reflexive method invokation under FieldValue_Object.getFieldValue.


stackTrace

public static java.lang.String stackTrace(java.lang.Throwable t)
Get a string which represents the Throwable's call stack.

Parameters:
t - the Throwable to get the stack trace from
Returns:
a string which represents the Throwable's call stack

getRootCause

public static java.lang.Throwable getRootCause(java.lang.Throwable t)
Returns the root cause that cause that causes this exception.

Parameters:
t - the Throwable instance whose root cause is to be returned. If this is null, then null is returned.
Returns:
the root cause that cause that causes this exception, can be null

getenv

public static java.util.Map getenv()
For internal use only

Returns:
the complete current environment

getenv

public static java.lang.String getenv(java.lang.String envVar)
For internal use only

Parameters:
envVar - the name of the requested environment variable. Must not be null.
Returns:
the value of the requested environment variable. Null if the environment variable does not exist

setJ2EEServerInUse

public static void setJ2EEServerInUse()

setJ2EEServerInUse

public static void setJ2EEServerInUse(boolean flag)

usingJ2EEServer

public static boolean usingJ2EEServer()

setSystemDirectory

public static final void setSystemDirectory(java.lang.String systemDir)
Defines the location of the system directory.

Parameters:
systemDir - path of the system directory. Cannot be null.
See Also:
getSystemDirectory()

setConfigDirectory

public static final void setConfigDirectory(java.lang.String configDir)
Defines the location of the config directory.

Parameters:
configDir - path of the config directory. Cannot be null.
See Also:
getConfigDirectory()

setLocalTempDirectory

public static final void setLocalTempDirectory(java.lang.String tempDir)
Defines the location of the local temporary directory.

Parameters:
tempDir - path of the temp directory. Cannot be null.
See Also:
getLocalTempDirectory()

setSharedTempDirectory

public static final void setSharedTempDirectory(java.lang.String tempDir)
Defines the location of the shared temporary directory.

Parameters:
tempDir - path of the temp directory. Cannot be null.
See Also:
getSharedTempDirectory()

getSystemDirectory

public static java.io.File getSystemDirectory()
Returns the sytem directory.

Returns:
the sytem directory.

getConfigDirectory

public static java.io.File getConfigDirectory()
Returns the config directory.

Returns:
the config directory.

getConfigDirectoryString

public static java.lang.String getConfigDirectoryString()
Returns:
the config directory name as a string

getConfigURL

public static java.net.URL getConfigURL()
Returns the config URL.

Returns:
the config URL.

getLocalTempDirectory

public static java.io.File getLocalTempDirectory()
Returns the temporary directory for the local server This directory can be used to store temporary files which do not need to be shared accross multiple processes

Returns:
the local temporary directory

getSharedTempDirectory

public static java.io.File getSharedTempDirectory()
Returns the temporary directory which can be shared accross multiple processes This directory can be used to store temporary files which require to be accessible by different nodes

Returns:
the shared temporary directory

fixRelativePath

public static java.lang.String fixRelativePath(java.lang.String path)
Modify a relative file path to use custom config and directory locations


getInstallDirectory

public static java.io.File getInstallDirectory()
Returns the installation directory. Note: this might return am empty abstract path File, so be prepared to deal with that. On Mac, unix, and windows it should be correct, though.

Returns:
the installation directory or an empty abstract path

isDevelopment

public static boolean isDevelopment()
Are we in development, not production mode? This is a low level check needed for internal testing. Where possible clients should use the higher level isProduction method defined in ServerInterface.

Returns:
true if development, false if production
See Also:
ServerInterface.isProduction()

getInternalDirectory

public static java.io.File getInternalDirectory()
Returns the internal directory. Can be null.

Returns:
the internal directory, or null if the internal directory does not exist.

validateCaller

public static void validateCaller(SystemUtil.ValidCaller[] validCallers,
                                  boolean fatalAssert)
Validates that a method is allowed to call another method.

Parameters:
validCallers - an array of valid callers. See the ValidCaller constructor for details on what an entry looks like. To improve this method's performance, valid callers that require a search through the full stack should be at the end of the array.
fatalAssert - if true, an invalid caller causes a fatal assert, otherwise a non-fatal assert.


AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.