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

com.arpnetworking.utility.Reconfigurator Maven / Gradle / Ivy

Go to download

(Re)Aggregates host level statistics across clusters and writes both host and cluster statistics to various destinations.

There is a newer version: 1.13.7
Show newest version
/*
 * Copyright 2015 Groupon.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.arpnetworking.utility;

import com.arpnetworking.configuration.Configuration;
import com.arpnetworking.configuration.Listener;
import com.arpnetworking.logback.annotations.LogValue;
import com.arpnetworking.steno.LogValueMapFactory;

import java.util.Optional;

/**
 * Manages configuration and reconfiguration of a Relaunchable instance
 * using a POJO representation of its configuration. The Relaunchable
 * is updated with each new configuration. The configuration must validate
 * on construction and throw an exception if the configuration is invalid.
 *
 * @param  The Relaunchable type to configure.
 * @param  The type representing the validated configuration.
 *
 * @author Ville Koskela (ville dot koskela at inscopemetrics dot com)
 */
public class Reconfigurator, S> implements Listener {

    /**
     * Public constructor.
     *
     * @param relaunchable The Relaunchable instance.
     * @param configurationClass The configuration class.
     */
    public Reconfigurator(final Relaunchable relaunchable, final Class configurationClass) {
        _relaunchable = relaunchable;
        _configurationClass = configurationClass;
    }

    @Override
    public synchronized void offerConfiguration(final Configuration configuration) throws Exception {
        _offeredConfiguration = configuration.getAs(_configurationClass);
    }

    @Override
    public synchronized void applyConfiguration() {
        // Swap configurations
        _configuration = _offeredConfiguration;
        _relaunchable.relaunch(_configuration.get());
    }

    /**
     * Generate a Steno log compatible representation.
     *
     * @return Steno log compatible representation.
     */
    @LogValue
    public Object toLogValue() {
        return LogValueMapFactory.builder(this)
                .put("configurationClass", _configurationClass)
                .put("relaunchable", _relaunchable)
                .build();
    }

    @Override
    public String toString() {
        return toLogValue().toString();
    }

    /* package private */ synchronized Relaunchable getRelaunchable() {
        return _relaunchable;
    }

    /* package private */ synchronized Optional getConfiguration() {
        return _configuration;
    }

    /* package private */ synchronized Optional getOfferedConfiguration() {
        return _offeredConfiguration;
    }

    private final Relaunchable _relaunchable;
    private final Class _configurationClass;

    private Optional _configuration = Optional.empty();
    private Optional _offeredConfiguration = Optional.empty();
}