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

com.ksc.regions.Region Maven / Gradle / Ivy

Go to download

The KSC SDK for Java - Core module holds the classes that is used by the individual service clients to interact with KSC Web Services. Users need to depend on KSC-java-sdk artifact for accessing individual client classes.

The newest version!
/*
 * Copyright 2013-2016 ksyun.com, Inc. or its affiliates. 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.
 * A copy of the License is located at
 *
 *  http://ksyun.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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.ksc.regions;

import com.ksc.KscWebServiceClient;
import com.ksc.ClientConfiguration;
import com.ksc.auth.AWSCredentialsProvider;
import com.ksc.util.ValidationUtils;

import java.lang.reflect.Constructor;
import java.util.Collection;

/**
 * Metadata for an KSC region, including its name and what services
 * are available in it.
 */
public class Region {

    private final RegionImpl regionImpl;

    public Region(RegionImpl regionImpl) {
        ValidationUtils.assertNotNull(regionImpl, "region implementation");
        this.regionImpl = regionImpl;
    }

    /**
     * Returns the region with the id given, or null if it cannot be found in
     * the current regions.xml file.
     */
    public static Region getRegion(Regions region) {
        return RegionUtils.getRegion(region.getName());
    }

    /**
     * The unique system ID for this region; ex: "us-east-1".
     *
     * @return The unique system ID for this region.
     */
    public String getName() {
        return regionImpl.getName();
    }

    /**
     * Returns the domain for this region; ex: "ksyun.com".
     *
     * @return The domain for this region.
     */
    public String getDomain() {
        return regionImpl.getDomain();
    }

    /**
     * Returns the partition this region is in. I.E. 'ksc' or 'ksc-cn'
     *
     * @return The partition this region is in.
     */
    public String getPartition() {
        return regionImpl.getPartition();
    }

    /**
     * Returns the endpoint for the service given.
     *
     * @see ServiceAbbreviations
     */
    public String getServiceEndpoint(String serviceName) {
        return regionImpl.getServiceEndpoint(serviceName);
    }

    /**
     * Returns whether the given service is supported in this region.
     *
     * @see ServiceAbbreviations
     */
    public boolean isServiceSupported(String serviceName) {
        return regionImpl.isServiceSupported(serviceName);
    }

    /**
     * Returns whether the given service support the https protocol in this region.
     *
     * @see ServiceAbbreviations
     */
    public boolean hasHttpsEndpoint(String serviceName) {
        return regionImpl.hasHttpsEndpoint(serviceName);
    }

    /**
     * Returns whether the given service support the http protocol in this region.
     *
     * @see ServiceAbbreviations
     */
    public boolean hasHttpEndpoint(String serviceName) {
        return regionImpl.hasHttpEndpoint(serviceName);
    }

    /**
     * Returns a immutable collection of all endpoints available in the
     * metadata.
     */
    public Collection getAvailableEndpoints() {
        return regionImpl.getAvailableEndpoints();
    }

    /**
     * Creates a new service client of the class given and configures it. If
     * credentials or config are null, defaults will be used.
     *
     * @param serviceClass The service client class to instantiate, e.g. AmazonS3Client.class
     * @param credentials  The credentials provider to use, or null for the default
     *                     credentials provider
     * @param config       The configuration to use, or null for the default
     *                     configuration
     * @see ServiceAbbreviations
     */
    public  T createClient(Class serviceClass,
                                                             AWSCredentialsProvider credentials,
                                                             ClientConfiguration config) {
        Constructor constructor;
        T client;
        try {
            if (credentials == null && config == null) {
                constructor = serviceClass.getConstructor();
                client = constructor.newInstance();
            } else if (credentials == null) {
                constructor = serviceClass.getConstructor(ClientConfiguration.class);
                client = constructor.newInstance(config);
            } else if (config == null) {
                constructor = serviceClass.getConstructor(AWSCredentialsProvider.class);
                client = constructor.newInstance(credentials);
            } else {
                constructor = serviceClass.getConstructor(AWSCredentialsProvider.class, ClientConfiguration.class);
                client = constructor.newInstance(credentials, config);
            }

            client.setRegion(this);
            return client;
        } catch (Exception e) {
            throw new RuntimeException("Couldn't instantiate instance of " + serviceClass, e);
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Region == false)
            return false;

        Region region = (Region) obj;
        return this.getName().equals(region.getName());
    }

    @Override
    public int hashCode() {
        return getName().hashCode();
    }

    @Override
    public String toString() {
        return getName();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy