| 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.openstack.swift.blobstore; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.blobstore.util.BlobStoreUtils.createParentIfNeededAsync; |
| 23 | |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Provider; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.blobstore.BlobStoreContext; |
| 31 | import org.jclouds.blobstore.domain.Blob; |
| 32 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 33 | import org.jclouds.blobstore.domain.PageSet; |
| 34 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 35 | import org.jclouds.blobstore.domain.internal.PageSetImpl; |
| 36 | import org.jclouds.blobstore.functions.BlobToHttpGetOptions; |
| 37 | import org.jclouds.blobstore.internal.BaseBlobStore; |
| 38 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 39 | import org.jclouds.blobstore.strategy.internal.FetchBlobMetadata; |
| 40 | import org.jclouds.blobstore.util.BlobUtils; |
| 41 | import org.jclouds.collect.Memoized; |
| 42 | import org.jclouds.domain.Location; |
| 43 | import org.jclouds.http.options.GetOptions; |
| 44 | import org.jclouds.openstack.swift.CommonSwiftClient; |
| 45 | import org.jclouds.openstack.swift.blobstore.functions.BlobStoreListContainerOptionsToListContainerOptions; |
| 46 | import org.jclouds.openstack.swift.blobstore.functions.BlobToObject; |
| 47 | import org.jclouds.openstack.swift.blobstore.functions.ContainerToResourceList; |
| 48 | import org.jclouds.openstack.swift.blobstore.functions.ContainerToResourceMetadata; |
| 49 | import org.jclouds.openstack.swift.blobstore.functions.ObjectToBlob; |
| 50 | import org.jclouds.openstack.swift.blobstore.functions.ObjectToBlobMetadata; |
| 51 | import org.jclouds.openstack.swift.domain.ContainerMetadata; |
| 52 | |
| 53 | import com.google.common.base.Function; |
| 54 | import com.google.common.base.Supplier; |
| 55 | import com.google.common.collect.Iterables; |
| 56 | |
| 57 | /** |
| 58 | * |
| 59 | * @author Adrian Cole |
| 60 | */ |
| 61 | @Singleton |
| 62 | public class SwiftBlobStore extends BaseBlobStore { |
| 63 | private final CommonSwiftClient sync; |
| 64 | private final ContainerToResourceMetadata container2ResourceMd; |
| 65 | private final BlobStoreListContainerOptionsToListContainerOptions container2ContainerListOptions; |
| 66 | private final ContainerToResourceList container2ResourceList; |
| 67 | private final ObjectToBlob object2Blob; |
| 68 | private final BlobToObject blob2Object; |
| 69 | private final ObjectToBlobMetadata object2BlobMd; |
| 70 | private final BlobToHttpGetOptions blob2ObjectGetOptions; |
| 71 | private final Provider<FetchBlobMetadata> fetchBlobMetadataProvider; |
| 72 | |
| 73 | @Inject |
| 74 | SwiftBlobStore(BlobStoreContext context, BlobUtils blobUtils, Supplier<Location> defaultLocation, |
| 75 | @Memoized Supplier<Set<? extends Location>> locations, CommonSwiftClient sync, |
| 76 | ContainerToResourceMetadata container2ResourceMd, |
| 77 | BlobStoreListContainerOptionsToListContainerOptions container2ContainerListOptions, |
| 78 | ContainerToResourceList container2ResourceList, ObjectToBlob object2Blob, BlobToObject blob2Object, |
| 79 | ObjectToBlobMetadata object2BlobMd, BlobToHttpGetOptions blob2ObjectGetOptions, |
| 80 | Provider<FetchBlobMetadata> fetchBlobMetadataProvider) { |
| 81 | super(context, blobUtils, defaultLocation, locations); |
| 82 | this.sync = sync; |
| 83 | this.container2ResourceMd = container2ResourceMd; |
| 84 | this.container2ContainerListOptions = container2ContainerListOptions; |
| 85 | this.container2ResourceList = container2ResourceList; |
| 86 | this.object2Blob = object2Blob; |
| 87 | this.blob2Object = blob2Object; |
| 88 | this.object2BlobMd = object2BlobMd; |
| 89 | this.blob2ObjectGetOptions = blob2ObjectGetOptions; |
| 90 | this.fetchBlobMetadataProvider = checkNotNull(fetchBlobMetadataProvider, "fetchBlobMetadataProvider"); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * This implementation invokes {@link CommonSwiftClient#listContainers} |
| 95 | */ |
| 96 | @Override |
| 97 | public PageSet<? extends StorageMetadata> list() { |
| 98 | return new Function<Set<ContainerMetadata>, org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata>>() { |
| 99 | public org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata> apply(Set<ContainerMetadata> from) { |
| 100 | return new PageSetImpl<StorageMetadata>(Iterables.transform(from, container2ResourceMd), null); |
| 101 | } |
| 102 | }.apply(sync.listContainers()); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * This implementation invokes {@link CommonSwiftClient#containerExists} |
| 107 | * |
| 108 | * @param container |
| 109 | * container name |
| 110 | */ |
| 111 | @Override |
| 112 | public boolean containerExists(String container) { |
| 113 | return sync.containerExists(container); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * This implementation invokes {@link CommonSwiftClient#putBucketInRegion} |
| 118 | * |
| 119 | * @param location |
| 120 | * currently ignored |
| 121 | * @param container |
| 122 | * container name |
| 123 | */ |
| 124 | @Override |
| 125 | public boolean createContainerInLocation(Location location, String container) { |
| 126 | return sync.createContainer(container); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * This implementation invokes {@link CommonSwiftClient#listObjects} |
| 131 | * |
| 132 | * @param container |
| 133 | * container name |
| 134 | */ |
| 135 | @Override |
| 136 | public PageSet<? extends StorageMetadata> list(String container, ListContainerOptions options) { |
| 137 | org.jclouds.openstack.swift.options.ListContainerOptions httpOptions = container2ContainerListOptions |
| 138 | .apply(options); |
| 139 | PageSet<? extends StorageMetadata> list = container2ResourceList.apply(sync.listObjects(container, httpOptions)); |
| 140 | return options.isDetailed() ? fetchBlobMetadataProvider.get().setContainerName(container).apply(list) : list; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * This implementation invokes {@link CommonSwiftClient#blobExists} |
| 145 | * |
| 146 | * @param container |
| 147 | * container name |
| 148 | * @param key |
| 149 | * file name |
| 150 | */ |
| 151 | @Override |
| 152 | public boolean blobExists(String container, String key) { |
| 153 | return sync.objectExists(container, key); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * This implementation invokes {@link CommonSwiftClient#getObjectInfo} |
| 158 | * |
| 159 | * @param container |
| 160 | * container name |
| 161 | * @param key |
| 162 | * file name |
| 163 | */ |
| 164 | @Override |
| 165 | public BlobMetadata blobMetadata(String container, String key) { |
| 166 | return object2BlobMd.apply(sync.getObjectInfo(container, key)); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * This implementation invokes {@link CommonSwiftClient#getObject} |
| 171 | * |
| 172 | * @param container |
| 173 | * container name |
| 174 | * @param key |
| 175 | * file name |
| 176 | */ |
| 177 | @Override |
| 178 | public Blob getBlob(String container, String key, org.jclouds.blobstore.options.GetOptions optionsList) { |
| 179 | GetOptions httpOptions = blob2ObjectGetOptions.apply(optionsList); |
| 180 | return object2Blob.apply(sync.getObject(container, key, httpOptions)); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * This implementation invokes {@link CommonSwiftClient#putObject} |
| 185 | * |
| 186 | * @param container |
| 187 | * container name |
| 188 | * @param blob |
| 189 | * object |
| 190 | */ |
| 191 | @Override |
| 192 | public String putBlob(String container, Blob blob) { |
| 193 | createParentIfNeededAsync(context.getAsyncBlobStore(), container, blob); |
| 194 | return sync.putObject(container, blob2Object.apply(blob)); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * This implementation invokes {@link CommonSwiftClient#removeObject} |
| 199 | * |
| 200 | * @param container |
| 201 | * container name |
| 202 | * @param key |
| 203 | * file name |
| 204 | */ |
| 205 | @Override |
| 206 | public void removeBlob(String container, String key) { |
| 207 | sync.removeObject(container, key); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | protected boolean deleteAndVerifyContainerGone(String container) { |
| 212 | sync.deleteContainerIfEmpty(container); |
| 213 | return !sync.containerExists(container); |
| 214 | } |
| 215 | } |