mirror of https://github.com/apache/jclouds.git
Merge pull request #756 from andrewgaul/reorder-strategy
Shuffle strategy methods into consistent order
This commit is contained in:
commit
92f9cc4c0c
|
@ -33,6 +33,10 @@ import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
|
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
|
import com.google.common.io.Closeables;
|
||||||
|
import com.google.common.io.Files;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.commons.io.filefilter.DirectoryFileFilter;
|
import org.apache.commons.io.filefilter.DirectoryFileFilter;
|
||||||
import org.jclouds.blobstore.LocalStorageStrategy;
|
import org.jclouds.blobstore.LocalStorageStrategy;
|
||||||
|
@ -50,10 +54,6 @@ import org.jclouds.io.Payloads;
|
||||||
import org.jclouds.logging.Logger;
|
import org.jclouds.logging.Logger;
|
||||||
import org.jclouds.rest.annotations.ParamValidators;
|
import org.jclouds.rest.annotations.ParamValidators;
|
||||||
|
|
||||||
import com.google.common.base.Throwables;
|
|
||||||
import com.google.common.io.Closeables;
|
|
||||||
import com.google.common.io.Files;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Alfredo "Rainbowbreeze" Morresi
|
* @author Alfredo "Rainbowbreeze" Morresi
|
||||||
|
@ -92,32 +92,15 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean blobExists(String container, String key) {
|
public Iterable<String> getAllContainerNames() {
|
||||||
filesystemContainerNameValidator.validate(container);
|
Iterable<String> containers = new Iterable<String>() {
|
||||||
filesystemBlobKeyValidator.validate(key);
|
|
||||||
return buildPathAndChecksIfFileExists(container, key);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Blob getBlob(final String container, final String key) {
|
public Iterator<String> iterator() {
|
||||||
BlobBuilder builder = blobBuilders.get();
|
return new FileIterator(buildPathStartingFromBaseDir(), DirectoryFileFilter.INSTANCE);
|
||||||
builder.name(key);
|
|
||||||
File file = getFileForBlobKey(container, key);
|
|
||||||
try {
|
|
||||||
builder.payload(file).calculateMD5();
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error("An error occurred calculating MD5 for blob %s from container ", key, container);
|
|
||||||
Throwables.propagateIfPossible(e);
|
|
||||||
}
|
|
||||||
Blob blob = builder.build();
|
|
||||||
if (blob.getPayload().getContentMetadata().getContentMD5() != null)
|
|
||||||
blob.getMetadata().setETag(CryptoStreams.hex(blob.getPayload().getContentMetadata().getContentMD5()));
|
|
||||||
return blob;
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public boolean createContainer(String container) {
|
return containers;
|
||||||
filesystemContainerNameValidator.validate(container);
|
|
||||||
return createContainerInLocation(container, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -134,22 +117,15 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
||||||
deleteDirectory(container, null);
|
deleteDirectory(container, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Empty the directory of its content (files and subdirectories)
|
|
||||||
*
|
|
||||||
* @param container
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void clearContainer(final String container) {
|
public void clearContainer(final String container) {
|
||||||
filesystemContainerNameValidator.validate(container);
|
|
||||||
clearContainer(container, ListContainerOptions.Builder.recursive());
|
clearContainer(container, ListContainerOptions.Builder.recursive());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearContainer(String container, ListContainerOptions options) {
|
public void clearContainer(String container, ListContainerOptions options) {
|
||||||
filesystemContainerNameValidator.validate(container);
|
filesystemContainerNameValidator.validate(container);
|
||||||
// TODO
|
// TODO implement options
|
||||||
// now all is deleted, check it based on options
|
|
||||||
try {
|
try {
|
||||||
File containerFile = openFolder(container);
|
File containerFile = openFolder(container);
|
||||||
File[] children = containerFile.listFiles();
|
File[] children = containerFile.listFiles();
|
||||||
|
@ -163,85 +139,11 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Blob newBlob(@ParamValidators({ FilesystemBlobKeyValidator.class }) String name) {
|
|
||||||
filesystemBlobKeyValidator.validate(name);
|
|
||||||
return blobBuilders.get().name(name).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeBlob(final String container, final String blobKey) {
|
public boolean blobExists(String container, String key) {
|
||||||
filesystemContainerNameValidator.validate(container);
|
filesystemContainerNameValidator.validate(container);
|
||||||
filesystemBlobKeyValidator.validate(blobKey);
|
filesystemBlobKeyValidator.validate(key);
|
||||||
String fileName = buildPathStartingFromBaseDir(container, blobKey);
|
return buildPathAndChecksIfFileExists(container, key);
|
||||||
logger.debug("Deleting blob %s", fileName);
|
|
||||||
File fileToBeDeleted = new File(fileName);
|
|
||||||
fileToBeDeleted.delete();
|
|
||||||
|
|
||||||
// now examins if the key of the blob is a complex key (with a directory structure)
|
|
||||||
// and eventually remove empty directory
|
|
||||||
removeDirectoriesTreeOfBlobKey(container, blobKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return an iterator that reports all the containers under base path
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Iterable<String> getAllContainerNames() {
|
|
||||||
Iterable<String> containers = new Iterable<String>() {
|
|
||||||
@Override
|
|
||||||
public Iterator<String> iterator() {
|
|
||||||
return new FileIterator(buildPathStartingFromBaseDir(), DirectoryFileFilter.INSTANCE);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return containers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a {@link File} object that links to the blob
|
|
||||||
*
|
|
||||||
* @param container
|
|
||||||
* @param blobKey
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public File getFileForBlobKey(String container, String blobKey) {
|
|
||||||
filesystemContainerNameValidator.validate(container);
|
|
||||||
filesystemBlobKeyValidator.validate(blobKey);
|
|
||||||
String fileName = buildPathStartingFromBaseDir(container, blobKey);
|
|
||||||
File blobFile = new File(fileName);
|
|
||||||
return blobFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String putBlob(final String containerName, final Blob blob) throws IOException {
|
|
||||||
String blobKey = blob.getMetadata().getName();
|
|
||||||
Payload payload = blob.getPayload();
|
|
||||||
filesystemContainerNameValidator.validate(containerName);
|
|
||||||
filesystemBlobKeyValidator.validate(blobKey);
|
|
||||||
File outputFile = getFileForBlobKey(containerName, blobKey);
|
|
||||||
FileOutputStream output = null;
|
|
||||||
try {
|
|
||||||
Files.createParentDirs(outputFile);
|
|
||||||
if (payload.getRawContent() instanceof File)
|
|
||||||
Files.copy((File) payload.getRawContent(), outputFile);
|
|
||||||
else {
|
|
||||||
output = new FileOutputStream(outputFile);
|
|
||||||
payload.writeTo(output);
|
|
||||||
}
|
|
||||||
Payloads.calculateMD5(payload, crypto.md5());
|
|
||||||
String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5());
|
|
||||||
return eTag;
|
|
||||||
} catch (IOException ex) {
|
|
||||||
if (outputFile != null) {
|
|
||||||
outputFile.delete();
|
|
||||||
}
|
|
||||||
throw ex;
|
|
||||||
} finally {
|
|
||||||
Closeables.closeQuietly(output);
|
|
||||||
payload.release();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -275,6 +177,67 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
||||||
return blobNames;
|
return blobNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Blob getBlob(final String container, final String key) {
|
||||||
|
BlobBuilder builder = blobBuilders.get();
|
||||||
|
builder.name(key);
|
||||||
|
File file = getFileForBlobKey(container, key);
|
||||||
|
try {
|
||||||
|
builder.payload(file).calculateMD5();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("An error occurred calculating MD5 for blob %s from container ", key, container);
|
||||||
|
Throwables.propagateIfPossible(e);
|
||||||
|
}
|
||||||
|
Blob blob = builder.build();
|
||||||
|
if (blob.getPayload().getContentMetadata().getContentMD5() != null)
|
||||||
|
blob.getMetadata().setETag(CryptoStreams.hex(blob.getPayload().getContentMetadata().getContentMD5()));
|
||||||
|
return blob;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String putBlob(final String containerName, final Blob blob) throws IOException {
|
||||||
|
String blobKey = blob.getMetadata().getName();
|
||||||
|
Payload payload = blob.getPayload();
|
||||||
|
filesystemContainerNameValidator.validate(containerName);
|
||||||
|
filesystemBlobKeyValidator.validate(blobKey);
|
||||||
|
File outputFile = getFileForBlobKey(containerName, blobKey);
|
||||||
|
FileOutputStream output = null;
|
||||||
|
try {
|
||||||
|
Files.createParentDirs(outputFile);
|
||||||
|
if (payload.getRawContent() instanceof File)
|
||||||
|
Files.copy((File) payload.getRawContent(), outputFile);
|
||||||
|
else {
|
||||||
|
output = new FileOutputStream(outputFile);
|
||||||
|
payload.writeTo(output);
|
||||||
|
}
|
||||||
|
Payloads.calculateMD5(payload, crypto.md5());
|
||||||
|
String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5());
|
||||||
|
return eTag;
|
||||||
|
} catch (IOException ex) {
|
||||||
|
if (outputFile != null) {
|
||||||
|
outputFile.delete();
|
||||||
|
}
|
||||||
|
throw ex;
|
||||||
|
} finally {
|
||||||
|
Closeables.closeQuietly(output);
|
||||||
|
payload.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeBlob(final String container, final String blobKey) {
|
||||||
|
filesystemContainerNameValidator.validate(container);
|
||||||
|
filesystemBlobKeyValidator.validate(blobKey);
|
||||||
|
String fileName = buildPathStartingFromBaseDir(container, blobKey);
|
||||||
|
logger.debug("Deleting blob %s", fileName);
|
||||||
|
File fileToBeDeleted = new File(fileName);
|
||||||
|
fileToBeDeleted.delete();
|
||||||
|
|
||||||
|
// now examins if the key of the blob is a complex key (with a directory structure)
|
||||||
|
// and eventually remove empty directory
|
||||||
|
removeDirectoriesTreeOfBlobKey(container, blobKey);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Location getLocation(final String containerName) {
|
public Location getLocation(final String containerName) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -285,6 +248,31 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
|
||||||
return File.separator;
|
return File.separator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean createContainer(String container) {
|
||||||
|
filesystemContainerNameValidator.validate(container);
|
||||||
|
return createContainerInLocation(container, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Blob newBlob(@ParamValidators({ FilesystemBlobKeyValidator.class }) String name) {
|
||||||
|
filesystemBlobKeyValidator.validate(name);
|
||||||
|
return blobBuilders.get().name(name).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a {@link File} object that links to the blob
|
||||||
|
*
|
||||||
|
* @param container
|
||||||
|
* @param blobKey
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public File getFileForBlobKey(String container, String blobKey) {
|
||||||
|
filesystemContainerNameValidator.validate(container);
|
||||||
|
filesystemBlobKeyValidator.validate(blobKey);
|
||||||
|
String fileName = buildPathStartingFromBaseDir(container, blobKey);
|
||||||
|
File blobFile = new File(fileName);
|
||||||
|
return blobFile;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean directoryExists(String container, String directory) {
|
public boolean directoryExists(String container, String directory) {
|
||||||
return buildPathAndChecksIfDirectoryExists(container, directory);
|
return buildPathAndChecksIfDirectoryExists(container, directory);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,19 @@ import org.jclouds.domain.Location;
|
||||||
*/
|
*/
|
||||||
public interface LocalStorageStrategy {
|
public interface LocalStorageStrategy {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a container exists
|
||||||
|
* @param container
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
boolean containerExists(String container);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an iterator that reports all the containers under base path
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Iterable<String> getAllContainerNames();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new container
|
* Creates a new container
|
||||||
*
|
*
|
||||||
|
@ -45,13 +58,6 @@ public interface LocalStorageStrategy {
|
||||||
*/
|
*/
|
||||||
void deleteContainer(String container);
|
void deleteContainer(String container);
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a container exists
|
|
||||||
* @param container
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
boolean containerExists(String container);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Empty the container of its content (files and subdirectories), but doesn't
|
* Empty the container of its content (files and subdirectories), but doesn't
|
||||||
* delete the container itself
|
* delete the container itself
|
||||||
|
@ -70,12 +76,6 @@ public interface LocalStorageStrategy {
|
||||||
*/
|
*/
|
||||||
void clearContainer(String container, ListContainerOptions options);
|
void clearContainer(String container, ListContainerOptions options);
|
||||||
|
|
||||||
/**
|
|
||||||
* Return an iterator that reports all the containers under base path
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
Iterable<String> getAllContainerNames();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return true if a blob named by key exists
|
* Return true if a blob named by key exists
|
||||||
* @param container
|
* @param container
|
||||||
|
@ -84,6 +84,14 @@ public interface LocalStorageStrategy {
|
||||||
*/
|
*/
|
||||||
boolean blobExists(String container, String key);
|
boolean blobExists(String container, String key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all the blobs key inside a container
|
||||||
|
* @param container
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
Iterable<String> getBlobKeysInsideContainer(String container) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the blob with the given key belonging to the container with the given
|
* Load the blob with the given key belonging to the container with the given
|
||||||
* name. There must exist a resource on the file system whose complete name
|
* name. There must exist a resource on the file system whose complete name
|
||||||
|
@ -98,21 +106,6 @@ public interface LocalStorageStrategy {
|
||||||
*/
|
*/
|
||||||
Blob getBlob(String containerName, String blobName);
|
Blob getBlob(String containerName, String blobName);
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all the blobs key inside a container
|
|
||||||
* @param container
|
|
||||||
* @return
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
Iterable<String> getBlobKeysInsideContainer(String container) throws IOException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove blob named by the given key
|
|
||||||
* @param container
|
|
||||||
* @param key
|
|
||||||
*/
|
|
||||||
void removeBlob(String container, String key);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a {@link Blob} into a file
|
* Write a {@link Blob} into a file
|
||||||
* @param container
|
* @param container
|
||||||
|
@ -122,6 +115,13 @@ public interface LocalStorageStrategy {
|
||||||
*/
|
*/
|
||||||
String putBlob(String containerName, Blob blob) throws IOException;
|
String putBlob(String containerName, Blob blob) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove blob named by the given key
|
||||||
|
* @param container
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
|
void removeBlob(String container, String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param containerName name of container
|
* @param containerName name of container
|
||||||
* @return Location of container or null
|
* @return Location of container or null
|
||||||
|
|
|
@ -77,25 +77,14 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
|
||||||
this.uriBuilders = uriBuilders;
|
this.uriBuilders = uriBuilders;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Iterable<String> getAllContainerNames() {
|
|
||||||
return containerToBlobs.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containerExists(final String containerName) {
|
public boolean containerExists(final String containerName) {
|
||||||
return containerToBlobs.containsKey(containerName);
|
return containerToBlobs.containsKey(containerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearContainer(final String containerName) {
|
public Iterable<String> getAllContainerNames() {
|
||||||
containerToBlobs.get(containerName).clear();
|
return containerToBlobs.keySet();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void clearContainer(String container, ListContainerOptions options) {
|
|
||||||
// TODO implement options
|
|
||||||
clearContainer(container);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -114,12 +103,28 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
|
||||||
containerToBlobs.remove(containerName);
|
containerToBlobs.remove(containerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearContainer(final String containerName) {
|
||||||
|
clearContainer(containerName, ListContainerOptions.Builder.recursive());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearContainer(String containerName, ListContainerOptions options) {
|
||||||
|
// TODO implement options
|
||||||
|
containerToBlobs.get(containerName).clear();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean blobExists(final String containerName, final String blobName) {
|
public boolean blobExists(final String containerName, final String blobName) {
|
||||||
Map<String, Blob> map = containerToBlobs.get(containerName);
|
Map<String, Blob> map = containerToBlobs.get(containerName);
|
||||||
return map != null && map.containsKey(blobName);
|
return map != null && map.containsKey(blobName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<String> getBlobKeysInsideContainer(final String containerName) {
|
||||||
|
return containerToBlobs.get(containerName).keySet();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Blob getBlob(final String containerName, final String blobName) {
|
public Blob getBlob(final String containerName, final String blobName) {
|
||||||
Map<String, Blob> map = containerToBlobs.get(containerName);
|
Map<String, Blob> map = containerToBlobs.get(containerName);
|
||||||
|
@ -143,11 +148,6 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
|
||||||
map.remove(blobName);
|
map.remove(blobName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Iterable<String> getBlobKeysInsideContainer(final String containerName) {
|
|
||||||
return containerToBlobs.get(containerName).keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Location getLocation(final String containerName) {
|
public Location getLocation(final String containerName) {
|
||||||
return containerToLocation.get(containerName);
|
return containerToLocation.get(containerName);
|
||||||
|
|
Loading…
Reference in New Issue