com.dtolabs.rundeck.core.dispatcher
Class DataContextUtils

java.lang.Object
  extended by com.dtolabs.rundeck.core.dispatcher.DataContextUtils

public class DataContextUtils
extends java.lang.Object

DataContextUtils provides methods for using a set of context data to substitute property references, generate environment variables, and expand tokens in a file.


Nested Class Summary
static interface DataContextUtils.EnvironmentConfigurable
          Can be configured with environment variables
static class DataContextUtils.UnresolvedDataReferenceException
          Indicates that the value of a property reference could not be resolved.
 
Field Summary
static java.lang.String ENV_VAR_PREFIX
          Prefix string used for all environment variable names
static java.lang.String PROPERTY_REF_REGEX
           
static org.apache.commons.collections.Predicate stringContainsPropertyReferencePredicate
          evaluates to true if a string contains a property reference
 
Constructor Summary
DataContextUtils()
           
 
Method Summary
static java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> addContext(java.lang.String key, java.util.Map<java.lang.String,java.lang.String> data, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> context)
          Return a new context with appended data set
static void addEnvVars(DataContextUtils.EnvironmentConfigurable sshexecTask, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext)
          add Env elements to pass environment variables to the ExtSSHExec
static void addEnvVarsFromContextForExec(org.apache.tools.ant.taskdefs.ExecTask execTask, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext)
          Add embedded env elements for any included context data for the script
static java.lang.String escapeShell(java.lang.String s)
          Escape characters meaningful to bash shell unless the string is already surrounded in single quotes
static java.lang.String escapeWindowsShell(java.lang.String s)
          Escape characters meaningful to windows unless the string is already surrounded in single quotes
static java.util.Map<java.lang.String,java.lang.String> flattenDataContext(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext)
          Flattens the data context into a simple key/value pair, using a "." separator for keys.
static java.lang.String generateEnvVarName(java.lang.String key)
          Generate environment variable name from option name
static java.util.Map<java.lang.String,java.lang.String> generateEnvVarsFromContext(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext)
          Generate a set of key value pairs to use for environment variables, from the context data set
static java.util.Map<java.lang.String,java.lang.String> generateEnvVarsFromData(java.util.Map<java.lang.String,java.lang.String> options, java.lang.String prefix)
          Convert option keys into environment variable names.
static java.lang.String join(java.util.Collection<java.lang.String> list, java.lang.String separator)
          Join a list of strings into a single string with a separator
static java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> merge(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> targetContext, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> newContext)
          Merge one context onto another by adding or replacing values.
static java.util.Map<java.lang.String,java.lang.String> nodeData(INodeEntry nodeentry)
          Generate a dataset for a INodeEntry
static java.util.Map<java.lang.String,java.lang.Object> replaceDataReferences(java.util.Map<java.lang.String,java.lang.Object> input, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data)
          Recursively replace data references in the values in a map which contains either string, collection or Map values.
static java.lang.String[] replaceDataReferences(java.lang.String[] args, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data)
          Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context
static java.lang.String[] replaceDataReferences(java.lang.String[] args, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data, Converter<java.lang.String,java.lang.String> converter, boolean failIfUnexpanded)
          Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context
static java.lang.String[] replaceDataReferences(java.lang.String[] args, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data, Converter<java.lang.String,java.lang.String> converter, boolean failIfUnexpanded, boolean blankIfUnexpanded)
          Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context
static java.lang.String replaceDataReferences(java.lang.String input, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data)
          Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context
static java.lang.String replaceDataReferences(java.lang.String input, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data, Converter<java.lang.String,java.lang.String> converter, boolean failOnUnexpanded)
          Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context
static java.lang.String replaceDataReferences(java.lang.String input, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data, Converter<java.lang.String,java.lang.String> converter, boolean failOnUnexpanded, boolean blankIfUnexpanded)
          Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context
static Converter<java.lang.String,java.lang.String> replaceDataReferencesConverter(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data)
          Return a converter that can expand the property references within a string
static Converter<java.lang.String,java.lang.String> replaceDataReferencesConverter(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data, Converter<java.lang.String,java.lang.String> converter, boolean failOnUnexpanded)
          Return a converter that can expand the property references within a string
static java.io.File replaceTokensInFile(java.io.File sourceFile, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext, Framework framework)
          Copies the source file to a temp file, replacing the @key.X@ tokens with the values from the data context
