| 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.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.blobstore.options.ListContainerOptions.Builder.recursive; |
| 23 | |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | |
| 28 | import org.jclouds.blobstore.BlobStore; |
| 29 | import org.jclouds.blobstore.BlobStoreContext; |
| 30 | import org.jclouds.blobstore.ContainerNotFoundException; |
| 31 | import org.jclouds.blobstore.domain.Blob; |
| 32 | import org.jclouds.blobstore.domain.BlobBuilder; |
| 33 | import org.jclouds.blobstore.domain.PageSet; |
| 34 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 35 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 36 | import org.jclouds.blobstore.util.BlobUtils; |
| 37 | import org.jclouds.blobstore.util.internal.BlobUtilsImpl; |
| 38 | import org.jclouds.collect.Memoized; |
| 39 | import org.jclouds.domain.Location; |
| 40 | import org.jclouds.util.Assertions; |
| 41 | |
| 42 | import com.google.common.base.Supplier; |
| 43 | |
| 44 | /** |
| 45 | * |
| 46 | * @author Adrian Cole |
| 47 | */ |
| 48 | public abstract class BaseBlobStore implements BlobStore { |
| 49 | |
| 50 | protected final BlobStoreContext context; |
| 51 | protected final BlobUtils blobUtils; |
| 52 | protected final Supplier<Location> defaultLocation; |
| 53 | protected final Supplier<Set<? extends Location>> locations; |
| 54 | |
| 55 | @Inject |
| 56 | protected BaseBlobStore(BlobStoreContext context, BlobUtils blobUtils, Supplier<Location> defaultLocation, |
| 57 | @Memoized Supplier<Set<? extends Location>> locations) { |
| 58 | this.context = checkNotNull(context, "context"); |
| 59 | this.blobUtils = checkNotNull(blobUtils, "blobUtils"); |
| 60 | this.defaultLocation = checkNotNull(defaultLocation, "defaultLocation"); |
| 61 | this.locations = checkNotNull(locations, "locations"); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public BlobStoreContext getContext() { |
| 66 | return context; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * invokes {@link BlobUtilsImpl#newBlob } |
| 71 | */ |
| 72 | @Override |
| 73 | public Blob newBlob(String name) { |
| 74 | return blobUtils.newBlob(name); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * invokes {@link BlobUtilsImpl#blobBuilder } |
| 79 | */ |
| 80 | @Override |
| 81 | public BlobBuilder blobBuilder(String name) { |
| 82 | return blobUtils.blobBuilder().name(name); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * This implementation invokes |
| 87 | * {@link #list(String,org.jclouds.blobstore.options.ListContainerOptions)} |
| 88 | * |
| 89 | * @param container |
| 90 | * container name |
| 91 | */ |
| 92 | @Override |
| 93 | public PageSet<? extends StorageMetadata> list(String container) { |
| 94 | return this.list(container, org.jclouds.blobstore.options.ListContainerOptions.NONE); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * This implementation invokes {@link BlobUtilsImpl#directoryExists} |
| 99 | * |
| 100 | * @param container |
| 101 | * container name |
| 102 | * @param directory |
| 103 | * virtual path |
| 104 | */ |
| 105 | @Override |
| 106 | public boolean directoryExists(String containerName, String directory) { |
| 107 | return blobUtils.directoryExists(containerName, directory); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * This implementation invokes {@link BlobUtilsImpl#createDirectory} |
| 112 | * |
| 113 | * @param container |
| 114 | * container name |
| 115 | * @param directory |
| 116 | * virtual path |
| 117 | */ |
| 118 | @Override |
| 119 | public void createDirectory(String containerName, String directory) { |
| 120 | blobUtils.createDirectory(containerName, directory); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * This implementation invokes {@link #countBlobs} with the |
| 125 | * {@link ListContainerOptions#recursive} option. |
| 126 | * |
| 127 | * @param container |
| 128 | * container name |
| 129 | */ |
| 130 | @Override |
| 131 | public long countBlobs(String container) { |
| 132 | return countBlobs(container, recursive()); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * This implementation invokes {@link BlobUtilsImpl#countBlobs} |
| 137 | * |
| 138 | * @param container |
| 139 | * container name |
| 140 | */ |
| 141 | @Override |
| 142 | public long countBlobs(String containerName, ListContainerOptions options) { |
| 143 | return blobUtils.countBlobs(containerName, options); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * This implementation invokes {@link #clearContainer} with the |
| 148 | * {@link ListContainerOptions#recursive} option. |
| 149 | * |
| 150 | * @param container |
| 151 | * container name |
| 152 | */ |
| 153 | @Override |
| 154 | public void clearContainer(String containerName) { |
| 155 | clearContainer(containerName, recursive()); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * This implementation invokes {@link BlobUtilsImpl#clearContainer} |
| 160 | * |
| 161 | * @param container |
| 162 | * container name |
| 163 | */ |
| 164 | @Override |
| 165 | public void clearContainer(String containerName, ListContainerOptions options) { |
| 166 | blobUtils.clearContainer(containerName, options); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * This implementation invokes {@link BlobUtilsImpl#deleteDirectory}. |
| 171 | * |
| 172 | * @param container |
| 173 | * container name |
| 174 | */ |
| 175 | @Override |
| 176 | public void deleteDirectory(String containerName, String directory) { |
| 177 | blobUtils.deleteDirectory(containerName, directory); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * This implementation invokes |
| 182 | * {@link #getBlob(String,String,org.jclouds.blobstore.options.GetOptions)} |
| 183 | * |
| 184 | * @param container |
| 185 | * container name |
| 186 | * @param key |
| 187 | * blob key |
| 188 | */ |
| 189 | @Override |
| 190 | public Blob getBlob(String container, String key) { |
| 191 | return getBlob(container, key, org.jclouds.blobstore.options.GetOptions.NONE); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * This implementation invokes {@link #deleteAndEnsurePathGone} |
| 196 | * |
| 197 | * @param container |
| 198 | * bucket name |
| 199 | */ |
| 200 | @Override |
| 201 | public void deleteContainer(final String container) { |
| 202 | clearAndDeleteContainer(container); |
| 203 | } |
| 204 | |
| 205 | protected void clearAndDeleteContainer(final String container) { |
| 206 | try { |
| 207 | if (!Assertions.eventuallyTrue(new Supplier<Boolean>() { |
| 208 | public Boolean get() { |
| 209 | try { |
| 210 | clearContainer(container, recursive()); |
| 211 | return deleteAndVerifyContainerGone(container); |
| 212 | } catch (ContainerNotFoundException e) { |
| 213 | return true; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | }, 30000)) { |
| 218 | throw new IllegalStateException(container + " still exists after deleting!"); |
| 219 | } |
| 220 | } catch (InterruptedException e) { |
| 221 | new IllegalStateException(container + " interrupted during deletion!", e); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public Set<? extends Location> listAssignableLocations() { |
| 227 | return locations.get(); |
| 228 | } |
| 229 | |
| 230 | protected abstract boolean deleteAndVerifyContainerGone(String container); |
| 231 | |
| 232 | } |