com.flexive.shared
Class FxSharedUtils

java.lang.Object
  extended by com.flexive.shared.FxSharedUtils

public final class FxSharedUtils
extends java.lang.Object

Flexive shared utility functions.

Author:
Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)

Nested Class Summary
static class FxSharedUtils.AssignmentPositionSorter
          Comparator for sorting Assignments according to their position.
(package private) static class FxSharedUtils.AsyncStreamBuffer
          Helper thread to asynchronously read and buffer an InputStream
static interface FxSharedUtils.ParameterMapper<K,V>
          Maps keys to values.
static class FxSharedUtils.ProcessResult
          Helperclass holding the result of the executeCommand method
static class FxSharedUtils.SelectableObjectSorter
          Comparator for sorting SelectableObjectWithName instances by ID.
 
Field Summary
static java.lang.String COOKIE_FORCE_TEST_DIVISION
          Browser tests set this cookie to force using the test division instead of the actual division defined by the URL domain.
static java.security.MessageDigest digest
           
static java.lang.String SHARED_BUNDLE
          Shared message resources bundle
static boolean USE_JDK6_EXTENSION
          Are JDK 6+ extensions allowed to be run on the current VM?
static boolean WINDOWS
           
static java.lang.String XPATH_ENCODEDSLASH
          The character(s) representing a "xpath slash" (/) in a public URL.
 
Method Summary
static void checkParameterEmpty(java.lang.Object value, java.lang.String parameterName)
          Check if the given value is empty (empty string or null for String objects, empty collection, null for other objects) and throw an exception if empty.
static void checkParameterMultilang(FxValue value, java.lang.String paramName)
          Check if the given parameter is multilingual and throw an exception if not
static void checkParameterNull(java.lang.Object value, java.lang.String parameterName)
          Check if the given value is empty (empty string or null for String objects, empty collection, null for other objects) and throw an exception if empty.
static java.lang.String decodeXPath(java.lang.String escapedXPath)
          Decodes a previously escaped XPath.
static java.lang.String escapePath(java.lang.String path)
          Escape a path for usage on the current operating systems shell
static java.lang.String escapeXPath(java.lang.String xpath)
          Escapes the given XPath for use in a public URI.
static FxSharedUtils.ProcessResult executeCommand(java.lang.String command, java.lang.String... arguments)
          Execute a command on the operating system
static
<K,V> V
get(java.util.Map<K,V> map, K key, V defaultValue)
          Returns map.get(key) if key exists, defaultValue otherwise.
static java.lang.String getBuildDate()
          Get the date flexive was compiled
static java.lang.String getBuildNumber()
          Get the subversion build number
static java.lang.String getBuildUser()
          Get the name of the user that built flexive
static java.lang.String getBundledGroovyVersion()
          Get the version of the bundled groovy runtime
static byte[] getBytes(java.lang.String s)
          Returns the UTF-8 byte representation of the given string.
static java.text.Collator getCollator()
          Returns a collator for the calling user's locale.
static int getColumnIndex(java.util.List<java.lang.String> columnNames, java.lang.String name)
          Return the index of the given column name.
static int getColumnIndex(java.lang.String[] columnNames, java.lang.String name)
          Return the index of the given column name.
static long getDBVersion()
          Get the database version
static FxDropApplication getDropApplication(java.lang.String name)
          Returns the drop application with the given name.
static java.util.List<FxDropApplication> getDropApplications()
          Get a list of all installed and deployed drops.
static java.util.List<java.lang.String> getDrops()
          Get a list of all installed and deployed drops
static java.lang.String getEmptyResultMessage()
          Returns the localized "empty" message for empty result fields
static FxString getEnumLabel(java.lang.Enum<?> value, java.lang.Object... args)
          Returns the localized label for the given enum value.
static java.util.List<java.lang.String> getEnumNames(java.util.Collection<? extends java.lang.Enum> values)
          Extracts the names of the given enum elements and returns them as string.
