def write_ordered_variables(program_name,
                            variable_map,
                            file_path,
                            append=False):
    """
    Write variables to file while preserving order of the variables.
    :param program_name: name of the calling program
    :param variable_map: map or variable properties to write to file
    :param file_path: the file to which to write the properties
    :param append: defaults to False. Append properties to the end of file
    :raises VariableException if an error occurs while storing the variables in the file
    """
    _method_name = 'write_ordered_variables'
    _logger.entering(program_name,
                     file_path,
                     append,
                     class_name=_class_name,
                     method_name=_method_name)
    pw = None
    try:
        pw = PrintWriter(FileOutputStream(File(file_path), Boolean(append)),
                         Boolean('true'))
        for key, value in variable_map.iteritems():
            formatted = '%s=%s' % (key, value)
            pw.println(formatted)
        pw.close()
    except IOException, ioe:
        _logger.fine('WLSDPLY-20007', file_path, ioe.getLocalizedMessage())
        ex = exception_helper.create_variable_exception(
            'WLSDPLY-20007', file_path, ioe.getLocalizedMessage(), error=ioe)
        _logger.throwing(ex, class_name=_class_name, method_name=_method_name)
        if pw is not None:
            pw.close()
        raise ex
def load_variables(file_path, allow_multiple_files=False):
    """
    Load a dictionary of variables from the specified file(s).
    :param file_path: the file from which to load properties
    :param allow_multiple_files: if True, allow a comma-separated list of variable files
    :return the dictionary of variables
    :raises VariableException if an I/O error occurs while loading the variables from the file
    """
    method_name = "load_variables"

    if allow_multiple_files:
        paths = file_path.split(CommandLineArgUtil.MODEL_FILES_SEPARATOR)
    else:
        paths = [file_path]

    variable_map = {}

    for path in paths:
        try:
            variable_map.update(string_utils.load_properties(path))
        except IOException, ioe:
            ex = exception_helper.create_variable_exception(
                'WLSDPLY-01730', path, ioe.getLocalizedMessage(), error=ioe)
            _logger.throwing(ex,
                             class_name=_class_name,
                             method_name=method_name)
            raise ex
Beispiel #3
0
def write_variables(program_name, variable_map, file_path, append=False):
    """
    Write the dictionary of variables to the specified file.
    :param program_name: name of tool that invoked the method which will be written to the variable properties file
    :param variable_map: the dictionary of variables
    :param file_path: the file to which to write the properties
    :param append: defaults to False. Append properties to the end of file
    :raises VariableException if an error occurs while storing the variables in the file
    """
    _method_name = 'write_variables'
    _logger.entering(program_name, file_path, append, class_name=_class_name, method_name=_method_name)
    props = Properties()
    for key in variable_map:
        value = variable_map[key]
        props.setProperty(key, value)

    comment = exception_helper.get_message('WLSDPLY-01731', program_name)
    output_stream = None
    try:
        output_stream = FileOutputStream(File(file_path), Boolean(append))
        props.store(output_stream, comment)
        output_stream.close()
    except IOException, ioe:
        ex = exception_helper.create_variable_exception('WLSDPLY-20007', file_path,
                                                        ioe.getLocalizedMessage(), error=ioe)
        _logger.throwing(ex, class_name=_class_name, method_name=_method_name)
        if output_stream is not None:
            output_stream.close()
        raise ex
def _read_value_from_file(file_path, model_context):
    """
    Read a single text value from the first line in the specified file.
    :param file_path: the file from which to read the value
    :return: the text value
    :raises BundleAwareException if an error occurs while reading the value
    """
    method_name = '_read_value_from_file'

    try:
        file_reader = BufferedReader(FileReader(file_path))
        line = file_reader.readLine()
        file_reader.close()
    except IOException, e:
        if model_context.get_validation_method() == 'strict':
            _logger.severe('WLSDPLY-01733',
                           file_path,
                           e.getLocalizedMessage(),
                           class_name=_class_name,
                           method_name=method_name)
            ex = exception_helper.create_variable_exception(
                'WLSDPLY-01733', file_path, e.getLocalizedMessage(), error=e)
            _logger.throwing(ex,
                             class_name=_class_name,
                             method_name=method_name)
            raise ex
        else:
            _logger.info('WLSDPLY-01733',
                         file_path,
                         e.getLocalizedMessage(),
                         error=e,
                         class_name=_class_name,
                         method_name=method_name)
            line = ''
