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

com.arpnetworking.utility.Configurator 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 2014 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 Launchable instance
 * using a POJO representation of its configuration. The Launchable
 * is instantiated with each new configuration. The configuration must validate
 * on construction and throw an exception if the configuration is invalid.
 *
 * @param  The Launchable type to configure.
 * @param  The type representing the validated configuration.
 *
 * @author Ville Koskela (ville dot koskela at inscopemetrics dot com)
 */
public class Configurator implements Listener, Launchable {

    /**
     * Public constructor.
     *
     * @param factory The factory to create a launchable.
     * @param configurationClass The configuration class.
     */
    public Configurator(final ConfiguredLaunchableFactory factory, final Class configurationClass) {
        _factory = factory;
        _configurationClass = configurationClass;
    }

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

    @SuppressWarnings("unchecked")
    @Override
    public synchronized void applyConfiguration() {
        // Shutdown
        shutdown();

        // Swap configurations
        _configuration = _offeredConfiguration;
        _launchable = Optional.of(_factory.create(_configuration.get()));

        // (Re)launch
        launch();
    }

    @Override
    public synchronized void launch() {
        if (_launchable.isPresent()) {
            _launchable.get().launch();
        }
    }

    @Override
    public synchronized void shutdown() {
        if (_launchable.isPresent()) {
            _launchable.get().shutdown();
            _launchable = Optional.empty();
        }
    }

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

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


    /* package private */ synchronized Optional getLaunchable() {
        return _launchable;
    }

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

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

    private final ConfiguredLaunchableFactory _factory;
    private final Class _configurationClass;

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