| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.blobstore.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.collect.Iterables.transform; |
| 23 | |
| 24 | import java.util.Collection; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Provider; |
| 29 | |
| 30 | import org.jclouds.blobstore.BlobMap; |
| 31 | import org.jclouds.blobstore.BlobStore; |
| 32 | import org.jclouds.blobstore.KeyNotFoundException; |
| 33 | import org.jclouds.blobstore.domain.Blob; |
| 34 | import org.jclouds.blobstore.domain.BlobBuilder; |
| 35 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 36 | import org.jclouds.blobstore.strategy.ContainsValueInListStrategy; |
| 37 | import org.jclouds.blobstore.strategy.GetBlobsInListStrategy; |
| 38 | import org.jclouds.blobstore.strategy.PutBlobsStrategy; |
| 39 | import org.jclouds.blobstore.strategy.internal.ListContainerAndRecurseThroughFolders; |
| 40 | |
| 41 | import com.google.common.base.Function; |
| 42 | import com.google.common.collect.ImmutableSet; |
| 43 | |
| 44 | /** |
| 45 | * Map representation of a live connection to a Blob Service. |
| 46 | * |
| 47 | * @see BlobStore |
| 48 | * @see BaseBlobMap |
| 49 | * |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | public class BlobMapImpl extends BaseBlobMap<Blob> implements BlobMap { |
| 53 | public static class CorrectBlobName implements Function<java.util.Map.Entry<? extends String, ? extends Blob>, Blob> { |
| 54 | private final Function<String, String> prefixer; |
| 55 | |
| 56 | public CorrectBlobName(Function<String, String> prefixer) { |
| 57 | this.prefixer = checkNotNull(prefixer, "prefixer"); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public Blob apply(java.util.Map.Entry<? extends String, ? extends Blob> arg0) { |
| 62 | return apply(arg0.getKey(), arg0.getValue()); |
| 63 | } |
| 64 | |
| 65 | public Blob apply(String key, Blob blob) { |
| 66 | blob.getMetadata().setName(prefixer.apply(key)); |
| 67 | return blob; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private final CorrectBlobName correctBlobName; |
| 72 | private final Provider<BlobBuilder> blobBuilders; |
| 73 | |
| 74 | @Inject |
| 75 | public BlobMapImpl(BlobStore blobstore, GetBlobsInListStrategy getAllBlobs, |
| 76 | ContainsValueInListStrategy containsValueStrategy, PutBlobsStrategy putBlobsStrategy, |
| 77 | ListContainerAndRecurseThroughFolders listStrategy, String containerName, ListContainerOptions options, |
| 78 | Provider<BlobBuilder> blobBuilders) { |
| 79 | super(blobstore, getAllBlobs, containsValueStrategy, putBlobsStrategy, listStrategy, containerName, options); |
| 80 | this.correctBlobName = new CorrectBlobName(prefixer); |
| 81 | this.blobBuilders = checkNotNull(blobBuilders, "blobBuilders"); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Blob get(Object key) { |
| 86 | String realKey = prefixer.apply(checkNotNull(key, "key").toString()); |
| 87 | Blob blob = blobstore.getBlob(containerName, realKey); |
| 88 | return blob != null ? stripPrefix(blob) : null; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Blob put(String key, Blob value) { |
| 93 | Blob returnVal = getLastValue(checkNotNull(key, "key")); |
| 94 | blobstore.putBlob(containerName, correctBlobName.apply(key, value)); |
| 95 | return returnVal; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void putAll(Map<? extends String, ? extends Blob> map) { |
| 100 | putBlobsStrategy.execute(containerName, transform(checkNotNull(map, "map").entrySet(), correctBlobName)); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Blob remove(Object key) { |
| 105 | Blob old = getLastValue(checkNotNull(key, "key")); |
| 106 | String realKey = prefixer.apply(key.toString()); |
| 107 | blobstore.removeBlob(containerName, realKey); |
| 108 | return old; |
| 109 | } |
| 110 | |
| 111 | private Blob getLastValue(Object key) { |
| 112 | Blob old; |
| 113 | try { |
| 114 | old = get(checkNotNull(key, "key")); |
| 115 | } catch (KeyNotFoundException e) { |
| 116 | old = null; |
| 117 | } |
| 118 | return old; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Collection<Blob> values() { |
| 123 | return ImmutableSet.copyOf(getAllBlobs.execute(containerName, options)); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Blob newBlob(String name) { |
| 128 | return blobBuilder().name(name).build(); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public BlobBuilder blobBuilder() { |
| 133 | return blobBuilders.get(); |
| 134 | } |
| 135 | } |