| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.s3.blobstore.config; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
| 24 | import javax.inject.Singleton; |
| 25 | |
| 26 | import org.jclouds.blobstore.AsyncBlobStore; |
| 27 | import org.jclouds.blobstore.BlobRequestSigner; |
| 28 | import org.jclouds.blobstore.BlobStore; |
| 29 | import org.jclouds.blobstore.BlobStoreContext; |
| 30 | import org.jclouds.blobstore.attr.ConsistencyModel; |
| 31 | import org.jclouds.blobstore.config.BlobStoreMapModule; |
| 32 | import org.jclouds.blobstore.internal.BlobStoreContextImpl; |
| 33 | import org.jclouds.domain.Location; |
| 34 | import org.jclouds.location.config.RegionsLocationModule; |
| 35 | import org.jclouds.s3.S3AsyncClient; |
| 36 | import org.jclouds.s3.S3Client; |
| 37 | import org.jclouds.s3.blobstore.S3AsyncBlobStore; |
| 38 | import org.jclouds.s3.blobstore.S3BlobRequestSigner; |
| 39 | import org.jclouds.s3.blobstore.S3BlobStore; |
| 40 | import org.jclouds.s3.blobstore.functions.LocationFromBucketLocation; |
| 41 | import org.jclouds.s3.domain.AccessControlList; |
| 42 | import org.jclouds.s3.domain.BucketMetadata; |
| 43 | |
| 44 | import com.google.common.base.Function; |
| 45 | import com.google.common.collect.MapMaker; |
| 46 | import com.google.inject.AbstractModule; |
| 47 | import com.google.inject.Provides; |
| 48 | import com.google.inject.Scopes; |
| 49 | import com.google.inject.TypeLiteral; |
| 50 | |
| 51 | /** |
| 52 | * Configures the {@link S3BlobStoreContext}; requires {@link S3AsyncBlobStore} bound. |
| 53 | * |
| 54 | * @author Adrian Cole |
| 55 | */ |
| 56 | public class S3BlobStoreContextModule extends AbstractModule { |
| 57 | |
| 58 | @Override |
| 59 | protected void configure() { |
| 60 | install(new BlobStoreMapModule()); |
| 61 | install(new RegionsLocationModule()); |
| 62 | bind(ConsistencyModel.class).toInstance(ConsistencyModel.EVENTUAL); |
| 63 | bind(AsyncBlobStore.class).to(S3AsyncBlobStore.class).in(Scopes.SINGLETON); |
| 64 | bind(BlobStore.class).to(S3BlobStore.class).in(Scopes.SINGLETON); |
| 65 | bindContext(); |
| 66 | bind(BlobRequestSigner.class).to(S3BlobRequestSigner.class); |
| 67 | bindBucketLocationStrategy(); |
| 68 | } |
| 69 | |
| 70 | protected void bindContext() { |
| 71 | bind(BlobStoreContext.class).to(new TypeLiteral<BlobStoreContextImpl<S3Client, S3AsyncClient>>() { |
| 72 | }).in(Scopes.SINGLETON); |
| 73 | } |
| 74 | |
| 75 | protected void bindBucketLocationStrategy() { |
| 76 | bind(new TypeLiteral<Function<BucketMetadata, Location>>() { |
| 77 | }).to(LocationFromBucketLocation.class); |
| 78 | } |
| 79 | |
| 80 | @Provides |
| 81 | @Singleton |
| 82 | protected Map<String, AccessControlList> bucketAcls(final S3Client client) { |
| 83 | return new MapMaker().expireAfterWrite(30, TimeUnit.SECONDS).makeComputingMap( |
| 84 | new Function<String, AccessControlList>() { |
| 85 | public AccessControlList apply(String bucketName) { |
| 86 | return client.getBucketACL(bucketName); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public String toString() { |
| 91 | return "getBucketAcl()"; |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | } |