From c6e955efa8482ee48bc77b2df05a360341b21037 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 18 Sep 2013 07:36:59 +0200 Subject: [PATCH] ability to cancel write to local file --- .../impl/stores/OldStreamProviderAdapter.java | 5 +++-- .../impl/stores/StateStorageManagerImpl.java | 8 ++++++-- .../components/impl/stores/StorageUtil.java | 6 +++--- .../impl/stores/StreamProvider.java | 2 +- .../impl/stores/XmlElementStorage.java | 19 ++++++++++++------- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/OldStreamProviderAdapter.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/OldStreamProviderAdapter.java index 42156342aec3..6ff844dcbcf3 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/OldStreamProviderAdapter.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/OldStreamProviderAdapter.java @@ -27,14 +27,15 @@ public final class OldStreamProviderAdapter extends StreamProvider implements Cu } @Override - public void saveContent(@NotNull String fileSpec, @NotNull byte[] content, int size, @NotNull RoamingType roamingType, boolean async) throws IOException { + public boolean saveContent(@NotNull String fileSpec, @NotNull byte[] content, int size, @NotNull RoamingType roamingType, boolean async) throws IOException { if (myRoamingType == roamingType) { if (roamingType == RoamingType.PER_USER && StorageUtil.isProjectOrModuleFile(fileSpec)) { - return; + return false; } myProvider.saveContent(fileSpec, new BufferExposingByteArrayInputStream(content, size), size, roamingType, async); } + return false; } @Nullable diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageManagerImpl.java index dcfb8071b6ca..3bc9ec09b3dc 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageManagerImpl.java @@ -90,11 +90,14 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di } @Override - public void saveContent(@NotNull String fileSpec, @NotNull byte[] content, int size, @NotNull RoamingType roamingType, boolean async) throws IOException { + public boolean saveContent(@NotNull String fileSpec, @NotNull byte[] content, int size, @NotNull RoamingType roamingType, boolean async) throws IOException { + boolean result = false; for (StreamProvider streamProvider : getStreamProviders()) { try { if (streamProvider.isEnabled()) { - streamProvider.saveContent(fileSpec, content, size, roamingType, async); + if (streamProvider.saveContent(fileSpec, content, size, roamingType, async)) { + result = true; + } } } catch (ConnectException e) { @@ -104,6 +107,7 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di LOG.debug(e); } } + return result; } @Override diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StorageUtil.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StorageUtil.java index 780073c4e24e..c8300b6f30b1 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StorageUtil.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StorageUtil.java @@ -256,14 +256,14 @@ public class StorageUtil { } } - public static void sendContent(@NotNull StreamProvider provider, @NotNull String fileSpec, @NotNull Document copy, @NotNull RoamingType type, boolean async) throws IOException { + public static boolean sendContent(@NotNull StreamProvider provider, @NotNull String fileSpec, @NotNull Document copy, @NotNull RoamingType type, boolean async) throws IOException { if (!provider.isEnabled()) { - return; + return false; } // we should use standard line-separator (\n) - stream provider can share file content on any OS BufferExposingByteArrayOutputStream content = documentToBytes(copy, false); - provider.saveContent(fileSpec, content.getInternalBuffer(), content.size(), type, async); + return provider.saveContent(fileSpec, content.getInternalBuffer(), content.size(), type, async); } public static void logStateDiffInfo(Set> changedFiles, Set componentNames) throws IOException { diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StreamProvider.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StreamProvider.java index a45fa67464c7..deec021f61ea 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StreamProvider.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StreamProvider.java @@ -21,7 +21,7 @@ public abstract class StreamProvider { * @param roamingType * @param async */ - public abstract void saveContent(@NotNull String fileSpec, @NotNull byte[] content, int size, @NotNull RoamingType roamingType, boolean async) throws IOException; + public abstract boolean saveContent(@NotNull String fileSpec, @NotNull byte[] content, int size, @NotNull RoamingType roamingType, boolean async) throws IOException; @Nullable public abstract InputStream loadContent(@NotNull String fileSpec, @NotNull RoamingType roamingType) throws IOException; diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/XmlElementStorage.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/XmlElementStorage.java index eacfef4b4511..f82655297659 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/XmlElementStorage.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/XmlElementStorage.java @@ -405,7 +405,11 @@ public abstract class XmlElementStorage implements StateStorage, Disposable { try { if (myStreamProvider != null && myStreamProvider.isEnabled() && (myProviderUpToDateHash == -1 || myProviderUpToDateHash != hash)) { try { - saveForProviders(myStreamProvider); + //noinspection IfStatementWithIdenticalBranches + if (saveForProviders(myStreamProvider)) { + //noinspection UnnecessaryReturnStatement + return; + } } finally { myProviderUpToDateHash = hash; @@ -419,10 +423,8 @@ public abstract class XmlElementStorage implements StateStorage, Disposable { private void saveLocally(final Integer hash) { try { - if (!isHashUpToDate(hash)) { - if (_needsSave(hash)) { - doSave(); - } + if (!isHashUpToDate(hash) && _needsSave(hash)) { + doSave(); } } finally { @@ -430,7 +432,7 @@ public abstract class XmlElementStorage implements StateStorage, Disposable { } } - private void saveForProviders(@NotNull StreamProvider streamProvider) { + private boolean saveForProviders(@NotNull StreamProvider streamProvider) { for (final RoamingType roamingType : RoamingType.values()) { if (roamingType == RoamingType.DISABLED) { continue; @@ -466,17 +468,20 @@ public abstract class XmlElementStorage implements StateStorage, Disposable { } try { - StorageUtil.sendContent(streamProvider, myFileSpec, actualDocument, roamingType, true); + boolean result = StorageUtil.sendContent(streamProvider, myFileSpec, actualDocument, roamingType, true); TObjectLongHashMap versions = loadVersions(actualDocument.getRootElement().getChildren(StorageData.COMPONENT)); if (!versions.isEmpty()) { Document versionDoc = new Document(StateStorageManagerImpl.createComponentVersionsXml(versions)); StorageUtil.sendContent(streamProvider, myFileSpec + VERSION_FILE_SUFFIX, versionDoc, roamingType, true); } + return result; } catch (IOException e) { LOG.warn(e); } } + + return false; } private boolean isHashUpToDate(final Integer hash) {