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

io.deepsense.neptune.clientlibrary.services.serverconfigurationprovider.ServerConfigurationProvider Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.services.serverconfigurationprovider;

import com.google.common.base.Preconditions;
import io.deepsense.neptune.apiclient.ApiException;
import io.deepsense.neptune.apiclient.model.ConfigInfo;
import io.deepsense.neptune.clientlibrary.config.internal.InternalConfiguration;
import io.deepsense.neptune.clientlibrary.services.apiservice.ApiService;
import io.deepsense.neptune.clientlibrary.utils.exceptions.ApiExceptionWrapper;

import java.util.Optional;

public class ServerConfigurationProvider {

    private final InternalConfiguration internalConfiguration;

    private final ApiService apiService;

    private ConfigInfo configInfo;

    public ServerConfigurationProvider(InternalConfiguration internalConfiguration, ApiService apiService) {
        this.internalConfiguration = Preconditions.checkNotNull(internalConfiguration);
        this.apiService = Preconditions.checkNotNull(apiService);
    }

    public ConfigInfo getConfigInfo() {
        if (isConfigInfoReady()) {
            return configInfo;
        } else {
            try {
                configInfo = apiService.getConfigInfo();
                return configInfo;
            } catch (ApiException exc) {
                throw ApiExceptionWrapper.wrappedApiException(exc);
            }
        }
    }

    public long getMaxChannelValueSize() {
        requireConfigInfo();
        double maxBatchSizeAsFractionOfRequestSize =
                internalConfiguration.getChannelValueBufferConfiguration().getMaxBatchSizeAsFractionOfRequestSize();
        long maxFormContentSize = configInfo.getMaxFormContentSize();
        return (long) (maxBatchSizeAsFractionOfRequestSize * (double) maxFormContentSize);
    }

    private boolean isConfigInfoReady() {
        return Optional.ofNullable(configInfo).isPresent();
    }

    private void requireConfigInfo() {
        if (!isConfigInfoReady()) {
            getConfigInfo();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy