<commit_before>package com.dtolabs.rundeck.server.plugins.builder

import com.dtolabs.rundeck.core.logging.LogFileStorageException
import com.dtolabs.rundeck.core.plugins.configuration.Configurable
import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException
import com.dtolabs.rundeck.core.plugins.configuration.Describable
import com.dtolabs.rundeck.core.plugins.configuration.Description
import com.dtolabs.rundeck.plugins.logging.KeyedLogFileStoragePlugin
import org.apache.log4j.Logger

/**
 * $INTERFACE is ...
 * User: greg
 * Date: 6/3/13
 * Time: 2:50 PM
 */
class ScriptLogFileStoragePlugin implements KeyedLogFileStoragePlugin, Describable, Configurable {
    static Logger logger = Logger.getLogger(ScriptLogFileStoragePlugin)
    Description description
    private Map<String, Closure> handlers
    Map configuration
    Map<String, ? extends Object> pluginContext

    ScriptLogFileStoragePlugin(Map<String, Closure> handlers, Description description) {
        this.handlers = handlers
        this.description = description
    }

    @Override
    void configure(Properties configuration) throws ConfigurationException {
        this.configuration = new HashMap(configuration)
    }


    @Override
    void initialize(Map<String, ? extends Object> context) {
        this.pluginContext = context
        ['available', 'retrieve', 'store'].each {
            if (!handlers[it]) {
                throw new RuntimeException("ScriptLogFileStoragePlugin: '${it}' closure not defined for plugin ${description.name}")
            }
        }
    }