static java.lang.String getFlexiveEdition()
          Get the name of this flexive edition
static java.lang.String getFlexiveEditionFull()
          Get the name of this flexive edition with the product name
static java.lang.String getFlexiveVersion()
          Get the flexive version
static java.lang.String getHeader()
          Get the default html header title
static java.lang.String getLocalizedMessage(java.lang.String resourceBundle, long localeId, java.lang.String localeIso, java.lang.String key, java.lang.Object... args)
          Get the localized message for a given language code and ISO
static
<K,V> java.util.Map<K,V>
getMappedFunction(FxSharedUtils.ParameterMapper<K,V> mapper)
          Projects a single-parameter function on a hashmap.
static FxString getMessage(java.lang.String resourceBundle, java.lang.String key, java.lang.Object... args)
          Returns a multilingual FxString with all translations for the given property key.
static long[] getSelectableObjectIds(java.util.Collection<? extends SelectableObject> values)
          Extract the unique IDs of the given SelectableObject collection.
static java.util.List<StepDefinition> getUsedStepDefinitions(java.util.List<? extends Step> steps, java.util.List<? extends StepDefinition> stepDefinitions)
          Returns a list of all used step definitions for the given steps
static java.lang.String hashPassword(long accountId, java.lang.String password)
          Compute the hash of the given flexive password.
static int indexOfSelectableObject(java.util.List<? extends SelectableObject> values, long id)
          Returns the index of the SelectableObject with the given ID, or -1 if none was found.
static boolean isGroovyScript(java.lang.String name)
          Is the script (most likely) a groovy script?