static java.io.File replaceTokensInFile(java.lang.String sourceFile, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext, Framework framework)
          Copies the source file to a temp file, replacing the @key.X@ tokens with the values from the data context
static java.io.File replaceTokensInScript(java.lang.String script, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext, Framework framework)
          Copies the source file to a temp file, replacing the @key.X@ tokens with the values from the data context
static java.io.File replaceTokensInStream(java.io.InputStream stream, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext, Framework framework)
          Copies the source stream to a temp file, replacing the @key.X@ tokens with the values from the data context
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENV_VAR_PREFIX

public static final java.lang.String ENV_VAR_PREFIX
Prefix string used for all environment variable names

See Also:
Constant Field Values

PROPERTY_REF_REGEX

public static final java.lang.String PROPERTY_REF_REGEX
See Also:
Constant Field Values

stringContainsPropertyReferencePredicate

public static final org.apache.commons.collections.Predicate stringContainsPropertyReferencePredicate
evaluates to true if a string contains a property reference

Constructor Detail

DataContextUtils

public DataContextUtils()
Method Detail

replaceDataReferencesConverter

public static Converter<java.lang.String,java.lang.String> replaceDataReferencesConverter(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data)
Return a converter that can expand the property references within a string

Parameters:
data - property context data
Returns:
a Converter to expand property values within a string

replaceDataReferencesConverter

public static Converter<java.lang.String,java.lang.String> replaceDataReferencesConverter(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data,
                                                                                          Converter<java.lang.String,java.lang.String> converter,
                                                                                          boolean failOnUnexpanded)
Return a converter that can expand the property references within a string

Parameters:
data - property context data
converter - secondary converter to apply to property values before replacing in a string
failOnUnexpanded - if true, fail if a property value cannot be expanded
Returns:
a Converter to expand property values within a string

replaceDataReferences

public static java.lang.String[] replaceDataReferences(java.lang.String[] args,
                                                       java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data,
                                                       Converter<java.lang.String,java.lang.String> converter,
                                                       boolean failIfUnexpanded)
Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context

Parameters:
args - argument string array
data - data context
Returns:
string array with replaced embedded properties

replaceDataReferences

public static java.lang.String[] replaceDataReferences(java.lang.String[] args,
                                                       java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data,
                                                       Converter<java.lang.String,java.lang.String> converter,
                                                       boolean failIfUnexpanded,
                                                       boolean blankIfUnexpanded)
Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context

Parameters:
args - argument string array
data - data context
Returns:
string array with replaced embedded properties

replaceDataReferences

public static java.lang.String[] replaceDataReferences(java.lang.String[] args,
                                                       java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data)
Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context

Parameters:
args - argument string array
data - data context
Returns:
string array with replaced embedded properties

replaceDataReferences

public static java.util.Map<java.lang.String,java.lang.Object> replaceDataReferences(java.util.Map<java.lang.String,java.lang.Object> input,
                                                                                     java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data)
Recursively replace data references in the values in a map which contains either string, collection or Map values.

Parameters:
input - input map
data - context data
Returns:
Map with all string values having references replaced

replaceDataReferences

public static java.lang.String replaceDataReferences(java.lang.String input,
                                                     java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data)
Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context

Parameters:
input - input string
data - data context map
Returns:
string with values substituted, or original string

merge

public static java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> merge(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> targetContext,
                                                                                                     java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> newContext)
Merge one context onto another by adding or replacing values.

Parameters:
targetContext - the target of the merge
newContext - context to merge

replaceDataReferences

public static java.lang.String replaceDataReferences(java.lang.String input,
                                                     java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data,
                                                     Converter<java.lang.String,java.lang.String> converter,
                                                     boolean failOnUnexpanded)
Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context

Parameters:
input - input string
data - data context map
converter - converter to encode/convert the expanded values
failOnUnexpanded -
Returns:
string with values substituted, or original string

replaceDataReferences

public static java.lang.String replaceDataReferences(java.lang.String input,
                                                     java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> data,
                                                     Converter<java.lang.String,java.lang.String> converter,
                                                     boolean failOnUnexpanded,
                                                     boolean blankIfUnexpanded)
Replace the embedded properties of the form '${key.name}' in the input Strings with the value from the data context

Parameters:
input - input string
data - data context map
converter - converter to encode/convert the expanded values
failOnUnexpanded -
Returns:
string with values substituted, or original string