    @Override
    boolean isAvailable() throws LogFileStorageException {
        return isAvailable(null)
    }
    boolean isAvailable(String filekey) throws LogFileStorageException {
        logger.debug("isAvailable ${pluginContext}")
        def closure = handlers.available
        def binding = [
                configuration: configuration,
                context: pluginContext
        ]
        def result = null
        if (closure.getMaximumNumberOfParameters() == 2) {
            def Closure newclos = closure.clone()
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            newclos.delegate = binding
            try {
                result = newclos.call(pluginContext + filekey?[key:filekey]:[:], configuration)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else if (closure.getMaximumNumberOfParameters() == 1 && closure.parameterTypes[0] == Object) {
            def Closure newclos = closure.clone()
            newclos.delegate = binding
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            try {
                result = newclos.call(pluginContext + filekey ? [key: filekey] : [:])
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else {
            throw new RuntimeException("ScriptLogFileStoragePlugin: 'available' closure signature invalid for plugin ${description.name}, cannot open")
        }
        return result ? true : false
    }

    @Override
    boolean store(InputStream stream, long length, Date lastModified) throws IOException, LogFileStorageException {
        store(null, stream, length, lastModified)
    }

    boolean store(String filekey, InputStream stream, long length, Date lastModified) throws IOException, LogFileStorageException {
        logger.debug("store ${pluginContext}")
        def closure = handlers.store
        def binding = [
                configuration: configuration,
                context: pluginContext + filekey ? [key: filekey] : [:],
                stream: stream,
                length: length,
                lastModified: lastModified
        ]
        if (closure.getMaximumNumberOfParameters() == 3) {
            def Closure newclos = closure.clone()
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            newclos.delegate = binding
            try {
                return newclos.call(binding.context, binding.configuration, binding.stream)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else if (closure.getMaximumNumberOfParameters() == 2) {
            def Closure newclos = closure.clone()
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            newclos.delegate = binding
            try {
                return newclos.call(binding.context, binding.stream)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else if (closure.getMaximumNumberOfParameters() == 1) {
            def Closure newclos = closure.clone()
            newclos.delegate = binding
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            try {
                return newclos.call(binding.stream)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else {
            throw new RuntimeException("ScriptLogFileStoragePlugin: 'store' closure signature invalid for plugin ${description.name}, cannot open")
        }
    }

    @Override
    boolean retrieve(OutputStream stream) throws IOException, LogFileStorageException {
        return retrieve(null, stream)
    }

    @Override
    boolean retrieve(String filekey, OutputStream stream) throws IOException, LogFileStorageException {
        logger.debug("retrieve ${pluginContext}")
        def closure = handlers.retrieve
        def binding = [
                configuration: configuration,
                context: pluginContext + filekey ? [key: filekey] : [:],
                stream: stream,
        ]
        if (closure.getMaximumNumberOfParameters() == 3) {
            def Closure newclos = closure.clone()
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            newclos.delegate = binding
            try {
                return newclos.call(binding.context, binding.configuration, binding.stream)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else if (closure.getMaximumNumberOfParameters() == 2) {
            def Closure newclos = closure.clone()
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            newclos.delegate = binding
            try {
                return newclos.call(binding.context, binding.stream)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else if (closure.getMaximumNumberOfParameters() == 1) {
            def Closure newclos = closure.clone()
            newclos.delegate = binding
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            try {<commit_msg>Fix use of additional filekey in context data<commit_after>package com.dtolabs.rundeck.server.plugins.builder

import com.dtolabs.rundeck.core.logging.LogFileStorageException
import com.dtolabs.rundeck.core.plugins.configuration.Configurable
import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException
import com.dtolabs.rundeck.core.plugins.configuration.Describable
import com.dtolabs.rundeck.core.plugins.configuration.Description
import com.dtolabs.rundeck.plugins.logging.KeyedLogFileStoragePlugin
import org.apache.log4j.Logger

/**
 * $INTERFACE is ...
 * User: greg
 * Date: 6/3/13
 * Time: 2:50 PM
 */
class ScriptLogFileStoragePlugin implements KeyedLogFileStoragePlugin, Describable, Configurable {
    static Logger logger = Logger.getLogger(ScriptLogFileStoragePlugin)
    Description description
    private Map<String, Closure> handlers
    Map configuration
    Map<String, ? extends Object> pluginContext

    ScriptLogFileStoragePlugin(Map<String, Closure> handlers, Description description) {
        this.handlers = handlers
        this.description = description
    }

    @Override
    void configure(Properties configuration) throws ConfigurationException {
        this.configuration = new HashMap(configuration)
    }


    @Override
    void initialize(Map<String, ? extends Object> context) {
        this.pluginContext = context
        ['available', 'retrieve', 'store'].each {
            if (!handlers[it]) {
                throw new RuntimeException("ScriptLogFileStoragePlugin: '${it}' closure not defined for plugin ${description.name}")
            }
        }
    }

    @Override
    boolean isAvailable() throws LogFileStorageException {
        return isAvailable(null)
    }
    boolean isAvailable(String filekey) throws LogFileStorageException {
        logger.debug("isAvailable(${filekey}) ${pluginContext}")
        def closure = handlers.available
        def binding = [
                configuration: configuration,
                context: pluginContext + (filekey ? [key: filekey] : [:])
        ]
        def result = null
        if (closure.getMaximumNumberOfParameters() == 2) {
            def Closure newclos = closure.clone()
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            newclos.delegate = binding
            try {
                result = newclos.call(binding.context , binding.configuration)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else if (closure.getMaximumNumberOfParameters() == 1 && closure.parameterTypes[0] == Object) {
            def Closure newclos = closure.clone()
            newclos.delegate = binding
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            try {
                result = newclos.call(binding.context)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else {
            throw new RuntimeException("ScriptLogFileStoragePlugin: 'available' closure signature invalid for plugin ${description.name}, cannot open")
        }
        return result ? true : false
    }

    @Override
    boolean store(InputStream stream, long length, Date lastModified) throws IOException, LogFileStorageException {
        store(null, stream, length, lastModified)
    }

    boolean store(String filekey, InputStream stream, long length, Date lastModified) throws IOException, LogFileStorageException {
        logger.debug("store($filekey) ${pluginContext}")
        def closure = handlers.store
        def binding = [
                configuration: configuration,
                context: pluginContext +( filekey ? [key: filekey] : [:]),
                stream: stream,
                length: length,
                lastModified: lastModified
        ]
        if (closure.getMaximumNumberOfParameters() == 3) {
            def Closure newclos = closure.clone()
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            newclos.delegate = binding
            try {
                return newclos.call(binding.context, binding.configuration, binding.stream)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else if (closure.getMaximumNumberOfParameters() == 2) {
            def Closure newclos = closure.clone()
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            newclos.delegate = binding
            try {
                return newclos.call(binding.context, binding.stream)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else if (closure.getMaximumNumberOfParameters() == 1) {
            def Closure newclos = closure.clone()
            newclos.delegate = binding
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            try {
                return newclos.call(binding.stream)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else {
            throw new RuntimeException("ScriptLogFileStoragePlugin: 'store' closure signature invalid for plugin ${description.name}, cannot open")
        }
    }

    @Override
    boolean retrieve(OutputStream stream) throws IOException, LogFileStorageException {
        return retrieve(null, stream)
    }

    @Override
    boolean retrieve(String filekey, OutputStream stream) throws IOException, LogFileStorageException {
        logger.debug("retrieve($filekey) ${pluginContext}")
        def closure = handlers.retrieve
        def binding = [
                configuration: configuration,
                context: pluginContext + (filekey ? [key: filekey] : [:]),
                stream: stream,
        ]
        if (closure.getMaximumNumberOfParameters() == 3) {
            def Closure newclos = closure.clone()
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            newclos.delegate = binding
            try {
                return newclos.call(binding.context, binding.configuration, binding.stream)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else if (closure.getMaximumNumberOfParameters() == 2) {
            def Closure newclos = closure.clone()
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            newclos.delegate = binding
            try {
                return newclos.call(binding.context, binding.stream)
            } catch (Exception e) {
                throw new LogFileStorageException(e.getMessage(), e)
            }
        } else if (closure.getMaximumNumberOfParameters() == 1) {
            def Closure newclos = closure.clone()
            newclos.delegate = binding
            newclos.resolveStrategy = Closure.DELEGATE_ONLY
            try {
{'fraction_non_alphanumeric': 0.08620565069730472, 'fraction_numerical': 0.0024568249150950213, 'mean_word_length': 2.3062589584328714, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7665641', 'n_tokens_mistral': 3495, 'n_tokens_neox': 3346, 'n_words': 816}