Class MSystem


  • public class MSystem
    extends Object
    • Constructor Detail

      • MSystem

        public MSystem()
    • 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 path
        properties - A pre-instanciated properties object
        propertyFile - 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)
      • equals

        public static boolean equals​(Object a,
                                     Object b)
      • 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
      • getSimpleName

        public static String getSimpleName​(Object obj)
      • 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 check
        index - 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
      • 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 needed
        workingDirectory - pwd for the process or null if not needed
        stdin - Set StdIn stream or null if not needed
        redirectErrorStream - 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()
      • 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()
      • getJvmUptime

        public static long getJvmUptime()
      • getJvmStartTime

        public static long getJvmStartTime()
      • getSystemUptime

        public static long getSystemUptime()