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

com.bytex.snamp.configuration.impl.InMemoryConfigurationManager Maven / Gradle / Ivy

package com.bytex.snamp.configuration.impl;

import com.bytex.snamp.AbstractAggregator;
import com.bytex.snamp.Acceptor;
import com.bytex.snamp.concurrent.ConcurrentResourceAccessor;
import com.bytex.snamp.configuration.AgentConfiguration;
import com.bytex.snamp.configuration.ConfigurationManager;
import com.google.common.collect.ImmutableMap;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.function.Function;

/**
 * Represents in-memory configuration manager.
 * This class cannot be inherited.
 * @author Roman Sakno
 * @version 2.0
 * @since 1.2
 */
public final class InMemoryConfigurationManager extends AbstractAggregator implements ConfigurationManager {
    private final ConcurrentResourceAccessor currentConfiguration =
            new ConcurrentResourceAccessor<>(new SerializableAgentConfiguration());

    @Nonnull
    @Override
    public ImmutableMap getConfiguration() {
        return currentConfiguration.read(ImmutableMap::copyOf);
    }

    /**
     * Process SNAMP configuration.
     *
     * @param handler A handler used to process configuration. Cannot be {@literal null}.
     * @throws E           An exception thrown by handler.
     * @throws IOException Unrecoverable exception thrown by configuration infrastructure.
     * @since 1.2
     */
    @Override
    public  void processConfiguration(final ConfigurationProcessor handler) throws E, IOException {
        currentConfiguration.changeResource(config -> {
            final SerializableAgentConfiguration copy = config.clone();
            return handler.process(copy) ? copy : config;
        });
    }

    /**
     * Read SNAMP configuration.
     *
     * @param handler A handler used to read configuration. Cannot be {@literal null}.
     * @throws E           An exception thrown by handler.
     * @throws IOException Unrecoverable exception thrown by configuration infrastructure.
     * @since 1.2
     */
    @Override
    public  void readConfiguration(final Acceptor handler) throws E, IOException {
        currentConfiguration.read(config -> {
            handler.accept(config);
            return null;
        });
    }

    /**
     * Read SNAMP configuration and transform it into custom object.
     *
     * @param handler A handler used to read configuration. Cannot be {@literal null}.
     * @throws IOException Unrecoverable exception thrown by configuration infrastructure.
     * @since 1.2
     */
    @Override
    public  O transformConfiguration(final Function handler) throws IOException {
        return currentConfiguration.read(handler::apply);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy