| 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.blobstore.internal; |
| 20 | |
| 21 | import static com.google.common.base.Functions.identity; |
| 22 | import static com.google.common.base.Preconditions.checkArgument; |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | import static com.google.common.collect.Iterables.transform; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import javax.inject.Inject; |
| 30 | |
| 31 | import org.jclouds.blobstore.BlobStore; |
| 32 | import org.jclouds.blobstore.ListableMap; |
| 33 | import org.jclouds.blobstore.domain.Blob; |
| 34 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 35 | import org.jclouds.blobstore.domain.MutableBlobMetadata; |
| 36 | import org.jclouds.blobstore.domain.internal.MutableBlobMetadataImpl; |
| 37 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 38 | import org.jclouds.blobstore.options.ListContainerOptions.ImmutableListContainerOptions; |
| 39 | import org.jclouds.blobstore.strategy.ContainsValueInListStrategy; |
| 40 | import org.jclouds.blobstore.strategy.GetBlobsInListStrategy; |
| 41 | import org.jclouds.blobstore.strategy.PutBlobsStrategy; |
| 42 | import org.jclouds.blobstore.strategy.internal.ListContainerAndRecurseThroughFolders; |
| 43 | |
| 44 | import com.google.common.base.Function; |
| 45 | import com.google.common.collect.ImmutableSet; |
| 46 | |
| 47 | /** |
| 48 | * Implements core Map functionality with a {@link BlobStore} |
| 49 | * |
| 50 | * |
| 51 | * @author Adrian Cole |
| 52 | */ |
| 53 | public abstract class BaseBlobMap<V> implements ListableMap<String, V> { |
| 54 | protected final BlobStore blobstore; |
| 55 | protected final String containerName; |
| 56 | protected final Function<String, String> prefixer; |
| 57 | protected final Function<String, String> pathStripper; |
| 58 | protected final ListContainerOptions options; |
| 59 | protected final GetBlobsInListStrategy getAllBlobs; |
| 60 | protected final ContainsValueInListStrategy containsValueStrategy; |
| 61 | protected final ListContainerAndRecurseThroughFolders listStrategy; |
| 62 | protected final PutBlobsStrategy putBlobsStrategy; |
| 63 | |
| 64 | static class StripPath implements Function<String, String> { |
| 65 | private final String prefix; |
| 66 | private final String delimiter; |
| 67 | |
| 68 | StripPath(String prefix, String delimiter) { |
| 69 | this.prefix = checkNotNull(prefix, "prefix"); |
| 70 | this.delimiter = checkNotNull(delimiter, "delimiter"); |
| 71 | } |
| 72 | |
| 73 | public String apply(String from) { |
| 74 | return from.replaceFirst(prefix + delimiter, ""); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static class PrefixKey implements Function<String, String> { |
| 79 | private final String prefix; |
| 80 | private final String delimiter; |
| 81 | |
| 82 | PrefixKey(String prefix, String delimiter) { |
| 83 | this.prefix = checkNotNull(prefix, "prefix"); |
| 84 | this.delimiter = checkNotNull(delimiter, "delimiter"); |
| 85 | } |
| 86 | |
| 87 | public String apply(String from) { |
| 88 | return prefix + delimiter + from; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @Inject |
| 93 | public BaseBlobMap(BlobStore blobstore, GetBlobsInListStrategy getAllBlobs, |
| 94 | ContainsValueInListStrategy containsValueStrategy, PutBlobsStrategy putBlobsStrategy, |
| 95 | ListContainerAndRecurseThroughFolders listStrategy, String containerName, ListContainerOptions options) { |
| 96 | this.blobstore = checkNotNull(blobstore, "blobstore"); |
| 97 | this.containerName = checkNotNull(containerName, "container"); |
| 98 | checkArgument(containerName.indexOf('/') == -1, |
| 99 | "please specify directory path using the option: inDirectory, not encoded in the container name"); |
| 100 | this.options = checkNotNull(options, "options") instanceof ImmutableListContainerOptions ? options |
| 101 | : new ImmutableListContainerOptions(options); |
| 102 | String dir = options.getDir(); |
| 103 | if (dir == null) { |
| 104 | prefixer = identity(); |
| 105 | pathStripper = prefixer; |
| 106 | } else { |
| 107 | prefixer = new PrefixKey(dir, "/"); |
| 108 | pathStripper = new StripPath(dir, "/"); |
| 109 | } |
| 110 | this.getAllBlobs = checkNotNull(getAllBlobs, "getAllBlobs"); |
| 111 | this.listStrategy = checkNotNull(listStrategy, "listStrategy"); |
| 112 | this.containsValueStrategy = checkNotNull(containsValueStrategy, "containsValueStrategy"); |
| 113 | this.putBlobsStrategy = checkNotNull(putBlobsStrategy, "putBlobsStrategy"); |
| 114 | checkArgument(!containerName.equals(""), "container name must not be a blank string!"); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Set<java.util.Map.Entry<String, V>> entrySet() { |
| 119 | return ImmutableSet.copyOf(transform(list(), new Function<BlobMetadata, Map.Entry<String, V>>() { |
| 120 | @Override |
| 121 | public java.util.Map.Entry<String, V> apply(BlobMetadata from) { |
| 122 | return new Entry(pathStripper.apply(from.getName())); |
| 123 | } |
| 124 | })); |
| 125 | } |
| 126 | |
| 127 | public class Entry implements java.util.Map.Entry<String, V> { |
| 128 | |
| 129 | private final String key; |
| 130 | |
| 131 | Entry(String key) { |
| 132 | this.key = key; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public String getKey() { |
| 137 | return key; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public V getValue() { |
| 142 | return get(prefixer.apply(key)); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public V setValue(V value) { |
| 147 | return put(prefixer.apply(key), value); |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public int size() { |
| 154 | return (int) blobstore.countBlobs(containerName, options); |
| 155 | } |
| 156 | |
| 157 | protected Iterable<Blob> getAllBlobs() { |
| 158 | Iterable<Blob> returnVal = getAllBlobs.execute(containerName, options); |
| 159 | if (options != null) { |
| 160 | for (Blob from : returnVal) |
| 161 | stripPrefix(from); |
| 162 | } |
| 163 | return returnVal; |
| 164 | } |
| 165 | |
| 166 | protected Blob stripPrefix(Blob from) { |
| 167 | from.getMetadata().setName(pathStripper.apply(from.getMetadata().getName())); |
| 168 | return from; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public boolean containsValue(Object value) { |
| 173 | return containsValueStrategy.execute(containerName, value, options); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void clear() { |
| 178 | blobstore.clearContainer(containerName, options); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public Set<String> keySet() { |
| 183 | return ImmutableSet.copyOf(transform(list(), new Function<BlobMetadata, String>() { |
| 184 | @Override |
| 185 | public String apply(BlobMetadata from) { |
| 186 | return from.getName(); |
| 187 | } |
| 188 | })); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public boolean containsKey(Object key) { |
| 193 | String realKey = prefixer.apply(checkNotNull(key, "key").toString()); |
| 194 | return blobstore.blobExists(containerName, realKey); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public boolean isEmpty() { |
| 199 | return size() == 0; |
| 200 | } |
| 201 | |
| 202 | public Iterable<? extends BlobMetadata> list() { |
| 203 | return transform(listStrategy.execute(containerName, options), new Function<BlobMetadata, BlobMetadata>() { |
| 204 | public BlobMetadata apply(BlobMetadata from) { |
| 205 | MutableBlobMetadata md = new MutableBlobMetadataImpl(from); |
| 206 | if (options.getDir() != null) |
| 207 | md.setName(pathStripper.apply(from.getName())); |
| 208 | return md; |
| 209 | } |
| 210 | |
| 211 | }); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public String toString() { |
| 216 | return "[containerName=" + containerName + ", options=" + options + "]"; |
| 217 | } |
| 218 | |
| 219 | } |