org.gcube.contentmanagement.baselayer
Class BaseLayerUtils

java.lang.Object
  extended by org.gcube.contentmanagement.baselayer.BaseLayerUtils
All Implemented Interfaces:
InstantiableManager

public class BaseLayerUtils
extends java.lang.Object
implements InstantiableManager

Convinience tools for converting string representation of BasicStorageHint values to something more usable. Throws corresponding exception if hint was not of expected type.


Field Summary
static int CHAR_NOT_FOUND
          Constant that represents that a character has not been found inside a String.
static java.lang.String HINT_NAME_COPY_TO_MESSAGE
          Deprecated. no longer needed since InMemoryRawContentLocation has been introduced
 
Fields inherited from interface org.gcube.contentmanagement.baselayer.InstantiableManager
MANAGER_INSTANTIATION_METHOD_NAME
 
Constructor Summary
BaseLayerUtils()
           
 
Method Summary
static boolean consumeHint(java.lang.String hintName, BasicStorageHints hints)
          Checks if a hint has been set and if so, if the value corresponds to the boolean true
static boolean containsDownloadCredentials(BasicStorageHints downloadHints)
          Returns whether the hints define download credentials such as username and/or password
static boolean convertHintToBoolean(java.lang.String hintValue, java.lang.String hintName)
          Convenience method to convert the string value of a hint into a boolean value with proper (explicite) failure handling instead of runtime exceptions.
static double convertHintToDouble(java.lang.String hintValue, java.lang.String hintName)
          Convenience method to convert the string value of a hint into a double value with proper (explicite) failure handling instead of runtime exceptions.
static float convertHintToFloat(java.lang.String hintValue, java.lang.String hintName)
          Convenience method to convert the string value of a hint into a float value with proper (explicite) failure handling instead of runtime exceptions.
static int convertHintToInteger(java.lang.String hintValue, java.lang.String hintName)
          Convenience method to convert the string value of a hint into an integer value with proper (explicite) failure handling instead of runtime exceptions.
static long convertHintToLong(java.lang.String hintValue, java.lang.String hintName)
          Convenience method to convert the string value of a hint into a long value with proper (explicite) failure handling instead of runtime exceptions.
static RawContentLocation getExternalLocation(java.lang.String fileLocation, BasicStorageHints hints)
          Generates a location that can be stored as an external location
static RawFileContentManager getInMemoryManager()
          Returns a manager appropriate for in memory handling of content.
static java.lang.String getNewUUID()
          Generates a new unique identifier using a singleton instance of UUIDGen
static int getNumberOfProtocolHandlers()
          Returns the number of registered protocol handlers
static RawFileContentManager getProtocolHandler(int position)
          Returns a protocol handler that is stored at a particular position of the list of handlers.
static RawFileContentManager getProtocolHandlerFor(java.lang.String fileLocation, BasicStorageHints hints)
          Returns a content manager that is able to retrieve a file from the given location.
static java.lang.Object instantiateManager(java.lang.String managerClassName, java.lang.String[] params, java.lang.String managerName, CredentialsProvider provider)
          Convenience method to instatiate a manager using the Java reflection
static RawContentLocation makePseudoUniqueLocation(RawContentLocation location, RawFileContentManager manager, BasicStorageHints hints)
          Generates a location that is to best knowledge unique.
static void registerProtocolHandler(RawFileContentManager manager)
          Registers a content manager as protocol handler by adding it to the end of the list of registered handlers
static void registerProtocolHandler(RawFileContentManager manager, int position)
          Registers a content manager as protocol handler by adding it at a particular position of the list of registered handlers
static void registerProtocolHandler(java.lang.String managerClassName, java.lang.String[] params, java.lang.String handlerName, CredentialsProvider provider)
          Registers a newly instantiated protocol handler for subsequent use
static java.lang.String removeProtocolUsernamePasswordFromURL(java.lang.String url)
          Removes the protocol, username / password information that is contained inside the URL, e.g.
static boolean requestsFor(java.lang.String hintName, BasicStorageHints hints)
          Checks if a hint has been set and if so, if the value corresponds to the boolean true
static void unregisterProtocolHandler(int position)
          Removes a protocol handler at a particular position of the list of registered handlers
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHAR_NOT_FOUND

public static final int CHAR_NOT_FOUND
Constant that represents that a character has not been found inside a String. This is in accordance to the definition of String.indexOf(char c).

See Also:
String.indexOf(int), Constant Field Values

HINT_NAME_COPY_TO_MESSAGE

