Class PubServerUtils


  • public class PubServerUtils
    extends java.lang.Object
    Utility class with static methods for several purposes as e.g. hashing (MD5, SHA256) or object handling (merge, copy, deep or flat), parsing strings to simple values, etc.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String calculateMD5​(byte[] content)  
      static java.lang.String calculateMD5​(java.lang.String content)  
      static java.lang.String calculateRepositoryPath​(java.lang.String pluginName, java.lang.String client, java.lang.String nodeName)
      Calculate the absolute path of a node for a given Plugin or domain (such as "global"), client name and node name.
      static java.lang.String calculateRepositoryRoot​(java.lang.String pluginName, java.lang.String client)
      Calculate the absolute path of the repository root for a given Plugin or domain (such as "global") and client name.
      static java.lang.String calculateSHA256​(byte[] content)  
      static java.lang.String calculateSHA256​(java.lang.String content)  
      static byte[] compress​(byte[] data)
      Compress a byte array using Deflate algorithm.
      static java.lang.String compressToBase64​(byte[] data)
      Compress a byte array using Deflate algorithm.
      static double convertNanoToMilliseconds​(long nanoSeconds)
      Convert nano seconds to milliseconds and return as double
      static java.lang.String convertToASCIIString​(java.lang.String strToConvert)
      Convert String to ASCIIString, needed for downloading files witch special characters
      static java.lang.String debug​(java.lang.Object obj)
      Get a detailed debug string for an arbitrary object.
      static java.lang.String debugList​(java.util.List<?> in)
      Get a nicely formatted debug string for an arbitrary list, using default toString method on the contained objects.
      static byte[] decompress​(byte[] data)
      Uncompress a byte array using Inflate algorithm.
      static byte[] decompressFromBase64​(java.lang.String data)
      Uncompresses a base64 encoded byte array using Inflate algorithm.
      static java.lang.String formatDateTime​(java.util.Date dateTime)
      Formats a date according to ISO 8601 date (also xml:dateTime) (e.g.
      static java.lang.String formatSimpleDate​(java.util.Date dateTime)
      Formats a date for ISON (e.g.
      static java.lang.String getCharacterDataFromElement​(org.w3c.dom.Element e)  
      static byte[] getFileIntoByteArray​(java.io.File file)
      Reads a file and returns it as byte array.
      static java.lang.String getFingerprintOfGlassfishInstance()  
      static java.lang.Class<?> getFirstParameterTypeArgument​(java.lang.reflect.Method method, int parameterIndex)
      Get the Class of the first type argument of a parameter at a certain index.
      In PubServer environment we generally have only one type argument (e.g.
      static java.lang.String getHostName()
      Retrieve the name of the local machine.
      static java.lang.reflect.Type[] getParameterTypeArguments​(java.lang.reflect.Method method, int parameterIndex)
      Get the type arguments (in order of declaration) of a parameter at a certain index or null, if this parameter is of no generic type.
      static java.lang.reflect.Type[] getParameterTypeArguments​(java.lang.reflect.Type type)
      Returns the type arguments (in order of declaration) of a Java parameter type or null, if the type provided is no generic type.
      static java.lang.Class<?> getReturnTypeArgument​(java.lang.reflect.Method method)
      Get the Class of the first type argument of a method's return type.
      In PubServer environment we generally have only one type argument (e.g.
      static java.lang.Class<?>[] getReturnTypeArguments​(java.lang.reflect.Method method)
      Gets the classes of the type arguments of a method return type.
      static <T> T getSingleResultOrNull​(javax.persistence.TypedQuery<T> query)
      Retrieving a single record from the a persistence datasource.
      static java.lang.String getStackTrace​(java.lang.Throwable e)
      returns the stack trace of an exception as a string
      static boolean getSystemSetting​(java.lang.String name, boolean defaultValue)
      Reads a boolean value by key name either from system properties or from system environment (in this order).
      static int getSystemSetting​(java.lang.String name, int defaultValue)
      Reads a numeric value by key name either from system properties or from system environment (in this order).
      static int getSystemSetting​(java.lang.String name, int defaultValue, int min, int max)
      Reads a numeric value by key name either from system properties or from system environment (in this order).
      static java.lang.String getSystemSetting​(java.lang.String name, java.lang.String defaultValue)
      Reads a value by key name either from system properties or from system environment (in this order).
      static java.lang.Class<?>[] getTypeArguments​(java.lang.reflect.Type type)  
      static java.lang.String getTypeString​(java.lang.Class<?> clazz)  
      static boolean isPrimitive​(java.lang.Class<?> clazz)
      Helper for the debug methods below
      static boolean isRootMetaData​(Entity checkedEntity, java.util.Map<java.lang.String,​Entity> allEntities)  
      static boolean isRootMetaDataByTag​(Entity checkedEntity, java.util.Map<java.lang.String,​Entity> allEntities, PluginConfigTag tag)  
      static boolean isRootMetaDataForPageTemplate​(Entity checkedEntity, java.util.Map<java.lang.String,​Entity> allEntities)  
      static boolean isRootMetaDataForPageTemplateElement​(Entity checkedEntity, java.util.Map<java.lang.String,​Entity> allEntities)  
      static boolean isRootMetaDataForRealGridElement​(Entity checkedEntity, java.util.Map<java.lang.String,​Entity> allEntities)  
      static boolean isRootMetaDataForVirtualEntities​(Entity checkedEntity, java.util.Map<java.lang.String,​Entity> allEntities)  
      static boolean isSuperSetOf​(java.lang.Class<?>[] classes, java.lang.Class<?>[] otherClasses)
      Return true, if each of classes is assignable from or equals to any of otherClasses, or, in other words, otherClasses is more specific than classes.
      We use this method to check, if a Object representation of a XML document must be revalidated when assigning a new set of custom classes.
      static void logStackTrace​(org.slf4j.Logger myLogger, java.lang.Throwable e)
      Logs the stack trace to the debug log
      static java.lang.Boolean parseBool​(java.lang.String input, java.lang.Boolean defaultValue)
      Tries to parse an input string into a boolean value.
      static java.lang.String parseClientName​(java.lang.String repositoryPath)
      Parse the client name from a absolute path in the repository.
      static java.util.Date parseDateTime​(java.lang.String dateString)
      Parses a string according to ISO 8601 date (also xml:dateTime) (e.g.
      static java.lang.Float parseFloat​(java.lang.String input, java.lang.Float defaultValue)
      Tries to parse an input string into a float value.
      static java.lang.Integer parseInt​(java.lang.String input, java.lang.Integer defaultValue)
      Tries to parse an input string into an integer value.
      static java.lang.String parsePluginName​(java.lang.String repositoryPath)
      Parse the Plugin name from a absolute path in the repository.
      static java.util.Date parseSimpleDate​(java.lang.String dateString)
      Parses a string according to American rules (e.g.
      static java.util.Date timeStampToDate​(java.lang.String dateString)
      Converts timestamp to java.util.Date
      • Methods inherited from class java.lang.Object

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

      • calculateSHA256

        public static final java.lang.String calculateSHA256​(java.lang.String content)
        Parameters:
        content - string to be hashed, null returns an empty string
        Returns:
        string with SHA256 value
      • calculateSHA256

        public static final java.lang.String calculateSHA256​(byte[] content)
        Parameters:
        content - byte array to be hashed, null returns an empty string
        Returns:
        string with SHA256 value
      • calculateMD5

        public static final java.lang.String calculateMD5​(java.lang.String content)
        Parameters:
        content - string to be hashed, null returns an empty string
        Returns:
        string with MD5 value
      • calculateMD5

        public static final java.lang.String calculateMD5​(byte[] content)
        Parameters:
        content - byte array to be hashed, null returns an empty string
        Returns:
        string with MD5 value
      • getCharacterDataFromElement

        public static java.lang.String getCharacterDataFromElement​(org.w3c.dom.Element e)
        Parameters:
        e - XML DOM Element
        Returns:
        string CharacterData from the Element
      • isPrimitive

        public static boolean isPrimitive​(java.lang.Class<?> clazz)
        Helper for the debug methods below
        Parameters:
        clazz -
        Returns:
        isPrimitive
        See Also:
        Class.isPrimitive()
      • debugList

        public static java.lang.String debugList​(java.util.List<?> in)
        Get a nicely formatted debug string for an arbitrary list, using default toString method on the contained objects.
        Parameters:
        in -
        Returns:
        debug string
      • debug

        public static java.lang.String debug​(java.lang.Object obj)
        Get a detailed debug string for an arbitrary object.
        Parameters:
        obj -
        Returns:
        debug string
      • parseInt

        public static final java.lang.Integer parseInt​(java.lang.String input,
                                                       java.lang.Integer defaultValue)
        Tries to parse an input string into an integer value.
        Parameters:
        input -
        defaultValue -
        Returns:
        If input is null or in case of a parsing error, the defaultValue will be returned. The method will swallow all NumberFormatExceptions.
      • parseFloat

        public static final java.lang.Float parseFloat​(java.lang.String input,
                                                       java.lang.Float defaultValue)
        Tries to parse an input string into a float value.
        Parameters:
        input -
        defaultValue -
        Returns:
        If input is null or in case of a parsing error, the defaultValue will be returned. The method will swallow all NumberFormatExceptions.
      • parseBool

        public static final java.lang.Boolean parseBool​(java.lang.String input,
                                                        java.lang.Boolean defaultValue)
        Tries to parse an input string into a boolean value.
        Parameters:
        input -
        defaultValue -
        Returns:
        Boolean If input is null or in case of a parsing error, the defaultValue will be returned. True|yes|on or 1 or -1 will return true, otherwise false will be returned.
      • getSingleResultOrNull

        public static <T> T getSingleResultOrNull​(javax.persistence.TypedQuery<T> query)
        Retrieving a single record from the a persistence datasource. This is JPA sugar, because JPAs getSingleResult will throw an NoResultException if no data found and not a simple NULL.
        Parameters:
        query -
        Returns:
        an entity of the querid type or null
      • getSystemSetting

        public static java.lang.String getSystemSetting​(java.lang.String name,
                                                        java.lang.String defaultValue)
        Reads a value by key name either from system properties or from system environment (in this order). If not found, the default value is returned.
        Parameters:
        name -
        defaultValue -
        Returns:
        settings string or null
        See Also:
        System.getProperty(String), System.getenv(String)
      • getSystemSetting

        public static boolean getSystemSetting​(java.lang.String name,
                                               boolean defaultValue)
        Reads a boolean value by key name either from system properties or from system environment (in this order). If not found, the default value is returned.
        Parameters:
        name -
        defaultValue -
        Returns:
        settings boolean or false
        See Also:
        getSystemSetting(String, String), parseBool(String, Boolean)
      • getSystemSetting

        public static int getSystemSetting​(java.lang.String name,
                                           int defaultValue)
        Reads a numeric value by key name either from system properties or from system environment (in this order). If not found, the default value is returned.
        Parameters:
        name -
        defaultValue -
        Returns:
        settings number or 0
        See Also:
        getSystemSetting(String, String), parseInt(String, Integer)
      • getSystemSetting

        public static int getSystemSetting​(java.lang.String name,
                                           int defaultValue,
                                           int min,
                                           int max)
        Reads a numeric value by key name either from system properties or from system environment (in this order). If not found or if the value is not in the range [min..max], the default value is returned.
        Parameters:
        name -
        defaultValue -
        min - minimum allowed value
        max - maximum allowed value
        Returns:
        settings number or 0
        See Also:
        getSystemSetting(String, int)
      • parseDateTime

        public static java.util.Date parseDateTime​(java.lang.String dateString)
        Parses a string according to ISO 8601 date (also xml:dateTime) (e.g. 2014-02-18T14:06:08+0100)
        Parameters:
        dateString -
        Returns:
        return Date or null (if string cannot be parsed)
      • parseSimpleDate

        public static java.util.Date parseSimpleDate​(java.lang.String dateString)
        Parses a string according to American rules (e.g. 12/24/1970 12:13:14)
        Parameters:
        dateString -
        Returns:
        return Date or null (if string cannot be parsed)
      • timeStampToDate

        public static java.util.Date timeStampToDate​(java.lang.String dateString)
        Converts timestamp to java.util.Date
        Parameters:
        dateString -
        Returns:
      • formatDateTime

        public static java.lang.String formatDateTime​(java.util.Date dateTime)
        Formats a date according to ISO 8601 date (also xml:dateTime) (e.g. 2004-02-12T15:19:21+00:00).
        Parameters:
        dateTime - if null current time will be used
        Returns:
        DateTime as String
      • formatSimpleDate

        public static java.lang.String formatSimpleDate​(java.util.Date dateTime)
        Formats a date for ISON (e.g. 2004-02-12 15:19)
        Parameters:
        dateTime - if null current time will be used
        Returns:
        DateTime as String
      • getStackTrace

        public static java.lang.String getStackTrace​(java.lang.Throwable e)
        returns the stack trace of an exception as a string
        Parameters:
        e -
        Returns:
        string representation of stack trace
      • logStackTrace

        public static void logStackTrace​(org.slf4j.Logger myLogger,
                                         java.lang.Throwable e)
        Logs the stack trace to the debug log
        Parameters:
        myLogger -
        e -
      • convertNanoToMilliseconds

        public static final double convertNanoToMilliseconds​(long nanoSeconds)
        Convert nano seconds to milliseconds and return as double
        Parameters:
        nanoSeconds - 1,000,000,000 part of a second
        Returns:
        msec as double
      • getFileIntoByteArray

        public static byte[] getFileIntoByteArray​(java.io.File file)
        Reads a file and returns it as byte array.
        Parameters:
        file -
        Returns:
        bytes read or null in case of IOException
      • calculateRepositoryRoot

        public static final java.lang.String calculateRepositoryRoot​(java.lang.String pluginName,
                                                                     java.lang.String client)
        Calculate the absolute path of the repository root for a given Plugin or domain (such as "global") and client name. The absolute path is setup according to this rule: Constants.PLUGIN_CONFIG_PATH+"/" + pluginName + "/" + client + "/"
        Parameters:
        pluginName - name of the owner Plugin (or domain in case of global configurations)
        client - name of the client
        Returns:
        the absolute path of the repository root
      • calculateRepositoryPath

        public static final java.lang.String calculateRepositoryPath​(java.lang.String pluginName,
                                                                     java.lang.String client,
                                                                     java.lang.String nodeName)
        Calculate the absolute path of a node for a given Plugin or domain (such as "global"), client name and node name. The absolute path is setup according to this rule: Constants.PLUGIN_CONFIG_PATH+"/" + pluginName + "/" + client + "/" + nodeName
        Parameters:
        pluginName - name of the owner Plugin (or domain in case of global configurations)
        client - name of the client
        nodeName -
        Returns:
        the absolute path for a repository node
      • parsePluginName

        public static final java.lang.String parsePluginName​(java.lang.String repositoryPath)
        Parse the Plugin name from a absolute path in the repository. This function does not guarantee that the name returned denotes a valid Plugin, it just returns the portion of the path reserved for the Plugin name, if the path was calculated according to the specification or using the calculateRepositoryPath(java.lang.String, java.lang.String, java.lang.String) method.
        Parameters:
        repositoryPath -
        Returns:
        name of the Plugin or domain (such as "global")
      • parseClientName

        public static final java.lang.String parseClientName​(java.lang.String repositoryPath)
        Parse the client name from a absolute path in the repository. This function does not guarantee that a valid name of a client is returned, it just returns the portion of the path reserved for the client name, if the path was calculated according to the specification or using the calculateRepositoryPath(java.lang.String, java.lang.String, java.lang.String) method.
        Parameters:
        repositoryPath -
        Returns:
        name of the client or null if no client name could be found
      • isSuperSetOf

        public static boolean isSuperSetOf​(java.lang.Class<?>[] classes,
                                           java.lang.Class<?>[] otherClasses)
        Return true, if each of classes is assignable from or equals to any of otherClasses, or, in other words, otherClasses is more specific than classes.
        We use this method to check, if a Object representation of a XML document must be revalidated when assigning a new set of custom classes.
        Parameters:
        classes -
        otherClasses -
        Returns:
        true, if classes is superset of other classes, false otherwise
      • getParameterTypeArguments

        public static java.lang.reflect.Type[] getParameterTypeArguments​(java.lang.reflect.Type type)
        Returns the type arguments (in order of declaration) of a Java parameter type or null, if the type provided is no generic type.
        Parameters:
        type -
        Returns:
      • getParameterTypeArguments

        public static java.lang.reflect.Type[] getParameterTypeArguments​(java.lang.reflect.Method method,
                                                                         int parameterIndex)
        Get the type arguments (in order of declaration) of a parameter at a certain index or null, if this parameter is of no generic type.
        Parameters:
        method -
        parameterIndex -
        Returns:
      • getFirstParameterTypeArgument

        public static java.lang.Class<?> getFirstParameterTypeArgument​(java.lang.reflect.Method method,
                                                                       int parameterIndex)
        Get the Class of the first type argument of a parameter at a certain index.
        In PubServer environment we generally have only one type argument (e.g. List<Bucket>), so this method will do for most cases.
        If the parameter provided is of no generic type, null will be returned.
        Parameters:
        method -
        parameterIndex -
        Returns:
      • getReturnTypeArgument

        public static java.lang.Class<?> getReturnTypeArgument​(java.lang.reflect.Method method)
        Get the Class of the first type argument of a method's return type.
        In PubServer environment we generally have only one type argument (e.g. List<Bucket>), so this method will do for most cases.
        If the return type of this method isn't generic, null will be returned.
        Parameters:
        method -
        Returns:
      • getReturnTypeArguments

        public static java.lang.Class<?>[] getReturnTypeArguments​(java.lang.reflect.Method method)

        Gets the classes of the type arguments of a method return type.

        If the return type of this method isn't generic, null will be returned.

        Parameters:
        method - the method
        Returns:
        type arguments of return type or null, if not a generic type.
      • getTypeArguments

        public static java.lang.Class<?>[] getTypeArguments​(java.lang.reflect.Type type)
      • getTypeString

        public static java.lang.String getTypeString​(java.lang.Class<?> clazz)
      • isRootMetaData

        public static boolean isRootMetaData​(Entity checkedEntity,
                                             java.util.Map<java.lang.String,​Entity> allEntities)
      • isRootMetaDataForPageTemplate

        public static boolean isRootMetaDataForPageTemplate​(Entity checkedEntity,
                                                            java.util.Map<java.lang.String,​Entity> allEntities)
      • isRootMetaDataForPageTemplateElement

        public static boolean isRootMetaDataForPageTemplateElement​(Entity checkedEntity,
                                                                   java.util.Map<java.lang.String,​Entity> allEntities)
      • isRootMetaDataForRealGridElement

        public static boolean isRootMetaDataForRealGridElement​(Entity checkedEntity,
                                                               java.util.Map<java.lang.String,​Entity> allEntities)
      • isRootMetaDataForVirtualEntities

        public static boolean isRootMetaDataForVirtualEntities​(Entity checkedEntity,
                                                               java.util.Map<java.lang.String,​Entity> allEntities)
      • isRootMetaDataByTag

        public static boolean isRootMetaDataByTag​(Entity checkedEntity,
                                                  java.util.Map<java.lang.String,​Entity> allEntities,
                                                  PluginConfigTag tag)
      • getHostName

        public static java.lang.String getHostName()
        Retrieve the name of the local machine. The following properties are checked in fixed sequence:
        • Application server hostName property
        • environment COMPUTERNAME property
        • environment HOSTNAME property
        • loopback adapter host address
        Returns:
      • convertToASCIIString

        public static java.lang.String convertToASCIIString​(java.lang.String strToConvert)
        Convert String to ASCIIString, needed for downloading files witch special characters
        Parameters:
        strToConvert - The string that must be converted to ASCII
        Returns:
        String converted String if it possible, if not normalized String
      • compress

        public static byte[] compress​(byte[] data)
                               throws java.io.IOException
        Compress a byte array using Deflate algorithm. Use decompress(byte[]) to uncompress.

        It is preferrable to use a streaming implementation.

        Parameters:
        data - byte array to compress (cannot be empty)
        Returns:
        compressed data
        Throws:
        java.io.IOException
      • decompress

        public static byte[] decompress​(byte[] data)
                                 throws java.io.IOException
        Uncompress a byte array using Inflate algorithm. Use compress(byte[]) to compress.
        Parameters:
        data - byte array to decompress
        Returns:
        uncompressed data
        Throws:
        java.io.IOException
      • compressToBase64

        public static java.lang.String compressToBase64​(byte[] data)
                                                 throws java.io.IOException
        Compress a byte array using Deflate algorithm. Use #decompressFromBase64(byte[]) to uncompress.
        Parameters:
        data - byte array to compress (cannot be empty)
        Returns:
        compressed data
        Throws:
        java.io.IOException
      • decompressFromBase64

        public static byte[] decompressFromBase64​(java.lang.String data)
                                           throws java.io.IOException
        Uncompresses a base64 encoded byte array using Inflate algorithm. Use compressToBase64(byte[]) to compress.
        Parameters:
        data - base64 string to decompress (cannot be empty)
        Returns:
        uncompressed data
        Throws:
        java.io.IOException
      • getFingerprintOfGlassfishInstance

        public static java.lang.String getFingerprintOfGlassfishInstance()
                                                                  throws java.io.IOException,
                                                                         java.security.KeyStoreException,
                                                                         java.security.cert.CertificateException,
                                                                         java.security.NoSuchAlgorithmException
        Throws:
        java.io.IOException
        java.security.KeyStoreException
        java.security.cert.CertificateException
        java.security.NoSuchAlgorithmException