static boolean isQuoted(java.lang.String value, char quoteChar)
          Returns true if the given string value is quoted with the given character (e.g.
static java.lang.String loadFile(java.io.File file)
          Load the contents of a file, returning it as a String.
static java.lang.String loadFromInputStream(java.io.InputStream in)
          Load a String from an InputStream (until end of stream)
static java.lang.String loadFromInputStream(java.io.InputStream in, int length)
          Load a String from an InputStream (until end of stream)
static java.lang.String lookupResource(java.lang.String resourceBundle, java.lang.String key, java.lang.String localeIso)
          Try to find a localized resource messages
static java.lang.String sha1Hash(byte[] bytes)
          Creates a SHA-1 hash for the given data and returns it in hexadecimal string encoding.
static java.lang.String[] splitLiterals(java.lang.String text)
          Splits the given comma-separated text.
static java.lang.String[] splitLiterals(java.lang.String text, char[] stringDelims, char separator)
          Splits the given text using separator.
static boolean storeFile(java.lang.String contents, java.io.File file)
          Rather primitive "write String to file" helper, returns false if failed
static java.lang.String stripQuotes(java.lang.String value, char quoteChar)
          Strips the quotes from the given string if it is quoted, otherwise it returns the input value itself.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SHARED_BUNDLE

public static final java.lang.String SHARED_BUNDLE
Shared message resources bundle

See Also:
Constant Field Values

XPATH_ENCODEDSLASH

public static final java.lang.String XPATH_ENCODEDSLASH
The character(s) representing a "xpath slash" (/) in a public URL.

See Also:
Constant Field Values

COOKIE_FORCE_TEST_DIVISION

public static final java.lang.String COOKIE_FORCE_TEST_DIVISION
Browser tests set this cookie to force using the test division instead of the actual division defined by the URL domain. TODO: security?

See Also:
Constant Field Values

digest

public static java.security.MessageDigest digest

USE_JDK6_EXTENSION

public static final boolean USE_JDK6_EXTENSION
Are JDK 6+ extensions allowed to be run on the current VM?


WINDOWS

public static final boolean WINDOWS
Method Detail

getDrops

public static java.util.List<java.lang.String> getDrops()
Get a list of all installed and deployed drops

Returns:
list of all installed and deployed drops

getDropApplications

public static java.util.List<FxDropApplication> getDropApplications()
Get a list of all installed and deployed drops.

Returns:
a list of all installed and deployed drops.
Since:
3.0.2

getDropApplication

public static FxDropApplication getDropApplication(java.lang.String name)
Returns the drop application with the given name.

Parameters:
name - the application name
Returns:
the drop application with the given name.
Since:
3.0.2

getColumnIndex

public static int getColumnIndex(java.lang.String[] columnNames,
                                 java.lang.String name)
Return the index of the given column name. If name has no prefix (e.g. "co."), then only a suffix match is performed (e.g. "name" matches "co.name" or "abc.name", whichever comes first.)

Parameters:
columnNames - all column names to be searched
name - the requested column name
Returns:
the 1-based index of the given column, or -1 if it does not exist

getColumnIndex

public static int getColumnIndex(java.util.List<java.lang.String> columnNames,
                                 java.lang.String name)
Return the index of the given column name. If name has no prefix (e.g. "co."), then only a suffix match is performed (e.g. "name" matches "co.name" or "abc.name", whichever comes first.)

Parameters:
columnNames - all column names to be searched
name - the requested column name
Returns:
the 1-based index of the given column, or -1 if it does not exist

hashPassword

public static java.lang.String hashPassword(long accountId,
                                            java.lang.String password)
Compute the hash of the given flexive password.

Parameters:
accountId - the user account ID
password - the cleartext password
Returns:
a hashed password

getCollator

public static java.text.Collator getCollator()
Returns a collator for the calling user's locale.

Returns:
a collator for the calling user's locale.

isGroovyScript

public static boolean isGroovyScript(java.lang.String name)
Is the script (most likely) a groovy script?

Parameters:
name - script name to check
Returns:
if this script could be a groovy script

checkParameterMultilang

public static void checkParameterMultilang(FxValue value,
                                           java.lang.String paramName)
Check if the given parameter is multilingual and throw an exception if not

Parameters:
value - the value to check
paramName - name of the parameter

sha1Hash

public static java.lang.String sha1Hash(byte[] bytes)
                                 throws java.security.NoSuchAlgorithmException
Creates a SHA-1 hash for the given data and returns it in hexadecimal string encoding.

Parameters:
bytes - data to be hashed
Returns:
hex-encoded hash
Throws:
java.security.NoSuchAlgorithmException - if the SHA-1 provider does not exist

executeCommand

public static FxSharedUtils.ProcessResult executeCommand(java.lang.String command,
                                                         java.lang.String... arguments)
Execute a command on the operating system

Parameters:
command - name of the command
arguments - arguments to pass to the command (one argument per String!)
Returns:
result

loadFile

public static java.lang.String loadFile(java.io.File file)
Load the contents of a file, returning it as a String. This method should only be used when really necessary since no real error handling is performed!!!

Parameters:
file - the File to load
Returns:
file contents

loadFromInputStream

public static java.lang.String loadFromInputStream(java.io.InputStream in)
Load a String from an InputStream (until end of stream)

Parameters:
in - InputStream
Returns:
the input stream contents, or an empty string if in was null.
Since:
3.0.2

loadFromInputStream

public static java.lang.String loadFromInputStream(java.io.InputStream in,
                                                   int length)
Load a String from an InputStream (until end of stream)

Parameters:
in - InputStream
length - length of the string if > -1 (NOT number of bytes to read!)
Returns:
the input stream contents, or an empty string if in was null.

storeFile

public static boolean storeFile(java.lang.String contents,
                                java.io.File file)
Rather primitive "write String to file" helper, returns false if failed

Parameters:
contents - the String to store
file - the file, if existing it will be overwritten
Returns:
if successful

getFlexiveVersion

public static java.lang.String getFlexiveVersion()
Get the flexive version

Returns:
flexive version

getBuildNumber

public static java.lang.String getBuildNumber()
Get the subversion build number

Returns:
subversion build number

getDBVersion

public static long getDBVersion()
Get the database version

Returns:
database version

getBuildDate

public static java.lang.String getBuildDate()
Get the date flexive was compiled

Returns:
compile date

getFlexiveEdition

public static java.lang.String getFlexiveEdition()
Get the name of this flexive edition

Returns:
flexive edition

getFlexiveEditionFull

public static java.lang.String getFlexiveEditionFull()
Get the name of this flexive edition with the product name

Returns:
flexive edition with product name

getBuildUser

public static java.lang.String getBuildUser()
Get the name of the user that built flexive

Returns:
build user

getHeader

public static java.lang.String getHeader()
Get the default html header title

Returns:
html header title

getBundledGroovyVersion

public static java.lang.String getBundledGroovyVersion()
Get the version of the bundled groovy runtime

Returns:
version of the bundled groovy runtime

getEmptyResultMessage

public static java.lang.String getEmptyResultMessage()
Returns the localized "empty" message for empty result fields

Returns:
the localized "empty" message for empty result fields

checkParameterNull

public static void checkParameterNull(java.lang.Object value,
                                      java.lang.String parameterName)
Check if the given value is empty (empty string or null for String objects, empty collection, null for other objects) and throw an exception if empty.

Parameters:
value - value to check
parameterName - name of the value (for the exception)

checkParameterEmpty

public static void checkParameterEmpty(java.lang.Object value,
                                       java.lang.String parameterName)
Check if the given value is empty (empty string or null for String objects, empty collection, null for other objects) and throw an exception if empty.

Parameters:
value - value to check
parameterName - name of the value (for the exception)

lookupResource

public static java.lang.String lookupResource(java.lang.String resourceBundle,
                                              java.lang.String key,
                                              java.lang.String localeIso)
Try to find a localized resource messages

Parameters:
resourceBundle - the name of the resource bundle to retrieve the message from
key - resource key
localeIso - locale of the resource bundle
Returns:
resource from a localized bundle

getLocalizedMessage

public static java.lang.String getLocalizedMessage(java.lang.String resourceBundle,
                                                   long localeId,
                                                   java.lang.String localeIso,
                                                   java.lang.String key,
                                                   java.lang.Object... args)
Get the localized message for a given language code and ISO

Parameters:
resourceBundle - the resource bundle to use
localeId - used locale if args contain FxString instances
localeIso - ISO code of the requested locale
key - the key in the resource bundle
args - arguments to replace in the message ({n})
Returns:
localized message

getMessage

public static FxString getMessage(java.lang.String resourceBundle,
                                  java.lang.String key,
                                  java.lang.Object... args)
Returns a multilingual FxString with all translations for the given property key.

Parameters:
resourceBundle - the resource bundle to be used
key - the message key
args - optional parameters to be replaced in the property translations
Returns:
a multilingual FxString with all translations for the given property key.

getEnumLabel

public static FxString getEnumLabel(java.lang.Enum<?> value,
                                    java.lang.Object... args)
Returns the localized label for the given enum value. The enum translations are stored in FxSharedMessages.properties and are standardized as FQCN.value, e.g. com.flexive.shared.search.query.ValueComparator.LIKE.

Parameters:
value - the enum value to be translated
args - optional arguments to be replaced in the localized messages
Returns:
the localized label for the given enum value

getUsedStepDefinitions

public static java.util.List<StepDefinition> getUsedStepDefinitions(java.util.List<? extends Step> steps,
                                                                    java.util.List<? extends StepDefinition> stepDefinitions)
Returns a list of all used step definitions for the given steps

Parameters:
steps - list of steps to be examined
stepDefinitions - all defined step definitions
Returns:
a list of all used step definitions for this workflow

splitLiterals

public static java.lang.String[] splitLiterals(java.lang.String text,
                                               char[] stringDelims,
                                               char separator)
Splits the given text using separator. String literals are supported, e.g. abc,def yields two elements, but 'abc,def' yields one (stringDelims = ['\''], separator = ',').

Parameters:
text - the text to be splitted
stringDelims - delimiters for literal string values, usually ' and "
separator - separator between tokens
Returns:
split string

splitLiterals

public static java.lang.String[] splitLiterals(java.lang.String text)
Splits the given comma-separated text. String literals are supported, e.g. abc,def yields two elements, but 'abc,def' yields one.

Parameters:
text - the text to be splitted
Returns:
split string

getMappedFunction

public static <K,V> java.util.Map<K,V> getMappedFunction(FxSharedUtils.ParameterMapper<K,V> mapper)
Projects a single-parameter function on a hashmap. Useful for calling parameterized functions from JSF-EL. Values returned by the mapper are cached in the hash map.

Parameters:
mapper - the parameter mapper wrapping the function to be called
Returns:
a hashmap projected on the given parameter mapper

escapePath

public static java.lang.String escapePath(java.lang.String path)
Escape a path for usage on the current operating systems shell

Parameters:
path - path to escape
Returns:
escaped path

escapeXPath

public static java.lang.String escapeXPath(java.lang.String xpath)
Escapes the given XPath for use in a public URI.

Parameters:
xpath - the xpath to be escaped
Returns:
the given XPath for use in a public URI.
See Also:
decodeXPath(String)

decodeXPath

public static java.lang.String decodeXPath(java.lang.String escapedXPath)
Decodes a previously escaped XPath.

Parameters:
escapedXPath - the escaped XPath
Returns:
the decoded XPath
See Also:
escapeXPath(String)

get

public static <K,V> V get(java.util.Map<K,V> map,
                          K key,
                          V defaultValue)
Returns map.get(key) if key exists, defaultValue otherwise.

Parameters:
map - a map
key - the required key
defaultValue - the default value to be returned if key does not exist in map
Returns:
map.get(key) if key exists, defaultValue otherwise.

isQuoted

public static boolean isQuoted(java.lang.String value,
                               char quoteChar)
Returns true if the given string value is quoted with the given character (e.g. 'value').

Parameters:
value - the string value to be checked
quoteChar - the quote character, for example '
Returns:
true if the given string value is quoted with the given character (e.g. 'value').

stripQuotes

public static java.lang.String stripQuotes(java.lang.String value,
                                           char quoteChar)
Strips the quotes from the given string if it is quoted, otherwise it returns the input value itself.

Parameters:
value - the value to be "unquoted"
quoteChar - the quote character, for example '
Returns:
the unquoted string, or value, if it was not quoted

getBytes

public static byte[] getBytes(java.lang.String s)
Returns the UTF-8 byte representation of the given string. Use this instead of String.getBytes(), since the latter will fail if the system locale is not UTF-8.

Parameters:
s - the string to be processed
Returns:
the UTF-8 byte representation of the given string

getEnumNames

public static java.util.List<java.lang.String> getEnumNames(java.util.Collection<? extends java.lang.Enum> values)
Extracts the names of the given enum elements and returns them as string. Useful if the toString() method of the Enum class was overwritten.

Parameters:
values - the enum values
Returns:
the names of the given enum elements

getSelectableObjectIds

public static long[] getSelectableObjectIds(java.util.Collection<? extends SelectableObject> values)
Extract the unique IDs of the given SelectableObject collection.

Parameters:
values - the input values
Returns:
the IDs of the input values

indexOfSelectableObject

public static int indexOfSelectableObject(java.util.List<? extends SelectableObject> values,
                                          long id)
Returns the index of the SelectableObject with the given ID, or -1 if none was found.

Parameters:
values - the values to be examined
id - the target ID
Returns:
the index of the SelectableObject with the given ID, or -1 if none was found.