Search in sources :

Example 1 with Annotator

use of io.nosqlbench.nb.api.annotations.Annotator in project nosqlbench by nosqlbench.

the class Annotators method init.

/**
 * Initialize the active annotators. This method must be called before any others.
 *
 * @param annotatorsConfig A (possibly empty) set of annotator configurations, in any form
 *                         supported by {@link ConfigLoader}
 */
public static synchronized void init(String annotatorsConfig) {
    ConfigLoader loader = new ConfigLoader();
    annotators = new ArrayList<>();
    LinkedHashMap<String, ServiceLoader.Provider<Annotator>> providers = getProviders();
    List<Map> configs = loader.load(annotatorsConfig, Map.class);
    if (configs != null) {
        for (Map cmap : configs) {
            Object typeObj = cmap.remove("type");
            String typename = typeObj.toString();
            ServiceLoader.Provider<Annotator> annotatorProvider = providers.get(typename);
            if (annotatorProvider == null) {
                throw new RuntimeException("Annotation provider with selector '" + typename + "' was not found.");
            }
            Annotator annotator = annotatorProvider.get();
            if (annotator instanceof NBMapConfigurable) {
                NBMapConfigurable NBMapConfigurable = (NBMapConfigurable) annotator;
                NBMapConfigurable.applyConfig(cmap);
            }
            if (annotator instanceof NBConfigurable) {
                NBConfigurable nbConfigurable = (NBConfigurable) annotator;
                NBConfiguration cfg = nbConfigurable.getConfigModel().apply(cmap);
                nbConfigurable.applyConfig(cfg);
            }
            annotators.add(annotator);
        }
    }
    logger.debug("Initialized " + Annotators.annotators.size() + " annotators, since the configuration is empty.");
}
Also used : NBConfigurable(io.nosqlbench.nb.api.config.standard.NBConfigurable) ConfigLoader(io.nosqlbench.nb.api.config.standard.ConfigLoader) NBMapConfigurable(io.nosqlbench.nb.api.config.standard.NBMapConfigurable) Annotator(io.nosqlbench.nb.api.annotations.Annotator) NBConfiguration(io.nosqlbench.nb.api.config.standard.NBConfiguration)

Example 2 with Annotator

use of io.nosqlbench.nb.api.annotations.Annotator in project nosqlbench by nosqlbench.

the class Annotators method getProviders.

private static synchronized LinkedHashMap<String, ServiceLoader.Provider<Annotator>> getProviders() {
    ServiceLoader<Annotator> loader = ServiceLoader.load(Annotator.class);
    LinkedHashMap<String, ServiceLoader.Provider<Annotator>> providers;
    providers = new LinkedHashMap<>();
    loader.stream().forEach(provider -> {
        Class<? extends Annotator> type = provider.type();
        if (!type.isAnnotationPresent(Service.class)) {
            throw new RuntimeException("Annotator services must be annotated with distinct selectors\n" + "such as @Service(Annotator.class,selector=\"myimpl42\")");
        }
        Service service = type.getAnnotation(Service.class);
        providers.put(service.selector(), provider);
    });
    return providers;
}
Also used : Annotator(io.nosqlbench.nb.api.annotations.Annotator) Service(io.nosqlbench.nb.annotations.Service)

Example 3 with Annotator

use of io.nosqlbench.nb.api.annotations.Annotator in project nosqlbench by nosqlbench.

the class Annotators method recordAnnotation.

public static synchronized void recordAnnotation(Annotation annotation) {
    for (Annotator annotator : getAnnotators()) {
        try {
            logger.trace("calling annotator " + annotator.getClass().getAnnotation(Service.class).selector());
            annotator.recordAnnotation(annotation);
        } catch (Exception e) {
            logger.error(e);
        }
    }
}
Also used : Annotator(io.nosqlbench.nb.api.annotations.Annotator) Service(io.nosqlbench.nb.annotations.Service)

Aggregations

Annotator (io.nosqlbench.nb.api.annotations.Annotator)3 Service (io.nosqlbench.nb.annotations.Service)2 ConfigLoader (io.nosqlbench.nb.api.config.standard.ConfigLoader)1 NBConfigurable (io.nosqlbench.nb.api.config.standard.NBConfigurable)1 NBConfiguration (io.nosqlbench.nb.api.config.standard.NBConfiguration)1 NBMapConfigurable (io.nosqlbench.nb.api.config.standard.NBMapConfigurable)1