escapeShell

public static java.lang.String escapeShell(java.lang.String s)
Escape characters meaningful to bash shell unless the string is already surrounded in single quotes

Parameters:
s - string
Returns:
escaped string

escapeWindowsShell

public static java.lang.String escapeWindowsShell(java.lang.String s)
Escape characters meaningful to windows unless the string is already surrounded in single quotes

Parameters:
s - string
Returns:
escaped string

replaceTokensInFile

public static java.io.File replaceTokensInFile(java.lang.String sourceFile,
                                               java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext,
                                               Framework framework)
                                        throws java.io.IOException
Copies the source file to a temp file, replacing the @key.X@ tokens with the values from the data context

Parameters:
sourceFile - source file path
dataContext - input data context
framework - the framework
Returns:
the token replaced temp file, or null if an error occurs.
Throws:
java.io.IOException

replaceTokensInFile

public static java.io.File replaceTokensInFile(java.io.File sourceFile,
                                               java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext,
                                               Framework framework)
                                        throws java.io.IOException
Copies the source file to a temp file, replacing the @key.X@ tokens with the values from the data context

Parameters:
sourceFile - source file
dataContext - input data context
framework - the framework
Returns:
the token replaced temp file, or null if an error occurs.
Throws:
java.io.IOException

replaceTokensInScript

public static java.io.File replaceTokensInScript(java.lang.String script,
                                                 java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext,
                                                 Framework framework)
                                          throws java.io.IOException
Copies the source file to a temp file, replacing the @key.X@ tokens with the values from the data context

Parameters:
script - source file path
dataContext - input data context
framework - the framework
Returns:
the token replaced temp file, or null if an error occurs.
Throws:
java.io.IOException

replaceTokensInStream

public static java.io.File replaceTokensInStream(java.io.InputStream stream,
                                                 java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext,
                                                 Framework framework)
                                          throws java.io.IOException
Copies the source stream to a temp file, replacing the @key.X@ tokens with the values from the data context

Parameters:
stream - source stream
dataContext - input data context
framework - the framework
Returns:
the token replaced temp file, or null if an error occurs.
Throws:
java.io.IOException

flattenDataContext

public static java.util.Map<java.lang.String,java.lang.String> flattenDataContext(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext)
Flattens the data context into a simple key/value pair, using a "." separator for keys.

Parameters:
dataContext -
Returns:

generateEnvVarsFromData

public static java.util.Map<java.lang.String,java.lang.String> generateEnvVarsFromData(java.util.Map<java.lang.String,java.lang.String> options,
                                                                                       java.lang.String prefix)
Convert option keys into environment variable names. Convert to uppercase and prepend "RD_"

Parameters:
options - the input options
prefix -
Returns:
map of environment variable names to values, or null if options was null

addEnvVarsFromContextForExec

public static void addEnvVarsFromContextForExec(org.apache.tools.ant.taskdefs.ExecTask execTask,
                                                java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext)
Add embedded env elements for any included context data for the script

Parameters:
execTask - execTask

addEnvVars

public static void addEnvVars(DataContextUtils.EnvironmentConfigurable sshexecTask,
                              java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext)
add Env elements to pass environment variables to the ExtSSHExec

Parameters:
sshexecTask - task

generateEnvVarsFromContext

public static java.util.Map<java.lang.String,java.lang.String> generateEnvVarsFromContext(java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> dataContext)
Generate a set of key value pairs to use for environment variables, from the context data set


generateEnvVarName

public static java.lang.String generateEnvVarName(java.lang.String key)
Generate environment variable name from option name

Parameters:
key - key
Returns:
env var name

addContext

public static java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> addContext(java.lang.String key,
                                                                                                          java.util.Map<java.lang.String,java.lang.String> data,
                                                                                                          java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> context)
Return a new context with appended data set

Parameters:
key - data key
data - data content
context - original context
Returns:
new context containing original data and the new dataset

nodeData

public static java.util.Map<java.lang.String,java.lang.String> nodeData(INodeEntry nodeentry)
Generate a dataset for a INodeEntry

Parameters:
nodeentry - node
Returns:
dataset

join

public static java.lang.String join(java.util.Collection<java.lang.String> list,
                                    java.lang.String separator)
Join a list of strings into a single string with a separator

Parameters:
list - strings
separator - separator
Returns:
joined string