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,9 +144,8 @@ 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
@ -158,9 +162,8 @@ 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,9 +211,8 @@ 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) {
@ -238,9 +240,8 @@ 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) {
@ -402,7 +403,11 @@ public class FilesystemStorageStrategyImplTest {
new Provider<BlobBuilder>() { 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;
}
} }
}, absoluteBasePath, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl()); }, absoluteBasePath, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl());
TestUtils.cleanDirectoryContent(absoluteContainerPath); TestUtils.cleanDirectoryContent(absoluteContainerPath);
@ -436,9 +441,8 @@ 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") });
@ -478,9 +482,8 @@ 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") });

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