new StreamProvider API: client must not worry about supplementary files (version files for example)

This commit is contained in:
Vladimir Krivosheev
2013-09-18 11:34:47 +02:00
parent cd5f5bfe05
commit d8c13288c2
8 changed files with 69 additions and 34 deletions
@@ -27,14 +27,13 @@ final class OldStreamProviderAdapter extends StreamProvider implements CurrentUs
}
@Override
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 false;
}
public boolean isApplicable(@NotNull String fileSpec, @NotNull RoamingType roamingType) {
return myRoamingType == roamingType && !(roamingType == RoamingType.PER_USER && StorageUtil.isProjectOrModuleFile(fileSpec));
}
myProvider.saveContent(fileSpec, new BufferExposingByteArrayInputStream(content, size), size, roamingType, async);
}
@Override
public boolean saveContent(@NotNull String fileSpec, @NotNull byte[] content, int size, @NotNull RoamingType roamingType, boolean async) throws IOException {
myProvider.saveContent(fileSpec, new BufferExposingByteArrayInputStream(content, size), size, roamingType, async);
return false;
}
@@ -556,15 +556,23 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di
return false;
}
@Override
public boolean isApplicable(@NotNull String fileSpec, @NotNull RoamingType roamingType) {
for (StreamProvider provider : myStreamProviders) {
if (provider.isApplicable(fileSpec, roamingType)) {
return true;
}
}
return false;
}
@Override
public boolean saveContent(@NotNull String fileSpec, @NotNull byte[] content, int size, @NotNull RoamingType roamingType, boolean async) throws IOException {
boolean result = false;
for (StreamProvider streamProvider : myStreamProviders) {
try {
if (streamProvider.isEnabled()) {
if (streamProvider.saveContent(fileSpec, content, size, roamingType, async)) {
result = true;
}
if (streamProvider.isEnabled() && streamProvider.isApplicable(fileSpec, roamingType) && streamProvider.saveContent(fileSpec, content, size, roamingType, async)) {
result = true;
}
}
catch (ConnectException e) {
@@ -581,7 +589,7 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di
public InputStream loadContent(@NotNull final String fileSpec, @NotNull final RoamingType roamingType) throws IOException {
for (StreamProvider streamProvider : myStreamProviders) {
try {
if (streamProvider.isEnabled()) {
if (streamProvider.isEnabled() && streamProvider.isApplicable(fileSpec, roamingType)) {
InputStream content = streamProvider.loadContent(fileSpec, roamingType);
if (content != null) {
return content;
@@ -603,7 +611,7 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di
public void deleteFile(@NotNull String fileSpec, @NotNull RoamingType roamingType) {
for (StreamProvider streamProvider : myStreamProviders) {
try {
if (streamProvider.isEnabled()) {
if (streamProvider.isEnabled() && streamProvider.isApplicable(fileSpec, roamingType)) {
streamProvider.deleteFile(fileSpec, roamingType);
}
}
@@ -616,6 +624,10 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di
@Override
public String getCurrentUserName() {
for (OldStreamProviderAdapter provider : myStreamProviders) {
if (!provider.isEnabled()) {
continue;
}
String userName = provider.getCurrentUserName();
if (userName != null) {
return userName;
@@ -257,6 +257,10 @@ public class StorageUtil {
}
public static boolean sendContent(@NotNull StreamProvider provider, @NotNull String fileSpec, @NotNull Document copy, @NotNull RoamingType type, boolean async) {
if (!provider.isApplicable(fileSpec, type)) {
return false;
}
try {
return doSendContent(provider, fileSpec, copy, type, async);
}
@@ -266,6 +270,15 @@ public class StorageUtil {
}
}
public static void deleteContent(@NotNull StreamProvider provider, @NotNull String fileSpec, @NotNull RoamingType type) {
if (provider.isApplicable(fileSpec, type)) {
provider.deleteFile(fileSpec, type);
}
}
/**
* You must call {@link StreamProvider#isApplicable(String, com.intellij.openapi.components.RoamingType)} before
*/
public static boolean doSendContent(StreamProvider provider, String fileSpec, Document copy, RoamingType type, boolean async) throws IOException {
// we should use standard line-separator (\n) - stream provider can share file content on any OS
BufferExposingByteArrayOutputStream content = documentToBytes(copy, false);
@@ -14,6 +14,13 @@ public abstract class StreamProvider {
public abstract boolean isEnabled();
/**
* fileSpec Only main fileSpec, not version
*/
public boolean isApplicable(@NotNull String fileSpec, @NotNull RoamingType roamingType) {
return true;
}
/**
* @param fileSpec
* @param content bytes of content, size of array is not actual size of data, you must use {@code size}
@@ -42,7 +42,7 @@ public abstract class XmlElementStorage implements StateStorage, Disposable {
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.components.impl.stores.XmlElementStorage");
@NonNls private static final String ATTR_NAME = "name";
public static final String VERSION_FILE_SUFFIX = ".ver";
private static final String VERSION_FILE_SUFFIX = ".ver";
protected TrackingPathMacroSubstitutor myPathMacroSubstitutor;
@NotNull private final String myRootElementName;
@@ -406,7 +406,7 @@ public abstract class XmlElementStorage implements StateStorage, Disposable {
if (myStreamProvider != null && myStreamProvider.isEnabled() && (myProviderUpToDateHash == -1 || myProviderUpToDateHash != hash)) {
try {
//noinspection IfStatementWithIdenticalBranches
if (saveForProviders(myStreamProvider)) {
if (saveForProvider(myStreamProvider)) {
//noinspection UnnecessaryReturnStatement
return;
}
@@ -432,9 +432,12 @@ public abstract class XmlElementStorage implements StateStorage, Disposable {
}
}
private boolean saveForProviders(@NotNull StreamProvider streamProvider) {
private boolean saveForProvider(@NotNull StreamProvider streamProvider) {
// it is not really correctly implemented - stream provider can save not the whole document, but only filtered (per roaming type)
// but right now it is not our case, so, we should refine it later, but it not leads to bug now
boolean result = false;
for (final RoamingType roamingType : RoamingType.values()) {
if (roamingType == RoamingType.DISABLED) {
if (roamingType == RoamingType.DISABLED || !streamProvider.isApplicable(myFileSpec, roamingType)) {
continue;
}
@@ -468,20 +471,21 @@ public abstract class XmlElementStorage implements StateStorage, Disposable {
}
try {
boolean result = StorageUtil.doSendContent(streamProvider, myFileSpec, actualDocument, roamingType, true);
if (StorageUtil.doSendContent(streamProvider, myFileSpec, actualDocument, roamingType, true)) {
result = true;
}
TObjectLongHashMap<String> 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);
StorageUtil.doSendContent(streamProvider, myFileSpec + VERSION_FILE_SUFFIX, versionDoc, roamingType, true);
}
return result;
}
catch (IOException e) {
LOG.warn(e);
}
}
return false;
return result;
}
private boolean isHashUpToDate(final Integer hash) {
@@ -695,6 +695,11 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
Document document = myProcessor.writeScheme(scheme);
if (document != null) {
String fileSpec = getFileFullPath(UniqueFileNamesProvider.convertName(scheme.getName())) + mySchemeExtension;
if (!provider.isApplicable(fileSpec, RoamingType.GLOBAL)) {
return;
}
Document wrapped = wrap(document, name, description);
if (provider instanceof CurrentUserHolder) {
wrapped = wrapped.clone();
@@ -703,8 +708,7 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
wrapped.getRootElement().setAttribute(USER, userName);
}
}
StorageUtil.doSendContent(provider, getFileFullPath(UniqueFileNamesProvider.convertName(scheme.getName())) + mySchemeExtension,
wrapped, RoamingType.GLOBAL, false);
StorageUtil.doSendContent(provider, fileSpec, wrapped, RoamingType.GLOBAL, false);
}
}
@@ -849,11 +853,9 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
}
private void deleteServerFiles(final String fileName) {
if (myProvider == null || !myProvider.isEnabled()) {
return;
if (myProvider != null && myProvider.isEnabled()) {
StorageUtil.deleteContent(myProvider, getFileFullPath(fileName), myRoamingType);
}
myProvider.deleteFile(getFileFullPath(fileName), myRoamingType);
}
private void saveSchemes(final Collection<T> schemes, final UniqueFileNamesProvider fileNameProvider) throws WriteExternalException {
@@ -53,7 +53,6 @@ public class StorageData {
myComponentStates = new THashMap<String, Element>(storageData.myComponentStates);
}
public void load(@NotNull Element rootElement) throws IOException {
final Element[] elements = JDOMUtil.getElements(rootElement);
for (Element element : elements) {
@@ -19,6 +19,7 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.application.impl.ApplicationImpl;
import com.intellij.openapi.components.RoamingType;
import com.intellij.openapi.components.impl.stores.StorageUtil;
import com.intellij.openapi.components.impl.stores.StreamProvider;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.io.FileUtil;
@@ -159,12 +160,10 @@ public class CompoundShelfFileProcessor {
}
private static void copyFileContentToProviders(final String newFilePath, final StreamProvider serverStreamProvider, final File file) throws IOException {
if (!serverStreamProvider.isEnabled()) {
return;
if (serverStreamProvider.isEnabled() && serverStreamProvider.isApplicable(newFilePath, RoamingType.PER_USER)) {
byte[] content = FileUtil.loadFileBytes(file);
serverStreamProvider.saveContent(newFilePath, content, content.length, RoamingType.PER_USER, true);
}
byte[] content = FileUtil.loadFileBytes(file);
serverStreamProvider.saveContent(newFilePath, content, content.length, RoamingType.PER_USER, true);
}
private static void copyFileToStream(final InputStream stream, final File file) throws IOException {
@@ -222,7 +221,7 @@ public class CompoundShelfFileProcessor {
public void delete(final String name) {
FileUtil.delete(new File(getBaseIODir(), name));
if (myServerStreamProvider != null && myServerStreamProvider.isEnabled()) {
myServerStreamProvider.deleteFile(FILE_SPEC + name, RoamingType.PER_USER);
StorageUtil.deleteContent(myServerStreamProvider, FILE_SPEC + name, RoamingType.PER_USER);
}
}
}