de.fischer.thotti.ec2.clients.EC2InternalClient.java Source code

Java tutorial

Introduction

Here is the source code for de.fischer.thotti.ec2.clients.EC2InternalClient.java

Source

/*
 * Copyright 2011 Oliver B. Fischer
 *
 * 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 de.fischer.thotti.ec2.clients;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.DescribeRegionsResult;
import com.amazonaws.services.ec2.model.Region;
import de.fischer.thotti.awscommon.AWSAccessCredentials;
import de.fischer.thotti.awscommon.AWSCommunicationException;
import de.fischer.thotti.awscommon.AWSRemoteServiceException;
import de.fischer.thotti.ec2.core.ThottiUnhandledException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class EC2InternalClient {
    private static Logger logger = LoggerFactory.getLogger(EC2InternalClient.class);

    /**
     * List of regions known at Amazon AWS.
     */
    private List<Region> awsRegions;

    private AmazonEC2Client amazonEC2Client;

    /**
     * Cache of all region contexts created by
     * this instance for reuse.
     */
    private Map<String, EC2RunnerContext> regionContextCache;

    /**
     * This context contains all information related the
     * the current processed region at Amazon AWS. Since
     * available resources can differ between the known
     * regions, these informations are bundled in a helper
     * context object.
     */
    private EC2RunnerContext regionContext;

    {
        regionContextCache = new HashMap<String, EC2RunnerContext>();
    }

    public EC2InternalClient(AWSAccessCredentials credentials) {
        if (null == credentials)
            throw new NullPointerException();

        MyAWSCredentials awsCredentials = new MyAWSCredentials();

        awsCredentials.setAccessKeyId(credentials.getAccessKeyId());
        awsCredentials.setSecretKey(credentials.getSecretKey());

        amazonEC2Client = new AmazonEC2Client(awsCredentials);
    }

    private void retrieveAWSRegionInformation() throws AWSCommunicationException {
        try {
            DescribeRegionsResult result = amazonEC2Client.describeRegions();
            awsRegions = result.getRegions();
        } catch (AmazonServiceException ase) {
            handleAmazonServiceException(ase);
        }

        if (logger.isDebugEnabled())
            for (Region r : awsRegions) {
                logger.debug("Found the following region '{}'.", new Object[] { r.getRegionName() });
            }
    }

    protected List<Region> getAWSRegions() throws AWSCommunicationException {
        if (awsRegions == null)
            retrieveAWSRegionInformation();

        return awsRegions;
    }

    public AmazonEC2Client getClient() {
        return amazonEC2Client;
    }

    protected void switchCommunicationEndPoint() {
        if (logger.isDebugEnabled())
            logger.debug("Switching Amazon AWS webservice endpoint to '{}'.",
                    new Object[] { regionContext.endPoint });

        getClient().setEndpoint(regionContext.endPoint);
    }

    /**
     * Handles an {@link AmazonServiceException} by logging the information
     * provided by Amazon AWS and turning the caught exception into
     * an {@link de.fischer.thotti.awscommon.AWSRemoteServiceException}.
     */
    protected void handleAmazonServiceException(AmazonServiceException exception) throws AWSRemoteServiceException {
        if (logger.isErrorEnabled()) {
            String message = exception.getMessage();
            String code = exception.getErrorCode();
            AmazonServiceException.ErrorType type = exception.getErrorType();
            String requestId = exception.getRequestId();
            String service = exception.getServiceName();

            logger.error("Amazon AWS was not able to fullfil the last operation. "
                    + "Error message = {}, error code = {}, error type = {}, " + "service = {}, requestID = {}",
                    new Object[] { message, code, type, requestId, service });
        }

        throw new AWSRemoteServiceException(exception.getMessage());
    }

    protected void handleAmazonClientException(AmazonClientException ace) {
        // @todo add proper exception handling!
        // @todo add proper logging messages
        // @todo warn about already created instances
        throw new ThottiUnhandledException(ace);
    }

    public EC2RunnerContext getRegionContext() {
        return regionContext;
    }

    public void setRegionContext(EC2RunnerContext regionContext) {
        this.regionContext = regionContext;
    }

    public Map<String, EC2RunnerContext> getRegionContextCache() {
        return regionContextCache;
    }

    static class MyAWSCredentials implements AWSCredentials {
        private String accessKeyId;
        private String secretKey;

        public void setAccessKeyId(String key) {
            accessKeyId = key;
        }

        public void setSecretKey(String key) {
            secretKey = key;
        }

        @Override
        public String getAWSAccessKeyId() {
            return accessKeyId;
        }

        @Override
        public String getAWSSecretKey() {
            return secretKey;
        }
    }
}