Package de.mhus.lib.core
Class MSystem
- java.lang.Object
-
- de.mhus.lib.core.MSystem
-
public class MSystem extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMSystem.OSstatic classMSystem.ScriptResultstatic classMSystem.TopThreadInfo
-
Constructor Summary
Constructors Constructor Description MSystem()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidbeep()makeshift system beep if awt.Toolkit.beep is not available.static <T extends Comparable<T>>
intcompareTo(T s1, T s2)Compares two objects even if they are null.static booleanequals(Object a, Object b)static MSystem.ScriptResultexecute(IProperties env, File workingDirectory, InputStream stdin, boolean redirectErrorStream, String... command)Executes a command and returns an array of 0 = strOut, 1 = stdErrstatic MSystem.ScriptResultexecute(String... command)Executes a command and returns an array of 0 = strOut, 1 = stdErrstatic <A extends Annotation>
AfindAnnotation(Class<?> clazz, Class<A> annotation)static StringfindCalling()static StringfindCalling(int returns)static StringfindSourceMethod()static StringfindSourceMethod(int returns)static longfreeMemory()Return the amount of free not allocated memorystatic StringfreeMemoryAsString()static StringgetAppIdent()Get the identification of the application host:pidstatic LinkedList<Field>getAttributes(Class<?> clazz)static byte[]getBytes(Class<?> c)static StringgetCanonicalClassName(Class<?> clazz)Return a unique class name for the class with package, main class name and sub class name.static Class<?>getClass(ClassLoader cl, String type)static StringgetClassName(Object obj)Returns the canonical name of the main class.static FieldgetDeclaredField(Class<?> clazz, String name)Get the field in this or any superclass.static StringgetHostname()Returns the name of the current system.static ObjectgetJavaVersion()static longgetJvmStartTime()static longgetJvmUptime()static Class<?>getMainClass(Object obj)Returns the class of the object or class or if the class is anonymous the surrounding main class.static StringgetMainClassName()Return the name of the main class or null if not found.static LinkedList<Method>getMethods(Class<?> clazz)static StringgetObjectId(Object o)Returns the id of the object like the original toString() will dostatic MSystem.OSgetOS()static StringgetPid()Returns the process id of the current application.static StringgetProperty(String key)For completeness.static StringgetProperty(String key, String def)For completeness.static StringgetSimpleName(Object obj)static longgetSystemUptime()static StringgetTemplateCanonicalName(Class<?> clazz, int index)Return the declared template class for the index.static StringgetTmpDirectory()Return the system temp directory.static FilegetUserHome()static booleanisLockedByThread(Object value)static booleanisMac()static booleanisSolaris()static booleanisUnix()static booleanisWindows()static Class<?>loadClass(String type, ClassLoader cl)Load the class for the given type declaration.static PropertiesloadProperties(Object owner, Properties properties, String propertyFile)Load and return a properties file.static URLlocateResource(Object owner, String fileName)1.static StringmaxMemoryAsString()static StringmemDisplayString()static StringsetProperty(String key, String value)Like System.setProperty().static MSystem.ScriptResultstartScript(File dir, String script, long timeout)Deprecated.static List<MSystem.TopThreadInfo>threadTop(long sleep)Watch for a defined time of milliseconds all threads and returns the used cpu time.static StringtoString(Object sender, Object... attributes)Return a string representation of the data.
-
-
-
Method Detail
-
getHostname
public static String getHostname()
Returns the name of the current system. COMPUTERNAME or HOSTNAME.- Returns:
- the hosts name
-
getPid
public static String getPid()
Returns the process id of the current application.- Returns:
- the current process id
-
loadProperties
public static Properties loadProperties(Object owner, Properties properties, String propertyFile)
Load and return a properties file. If the file not exists it will only log the impact and return a empty properties object. If the properties object is not null this instance will be used to load the file entries. 1. Find by system property {propertyname}.file= 2. Find in {current dir} 3. Find in {current dir}/config 4. Find in {CONFIGURATION}/config 5. Find in classpath without package name 6. Find in classpath with package of the owner (if set) 7. throw an error- Parameters:
owner- null or reference object for the class pathproperties- A pre-instanciated properties objectpropertyFile- Name of the properties file, e.g. something.properties- Returns:
- The loaded properties instance
-
locateResource
public static URL locateResource(Object owner, String fileName) throws IOException
1. Find by system property {propertyname}.file= 2. Find in {current dir} 3. Find in {current dir}/config 4. Find in {CONFIGURATION}/config 5. Find in classpath without package name 6. Find in classpath with package of the owner (if set) 7. throw an error- Parameters:
owner-fileName-- Returns:
- the reference to the resource
- Throws:
IOException
-
beep
public static void beep()
makeshift system beep if awt.Toolkit.beep is not available. Works also in JDK 1.02.
-
findCalling
public static String findCalling(int returns)
-
findCalling
public static String findCalling()
-
findSourceMethod
public static String findSourceMethod(int returns)
-
findSourceMethod
public static String findSourceMethod()
-
getMainClassName
public static String getMainClassName()
Return the name of the main class or null if not found.- Returns:
- the main class name
-
getTmpDirectory
public static String getTmpDirectory()
Return the system temp directory.- Returns:
- path to the tmp directory
-
toString
public static String toString(Object sender, Object... attributes)
Return a string representation of the data. use this to Implement toString()- Parameters:
sender-attributes-- Returns:
- Stringified attributes
-
findAnnotation
public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotation)
-
startScript
@Deprecated public static MSystem.ScriptResult startScript(File dir, String script, long timeout)
Deprecated.Start a script and return the result as struct. Deprecated use execute() instead- Parameters:
dir-script-timeout-- Returns:
- The result of execution
-
getAppIdent
public static String getAppIdent()
Get the identification of the application host:pid- Returns:
- host:pid
-
getObjectId
public static String getObjectId(Object o)
Returns the id of the object like the original toString() will do- Parameters:
o-- Returns:
- the id
-
getClassName
public static String getClassName(Object obj)
Returns the canonical name of the main class.- Parameters:
obj- Object or class- Returns:
- The name
-
getMainClass
public static Class<?> getMainClass(Object obj)
Returns the class of the object or class or if the class is anonymous the surrounding main class.- Parameters:
obj- object or class- Returns:
- The main class
-
getTemplateCanonicalName
public static String getTemplateCanonicalName(Class<?> clazz, int index)
Return the declared template class for the index. This is only possible if the class itself extends a templated class and declares the templates with classes.- Parameters:
clazz- The class to checkindex- Template index, starts at 0- Returns:
- The canonical name of the defined template
-
compareTo
public static <T extends Comparable<T>> int compareTo(T s1, T s2)
Compares two objects even if they are null. compareTo(null, null ) === 0 compareTo("", "" ) === 0 compareTo(null, "" ) === -1 compareTo("", null ) === 1 returns s1.comareTo(s2) groovy:000> s1 = new Date(); === Wed Jun 28 12:22:35 CEST 2017 groovy:000> s2 = new Date(); === Wed Jun 28 12:22:40 CEST 2017 groovy:000> groovy:000> s1.compareTo(s2) === -1 groovy:000> s2.compareTo(s1) === 1- Parameters:
s1-s2-- Returns:
- see Comparator
-
freeMemoryAsString
public static String freeMemoryAsString()
-
maxMemoryAsString
public static String maxMemoryAsString()
-
memDisplayString
public static String memDisplayString()
-
freeMemory
public static long freeMemory()
Return the amount of free not allocated memory- Returns:
- memory in bytes
-
getDeclaredField
public static Field getDeclaredField(Class<?> clazz, String name)
Get the field in this or any superclass.- Parameters:
clazz-name-- Returns:
- The field or null if not found
-
loadClass
public static Class<?> loadClass(String type, ClassLoader cl) throws ClassNotFoundException
Load the class for the given type declaration. Also primitive and array declarations are allowed.- Parameters:
type- The type as primitive int, long ... String, Date the class name or as Array with pending [].cl- The class loader or null to use the thread context class loader.- Returns:
- The class instance.
- Throws:
ClassNotFoundException
-
threadTop
public static List<MSystem.TopThreadInfo> threadTop(long sleep) throws InterruptedException
Watch for a defined time of milliseconds all threads and returns the used cpu time.- Parameters:
sleep-- Returns:
- List of found values
- Throws:
InterruptedException
-
execute
public static MSystem.ScriptResult execute(String... command) throws IOException
Executes a command and returns an array of 0 = strOut, 1 = stdErr- Parameters:
command-- Returns:
- 0 = strOut, 1 = stdErr
- Throws:
IOException
-
execute
public static MSystem.ScriptResult execute(IProperties env, File workingDirectory, InputStream stdin, boolean redirectErrorStream, String... command) throws IOException
Executes a command and returns an array of 0 = strOut, 1 = stdErr- Parameters:
env- Environment variables or null if not neededworkingDirectory- pwd for the process or null if not neededstdin- Set StdIn stream or null if not neededredirectErrorStream- Set true if error should be redirected to input (error String will be null)command-- Returns:
- 0 = strOut, 1 = stdErr
- Throws:
IOException
-
isWindows
public static boolean isWindows()
-
isMac
public static boolean isMac()
-
isUnix
public static boolean isUnix()
-
isSolaris
public static boolean isSolaris()
-
getOS
public static MSystem.OS getOS()
-
getUserHome
public static File getUserHome()
-
setProperty
public static String setProperty(String key, String value)
Like System.setProperty(). Sets the property with to a value. But this one accepts also null values and will clear the property in this case.- Parameters:
key-value-- Returns:
- Previous value
-
getProperty
public static String getProperty(String key)
For completeness. The same functionality like System.getProperty()- Parameters:
key-- Returns:
- Current value or null
-
getProperty
public static String getProperty(String key, String def)
For completeness. The same functionality like System.getProperty()- Parameters:
key-def-- Returns:
- Current value or def
-
getCanonicalClassName
public static String getCanonicalClassName(Class<?> clazz)
Return a unique class name for the class with package, main class name and sub class name.- Parameters:
clazz- The class to analyze- Returns:
- The name of the class
-
isLockedByThread
public static boolean isLockedByThread(Object value)
-
getJavaVersion
public static Object getJavaVersion()
-
getBytes
public static byte[] getBytes(Class<?> c) throws IOException
- Throws:
IOException
-
getJvmUptime
public static long getJvmUptime()
-
getJvmStartTime
public static long getJvmStartTime()
-
getSystemUptime
public static long getSystemUptime()
-
getMethods
public static LinkedList<Method> getMethods(Class<?> clazz)
-
getAttributes
public static LinkedList<Field> getAttributes(Class<?> clazz)
-
getClass
public static Class<?> getClass(ClassLoader cl, String type) throws ClassNotFoundException
- Throws:
ClassNotFoundException
-
-