- java.lang.Object
-
- org.refcodes.runtime.RuntimeUtility
-
public final class RuntimeUtility extends Object
Utility for acquiring runtime information on software systems, classes or objects.
-
-
Field Summary
Fields Modifier and Type Field Description static StringCTRL_C_SIGNAL
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddShutdownHook(Thread hook)Registers a new virtual-machine shutdown hook.protected static booleancontainsDir(File aDir, List<File>... aDirs)static StackTraceElementgetCallerStackTraceElement()Returns theStackTraceElementbelonging to the direct caller of this method.static StackTraceElementgetCallerStackTraceElement(Class<?> aCallee)Returns theStackTraceElementbelonging to the caller of the callee.static StackTraceElementgetCallerStackTraceElement(Class<?>... aCallees)Same asgetCallerStackTraceElement(Class)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.static StackTraceElementgetCallerStackTraceElement(StackTraceElement aCallee)Returns theStackTraceElementbelonging to the caller of the callee.static StackTraceElementgetCallerStackTraceElement(StackTraceElement... aCallees)Same asgetCallerStackTraceElement(StackTraceElement)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.static StackTraceElementgetCallerStackTraceElement(String aCalleeClassName)Returns theStackTraceElementbelonging to the caller of the callee.static StackTraceElementgetCallerStackTraceElement(String... aCalleeClassNames)Same asgetCallerStackTraceElement(String)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.static Class<?>getCallerType()Returns the type of the (Class) belonging to the direct caller of this method.static Class<?>getCallerType(Class<?> aCallee)Returns the type of the (Class) belonging to the caller of the callee.static Class<?>getCallerType(Class<?>... aCallees)Same asgetCallerType(Class)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.static Class<?>getCallerType(StackTraceElement aCallee)Returns the type of the (Class) belonging to the caller of the callee.static Class<?>getCallerType(StackTraceElement... aCallees)Same asgetCallerType(StackTraceElement)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.static Class<?>getCallerType(String aCallee)Returns the type of the (Class) belonging to the caller of the callee.static Class<?>getCallerType(String... aCallees)Same asgetCallerType(String)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.static ClassgetMainClass()Returns the main class launching the application.static LonggetPid()Bad hack to get the JVM's (process TID) PID of the process running your JVM instance.static booleanisUnderTest()Determines whether this code is executed from inside a unit test or not.static booleankill(Long aPid)Bad hack to kill an OS thread by PID.static ProcesskillProcess(Long aPid)Bad hack to kill an OS thread by PID.protected static voidputProperty(Map<String,String> aProperties, String aKey, String aValue, char aDelimiter)Puts a property and resolves the key in case of name collisions.static voidsetLoggingHandler(Handler aHandler)Redirects theLoggerconfiguration and sets the provided handler.static voidsetLoggingStreams(OutputStream aStdStream, OutputStream aErrStream, Level aLevel)Redirects theLoggerconfiguration to the provided streams.static StackTraceElementtoBestCallerStackTraceElement(Class<?> aCallee)Returns the best matchingStackTraceElementbelonging to the caller of the callee.The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*".static StackTraceElementtoBestCallerStackTraceElement(Class<?>[] aCallees)Returns the best matchingStackTraceElementbelonging to the caller of one of the callees (in the order passed to the method).The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*".static StackTraceElementtoBestCallerStackTraceElement(StackTraceElement aCallee)Returns the best matchingStackTraceElementbelonging to the caller of the callee.The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*".static StackTraceElementtoBestCallerStackTraceElement(StackTraceElement[] aCallees)Returns the best matchingStackTraceElementbelonging to the caller of one of the callees (in the order passed to the method).The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*".static StackTraceElementtoBestCallerStackTraceElement(String aCalleeClassName)Returns the best matchingStackTraceElementbelonging to the caller of the callee.static StackTraceElementtoBestCallerStackTraceElement(String[] aCallees)Returns the best matchingStackTraceElementbelonging to the caller of one of the callees (in the order passed to the method).The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*".static PrintStreamtoBootstrapStandardError()Returns the originalPrintStreamfor STDERR as initialized by the JVM at bootstrapping time (theSystem.errPrintStreammay have got modified by libraries or frameworks).static InputStreamtoBootstrapStandardIn()Returns the originalInputStreamfor STDIN as initialized by the JVM at bootstrapping time (heSystem.inInputStreammay have got modified by libraries or frameworks).static PrintStreamtoBootstrapStandardOut()Returns the originalPrintStreamfor STDOUT as initialized by the JVM at bootstrapping time (theSystem.outPrintStreammay have got modified by libraries or frameworks).static Class<?>toClass(StackTraceElement aStackTraceElement)Retrieves theClasstype to which theStackTraceElementbelongs.static StringtoClassName()Retrieves the class name of the caller of this method without the fully qualified package name part.static StringtoClassName(StackTraceElement aStackTraceElement)Returns the class name part from aStackTraceElement.static StringtoClassName(String aFullyQualifiedClassName)Returns the class name part from a fully qualified class name (which has the fully qualified package name as part of its name).static <T> TtoClone(T aObj)static StringtoFormatted(String aMessage, Object... aArguments)WrapsMessageFormat.format(String, Object...)to prevent failing when message cannot be formatted.static StringtoFullyQualifiedClassName()Retrieves the fully qualified method name of the caller of this method.static StringtoFullyQualifiedClassName(StackTraceElement aStackTraceElement)Retrieves the fully qualified class name from aStackTraceElement.static StringtoFullyQualifiedMethodName()Retrieves the fully qualified method name of the caller of this method.static StringtoFullyQualifiedMethodName(StackTraceElement aStackTraceElement)Retrieves the fully qualified method name from aStackTraceElement.static StringtoFullyQualifiedPackageName()Retrieves the fully qualified package name of the caller of this method without the class name part.static StringtoFullyQualifiedPackageName(StackTraceElement aStackTraceElement)Retrieves the fully qualified package name from aStackTraceElement.static StringtoFullyQualifiedPackageName(String aFullyQualifiedClassName)Returns the fully qualified package name part from a fully qualified class name (which has the fully qualified package name as part of its name).static FiletoLauncherDir()Generates the base path relative to the given class location.static StringtoMethodName()Retrieves the fully qualified method name of the caller of this method.static StringtoMethodName(StackTraceElement aStackTraceElement)Retrieves the method name from aStackTraceElement.protected static StringtoNextKey(Map<String,String> aProperties, String aKey, char aDelimiter)Returns the next free key in the properties in case of key name collisions..protected static StringtoOption(String aArg, String... aPrefixes)When the provided argument starts with one of the provided prefixes, then the portion of the argument without the prefix is returned, which represents the option in question.static Map<String,String>toProperties(String[] aArgs)Creates aMapfrom the provided command line arguments using theCommandArgPrefix.toPrefixes()prefixes to identify the keys (and the values) from the provided arguments.static Map<String,String>toProperties(String[] aArgs, String[] aPrefixes, char aDelimiter)Creates aMapfrom the provided command line arguments using the provided prefixes to identify the keys (and the values) from the provided arguments.static StringtoStackTrace()Gets the stack trace for the current thread.static StringtoString(String aToString, String aSuperToString)Creates a string of a super class'sObject.toString()method and the provided "toString" text.static PrintStreamtoSystemErr()Create aPrintStreamfromSystem.errbeing UTF-8 encoded and filtering out ANSI escape codes in case the underlying terminal does not support the such.static PrintStreamtoSystemOut()Create aPrintStreamfromSystem.outbeing UTF-8 encoded and filtering out ANSI escape codes in case the underlying terminal does not support the such.static FiletoUserWorkingDir()Tries to determine the current directory from which the Java programm was started for example as of bash'spwdcommand.
-
-
-
Field Detail
-
CTRL_C_SIGNAL
public static final String CTRL_C_SIGNAL
- See Also:
- Constant Field Values
-
-
Method Detail
-
setLoggingHandler
public static void setLoggingHandler(Handler aHandler)
Redirects theLoggerconfiguration and sets the provided handler.- Parameters:
aHandler- TheHandlerto be set as the one and only handler.
-
setLoggingStreams
public static void setLoggingStreams(OutputStream aStdStream, OutputStream aErrStream, Level aLevel)
Redirects theLoggerconfiguration to the provided streams.- Parameters:
aStdStream- TheOutputStreamto write all unproblematic logs with levels such asLevel.INFOorLevel.CONFIGto.aErrStream- TheOutputStreamto write all problematic logs with levels such asLevel.WARNINGorLevel.SEVEREto.aLevel- The logLevelas of which to log to the provided streams.
-
toFormatted
public static String toFormatted(String aMessage, Object... aArguments)
WrapsMessageFormat.format(String, Object...)to prevent failing when message cannot be formatted.
-
toBootstrapStandardOut
public static PrintStream toBootstrapStandardOut()
Returns the originalPrintStreamfor STDOUT as initialized by the JVM at bootstrapping time (theSystem.outPrintStreammay have got modified by libraries or frameworks).- Returns:
- The real
PrintStreamas initially set.
-
toBootstrapStandardError
public static PrintStream toBootstrapStandardError()
Returns the originalPrintStreamfor STDERR as initialized by the JVM at bootstrapping time (theSystem.errPrintStreammay have got modified by libraries or frameworks).- Returns:
- The real
PrintStreamas initially set.
-
toBootstrapStandardIn
public static InputStream toBootstrapStandardIn()
Returns the originalInputStreamfor STDIN as initialized by the JVM at bootstrapping time (heSystem.inInputStreammay have got modified by libraries or frameworks).- Returns:
- The real
InputStreamas initially set.
-
toSystemOut
public static PrintStream toSystemOut()
Create aPrintStreamfromSystem.outbeing UTF-8 encoded and filtering out ANSI escape codes in case the underlying terminal does not support the such.- Returns:
- The
System.outPrintStreamaccordingly wrapped.
-
toSystemErr
public static PrintStream toSystemErr()
Create aPrintStreamfromSystem.errbeing UTF-8 encoded and filtering out ANSI escape codes in case the underlying terminal does not support the such.- Returns:
- The
System.errPrintStreamaccordingly wrapped.
-
isUnderTest
public static boolean isUnderTest()
Determines whether this code is executed from inside a unit test or not.- Returns:
- True in case if executed from within a unit test.
-
toStackTrace
public static String toStackTrace()
Gets the stack trace for the current thread.- Returns:
- The stack trace from the current thread.
-
getCallerStackTraceElement
public static StackTraceElement getCallerStackTraceElement()
Returns theStackTraceElementbelonging to the direct caller of this method. When you use this method in your code, you get the stack trace element of your method (invoking this method).- Returns:
- The stack element of the direct caller of this method.
-
getCallerStackTraceElement
public static StackTraceElement getCallerStackTraceElement(Class<?> aCallee)
Returns theStackTraceElementbelonging to the caller of the callee. Best you do not use theObject.getClass()method, instead use YourClass.class as as of inheritance,Object.getClass()returns the wrong type not being the actual callee!- Parameters:
aCallee- The callee class which wants to find out who called it.- Returns:
- The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
-
getCallerStackTraceElement
public static StackTraceElement getCallerStackTraceElement(StackTraceElement aCallee)
Returns theStackTraceElementbelonging to the caller of the callee. Best you do not use theObject.getClass()method, instead use YourClass.class as as of inheritance,Object.getClass()returns the wrong type not being the actual callee!- Parameters:
aCallee- The calleeStackTraceElementwhich wants to find out who called it.- Returns:
- The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
-
getCallerStackTraceElement
public static StackTraceElement getCallerStackTraceElement(Class<?>... aCallees)
Same asgetCallerStackTraceElement(Class)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.- Parameters:
aCallees- the callees- Returns:
- the caller
StackTraceElement
-
getCallerStackTraceElement
public static StackTraceElement getCallerStackTraceElement(StackTraceElement... aCallees)
Same asgetCallerStackTraceElement(StackTraceElement)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.- Parameters:
aCallees- the callees- Returns:
- the caller
StackTraceElement
-
getCallerStackTraceElement
public static StackTraceElement getCallerStackTraceElement(String... aCalleeClassNames)
Same asgetCallerStackTraceElement(String)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.- Parameters:
aCalleeClassNames- the callee class names- Returns:
- the caller
StackTraceElement
-
getCallerStackTraceElement
public static StackTraceElement getCallerStackTraceElement(String aCalleeClassName)
Returns theStackTraceElementbelonging to the caller of the callee. The callee can also be a package namespace where the matchee's must begin with the given package namespace.- Parameters:
aCalleeClassName- The callee class name or package namespace which wants to find out who called it.- Returns:
- The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
-
toBestCallerStackTraceElement
public static StackTraceElement toBestCallerStackTraceElement(String aCalleeClassName)
Returns the best matchingStackTraceElementbelonging to the caller of the callee. The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*". The callee can also be a package namespace where the matchee's must begin with the given package namespace.- Parameters:
aCalleeClassName- The callee class name or package namespace which wants to find out who called it.- Returns:
- The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
-
toBestCallerStackTraceElement
public static StackTraceElement toBestCallerStackTraceElement(StackTraceElement aCallee)
Returns the best matchingStackTraceElementbelonging to the caller of the callee.The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*".- Parameters:
aCallee- The calleeStackTraceElementwhich wants to find out who called it.- Returns:
- The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
-
toBestCallerStackTraceElement
public static StackTraceElement toBestCallerStackTraceElement(Class<?> aCallee)
Returns the best matchingStackTraceElementbelonging to the caller of the callee.The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*".- Parameters:
aCallee- The calleeStackTraceElementwhich wants to find out who called it.- Returns:
- The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
-
toBestCallerStackTraceElement
public static StackTraceElement toBestCallerStackTraceElement(String[] aCallees)
Returns the best matchingStackTraceElementbelonging to the caller of one of the callees (in the order passed to the method).The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*". The callees can also be a package namespace where the matchee's must begin with the given package namespace.- Parameters:
aCallees- The callees names for which to find out who called it.- Returns:
- The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
-
toBestCallerStackTraceElement
public static StackTraceElement toBestCallerStackTraceElement(StackTraceElement[] aCallees)
Returns the best matchingStackTraceElementbelonging to the caller of one of the callees (in the order passed to the method).The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*".- Parameters:
aCallees- The calleesStackTraceElementinstances for which to find out who called it.- Returns:
- The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
-
toBestCallerStackTraceElement
public static StackTraceElement toBestCallerStackTraceElement(Class<?>[] aCallees)
Returns the best matchingStackTraceElementbelonging to the caller of one of the callees (in the order passed to the method).The "best matching"StackTraceElementis considered to be the one not belonging to any "internal" API in a package namespace such as "com.sun.*", "java.*" or "javax.*".- Parameters:
aCallees- The calleesClassinstances for which to find out who called it.- Returns:
- The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
-
getCallerType
public static Class<?> getCallerType()
Returns the type of the (Class) belonging to the direct caller of this method. When you use this method in your code, you get theClassof your method (invoking this method).- Returns:
- The type (
Class) of the direct caller of this method.
-
getCallerType
public static Class<?> getCallerType(Class<?> aCallee)
Returns the type of the (Class) belonging to the caller of the callee.- Parameters:
aCallee- The callee class which wants to find out who called it.- Returns:
- The type (
Class) of the caller of the caller of this method.
-
getCallerType
public static Class<?> getCallerType(Class<?>... aCallees)
Same asgetCallerType(Class)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.- Parameters:
aCallees- the callees- Returns:
- the caller type
-
getCallerType
public static Class<?> getCallerType(StackTraceElement aCallee)
Returns the type of the (Class) belonging to the caller of the callee.- Parameters:
aCallee- The callee class which wants to find out who called it.- Returns:
- The type (
Class) of the caller of the caller of this method.
-
getCallerType
public static Class<?> getCallerType(String... aCallees)
Same asgetCallerType(String)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.- Parameters:
aCallees- the callees- Returns:
- the caller type
-
getCallerType
public static Class<?> getCallerType(String aCallee)
Returns the type of the (Class) belonging to the caller of the callee. The callee can also be a package namespace where the matchee's must begin with the given package namespace.- Parameters:
aCallee- The callee class which wants to find out who called it.- Returns:
- The type (
Class) of the caller of the caller of this method.
-
getCallerType
public static Class<?> getCallerType(StackTraceElement... aCallees)
Same asgetCallerType(StackTraceElement)with the difference that the passed callees are tried out one after the other until the first caller determined for a callee is returned.- Parameters:
aCallees- the callees- Returns:
- the caller type
-
toClass
public static Class<?> toClass(StackTraceElement aStackTraceElement)
Retrieves theClasstype to which theStackTraceElementbelongs.- Parameters:
aStackTraceElement- TheStackTraceElementfor which to get the accordingClass.- Returns:
- The type (
Class) of the accordingStackTraceElement.
-
toMethodName
public static String toMethodName(StackTraceElement aStackTraceElement)
Retrieves the method name from aStackTraceElement.- Parameters:
aStackTraceElement- TheStackTraceElementfrom which to retrieve the method name.- Returns:
- The method name or null in case the
StackTraceElementwas null.
-
toClassName
public static String toClassName(StackTraceElement aStackTraceElement)
Returns the class name part from aStackTraceElement. Retrieves the fully qualified class name from aStackTraceElement.- Parameters:
aStackTraceElement- TheStackTraceElementfrom which to retrieve the class name.- Returns:
- The class name without the package declaration or null in case
the
StackTraceElementwas null.
-
toFullyQualifiedClassName
public static String toFullyQualifiedClassName(StackTraceElement aStackTraceElement)
Retrieves the fully qualified class name from aStackTraceElement.- Parameters:
aStackTraceElement- TheStackTraceElementfrom which to retrieve the fully qualified class name.- Returns:
- The fully qualified class name or null in case the stack trace element was null.
-
toFullyQualifiedMethodName
public static String toFullyQualifiedMethodName(StackTraceElement aStackTraceElement)
Retrieves the fully qualified method name from aStackTraceElement. This adds the method name to the fully qualified path name separated by a hash "#".- Parameters:
aStackTraceElement- TheStackTraceElementfrom which to retrieve the fully qualified method name.- Returns:
- The fully qualified method name or null in case the stack trace element was null.
-
toFullyQualifiedClassName
public static String toFullyQualifiedClassName()
Retrieves the fully qualified method name of the caller of this method. This adds the method name to the caller's fully qualified path name separated by a hash "#".- Returns:
- The fully qualified method name.
-
toFullyQualifiedMethodName
public static String toFullyQualifiedMethodName()
Retrieves the fully qualified method name of the caller of this method. This adds the method name to the caller's fully qualified path name separated by a hash "#".- Returns:
- The fully qualified method name.
-
toMethodName
public static String toMethodName()
Retrieves the fully qualified method name of the caller of this method. This adds the method name to the caller's fully qualified path name separated by a hash "#".- Returns:
- The fully qualified method name.
-
toClassName
public static String toClassName()
Retrieves the class name of the caller of this method without the fully qualified package name part.- Returns:
- The class name.
-
toFullyQualifiedPackageName
public static String toFullyQualifiedPackageName()
Retrieves the fully qualified package name of the caller of this method without the class name part.- Returns:
- The fully qualified package name.
-
toFullyQualifiedPackageName
public static String toFullyQualifiedPackageName(StackTraceElement aStackTraceElement)
Retrieves the fully qualified package name from aStackTraceElement.- Parameters:
aStackTraceElement- TheStackTraceElementfrom which to retrieve the fully qualified package name.- Returns:
- The fully qualified package name.
-
toClassName
public static String toClassName(String aFullyQualifiedClassName)
Returns the class name part from a fully qualified class name (which has the fully qualified package name as part of its name).- Parameters:
aFullyQualifiedClassName- The fully qualified class name.- Returns:
- The class name without the package declaration.
-
toFullyQualifiedPackageName
public static String toFullyQualifiedPackageName(String aFullyQualifiedClassName)
Returns the fully qualified package name part from a fully qualified class name (which has the fully qualified package name as part of its name).- Parameters:
aFullyQualifiedClassName- The fully qualified class name.- Returns:
- The fully qualified package name without the class name.
-
toClone
public static <T> T toClone(T aObj) throws CloneNotSupportedExceptionACloneableobject cannot directly be cloned by casting it to beCloneable:-( Thereforee this method does the job. Citation From Josh Bloch's Effective Java: "TheCloneableinterface was intended as a mixin interface for objects to advertise that they permit cloning. Unfortunately it fails to serve this purpose ... This is a highly atypical use of interfaces and not one to be emulated ... In order for implementing the interface to have any effect on a class, it and all of its superclasses must obey a fairly complex, unenforceable and largely undocumented protocol"- Type Parameters:
T- the generic type- Parameters:
aObj- The object to be cloned.- Returns:
- The cloned object.
- Throws:
CloneNotSupportedException- in case the object cannot be cloned.- See Also:
- "http://stackoverflow.com/questions/1138769/why-is-the-clone-method-protected-in-java-lang-object"
-
toString
public static String toString(String aToString, String aSuperToString)
Creates a string of a super class'sObject.toString()method and the provided "toString" text.- Parameters:
aToString- The provided "toString" text.aSuperToString- A super class'sObject.toString()method'sString.- Returns:
- The "concatenated" and formatted new
Stringto be returned by an implementing class'sObject.toString()method.
-
getPid
public static Long getPid()
Bad hack to get the JVM's (process TID) PID of the process running your JVM instance.- Returns:
- The PID (process TID) of the JVM running your thread.
- See Also:
- "http://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-process-id"
-
killProcess
public static Process killProcess(Long aPid) throws IOException
Bad hack to kill an OS thread by PID. The current threads does not wait till the operation finished.- Parameters:
aPid- The process TID (PID) of the process to kill.- Returns:
- The
Processobject representing the kill operation. This instance will let you wait till the operation finishedProcess.waitFor()and provides access to theProcess.exitValue() - Throws:
IOException- Thrown in case of failing to successfully execute the kill operation.- See Also:
- "http://stackoverflow.com/questions/9573696/kill-a-process-based-on-pid-in-java", "http://stackoverflow.com/questions/2950338/how-can-i-kill-a-linux-process-in-java-with-sigkill-process-destroy-does-sigte"
-
kill
public static boolean kill(Long aPid) throws IOException, InterruptedException
Bad hack to kill an OS thread by PID. The current threads does wait till the operation finished.- Parameters:
aPid- The process TID (PID) of the process to kill.- Returns:
- True in case killing the process was successful (e.g. the kill operation returned an exit code 0), else false.
- Throws:
IOException- Thrown in case of failing to successfully execute the kill operation.InterruptedException- the interrupted exception- See Also:
- "http://stackoverflow.com/questions/9573696/kill-a-process-based-on-pid-in-java", "http://stackoverflow.com/questions/2950338/how-can-i-kill-a-linux-process-in-java-with-sigkill-process-destroy-does-sigte"
-
toUserWorkingDir
public static File toUserWorkingDir()
Tries to determine the current directory from which the Java programm was started for example as of bash'spwdcommand. Pass as JVM argument via "-Dcurrent.dir=/path/to/current/dir".- Returns:
- The current path of the user invoking the Java programm or null if the current path cannot be determined.
-
toLauncherDir
public static File toLauncherDir()
Generates the base path relative to the given class location. Depending on the runtime, the path is truncated till the required path is determined. In case theSystemProperty.LAUNCHER_DIR(e.g. when providing the following argument-Dlauncher.dir=/path/to/launcher/dirwhen invoking your java's executable) is set and in case its value points to a directory, then this directory is returned.- Returns:
- The base path of this application.
-
getMainClass
public static Class getMainClass()
Returns the main class launching the application.- Returns:
- The main class.
-
toProperties
public static Map<String,String> toProperties(String[] aArgs)
Creates aMapfrom the provided command line arguments using theCommandArgPrefix.toPrefixes()prefixes to identify the keys (and the values) from the provided arguments.- Parameters:
aArgs- The command line arguments to convert to properties.- Returns:
- The
Mapcontaining the determined properties.
-
toProperties
public static Map<String,String> toProperties(String[] aArgs, String[] aPrefixes, char aDelimiter)
Creates aMapfrom the provided command line arguments using the provided prefixes to identify the keys (and the values) from the provided arguments.- Parameters:
aArgs- The command line arguments to convert to properties.aPrefixes- The prefixes to be used to identify options.aDelimiter- The delimiter to use when generating non colliding keys.- Returns:
- The
Mapcontaining the determined properties.
-
addShutdownHook
public static void addShutdownHook(Thread hook)
Registers a new virtual-machine shutdown hook. In case of running as a native image (GraalVM AOT), then aSignalhandler for capturing CTRL+C is registered exiting the application regularirly so that shutdown hooks registered withRuntime.addShutdownHook(Thread)are also called when inside a native image. Exiting vie CTRL+C then will exit with status code 130 as ofExitCode.CONTROL_C.- Parameters:
hook- An initialized but unstartedThreadobject
-
putProperty
protected static void putProperty(Map<String,String> aProperties, String aKey, String aValue, char aDelimiter)
Puts a property and resolves the key in case of name collisions.- Parameters:
aProperties- The properties which to modify.aKey- The key which to put.aValue- The value to put.aDelimiter- The delimiter to use when generating non colliding keys.
-
toNextKey
protected static String toNextKey(Map<String,String> aProperties, String aKey, char aDelimiter)
Returns the next free key in the properties in case of key name collisions..- Parameters:
aProperties- The properties for which to determine the next key.aKey- The key for which to test for name collisions.aDelimiter- The delimiter to use when creating non colliding keys.- Returns:
- The next non colliding key or null if there are no collisions.
-
toOption
protected static String toOption(String aArg, String... aPrefixes)
When the provided argument starts with one of the provided prefixes, then the portion of the argument without the prefix is returned, which represents the option in question.- Parameters:
aArg- The argument for which to retrieve the option's name.aPrefixes- The prefixes to use when determining options.- Returns:
- Either the option's name or null if the argument does not represent an option.
-
containsDir
@SafeVarargs protected static boolean containsDir(File aDir, List<File>... aDirs)
-
-