diff options
author | Eike Stepper | 2015-11-26 05:57:39 +0000 |
---|---|---|
committer | Eike Stepper | 2015-11-26 05:57:39 +0000 |
commit | 05214b4d2c3aa7a4c13e0adfc461cdb2b64e3762 (patch) | |
tree | f1cd32980536195e82d9c25d81eb49f7acdce7d5 | |
parent | d642955649c79d2e92fd1e2ad27073b1ea8fb35b (diff) | |
download | uss-05214b4d2c3aa7a4c13e0adfc461cdb2b64e3762.tar.gz uss-05214b4d2c3aa7a4c13e0adfc461cdb2b64e3762.tar.xz uss-05214b4d2c3aa7a4c13e0adfc461cdb2b64e3762.zip |
Handle external cache deletion
3 files changed, 43 insertions, 15 deletions
diff --git a/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Storage.java b/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Storage.java index 53f6512..2d617b2 100644 --- a/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Storage.java +++ b/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Storage.java @@ -14,6 +14,7 @@ import org.eclipse.userstorage.IBlob; import org.eclipse.userstorage.IStorage; import org.eclipse.userstorage.IStorageService; import org.eclipse.userstorage.StorageFactory; +import org.eclipse.userstorage.internal.util.IOUtil; import org.eclipse.userstorage.internal.util.IOUtil.TeeInputStream; import org.eclipse.userstorage.internal.util.StringUtil; import org.eclipse.userstorage.spi.ICredentialsProvider; @@ -234,27 +235,49 @@ public final class Storage implements IStorage public InputStream retrieveBlob(String key, Map<String, String> properties) throws IOException, NoServiceException { - StorageService service = getServiceSafe(); - InputStream contents = service.retrieveBlob(credentialsProvider, applicationToken, key, properties, cache != null); - + InputStream cacheStream = null; if (cache != null) { - if (contents == Blob.NOT_MODIFIED) + try { - return cache.internalGetInputStream(applicationToken, key); + cacheStream = cache.internalGetInputStream(applicationToken, key); } + catch (IOException ex) + { + Activator.log(ex); + } + } + + try + { + StorageService service = getServiceSafe(); + InputStream contents = service.retrieveBlob(credentialsProvider, applicationToken, key, properties, cacheStream != null); - if (contents == null) + if (cacheStream != null) { - cache.internalDelete(applicationToken, key); - return null; + if (contents == Blob.NOT_MODIFIED) + { + InputStream cacheStreamResult = cacheStream; + cacheStream = null; // Avoid closing the result stream in the finally block + return cacheStreamResult; + } + + if (contents == null) + { + cache.internalDelete(applicationToken, key); + return null; + } + + OutputStream output = cache.internalGetOutputStream(applicationToken, key, properties); + return new TeeInputStream(contents, output); } - OutputStream output = cache.internalGetOutputStream(applicationToken, key, properties); - return new TeeInputStream(contents, output); + return contents; + } + finally + { + IOUtil.closeSilent(cacheStream); } - - return contents; } public boolean updateBlob(String key, Map<String, String> properties, InputStream in) throws IOException, ConflictException, NoServiceException diff --git a/org.eclipse.userstorage/src/org/eclipse/userstorage/spi/StorageCache.java b/org.eclipse.userstorage/src/org/eclipse/userstorage/spi/StorageCache.java index 609e4f9..1c390d1 100644 --- a/org.eclipse.userstorage/src/org/eclipse/userstorage/spi/StorageCache.java +++ b/org.eclipse.userstorage/src/org/eclipse/userstorage/spi/StorageCache.java @@ -122,8 +122,8 @@ public abstract class StorageCache extends InternalStorageCache * must not be <code>null</code>.<p> * @param key the {@link IBlob#getKey() key} for which to return the contents stream, * must not be <code>null</code>.<p> - * @return an {@link InputStream} that represents the cached contents of this blob, never <code>null</code>.<p> - * @throws IOException if local I/O was unsuccessful, including the case that the contents disappeared from this cache.<p> + * @return an {@link InputStream} that represents the cached contents of the blob, or <code>null</code> if the blob does not exist in this cache.<p> + * @throws IOException if local I/O was unsuccessful.<p> */ @Override protected abstract InputStream getInputStream(String applicationToken, String key) throws IOException; diff --git a/org.eclipse.userstorage/src/org/eclipse/userstorage/util/FileStorageCache.java b/org.eclipse.userstorage/src/org/eclipse/userstorage/util/FileStorageCache.java index ec8c0c5..644afae 100644 --- a/org.eclipse.userstorage/src/org/eclipse/userstorage/util/FileStorageCache.java +++ b/org.eclipse.userstorage/src/org/eclipse/userstorage/util/FileStorageCache.java @@ -173,7 +173,12 @@ public class FileStorageCache extends StorageCache protected InputStream getInputStream(String applicationToken, String key) throws IOException { File file = getFile(applicationToken, key, null); - return new FileInputStream(file); + if (file.isFile()) + { + return new FileInputStream(file); + } + + return null; } /** |