Package com.amazonaws.hal

Source Code of com.amazonaws.hal.HalService

/*
* Copyright 2010-2013 Amazon.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://aws.amazon.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.amazonaws.hal;


import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWS4Signer;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.auth.Signer;
import com.amazonaws.hal.client.HalClient;

import java.util.Map;


/**
* The HalService simplifies the configuration and access to an AWS HAL-based service API.  It is constructed
* with a service endpoint, the interface class that describes the service's root resource, and an optional path
* to the service root (if not specified it is assumed to be "/").  A HalService can be further configured,
* using the builder pattern, to use a particular AWSCredentialsProvider or ClientConfiguration.
*
* Once configured, this class is used to retrieve the service's root resource.
*
* @param <T> The type of the root resource.
*/
public class HalService<T> {

    //-------------------------------------------------------------
    // Variables - Private
    //-------------------------------------------------------------

    private String endpoint;
    private Class<T> rootClass;
    private String rootPath;
    private ClientConfiguration clientConfiguration;
    private AWSCredentialsProvider awsCredentialsProvider;
    private Signer signer;
    private Map<String, Object> resourceCache;
    private HalClient halClient;

    public static String DEFAULT_ROOT_PATH = "/";


    //-------------------------------------------------------------
    // Constructors
    //-------------------------------------------------------------

    public HalService(String endpoint, Class<T> rootClass) {
        this(endpoint, rootClass, DEFAULT_ROOT_PATH);
    }


    public HalService(String endpoint, Class<T> rootClass, String rootPath) {
        this.endpoint = endpoint;
        this.rootClass = rootClass;
        this.rootPath = rootPath;
    }


    //-------------------------------------------------------------
    // Methods - Configuration
    //-------------------------------------------------------------

    public HalService<T> with(ClientConfiguration clientConfiguration) {
        setClientConfiguration(clientConfiguration);

        return this;
    }


    public void setClientConfiguration(ClientConfiguration clientConfiguration) {
        this.clientConfiguration = clientConfiguration;
    }


    public HalService<T> with(AWSCredentialsProvider awsCredentialsProvider) {
        setAwsCredentialsProvider(awsCredentialsProvider);

        return this;
    }


    public void setAwsCredentialsProvider(AWSCredentialsProvider awsCredentialsProvider) {
        this.awsCredentialsProvider = awsCredentialsProvider;
    }


    public HalService<T> with(Signer signer) {
        setSigner(signer);

        return this;
    }


    public void setSigner(Signer signer) {
        this.signer = signer;
    }


    public HalService<T> with(Map<String, Object> resourceCache) {
        setResourceCache(resourceCache);

        return this;
    }


    public void setResourceCache(Map<String, Object> resourceCache) {
        this.resourceCache = resourceCache;
    }


    //-------------------------------------------------------------
    // Methods - Public
    //-------------------------------------------------------------

    public T getRootResource() {
        return getHalClient().getResource(rootClass, rootPath);
    }


    //-------------------------------------------------------------
    // Methods - Private
    //-------------------------------------------------------------

    private HalClient getHalClient() {
        if (halClient == null) {
            this.halClient = new HalClient(clientConfiguration == null ? new ClientConfiguration() : clientConfiguration,
                                           endpoint,
                                           awsCredentialsProvider == null ? new DefaultAWSCredentialsProviderChain() : awsCredentialsProvider,
                                           signer == null ? new AWS4Signer() : signer,
                                           resourceCache == null ? ImmediatelyExpiringCache.getInstance() : resourceCache);
        }

        return halClient;
    }
}
TOP

Related Classes of com.amazonaws.hal.HalService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.