@Deprecated
public static final java.lang.String HINT_NAME_COPY_TO_MESSAGE
Deprecated. no longer needed since InMemoryRawContentLocation has been introduced
used to signal that some copying is pending

See Also:
Constant Field Values
Constructor Detail

BaseLayerUtils

public BaseLayerUtils()
Method Detail

getInMemoryManager

public static RawFileContentManager getInMemoryManager()
Returns a manager appropriate for in memory handling of content. Hides the particular implementation by providing generic RawFileContentManager.

Returns:
a manager for handling content in main memory

instantiateManager

public static java.lang.Object instantiateManager(java.lang.String managerClassName,
                                                  java.lang.String[] params,
                                                  java.lang.String managerName,
                                                  CredentialsProvider provider)
                                           throws BaseLayerException
Convenience method to instatiate a manager using the Java reflection

Parameters:
managerClassName - the fully qualified class name of the manager
params - all parameters that should be send to the constructor in correct order, e.g. as read from a configuration file
managerName - the name, that the manager should be given
provider - the provider of credentials or null, if no credentials can be provided
Returns:
the newly instantiated manager, e.g. a RawFileContentManager or RelationshipAndPropertyManager
Throws:
BaseLayerException - in any case the instantiation fails, e.g. class unknown

registerProtocolHandler

public static void registerProtocolHandler(java.lang.String managerClassName,
                                           java.lang.String[] params,
                                           java.lang.String handlerName,
                                           CredentialsProvider provider)
                                    throws BaseLayerException
Registers a newly instantiated protocol handler for subsequent use

Parameters:
managerClassName - the fully qualified class name of the manager
params - all parameters that should be send to the constructor in correct order, e.g. as read from a configuration file
handlerName - the name, that the manager should be given
provider - the provider of credentials or null, if no credentials can be provided
Throws:
BaseLayerException - in any case the instantiation fails, e.g. class unknown

registerProtocolHandler

public static void registerProtocolHandler(RawFileContentManager manager)
Registers a content manager as protocol handler by adding it to the end of the list of registered handlers

Parameters:
manager - the manager that should be used to handle a protocol. The manager itself determines, whether it feels responsible for some protocol.
See Also:
RawFileContentManager.handles(String)

registerProtocolHandler

public static void registerProtocolHandler(RawFileContentManager manager,
                                           int position)
Registers a content manager as protocol handler by adding it at a particular position of the list of registered handlers

Parameters:
manager - the manager that should be used to handle a protocol. The manager itself determines, whether it feels responsible for some protocol.
See Also:
RawFileContentManager.handles(String)

unregisterProtocolHandler

public static void unregisterProtocolHandler(int position)
Removes a protocol handler at a particular position of the list of registered handlers

Parameters:
position - the postion of the manager that should no longer be used to handle a protocol.

getProtocolHandler

public static RawFileContentManager getProtocolHandler(int position)
Returns a protocol handler that is stored at a particular position of the list of handlers.

Parameters:
position - the position of the handler
Returns:
the handler that is stored at the given position

getNumberOfProtocolHandlers

public static int getNumberOfProtocolHandlers()
Returns the number of registered protocol handlers

Returns:
the number of registered handlers

getProtocolHandlerFor

public static RawFileContentManager getProtocolHandlerFor(java.lang.String fileLocation,
                                                          BasicStorageHints hints)
                                                   throws ValueNotValidException
Returns a content manager that is able to retrieve a file from the given location.

Parameters:
fileLocation - the location (including protocol) from which the file should be retrieved
hints - all hints that should be taken into account by the manager to retrieve the file
Returns:
the first handler in the list of registered handlers that feels responsible for this location
Throws:
ValueNotValidException - if none of the registered handlers is applicable for the given file location

getExternalLocation

public static RawContentLocation getExternalLocation(java.lang.String fileLocation,
                                                     BasicStorageHints hints)
                                              throws ValueNotValidException
Generates a location that can be stored as an external location

Parameters:
fileLocation - the location of the file
hints - all hints that should be used to retrieve the file later on
Returns:
the location including an appropriate manager for the protocol / location
Throws:
ValueNotValidException - if none of the registered handlers is applicable for the given file location

convertHintToBoolean

public static boolean convertHintToBoolean(java.lang.String hintValue,
                                           java.lang.String hintName)
                                    throws ValueNotValidException
Convenience method to convert the string value of a hint into a boolean value with proper (explicite) failure handling instead of runtime exceptions.

Parameters:
hintValue - the value as a string
Returns:
the boolean value
Throws:
ValueNotValidException - if the string doesn't represent a boolean value
See Also:
Boolean.parseBoolean(String)

