com.stehno.sanctuary.core.remote.S3RemoteStore.java Source code

Java tutorial

Introduction

Here is the source code for com.stehno.sanctuary.core.remote.S3RemoteStore.java

Source

/*
 * Copyright (c) 2011 Christopher J. Stehno
 *
 *    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.stehno.sanctuary.core.remote;

import com.amazonaws.AmazonWebServiceClient;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

/**
 * @author cjstehno
 */
public class S3RemoteStore implements RemoteStore {
    // FIXME: need to switch to the encrypted version before real use

    private static final String BUCKET_NAME_PREFIX = "sanctuary.";
    private static final Log log = LogFactory.getLog(S3RemoteStore.class);
    private final AmazonS3 amazonS3;
    private final Set<String> buckets = new HashSet<String>();

    public S3RemoteStore(String s3AccessKey, String s3SecretKey) {
        this.amazonS3 = new AmazonS3Client(new BasicAWSCredentials(s3AccessKey, s3SecretKey));
    }

    @Override
    public void init() {
        // load known bucket names
        for (final Bucket bucket : amazonS3.listBuckets()) {
            final String name = bucket.getName();
            if (name.startsWith(BUCKET_NAME_PREFIX)) {
                buckets.add(name);

                if (log.isDebugEnabled())
                    log.debug("Found bucket: " + name);
            }
        }

        if (log.isInfoEnabled())
            log.info("Initialized");
    }

    @Override
    public void destroy() {
        if (amazonS3 != null) {
            ((AmazonWebServiceClient) amazonS3).shutdown();
        }

        if (log.isInfoEnabled())
            log.info("Destroyed");
    }

    @Override
    public void addFile(File rootDirectory, File file) {
        storeFile(rootDirectory, file, "Added");
    }

    @Override
    public void updateFile(File rootDirectory, File file) {
        storeFile(rootDirectory, file, "Updated");
    }

    @Override
    public void deleteFile(File rootDirectory, File file) {
        final String bucketName = extractBucketName(rootDirectory);

        // if root is for unknown bucket, create it
        if (buckets.contains(bucketName)) {
            final String key = extractKey(rootDirectory, file);
            amazonS3.deleteObject(bucketName, key);

            if (log.isInfoEnabled())
                log.info("Deleted[" + bucketName + "]: " + key);
        }
    }

    private void storeFile(File rootDirectory, File file, String action) {
        final String bucketName = extractBucketName(rootDirectory);

        // if root is for unknown bucket, create it
        if (!buckets.contains(bucketName)) {
            final Bucket bucket = amazonS3.createBucket(bucketName);
            buckets.add(bucket.getName());

            if (log.isInfoEnabled())
                log.info("Created bucket: " + bucketName);
        }

        final String key = extractKey(rootDirectory, file);
        amazonS3.putObject(bucketName, key, file);

        if (log.isDebugEnabled())
            log.debug(action + "[" + bucketName + "]: " + key);
    }

    private String extractKey(File rootDirectory, File file) {
        return file.toURI().toString().replace(rootDirectory.toURI().toString(), "");
    }

    private String extractBucketName(File directory) {
        String name = directory.toURI().toString();
        name = name.substring(name.lastIndexOf(':') + 1);
        name = name.replace("/", "");
        name = name.replace(".", "");
        name = name.replace("-", "");
        name = name.replace(" ", "");
        return BUCKET_NAME_PREFIX + name.toLowerCase();
    }
}