Beispiel #5
0
def _substitute(text, variables, model_context):
    """
    Substitute the variable placeholders with the variable value.
    :param text: the text to process for variable placeholders
    :param variables: the variables to use
    :param model_context: used to resolve variables in file paths
    :return: the replaced text
    """
    method_name = '_substitute'

    if '${' in text:
        tokens = _variable_pattern.findall(text)
        if tokens:
            for token in tokens:
                key = token[2:-1]
                # for ${key} variables, leave them in place if not defined.
                # there are cases where WebLogic allows ${key} values, such as server templates.
                # ${key} substitution is deprecated, so log if replacement occurs.
                if key in variables:
                    value = variables[key]
                    text = text.replace(token, value)
                    _logger.info('WLSDPLY-01735', token, key, method_name=method_name, class_name=_class_name)

    # skip lookups for text with no @@
    if '@@' in text:

        # do properties first, to cover the case @@FILE:/dir/@@PROP:name@@.txt@@
        tokens = _property_pattern.findall(text)
        if tokens:
            for token in tokens:
                key = token[7:-2]
                # for @@PROP:key@@ variables, throw an exception if key is not found.
                if key not in variables:
                    ex = exception_helper.create_variable_exception('WLSDPLY-01732', key)
                    _logger.throwing(ex, class_name=_class_name, method_name=method_name)
                    raise ex
                value = variables[key]
                text = text.replace(token, value)

        tokens = _file_variable_pattern.findall(text)
        if tokens:
            for token in tokens:
                path = token[7:-2]
                value = _read_value_from_file(path)
                text = text.replace(token, value)

        # special case for @@FILE:@@ORACLE_HOME@@/dir/name.txt@@
        tokens = _file_nested_variable_pattern.findall(text)
        if tokens:
            for token in tokens:
                path = token[7:-2]
                path = model_context.replace_token_string(path)
                value = _read_value_from_file(path)
                text = text.replace(token, value)

    return text
Beispiel #6
0
def _substitute(text, variables, model_context):
    """
    Substitute the variable placeholders with the variable value.
    :param text: the text to process for variable placeholders
    :param variables: the variables to use
    :param model_context: used to resolve variables in file paths
    :return: the replaced text
    """
    method_name = '_substitute'

    # skip lookups for text with no @@
    if '@@' in text:

        # do properties first, to cover the case @@FILE:/dir/@@PROP:name@@.txt@@
        matches = _property_pattern.findall(text)
        for token, key in matches:
            # log, or throw an exception if key is not found.
            if key not in variables:
                if model_context.get_validation_method() == 'strict':
                    _logger.severe('WLSDPLY-01732',
                                   key,
                                   class_name=_class_name,
                                   method_name=method_name)
                    ex = exception_helper.create_variable_exception(
                        'WLSDPLY-01732', key)
                    _logger.throwing(ex,
                                     class_name=_class_name,
                                     method_name=method_name)
                    raise ex
                else:
                    _logger.info('WLSDPLY-01732',
                                 key,
                                 class_name=_class_name,
                                 method_name=method_name)
                    continue

            value = variables[key]
            text = text.replace(token, value)

        tokens = _file_variable_pattern.findall(text)
        if tokens:
            for token in tokens:
                path = token[7:-2]
                value = _read_value_from_file(path, model_context)
                text = text.replace(token, value)

        # special case for @@FILE:@@ORACLE_HOME@@/dir/name.txt@@
        tokens = _file_nested_variable_pattern.findall(text)
        if tokens:
            for token in tokens:
                path = token[7:-2]
                path = model_context.replace_token_string(path)
                value = _read_value_from_file(path, model_context)
                text = text.replace(token, value)

    return text