convertHintToInteger

public static int convertHintToInteger(java.lang.String hintValue,
                                       java.lang.String hintName)
                                throws ValueNotValidException
Convenience method to convert the string value of a hint into an integer value with proper (explicite) failure handling instead of runtime exceptions.

Parameters:
hintValue - the value as a string
Returns:
the integer value
Throws:
ValueNotValidException - if the string doesn't represent a integer value
See Also:
Integer.parseInt(String)

convertHintToLong

public static long convertHintToLong(java.lang.String hintValue,
                                     java.lang.String hintName)
                              throws ValueNotValidException
Convenience method to convert the string value of a hint into a long value with proper (explicite) failure handling instead of runtime exceptions.

Parameters:
hintValue - the value as a string
Returns:
the long value
Throws:
ValueNotValidException - if the string doesn't represent a long value
See Also:
Long.parseLong(String)

convertHintToFloat

public static float convertHintToFloat(java.lang.String hintValue,
                                       java.lang.String hintName)
                                throws ValueNotValidException
Convenience method to convert the string value of a hint into a float value with proper (explicite) failure handling instead of runtime exceptions.

Parameters:
hintValue - the value as a string
Returns:
the float value
Throws:
ValueNotValidException - if the string doesn't represent a float value
See Also:
Float.parseFloat(String)

convertHintToDouble

public static double convertHintToDouble(java.lang.String hintValue,
                                         java.lang.String hintName)
                                  throws ValueNotValidException
Convenience method to convert the string value of a hint into a double value with proper (explicite) failure handling instead of runtime exceptions.

Parameters:
hintValue - the value as a string
Returns:
the double value
Throws:
ValueNotValidException - if the string doesn't represent a double value
See Also:
Double.parseDouble(String)

requestsFor

public static boolean requestsFor(java.lang.String hintName,
                                  BasicStorageHints hints)
                           throws ValueNotValidException
Checks if a hint has been set and if so, if the value corresponds to the boolean true

This method has not been placed in BasicStorageHints, because Exception handling is not as layer independend as it should be to place method directly there.

Parameters:
hintName - the name of a hint
hints - the hints to analyze
Returns:
true, if corresponding hint has been set to true; otherwise false will be returned (default value)
Throws:
ValueNotValidException - if hint value was set to a non-boolean value
See Also:
BasicStorageHints#HINT_NAME_APPEND_CONTENT

consumeHint

public static boolean consumeHint(java.lang.String hintName,
                                  BasicStorageHints hints)
                           throws ValueNotValidException
Checks if a hint has been set and if so, if the value corresponds to the boolean true

Parameters:
hintName - the name of a hint
hints - the hints to analyze
Returns:
true, if corresponding hint existed and has now been flagged as consumed; false if hint did not exist
Throws:
ValueNotValidException - if hint value was set to a non-boolean value
See Also:
BasicStorageHints#HINT_NAME_APPEND_CONTENT

containsDownloadCredentials

public static boolean containsDownloadCredentials(BasicStorageHints downloadHints)
Returns whether the hints define download credentials such as username and/or password

Parameters:
downloadHints - the hints to use for downloading
Returns:
true, if at least one of those hints has been set, otherwise false

getNewUUID

public static java.lang.String getNewUUID()
Generates a new unique identifier using a singleton instance of UUIDGen

Returns:
the generated ID
See Also:
UUIDGen.nextUUID()

removeProtocolUsernamePasswordFromURL

public static java.lang.String removeProtocolUsernamePasswordFromURL(java.lang.String url)
Removes the protocol, username / password information that is contained inside the URL, e.g. ftp://me:mypass@theserver/mydir/myfile.txt is converted to theserver/mydir/myfile.txt.

Parameters:
url - the URL from which this information should get removed
Returns:
the part after this kind of information

makePseudoUniqueLocation

public static RawContentLocation makePseudoUniqueLocation(RawContentLocation location,
                                                          RawFileContentManager manager,
                                                          BasicStorageHints hints)
                                                   throws BaseLayerException
Generates a location that is to best knowledge unique. For this purpose, a random number is added to the location until the content manager returns, that this location does not yet exist.

Parameters:
location - the location that should be made unique
manager - the manager used to check whether the location exists
hints - hints that should be passed to the content manager
Returns:
a location that is unique to best knowledge
Throws:
BaseLayerException - in case a problem occurs in calling the content manager to check whether the location exists
See Also:
RawFileContentManager.existsBinaryContent(RawContentLocation, BasicStorageHints)