de.fischer.thotti.s3.clients.S3InternalClient.java Source code

Java tutorial

Introduction

Here is the source code for de.fischer.thotti.s3.clients.S3InternalClient.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.s3.clients;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import de.fischer.thotti.awscommon.AWSAccessCredentials;
import de.fischer.thotti.core.logging.LoggingSupport;
import de.fischer.thotti.core.logging.SLF4JLogger;
import de.fischer.thotti.s3.S3ClientConstants;

/**
 * Abstract base class for all S3 client implementation providing
 * the functionality used by the {@link de.fischer.thotti.s3.S3Client}.
 *
 * @author Oliver B. Fischer
 *
 * @see de.fischer.thotti.s3.S3Client
 */
// @todo Reuse the error handling code from the EC2 Internal client!
public abstract class S3InternalClient {
    private AmazonS3Client amazonS3Client;

    private LoggingSupport loggingSupport;

    protected S3InternalClient(AWSAccessCredentials credentials, LoggingSupport logger) {
        if (null == credentials) {
            throw new NullPointerException();
        }

        MyAWSCredentials myCredentials = new MyAWSCredentials();

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

        amazonS3Client = new AmazonS3Client(myCredentials);

        if (null == logger) {
            loggingSupport = new SLF4JLogger(S3ClientConstants.S3_LOGGER_DOMAIN);
        } else {
            loggingSupport = logger;
        }
    }

    protected AmazonS3Client getAmazonS3Client() {
        return amazonS3Client;
    }

    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;
        }
    }

    public LoggingSupport getLoggingSupport() {
        return loggingSupport;
    }
}