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

org.metricssampler.extensions.base.DefaultSamplerXBean Maven / Gradle / Ivy

package org.metricssampler.extensions.base;

import static org.metricssampler.config.loader.xbeans.ValidationUtils.greaterThanZero;
import static org.metricssampler.config.loader.xbeans.ValidationUtils.notEmpty;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.metricssampler.config.ConfigurationException;
import org.metricssampler.config.InputConfig;
import org.metricssampler.config.OutputConfig;
import org.metricssampler.config.SamplerConfig;
import org.metricssampler.config.SelectorConfig;
import org.metricssampler.config.ValueTransformerConfig;
import org.metricssampler.config.loader.xbeans.SamplerXBean;
import org.metricssampler.config.loader.xbeans.SelectorGroupRefXBean;
import org.metricssampler.config.loader.xbeans.SelectorXBean;
import org.metricssampler.config.loader.xbeans.SimpleSelectorXBean;
import org.metricssampler.config.loader.xbeans.ValueTransformerXBean;
import org.metricssampler.config.loader.xbeans.VariableXBean;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("sampler")
public class DefaultSamplerXBean extends SamplerXBean {
	private static final String DEFAULT_POOL_NAME = "samplers";

	@XStreamAsAttribute
	private String input;

	@XStreamAsAttribute
	private String outputs;

	@XStreamAsAttribute
	private String pool;

	@XStreamAsAttribute
	private Boolean quiet = false;

	@XStreamAlias("reset-timeout")
	@XStreamAsAttribute
	private Integer resetTimeout;

	private List variables;

	private List selectors;

	@XStreamAlias("value-transformers")
	private List valueTransformers;
	
	public String getInput() {
		return input;
	}

	@Override
	public String getName() {
		final String name = super.getName();
		if (name == null) {
			return input;
		}
		return name;
	}

	public void setInput(final String input) {
		this.input = input;
	}

	public String getOutputs() {
		return outputs;
	}

	public void setOutputs(final String outputs) {
		this.outputs = outputs;
	}

	public String getPool() {
		return pool;
	}

	public void setPool(final String pool) {
		this.pool = pool;
	}

	public List getVariables() {
		return variables;
	}

	public void setVariables(final List variables) {
		this.variables = variables;
	}

	public List getSelectors() {
		return selectors;
	}

	public void setSelectors(final List selectors) {
		this.selectors = selectors;
	}

	public Boolean getQuiet() {
		return quiet;
	}

	public void setQuiet(final Boolean quiet) {
		this.quiet = quiet;
	}

	public Integer getResetTimeout() {
		return resetTimeout;
	}

	public void setResetTimeout(final Integer resetTimeout) {
		this.resetTimeout = resetTimeout;
	}

	@Override
	protected void validate() {
		super.validate();
		if (isInstantiatable()) {
			notEmpty(this, "input", getInput());
			notEmpty(this, "selectors", getSelectors());
			if (resetTimeout != null) {
				greaterThanZero(this, "reload-timeout", resetTimeout);
			}
		}
	}
	@Override
	public SamplerConfig toConfig(final Map inputs, final Map outputs, final Map> selectorTemplates, final Map globalVariables) {
		validate();

		final InputConfig inputConfig = configureInput(inputs);
		final List outputConfigs = configureOutputs(outputs);
		final List selectorConfigs = configureSelectors(selectorTemplates);
		final Map samplerVariables = VariableXBean.toMap(getVariables());
		final boolean ignored = getIgnored() != null ? getIgnored() : false;
		final boolean disabled = getDisabled() != null ? getDisabled() : false;
		final boolean quiet = getQuiet() != null ? getQuiet() : false;
		final String pool = getPool() != null ? getPool() : DEFAULT_POOL_NAME;
		final int resetTimeoutInt = resetTimeout != null ? resetTimeout : -1;
		final List valueTransformerConfigs = configureValueTransformers(valueTransformers);
		return new DefaultSamplerConfig(getName(), pool, getInterval(), ignored, disabled, inputConfig, outputConfigs, selectorConfigs, samplerVariables, globalVariables, valueTransformerConfigs, quiet, resetTimeoutInt);
	}

	protected List configureValueTransformers(final List valueTransformers) {
		if (valueTransformers == null) {
			return new LinkedList();
		}
		final List result = new ArrayList(valueTransformers.size());
		for (final ValueTransformerXBean item : valueTransformers) {
			result.add(item.toConfig());
		}
		return result;
	}

	protected List configureSelectors(final Map> templates) {
		final List result = new LinkedList();
		for (final SelectorXBean item : getSelectors()) {
			if (item instanceof SelectorGroupRefXBean) {
				result.addAll(((SelectorGroupRefXBean) item).toConfig(templates));
			} else if (item instanceof SimpleSelectorXBean) {
				result.add(((SimpleSelectorXBean) item).toConfig());
			} else {
				throw new ConfigurationException("Unsupporter selector: " + item);
			}
		}
		if (result.isEmpty()) {
			throw new ConfigurationException("Default sampler has no selectors");
		}
		return result;
	}

	protected InputConfig configureInput(final Map inputs) {
		final InputConfig result = inputs.get(getInput());
		if (result == null) {
			throw new ConfigurationException("Input named \"" + getInput() + "\" not found");
		}
		return result;
	}

	protected List configureOutputs(final Map outputs) {
		final List result = new LinkedList();
		if (getOutputs() == null) {
			for (final OutputConfig output : outputs.values()) {
				if (output.isDefault()) {
					result.add(output);
				}
			}
			if (result.isEmpty()) {
				throw new ConfigurationException("No outputs specified for sampler \"" + getName() + "\" and no default outputs found");
			}
		} else {
			for (final String name : getOutputs().split(",")) {
				final OutputConfig output = outputs.get(name);
				if (output == null) {
					throw new ConfigurationException("Output named \"" + name + "\" not found");
				}
				result.add(output);
			}
		}
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy