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

com.hazelcast.client.spi.impl.AwsAddressProvider Maven / Gradle / Ivy

There is a newer version: 3.12.13
Show newest version
/*
 * Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
 *
 * 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.hazelcast.client.spi.impl;

import com.hazelcast.aws.AWSClient;
import com.hazelcast.client.config.ClientAwsConfig;
import com.hazelcast.client.connection.AddressProvider;
import com.hazelcast.client.util.AddressHelper;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.LoggingService;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.logging.Level;

/**
 * Calls the AWS API to load ip addresses related to given credentials.
 */
public class AwsAddressProvider implements AddressProvider {

    private final ILogger logger;
    private final AWSClient awsClient;
    private volatile Map privateToPublic;

    public AwsAddressProvider(ClientAwsConfig awsConfig, LoggingService loggingService) {
        awsClient = new AWSClient(awsConfig);
        logger = loggingService.getLogger(AwsAddressProvider.class);
    }

    @Override
    public Collection loadAddresses() {
        updateLookupTable();
        final Map lookupTable = getLookupTable();
        final Collection addresses = new ArrayList(lookupTable.size());

        for (String privateAddress : lookupTable.keySet()) {
            addresses.addAll(AddressHelper.getSocketAddresses(privateAddress));
        }
        return addresses;
    }

    private Map getLookupTable() {
        Map table = privateToPublic;
        return table != null ? table : Collections.emptyMap();
    }

    private void updateLookupTable() {
        try {
            privateToPublic = awsClient.getAddresses();
        } catch (Exception e) {
            logger.log(Level.WARNING, "Aws addresses are failed to load : " + e.getMessage());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy