expose constructor for blobbuilderimpl

This commit is contained in:
Adrian Cole 2011-04-04 22:35:22 -07:00
parent f322931755
commit d69d31a5d2
2 changed files with 49 additions and 50 deletions

View File

@ -41,6 +41,7 @@ import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.BlobBuilder; import org.jclouds.blobstore.domain.BlobBuilder;
import org.jclouds.blobstore.domain.internal.BlobBuilderImpl; import org.jclouds.blobstore.domain.internal.BlobBuilderImpl;
import org.jclouds.blobstore.options.ListContainerOptions; import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.encryption.internal.JCECrypto;
import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyValidatorImpl; import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyValidatorImpl;
import org.jclouds.filesystem.predicates.validators.internal.FilesystemContainerNameValidatorImpl; import org.jclouds.filesystem.predicates.validators.internal.FilesystemContainerNameValidatorImpl;
import org.jclouds.filesystem.strategy.FilesystemStorageStrategy; import org.jclouds.filesystem.strategy.FilesystemStorageStrategy;
@ -76,7 +77,11 @@ public class FilesystemStorageStrategyImplTest {
storageStrategy = new FilesystemStorageStrategyImpl(new Provider<BlobBuilder>() { storageStrategy = new FilesystemStorageStrategyImpl(new Provider<BlobBuilder>() {
@Override @Override
public BlobBuilder get() { public BlobBuilder get() {
return new BlobBuilderImpl(); try {
return new BlobBuilderImpl(new JCECrypto());
} catch (Exception e) {
return null;
}
} }
}, TestUtils.TARGET_BASE_DIR, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl()); }, TestUtils.TARGET_BASE_DIR, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl());
@ -139,10 +144,9 @@ public class FilesystemStorageStrategyImplTest {
public void testDeleteDirectory() throws IOException { public void testDeleteDirectory() throws IOException {
TestUtils.createContainerAsDirectory(CONTAINER_NAME); TestUtils.createContainerAsDirectory(CONTAINER_NAME);
TestUtils.createBlobsInContainer( TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] {
CONTAINER_NAME, TestUtils.createRandomBlobKey("lev1" + FS + "lev2" + FS + "lev3" + FS, ".txt"),
new String[] { TestUtils.createRandomBlobKey("lev1" + FS + "lev2" + FS + "lev3" + FS, ".txt"), TestUtils.createRandomBlobKey("lev1" + FS + "lev2" + FS + "lev4" + FS, ".jpg") });
TestUtils.createRandomBlobKey("lev1" + FS + "lev2" + FS + "lev4" + FS, ".jpg") });
// delete directory in different ways // delete directory in different ways
storageStrategy.deleteDirectory(CONTAINER_NAME, "lev1" + FS + "lev2" + FS + "lev4"); storageStrategy.deleteDirectory(CONTAINER_NAME, "lev1" + FS + "lev2" + FS + "lev4");
@ -158,10 +162,9 @@ public class FilesystemStorageStrategyImplTest {
TestUtils.directoryExists(TARGET_CONTAINER_NAME, true); TestUtils.directoryExists(TARGET_CONTAINER_NAME, true);
// delete the directory and all the files inside // delete the directory and all the files inside
TestUtils.createBlobsInContainer( TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] {
CONTAINER_NAME, TestUtils.createRandomBlobKey("lev1" + FS + "lev2" + FS + "lev3" + FS, ".txt"),
new String[] { TestUtils.createRandomBlobKey("lev1" + FS + "lev2" + FS + "lev3" + FS, ".txt"), TestUtils.createRandomBlobKey("lev1" + FS + "lev2" + FS + "lev4" + FS, ".jpg") });
TestUtils.createRandomBlobKey("lev1" + FS + "lev2" + FS + "lev4" + FS, ".jpg") });
storageStrategy.deleteDirectory(CONTAINER_NAME, null); storageStrategy.deleteDirectory(CONTAINER_NAME, null);
TestUtils.directoryExists(TARGET_CONTAINER_NAME, false); TestUtils.directoryExists(TARGET_CONTAINER_NAME, false);
} }
@ -208,10 +211,9 @@ public class FilesystemStorageStrategyImplTest {
public void testClearContainer() throws IOException { public void testClearContainer() throws IOException {
storageStrategy.createContainer(CONTAINER_NAME); storageStrategy.createContainer(CONTAINER_NAME);
Set<String> blobs = TestUtils.createBlobsInContainer( Set<String> blobs = TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] {
CONTAINER_NAME, TestUtils.createRandomBlobKey("clean_container-", ".jpg"),
new String[] { TestUtils.createRandomBlobKey("clean_container-", ".jpg"), TestUtils.createRandomBlobKey("bf" + FS + "sd" + FS + "as" + FS + "clean_container-", ".jpg") });
TestUtils.createRandomBlobKey("bf" + FS + "sd" + FS + "as" + FS + "clean_container-", ".jpg") });
// test if file exits // test if file exits
for (String blob : blobs) { for (String blob : blobs) {
TestUtils.fileExists(TARGET_CONTAINER_NAME + FS + blob, true); TestUtils.fileExists(TARGET_CONTAINER_NAME + FS + blob, true);
@ -238,10 +240,9 @@ public class FilesystemStorageStrategyImplTest {
public void testClearContainerAndThenDeleteContainer() throws IOException { public void testClearContainerAndThenDeleteContainer() throws IOException {
storageStrategy.createContainer(CONTAINER_NAME); storageStrategy.createContainer(CONTAINER_NAME);
Set<String> blobs = TestUtils.createBlobsInContainer( Set<String> blobs = TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] {
CONTAINER_NAME, TestUtils.createRandomBlobKey("clean_container-", ".jpg"),
new String[] { TestUtils.createRandomBlobKey("clean_container-", ".jpg"), TestUtils.createRandomBlobKey("bf" + FS + "sd" + FS + "as" + FS + "clean_container-", ".jpg") });
TestUtils.createRandomBlobKey("bf" + FS + "sd" + FS + "as" + FS + "clean_container-", ".jpg") });
// test if file exits // test if file exits
for (String blob : blobs) { for (String blob : blobs) {
TestUtils.fileExists(TARGET_CONTAINER_NAME + FS + blob, true); TestUtils.fileExists(TARGET_CONTAINER_NAME + FS + blob, true);
@ -266,7 +267,7 @@ public class FilesystemStorageStrategyImplTest {
public void testDeleteContainer() throws IOException { public void testDeleteContainer() throws IOException {
final String BLOB_KEY1 = "blobName.jpg"; final String BLOB_KEY1 = "blobName.jpg";
final String BLOB_KEY2 = "aa" + FS + "bb" + FS + "cc" + FS + "dd" + FS + "ee" + FS + "ff" + FS + "23" + FS final String BLOB_KEY2 = "aa" + FS + "bb" + FS + "cc" + FS + "dd" + FS + "ee" + FS + "ff" + FS + "23" + FS
+ "blobName.jpg"; + "blobName.jpg";
boolean result; boolean result;
result = storageStrategy.createContainer(CONTAINER_NAME); result = storageStrategy.createContainer(CONTAINER_NAME);
@ -399,12 +400,16 @@ public class FilesystemStorageStrategyImplTest {
// create storageStrategy with an absolute path // create storageStrategy with an absolute path
FilesystemStorageStrategy storageStrategyAbsolute = new FilesystemStorageStrategyImpl( FilesystemStorageStrategy storageStrategyAbsolute = new FilesystemStorageStrategyImpl(
new Provider<BlobBuilder>() { new Provider<BlobBuilder>() {
@Override @Override
public BlobBuilder get() { public BlobBuilder get() {
return new BlobBuilderImpl(); try {
} return new BlobBuilderImpl(new JCECrypto());
}, absoluteBasePath, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl()); } catch (Exception e) {
return null;
}
}
}, absoluteBasePath, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl());
TestUtils.cleanDirectoryContent(absoluteContainerPath); TestUtils.cleanDirectoryContent(absoluteContainerPath);
String blobKey; String blobKey;
@ -422,8 +427,8 @@ public class FilesystemStorageStrategyImplTest {
public void testBlobExists() throws IOException { public void testBlobExists() throws IOException {
String[] sourceBlobKeys = new String[] { TestUtils.createRandomBlobKey("blobExists-", ".jpg"), String[] sourceBlobKeys = new String[] { TestUtils.createRandomBlobKey("blobExists-", ".jpg"),
TestUtils.createRandomBlobKey("blobExists-", ".jpg"), TestUtils.createRandomBlobKey("blobExists-", ".jpg"),
TestUtils.createRandomBlobKey("afasd" + FS + "asdma" + FS + "blobExists-", ".jpg") }; TestUtils.createRandomBlobKey("afasd" + FS + "asdma" + FS + "blobExists-", ".jpg") };
for (String blobKey : sourceBlobKeys) { for (String blobKey : sourceBlobKeys) {
assertFalse(storageStrategy.blobExists(CONTAINER_NAME, blobKey), "Blob " + blobKey + " exists"); assertFalse(storageStrategy.blobExists(CONTAINER_NAME, blobKey), "Blob " + blobKey + " exists");
@ -436,12 +441,11 @@ public class FilesystemStorageStrategyImplTest {
public void testRemoveBlob() throws IOException { public void testRemoveBlob() throws IOException {
storageStrategy.createContainer(CONTAINER_NAME); storageStrategy.createContainer(CONTAINER_NAME);
Set<String> blobKeys = TestUtils.createBlobsInContainer( Set<String> blobKeys = TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] {
CONTAINER_NAME, TestUtils.createRandomBlobKey("removeBlob-", ".jpg"),
new String[] { TestUtils.createRandomBlobKey("removeBlob-", ".jpg"), TestUtils.createRandomBlobKey("removeBlob-", ".jpg"),
TestUtils.createRandomBlobKey("removeBlob-", ".jpg"), TestUtils.createRandomBlobKey("346" + FS + "g3sx2" + FS + "removeBlob-", ".jpg"),
TestUtils.createRandomBlobKey("346" + FS + "g3sx2" + FS + "removeBlob-", ".jpg"), TestUtils.createRandomBlobKey("346" + FS + "g3sx2" + FS + "removeBlob-", ".jpg") });
TestUtils.createRandomBlobKey("346" + FS + "g3sx2" + FS + "removeBlob-", ".jpg") });
Set<String> remainingBlobKeys = new HashSet<String>(); Set<String> remainingBlobKeys = new HashSet<String>();
for (String key : blobKeys) { for (String key : blobKeys) {
@ -478,12 +482,11 @@ public class FilesystemStorageStrategyImplTest {
// create blobs // create blobs
storageStrategy.createContainer(CONTAINER_NAME); storageStrategy.createContainer(CONTAINER_NAME);
Set<String> createBlobKeys = TestUtils.createBlobsInContainer( Set<String> createBlobKeys = TestUtils.createBlobsInContainer(CONTAINER_NAME, new String[] {
CONTAINER_NAME, TestUtils.createRandomBlobKey("GetBlobKeys-", ".jpg"),
new String[] { TestUtils.createRandomBlobKey("GetBlobKeys-", ".jpg"), TestUtils.createRandomBlobKey("GetBlobKeys-", ".jpg"),
TestUtils.createRandomBlobKey("GetBlobKeys-", ".jpg"), TestUtils.createRandomBlobKey("563" + FS + "g3sx2" + FS + "removeBlob-", ".jpg"),
TestUtils.createRandomBlobKey("563" + FS + "g3sx2" + FS + "removeBlob-", ".jpg"), TestUtils.createRandomBlobKey("563" + FS + "g3sx2" + FS + "removeBlob-", ".jpg") });
TestUtils.createRandomBlobKey("563" + FS + "g3sx2" + FS + "removeBlob-", ".jpg") });
storageStrategy.getBlobKeysInsideContainer(CONTAINER_NAME); storageStrategy.getBlobKeysInsideContainer(CONTAINER_NAME);
List<String> retrievedBlobKeys = new ArrayList<String>(); List<String> retrievedBlobKeys = new ArrayList<String>();

View File

@ -26,10 +26,8 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map; import java.util.Map;
import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
@ -40,20 +38,23 @@ import org.jclouds.io.Payload;
import org.jclouds.io.Payloads; import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.PhantomPayload; import org.jclouds.io.payloads.PhantomPayload;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
/** /**
* @author Adrian Cole * @author Adrian Cole
*/ */
public class BlobBuilderImpl implements BlobBuilder { public class BlobBuilderImpl implements BlobBuilder {
private final Crypto crypto;
@Inject
public BlobBuilderImpl(Crypto crypto) {
this.crypto = checkNotNull(crypto, "crypto");
}
private Payload payload; private Payload payload;
private String name; private String name;
private Map<String, String> userMetadata = Maps.newLinkedHashMap(); private Map<String, String> userMetadata = Maps.newLinkedHashMap();
private StorageType type = StorageType.BLOB; private StorageType type = StorageType.BLOB;
@Inject
private Crypto crypto;
@Override @Override
public BlobBuilder name(String name) { public BlobBuilder name(String name) {
@ -129,15 +130,10 @@ public class BlobBuilderImpl implements BlobBuilder {
private final Payload payload; private final Payload payload;
private MessageDigest digest; private MessageDigest digest;
public PayloadBlobBuilderImpl(BlobBuilder builder, Payload payload, @Nullable Crypto crypto) { public PayloadBlobBuilderImpl(BlobBuilder builder, Payload payload, Crypto crypto) {
this.builder = checkNotNull(builder, "builder"); this.builder = checkNotNull(builder, "builder");
this.payload = checkNotNull(payload, "payload"); this.payload = checkNotNull(payload, "payload");
try { this.digest = checkNotNull(crypto, "crypto").md5();
this.digest = crypto != null ? crypto.md5() : MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
Throwables.propagate(e);
this.digest = null;
}
} }
@Override @Override