Package com.priint.pubserver.util
Class PubServerUtils
java.lang.Object
com.priint.pubserver.util.PubServerUtils
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumList of supported hashing types -
Method Summary
Modifier and TypeMethodDescriptionstatic final StringcalculateMD5(byte[] content) static final StringcalculateMD5(String content) static final StringcalculateRepositoryPath(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.static final StringcalculateRepositoryRoot(String pluginName, String client) Calculate the absolute path of the repository root for a given Plugin or domain (such as "global") and client name.static final StringcalculateSHA256(byte[] content) static final StringcalculateSHA256(String content) static byte[]compress(byte[] data) Compress a byte array using Deflate algorithm.static StringcompressToBase64(byte[] data) Compress a byte array using Deflate algorithm.static final doubleconvertNanoToMilliseconds(long nanoSeconds) Convert nano seconds to milliseconds and return as doublestatic StringconvertToASCIIString(String strToConvert) Convert String to ASCIIString, needed for downloading files witch special charactersstatic StringGet a detailed debug string for an arbitrary object.static StringGet 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(String data) Uncompresses a base64 encoded byte array using Inflate algorithm.static StringformatDateTime(Date dateTime) Formats a date according to ISO 8601 date (also xml:dateTime) (e.g.static StringformatSimpleDate(Date dateTime) Formats a date for ISON (e.g.static Stringstatic byte[]getFileIntoByteArray(File file) Reads a file and returns it as byte array.static Stringstatic 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.static StringRetrieve the name of the local machine.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.static 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 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.static Class<?>[]getReturnTypeArguments(Method method) Gets the classes of the type arguments of a method return type.static <T> TgetSingleResultOrNull(jakarta.persistence.TypedQuery<T> query) Retrieving a single record from the a persistence datasource.static Stringreturns the stack trace of an exception as a stringstatic booleangetSystemSetting(String name, boolean defaultValue) Reads a boolean value by key name either from system properties or from system environment (in this order).static intgetSystemSetting(String name, int defaultValue) Reads a numeric value by key name either from system properties or from system environment (in this order).static intgetSystemSetting(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 StringgetSystemSetting(String name, String defaultValue) Reads a value by key name either from system properties or from system environment (in this order).static Class<?>[]getTypeArguments(Type type) static StringgetTypeString(Class<?> clazz) static booleanisPrimitive(Class<?> clazz) Helper for the debug methods belowstatic booleanisRootMetaData(Entity checkedEntity, Map<String, Entity> allEntities) static booleanisRootMetaDataByTag(Entity checkedEntity, Map<String, Entity> allEntities, PluginConfigTag tag) static booleanisRootMetaDataForPageTemplate(Entity checkedEntity, Map<String, Entity> allEntities) static booleanisRootMetaDataForPageTemplateElement(Entity checkedEntity, Map<String, Entity> allEntities) static booleanisRootMetaDataForRealGridElement(Entity checkedEntity, Map<String, Entity> allEntities) static booleanisRootMetaDataForVirtualEntities(Entity checkedEntity, Map<String, Entity> allEntities) static booleanisSuperSetOf(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.static voidlogStackTrace(org.slf4j.Logger myLogger, Throwable e) Logs the stack trace to the debug logstatic final BooleanTries to parse an input string into a boolean value.static final StringparseClientName(String repositoryPath) Parse the client name from a absolute path in the repository.static DateparseDateTime(String dateString) Parses a string according to ISO 8601 date (also xml:dateTime) (e.g.static final FloatparseFloat(String input, Float defaultValue) Tries to parse an input string into a float value.static final IntegerTries to parse an input string into an integer value.static final StringparsePluginName(String repositoryPath) Parse the Plugin name from a absolute path in the repository.static DateparseSimpleDate(String dateString) Parses a string according to American rules (e.g.static DatetimeStampToDate(String dateString) Converts timestamp to java.util.Date
-
Method Details
-
calculateSHA256
- Parameters:
content- string to be hashed, null returns an empty string- Returns:
- string with SHA256 value
-
calculateSHA256
- Parameters:
content- byte array to be hashed, null returns an empty string- Returns:
- string with SHA256 value
-
calculateMD5
- Parameters:
content- string to be hashed, null returns an empty string- Returns:
- string with MD5 value
-
calculateMD5
- Parameters:
content- byte array to be hashed, null returns an empty string- Returns:
- string with MD5 value
-
getCharacterDataFromElement
- Parameters:
e- XML DOM Element- Returns:
- string CharacterData from the Element
-
isPrimitive
Helper for the debug methods below- Parameters:
clazz-- Returns:
- isPrimitive
- See Also:
-
debugList
Get a nicely formatted debug string for an arbitrary list, using default toString method on the contained objects.- Parameters:
in-- Returns:
- debug string
-
debug
Get a detailed debug string for an arbitrary object.- Parameters:
obj-- Returns:
- debug string
-
parseInt
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
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
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
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
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
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
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 valuemax- maximum allowed value- Returns:
- settings number or 0
- See Also:
-
parseDateTime
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
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
Converts timestamp to java.util.Date- Parameters:
dateString-- Returns:
-
formatDateTime
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
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
returns the stack trace of an exception as a string- Parameters:
e-- Returns:
- string representation of stack trace
-
logStackTrace
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
Reads a file and returns it as byte array.- Parameters:
file-- Returns:
- bytes read or null in case of IOException
-
calculateRepositoryRoot
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 clientnodeName-- Returns:
- the absolute path for a repository node
-
parsePluginName
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 thecalculateRepositoryPath(java.lang.String, java.lang.String, java.lang.String)method.- Parameters:
repositoryPath-- Returns:
- name of the Plugin or domain (such as "global")
-
parseClientName
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 thecalculateRepositoryPath(java.lang.String, java.lang.String, java.lang.String)method.- Parameters:
repositoryPath-- Returns:
- name of the client or
nullif no client name could be found
-
isSuperSetOf
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
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
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
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
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
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
-
getTypeString
-
isRootMetaData
-
isRootMetaDataForPageTemplate
-
isRootMetaDataForPageTemplateElement
-
isRootMetaDataForRealGridElement
-
isRootMetaDataForVirtualEntities
-
isRootMetaDataByTag
public static boolean isRootMetaDataByTag(Entity checkedEntity, Map<String, Entity> allEntities, PluginConfigTag tag) -
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
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
Compress a byte array using Deflate algorithm. Usedecompress(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
Uncompress a byte array using Inflate algorithm. Usecompress(byte[])to compress.- Parameters:
data- byte array to decompress- Returns:
- uncompressed data
- Throws:
IOException
-
compressToBase64
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
Uncompresses a base64 encoded byte array using Inflate algorithm. UsecompressToBase64(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
-