def _report_token_issue(message_key, method_name, model_context, *args):
    """
    Log a message at the level corresponding to the validation method (SEVERE for strict, INFO otherwise).
    Throw a variable exception if the level is strict.
    The lax validation method can be used to verify the model without resolving tokens.
    :param message_key: the message key to be logged and used for exceptions
    :param method_name: the name of the calling method for logging
    :param model_context: used to determine the validation method
    :param args: arguments for use in the message
    """
    log_method = _logger.info
    if model_context.get_validation_method() == 'strict':
        log_method = _logger.severe

    log_method(message_key,
               class_name=_class_name,
               method_name=method_name,
               *args)

    if model_context.get_validation_method() == 'strict':
        ex = exception_helper.create_variable_exception(message_key, *args)
        _logger.throwing(ex, class_name=_class_name, method_name=method_name)
        raise ex
def load_variables(file_path):
    """
    Load a dictionary of variables from the specified file.
    :param file_path: the file from which to load properties
    :return the dictionary of variables
    :raises VariableException if an I/O error occurs while loading the variables from the file
    """
    method_name = "load_variables"

    variable_map = {}
    props = Properties()
    input_stream = None
    try:
        input_stream = FileInputStream(file_path)
        props.load(input_stream)
        input_stream.close()
    except IOException, ioe:
        ex = exception_helper.create_variable_exception(
            'WLSDPLY-01730', file_path, ioe.getLocalizedMessage(), error=ioe)
        _logger.throwing(ex, class_name=_class_name, method_name=method_name)
        if input_stream is not None:
            input_stream.close()
        raise ex
Beispiel #9
0
def load_variables(file_path, allow_multiple_files=False):
    """
    Load a dictionary of variables from the specified file(s).
    :param file_path: the file from which to load properties
    :param allow_multiple_files: if True, allow a comma-separated list of variable files
    :return the dictionary of variables
    :raises VariableException if an I/O error occurs while loading the variables from the file
    """
    method_name = "load_variables"

    if allow_multiple_files:
        paths = file_path.split(CommandLineArgUtil.MODEL_FILES_SEPARATOR)
    else:
        paths = [file_path]

    variable_map = {}

    for path in paths:
        props = Properties()
        input_stream = None
        try:
            input_stream = FileInputStream(path)
            props.load(input_stream)
            input_stream.close()
        except IOException, ioe:
            ex = exception_helper.create_variable_exception(
                'WLSDPLY-01730', path, ioe.getLocalizedMessage(), error=ioe)
            _logger.throwing(ex,
                             class_name=_class_name,
                             method_name=method_name)
            if input_stream is not None:
                input_stream.close()
            raise ex

        for key in props.keySet():
            value = props.getProperty(key)
            variable_map[key] = value
    :return: the text value
    :raises BundleAwareException if an error occurs while reading the value
    """
    method_name = '_read_value_from_file'

    try:
        file_reader = BufferedReader(FileReader(file_path))
        line = file_reader.readLine()
        file_reader.close()
    except IOException, e:
        _report_token_issue('WLSDPLY-01733', method_name, model_context,
                            file_path, e.getLocalizedMessage())
        line = ''

    if line is None:
        ex = exception_helper.create_variable_exception(
            'WLSDPLY-01734', file_path)
        _logger.throwing(ex, class_name=_class_name, method_name=method_name)
        raise ex

    return str(line).strip()


def _resolve_secret_token(name, key, model_context):
    """
    Return the value associated with the specified secret name and key.
    If the name and key are found in the directory map, return the associated value.
    :param name: the name of the secret (a directory name or mapped name)
    :param key: the name of the file containing the secret
    :param model_context: used to determine the validation method (strict, lax, etc.)
    :return: the secret value, or None if it is not found
    """