Class SystemUtil


  • public final class SystemUtil
    extends java.lang.Object
    A utility class with some useful system-related functions.

    NOTE: This class is not considered part of the public API and may be changed without notice

    Version:
    $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/lang/SystemUtil.java#3 $
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  SystemUtil.XMLPropertiesInputStream
      This class marks an inputstream as containing XML, does nothing
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String STD_PROPERTIES
      ".properties"
      static java.lang.String XML_PROPERTIES
      ".xml"
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private SystemUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object clone​(java.lang.Cloneable pObject)  
      private static java.lang.Class getClass​(java.lang.String pClassName, boolean pInitialize, java.lang.ClassLoader pLoader)  
      private static java.io.InputStream getFileAsStream​(java.lang.String pName, boolean pGuessSuffix)
      Gets the named file as a stream from the current directory.
      private static java.io.InputStream getResourceAsStream​(java.lang.ClassLoader pClassLoader, java.lang.String pName, boolean pGuessSuffix)
      Gets the named resource as a stream from the given Class' Classoader.
      static boolean isClassAvailable​(java.lang.String pClassName)
      Tests if a named class is generally available.
      static boolean isClassAvailable​(java.lang.String pClassName, java.lang.Class pFromClass)
      Tests if a named class is available from another class.
      private static boolean isClassAvailable​(java.lang.String pClassName, java.lang.ClassLoader pLoader)  
      static boolean isFieldAvailable​(java.lang.String pClassName, java.lang.String pFieldName)  
      static boolean isFieldAvailable​(java.lang.String pClassName, java.lang.String pFieldName, java.lang.Class pFromClass)  
      private static boolean isFieldAvailable​(java.lang.String pClassName, java.lang.String pFieldName, java.lang.ClassLoader pLoader)  
      static boolean isMethodAvailable​(java.lang.String pClassName, java.lang.String pMethodName)  
      static boolean isMethodAvailable​(java.lang.String pClassName, java.lang.String pMethodName, java.lang.Class[] pParams)  
      static boolean isMethodAvailable​(java.lang.String pClassName, java.lang.String pMethodName, java.lang.Class[] pParams, java.lang.Class pFromClass)  
      private static boolean isMethodAvailable​(java.lang.String pClassName, java.lang.String pMethodName, java.lang.Class[] pParams, java.lang.ClassLoader pLoader)  
      private static java.util.Properties loadProperties​(java.io.InputStream pInput)
      Returns a Properties, loaded from the given inputstream.
      static java.util.Properties loadProperties​(java.lang.Class pClass)
      Utility method for loading a properties-file for a given class.
      static java.util.Properties loadProperties​(java.lang.Class pClass, java.lang.String pName)
      Utility method for loading a named properties-file for a class.
      static java.util.Properties loadProperties​(java.lang.String pName)
      Utility method for loading a named properties-file.
      static void main​(java.lang.String[] args)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • XML_PROPERTIES

        public static java.lang.String XML_PROPERTIES
        ".xml"
      • STD_PROPERTIES

        public static java.lang.String STD_PROPERTIES
        ".properties"
    • Constructor Detail

      • SystemUtil

        private SystemUtil()
    • Method Detail

      • getResourceAsStream

        private static java.io.InputStream getResourceAsStream​(java.lang.ClassLoader pClassLoader,
                                                               java.lang.String pName,
                                                               boolean pGuessSuffix)
        Gets the named resource as a stream from the given Class' Classoader. If the pGuessSuffix parameter is true, the method will try to append typical properties file suffixes, such as ".properties" or ".xml".
        Parameters:
        pClassLoader - the class loader to use
        pName - name of the resource
        pGuessSuffix - guess suffix
        Returns:
        an input stream reading from the resource
      • getFileAsStream

        private static java.io.InputStream getFileAsStream​(java.lang.String pName,
                                                           boolean pGuessSuffix)
        Gets the named file as a stream from the current directory. If the pGuessSuffix parameter is true, the method will try to append typical properties file suffixes, such as ".properties" or ".xml".
        Parameters:
        pName - name of the resource
        pGuessSuffix - guess suffix
        Returns:
        an input stream reading from the resource
      • loadProperties

        public static java.util.Properties loadProperties​(java.lang.Class pClass,
                                                          java.lang.String pName)
                                                   throws java.io.IOException
        Utility method for loading a named properties-file for a class.

        The properties-file is loaded through either:

        1. The given class' class loader (from classpath)
        2. Or, the system class loader (from classpath)
        3. Or, if it cannot be found in the classpath, an attempt to read from the current directory (or full path if given).

        Both normal java.util.Properties and com.twelvemonkeys.util.XMLProperties are supported (XML-properties must have ".xml" as its file extension).

        Parameters:
        pClass - The class to load properties for. If this parameter is null, the method will work exactly as loadProperties(String)
        pName - The name of the properties-file. If this parameter is null, the method will work exactly as loadProperties(Class)
        Returns:
        A Properties mapping read from the given file or for the given class.
        Throws:
        java.lang.NullPointerException - if both pName and pClass paramters are null
        java.io.IOException - if an error occurs during load.
        java.io.FileNotFoundException - if no properties-file could be found.
        See Also:
        loadProperties(String), loadProperties(Class), ClassLoader.getResourceAsStream(java.lang.String), ClassLoader.getSystemResourceAsStream(java.lang.String)
      • loadProperties

        public static java.util.Properties loadProperties​(java.lang.Class pClass)
                                                   throws java.io.IOException
        Utility method for loading a properties-file for a given class. The properties are searched for on the form "com/package/ClassName.properties" or "com/package/ClassName.xml".

        The properties-file is loaded through either:

        1. The given class' class loader (from classpath)
        2. Or, the system class loader (from classpath)
        3. Or, if it cannot be found in the classpath, an attempt to read from the current directory (or full path if given).

        Both normal java.util.Properties and com.twelvemonkeys.util.XMLProperties are supported (XML-properties must have ".xml" as its file extension).

        Parameters:
        pClass - The class to load properties for
        Returns:
        A Properties mapping for the given class.
        Throws:
        java.lang.NullPointerException - if the pClass paramters is null
        java.io.IOException - if an error occurs during load.
        java.io.FileNotFoundException - if no properties-file could be found.
        See Also:
        loadProperties(String), loadProperties(Class, String), ClassLoader.getResourceAsStream(java.lang.String), ClassLoader.getSystemResourceAsStream(java.lang.String)
      • loadProperties

        public static java.util.Properties loadProperties​(java.lang.String pName)
                                                   throws java.io.IOException
        Utility method for loading a named properties-file.

        The properties-file is loaded through either:

        1. The system class loader (from classpath)
        2. Or, if it cannot be found in the classpath, an attempt to read from the current directory.

        Both normal java.util.Properties and com.twelvemonkeys.util.XMLProperties are supported (XML-properties must have ".xml" as its file extension).

        Parameters:
        pName - The name of the properties-file.
        Returns:
        A Properties mapping read from the given file.
        Throws:
        java.lang.NullPointerException - if the pName paramters is null
        java.io.IOException - if an error occurs during load.
        java.io.FileNotFoundException - if no properties-file could be found.
        See Also:
        loadProperties(Class), loadProperties(Class, String), ClassLoader.getSystemResourceAsStream(java.lang.String)
      • loadProperties

        private static java.util.Properties loadProperties​(java.io.InputStream pInput)
                                                    throws java.io.IOException
        Returns a Properties, loaded from the given inputstream. If the given inputstream is null, then an empty Properties object is returned.
        Parameters:
        pInput - the inputstream to read from
        Returns:
        a Properties object read from the given stream, or an empty Properties mapping, if the stream is null.
        Throws:
        java.io.IOException - if an error occurred when reading from the input stream.
      • clone

        public static java.lang.Object clone​(java.lang.Cloneable pObject)
                                      throws java.lang.CloneNotSupportedException
        Throws:
        java.lang.CloneNotSupportedException
      • main

        public static void main​(java.lang.String[] args)
                         throws java.lang.CloneNotSupportedException
        Throws:
        java.lang.CloneNotSupportedException
      • isClassAvailable

        public static boolean isClassAvailable​(java.lang.String pClassName)
        Tests if a named class is generally available. If a class is considered available, a call to Class.forName(pClassName) will not result in an exception.
        Parameters:
        pClassName - the class name to test
        Returns:
        true if available
      • isClassAvailable

        public static boolean isClassAvailable​(java.lang.String pClassName,
                                               java.lang.Class pFromClass)
        Tests if a named class is available from another class. If a class is considered available, a call to Class.forName(pClassName, true, pFromClass.getClassLoader()) will not result in an exception.
        Parameters:
        pClassName - the class name to test
        pFromClass - the class to test from
        Returns:
        true if available
      • isClassAvailable

        private static boolean isClassAvailable​(java.lang.String pClassName,
                                                java.lang.ClassLoader pLoader)
      • isFieldAvailable

        public static boolean isFieldAvailable​(java.lang.String pClassName,
                                               java.lang.String pFieldName)
      • isFieldAvailable

        public static boolean isFieldAvailable​(java.lang.String pClassName,
                                               java.lang.String pFieldName,
                                               java.lang.Class pFromClass)
      • isFieldAvailable

        private static boolean isFieldAvailable​(java.lang.String pClassName,
                                                java.lang.String pFieldName,
                                                java.lang.ClassLoader pLoader)
      • isMethodAvailable

        public static boolean isMethodAvailable​(java.lang.String pClassName,
                                                java.lang.String pMethodName)
      • isMethodAvailable

        public static boolean isMethodAvailable​(java.lang.String pClassName,
                                                java.lang.String pMethodName,
                                                java.lang.Class[] pParams)
      • isMethodAvailable

        public static boolean isMethodAvailable​(java.lang.String pClassName,
                                                java.lang.String pMethodName,
                                                java.lang.Class[] pParams,
                                                java.lang.Class pFromClass)
      • isMethodAvailable

        private static boolean isMethodAvailable​(java.lang.String pClassName,
                                                 java.lang.String pMethodName,
                                                 java.lang.Class[] pParams,
                                                 java.lang.ClassLoader pLoader)
      • getClass

        private static java.lang.Class getClass​(java.lang.String pClassName,
                                                boolean pInitialize,
                                                java.lang.ClassLoader pLoader)
                                         throws java.lang.ClassNotFoundException
        Throws:
        java.lang.ClassNotFoundException