All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.timofeevda.jstressy.config.ConfigLoader.kt Maven / Gradle / Ivy

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2018 Denis Timofeev 
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *
 */

package com.github.timofeevda.jstressy.config

import com.fasterxml.jackson.core.Version
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.github.timofeevda.jstressy.api.config.ConfigurationService
import com.github.timofeevda.jstressy.api.config.parameters.StressyArrivalInterval
import com.github.timofeevda.jstressy.api.config.parameters.StressyConfiguration
import com.github.timofeevda.jstressy.api.config.parameters.StressyGlobals
import com.github.timofeevda.jstressy.api.config.parameters.StressyStage
import com.github.timofeevda.jstressy.api.config.parameters.StressyStressPlan
import com.github.timofeevda.jstressy.config.parameters.ArrivalInterval
import com.github.timofeevda.jstressy.config.parameters.Config
import com.github.timofeevda.jstressy.config.parameters.Globals
import com.github.timofeevda.jstressy.config.parameters.Stage
import com.github.timofeevda.jstressy.config.parameters.StressPlan
import com.github.timofeevda.jstressy.utils.logging.LazyLogging
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream

private const val STRESSY_YML = "stressy.yml"

/**
 * Example implementation of configuration service. Reads JStressy configuration from YAML file
 *
 * @author timofeevda
 */
open class ConfigLoader : ConfigurationService {

    companion object : LazyLogging()

    override var configuration: Config = Config()

    override var configurationFolder: String? = null

    @Throws(IOException::class)
    override fun readConfiguration(configurationFolder: String) {
        this.configurationFolder = configurationFolder
        val configFile = File(configurationFolder + File.separator + STRESSY_YML)
        readConfigurationFile(FileInputStream(configFile))
    }

    internal fun readConfigurationFile(configFileStream: InputStream) {
        val mapper = ObjectMapper(YAMLFactory())

        val typeResolver = SimpleAbstractTypeResolver()
                .addMapping(StressyConfiguration::class.java, Config::class.java)
                .addMapping(StressyGlobals::class.java, Globals::class.java)
                .addMapping(StressyStage::class.java, Stage::class.java)
                .addMapping(StressyStressPlan::class.java, StressPlan::class.java)
                .addMapping(StressyArrivalInterval::class.java, ArrivalInterval::class.java)

        val configurationModule = SimpleModule("StressyConfiguration", Version.unknownVersion())
        configurationModule.setAbstractTypes(typeResolver)

        mapper.registerModule(configurationModule)

        configuration = mapper.readValue(configFileStream, Config::class.java)

        // process stages and add arrival intervals from separate configs if needed
        configuration.stressPlan.stages.forEach { stage ->
            if (stage.arrivalIntervalsPath != null) {
                val configFile = File(stage.arrivalIntervalsPath)
                val arrivalIntervals = when {
                    configFile.isAbsolute -> mapper.readValue(FileInputStream(configFile),
                            Array::class.java).asList()
                    else -> {
                        mapper.readValue(FileInputStream(File(
                                configurationFolder + File.separator + stage.arrivalIntervalsPath)),
                                Array::class.java).asList()
                    }
                }
                stage.arrivalIntervals.addAll(arrivalIntervals)
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy