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("initial-reset-timeout")
    @XStreamAsAttribute
    private Integer initialResetTimeout;

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

	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 getInitialResetTimeout() {
		return initialResetTimeout;
	}

	public void setInitialResetTimeout(final Integer initialResetTimeout) {
		this.initialResetTimeout = initialResetTimeout;
	}

    public Integer getRegularResetTimeout() {
        return regularResetTimeout;
    }

    public void setRegularResetTimeout(Integer regularResetTimeout) {
        this.regularResetTimeout = regularResetTimeout;
    }


    @Override
	protected void validate() {
		super.validate();
		if (isInstantiatable()) {
			notEmpty(this, "input", getInput());
			notEmpty(this, "selectors", getSelectors());
			if (initialResetTimeout != null) {
				greaterThanZero(this, "initial-reset-timeout", initialResetTimeout);
			}
            if (regularResetTimeout != null) {
                greaterThanZero(this, "regular-reset-timeout", regularResetTimeout);
            }
		}
	}
	@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 initialResetTimeoutInt = initialResetTimeout != null ? initialResetTimeout : -1;
        final int regularResetTimeoutInt = regularResetTimeout != null ? regularResetTimeout : -1;
		final List valueTransformerConfigs = configureValueTransformers(valueTransformers);
		return new DefaultSamplerConfig(getName(), pool, getInterval(), ignored, disabled, inputConfig, outputConfigs, selectorConfigs, samplerVariables, globalVariables, valueTransformerConfigs, quiet, initialResetTimeoutInt, regularResetTimeoutInt);
	}

	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("Unsupported 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