Class PubServerUtils

java.lang.Object
com.priint.pubserver.util.PubServerUtils

public class PubServerUtils extends 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 Details

    • calculateSHA256

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

      public static final 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 String calculateMD5(String content)
      Parameters:
      content - string to be hashed, null returns an empty string
      Returns:
      string with MD5 value
    • calculateMD5

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

      public static String getCharacterDataFromElement(Element e)
      Parameters:
      e - XML DOM Element
      Returns:
      string CharacterData from the Element
    • isPrimitive

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

      public static String debugList(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 String debug(Object obj)
      Get a detailed debug string for an arbitrary object.
      Parameters:
      obj -
      Returns:
      debug string
    • parseInt

      public static final Integer parseInt(String input, 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 Float parseFloat(String input, 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 Boolean parseBool(String input, 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(jakarta.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 String getSystemSetting(String name, 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:
    • getSystemSetting

      public static boolean getSystemSetting(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

      public static int getSystemSetting(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

      public static int getSystemSetting(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:
    • parseDateTime

      public static Date parseDateTime(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 Date parseSimpleDate(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 Date timeStampToDate(String dateString)
      Converts timestamp to java.util.Date
      Parameters:
      dateString -
      Returns:
    • formatDateTime

      public static String formatDateTime(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 String formatSimpleDate(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 String getStackTrace(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, 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(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 String calculateRepositoryRoot(String pluginName, 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 String calculateRepositoryPath(String pluginName, String client, 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 String parsePluginName(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 String parseClientName(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(Class<?>[] classes, 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 Type[] getParameterTypeArguments(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 Type[] getParameterTypeArguments(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 Class<?> getFirstParameterTypeArgument(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 Class<?> getReturnTypeArgument(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 Class<?>[] getReturnTypeArguments(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 Class<?>[] getTypeArguments(Type type)
    • getTypeString

      public static String getTypeString(Class<?> clazz)
    • isRootMetaData

      public static boolean isRootMetaData(Entity checkedEntity, Map<String,Entity> allEntities)
    • isRootMetaDataForPageTemplate

      public static boolean isRootMetaDataForPageTemplate(Entity checkedEntity, Map<String,Entity> allEntities)
    • isRootMetaDataForPageTemplateElement

      public static boolean isRootMetaDataForPageTemplateElement(Entity checkedEntity, Map<String,Entity> allEntities)
    • isRootMetaDataForRealGridElement

      public static boolean isRootMetaDataForRealGridElement(Entity checkedEntity, Map<String,Entity> allEntities)
    • isRootMetaDataForVirtualEntities

      public static boolean isRootMetaDataForVirtualEntities(Entity checkedEntity, Map<String,Entity> allEntities)
    • isRootMetaDataByTag

      public static boolean isRootMetaDataByTag(Entity checkedEntity, Map<String,Entity> allEntities, PluginConfigTag tag)
    • getHostName

      public static 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 String convertToASCIIString(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 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:
      IOException
    • decompress

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

      public static String compressToBase64(byte[] data) throws 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:
      IOException
    • decompressFromBase64

      public static byte[] decompressFromBase64(String data) throws 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:
      IOException
    • getFingerprintOfGlassfishInstance

      public static String getFingerprintOfGlassfishInstance() throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException
      Throws:
      IOException
      KeyStoreException
      CertificateException
      NoSuchAlgorithmException