we don't need to manage list of stream providers, one is enough

This commit is contained in:
Vladimir Krivosheev
2013-09-18 11:34:47 +02:00
parent 3e0ccddacc
commit cd5f5bfe05
13 changed files with 251 additions and 312 deletions
@@ -35,6 +35,7 @@ public abstract class ExportSchemeAction<T extends Scheme, E extends Externaliza
mySchemesManager = manager;
}
@Override
public void update(AnActionEvent e) {
final Presentation presentation = e.getPresentation();
E profile = getSelectedScheme();
@@ -47,8 +48,8 @@ public abstract class ExportSchemeAction<T extends Scheme, E extends Externaliza
return selected != null && !mySchemesManager.isShared(selected);
}
@Override
public void actionPerformed(AnActionEvent e) {
doExport(getSelectedScheme(), mySchemesManager);
}
@@ -67,11 +68,9 @@ public abstract class ExportSchemeAction<T extends Scheme, E extends Externaliza
Messages.getInformationIcon());
}
catch (IOException e) {
Messages.showErrorDialog("Cannot share scheme '" + scheme.getName() + "': " + e.getLocalizedMessage(), "Share Shceme");
Messages.showErrorDialog("Cannot share scheme '" + scheme.getName() + "': " + e.getLocalizedMessage(), "Share Scheme");
}
}
}
catch (WriteExternalException e1) {
Messages.showErrorDialog("Cannot share scheme: " + e1.getLocalizedMessage(), "Share Scheme");
@@ -176,18 +176,13 @@ public class DefaultProjectStoreImpl extends ProjectStoreImpl {
}
@Override
public void registerStreamProvider(@NotNull com.intellij.openapi.components.impl.stores.StreamProvider streamProvider) {
throw new UnsupportedOperationException("Method registerStreamProvider not implemented in " + getClass());
public void setStreamProvider(@Nullable com.intellij.openapi.components.impl.stores.StreamProvider streamProvider) {
throw new UnsupportedOperationException("Method setStreamProvider not implemented in " + getClass());
}
@Nullable
@Override
public void unregisterStreamProvider(@NotNull com.intellij.openapi.components.impl.stores.StreamProvider streamProvider) {
throw new UnsupportedOperationException("Method unregisterStreamProvider not implemented in " + getClass());
}
@NotNull
@Override
public com.intellij.openapi.components.impl.stores.StreamProvider[] getStreamProviders() {
public com.intellij.openapi.components.impl.stores.StreamProvider getStreamProvider() {
throw new UnsupportedOperationException("Method getStreamProviders not implemented in " + getClass());
}
@@ -12,7 +12,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public final class OldStreamProviderAdapter extends StreamProvider implements CurrentUserHolder {
final class OldStreamProviderAdapter extends StreamProvider implements CurrentUserHolder {
final com.intellij.openapi.options.StreamProvider myProvider;
private final RoamingType myRoamingType;
@@ -58,14 +58,13 @@ public interface StateStorageManager {
@Nullable
String expandMacros(String file);
@Deprecated
void registerStreamProvider(StreamProvider streamProvider, final RoamingType type);
void registerStreamProvider(@NotNull com.intellij.openapi.components.impl.stores.StreamProvider streamProvider);
void setStreamProvider(@Nullable com.intellij.openapi.components.impl.stores.StreamProvider streamProvider);
void unregisterStreamProvider(@NotNull com.intellij.openapi.components.impl.stores.StreamProvider streamProvider);
@NotNull
com.intellij.openapi.components.impl.stores.StreamProvider[] getStreamProviders();
@Nullable
com.intellij.openapi.components.impl.stores.StreamProvider getStreamProvider();
void reset();
@@ -20,12 +20,14 @@ import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.options.CurrentUserHolder;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ObjectUtils;
import com.intellij.util.SmartList;
import com.intellij.util.io.fs.IFile;
import gnu.trove.THashMap;
@@ -74,78 +76,9 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di
private boolean isDirty;
private final List<StreamProvider> myStreamProviders = new SmartList<StreamProvider>();
private StreamProvider myStreamProvider;
private final StreamProvider myCompoundStreamProvider = new StreamProvider() {
@Override
public boolean isEnabled() {
synchronized (myStreamProviders) {
for (StreamProvider provider : myStreamProviders) {
if (provider.isEnabled()) {
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 : getStreamProviders()) {
try {
if (streamProvider.isEnabled()) {
if (streamProvider.saveContent(fileSpec, content, size, roamingType, async)) {
result = true;
}
}
}
catch (ConnectException e) {
LOG.debug("Cannot send user profile to server: " + e.getLocalizedMessage());
}
catch (Exception e) {
LOG.debug(e);
}
}
return result;
}
@Override
public InputStream loadContent(@NotNull final String fileSpec, @NotNull final RoamingType roamingType) throws IOException {
for (StreamProvider streamProvider : getStreamProviders()) {
try {
if (streamProvider.isEnabled()) {
InputStream content = streamProvider.loadContent(fileSpec, roamingType);
if (content != null) {
return content;
}
}
}
catch (ConnectException e) {
LOG.debug("Cannot send user profile o server: " + e.getLocalizedMessage());
}
catch (Exception e) {
LOG.debug(e);
}
}
return null;
}
@Override
public void deleteFile(@NotNull String fileSpec, @NotNull RoamingType roamingType) {
for (StreamProvider streamProvider : getStreamProviders()) {
try {
if (streamProvider.isEnabled()) {
streamProvider.deleteFile(fileSpec, roamingType);
}
}
catch (Exception e) {
LOG.debug(e);
}
}
}
};
private final OldStreamProviderManager myOldStreamProvider = new OldStreamProviderManager();
public StateStorageManagerImpl(@Nullable TrackingPathMacroSubstitutor pathMacroSubstitutor,
String rootTagName,
@@ -306,7 +239,7 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di
throw new IllegalArgumentException("Extension is missing for storage file: " + expandedFile);
}
return new FileBasedStorage(getMacroSubstitutor(fileSpec), myCompoundStreamProvider, expandedFile, fileSpec, myRootTagName, this,
return new FileBasedStorage(getMacroSubstitutor(fileSpec), getStreamProvider(), expandedFile, fileSpec, myRootTagName, this,
myPicoContainer, ComponentRoamingManager.getInstance(), this) {
@Override
@NotNull
@@ -379,13 +312,10 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di
isDirty = true;
}
@NotNull
@Nullable
@Override
public StreamProvider[] getStreamProviders() {
synchronized (myStreamProviders) {
Collection<StreamProvider> providers = myStreamProviders;
return providers.isEmpty() ? StreamProvider.EMPTY_ARRAY : providers.toArray(new StreamProvider[providers.size()]);
}
public StreamProvider getStreamProvider() {
return ObjectUtils.chooseNotNull(myStreamProvider, myOldStreamProvider);
}
protected TrackingPathMacroSubstitutor getMacroSubstitutor(@NotNull final String fileSpec) {
@@ -559,23 +489,14 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di
@Override
public void registerStreamProvider(com.intellij.openapi.options.StreamProvider streamProvider, final RoamingType type) {
synchronized (myStreamProviders) {
myStreamProviders.add(new OldStreamProviderAdapter(streamProvider, type));
synchronized (myOldStreamProvider) {
myOldStreamProvider.myStreamProviders.add(new OldStreamProviderAdapter(streamProvider, type));
}
}
@Override
public void registerStreamProvider(@NotNull StreamProvider streamProvider) {
synchronized (myStreamProviders) {
myStreamProviders.add(streamProvider);
}
}
@Override
public void unregisterStreamProvider(@NotNull StreamProvider streamProvider) {
synchronized (myStreamProviders) {
myStreamProviders.remove(streamProvider);
}
public void setStreamProvider(@Nullable StreamProvider streamProvider) {
myStreamProvider = streamProvider;
}
public void save() {
@@ -621,4 +542,86 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di
}
return root;
}
private static class OldStreamProviderManager extends StreamProvider implements CurrentUserHolder {
private final List<OldStreamProviderAdapter> myStreamProviders = new SmartList<OldStreamProviderAdapter>();
@Override
public boolean isEnabled() {
for (StreamProvider provider : myStreamProviders) {
if (provider.isEnabled()) {
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;
}
}
}
catch (ConnectException e) {
LOG.debug("Cannot send user profile to server: " + e.getLocalizedMessage());
}
catch (Exception e) {
LOG.debug(e);
}
}
return result;
}
@Override
public InputStream loadContent(@NotNull final String fileSpec, @NotNull final RoamingType roamingType) throws IOException {
for (StreamProvider streamProvider : myStreamProviders) {
try {
if (streamProvider.isEnabled()) {
InputStream content = streamProvider.loadContent(fileSpec, roamingType);
if (content != null) {
return content;
}
}
}
catch (ConnectException e) {
LOG.debug("Cannot send user profile o server: " + e.getLocalizedMessage());
}
catch (Exception e) {
LOG.debug(e);
}
}
return null;
}
@Override
public void deleteFile(@NotNull String fileSpec, @NotNull RoamingType roamingType) {
for (StreamProvider streamProvider : myStreamProviders) {
try {
if (streamProvider.isEnabled()) {
streamProvider.deleteFile(fileSpec, roamingType);
}
}
catch (Exception e) {
LOG.debug(e);
}
}
}
@Override
public String getCurrentUserName() {
for (OldStreamProviderAdapter provider : myStreamProviders) {
String userName = provider.getCurrentUserName();
if (userName != null) {
return userName;
}
}
return null;
}
}
}
@@ -256,17 +256,23 @@ public class StorageUtil {
}
}
public static boolean sendContent(@NotNull StreamProvider provider, @NotNull String fileSpec, @NotNull Document copy, @NotNull RoamingType type, boolean async) throws IOException {
if (!provider.isEnabled()) {
public static boolean sendContent(@NotNull StreamProvider provider, @NotNull String fileSpec, @NotNull Document copy, @NotNull RoamingType type, boolean async) {
try {
return doSendContent(provider, fileSpec, copy, type, async);
}
catch (IOException e) {
LOG.warn(e);
return false;
}
}
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);
return provider.saveContent(fileSpec, content.getInternalBuffer(), content.size(), type, async);
}
public static void logStateDiffInfo(Set<Pair<VirtualFile, StateStorage>> changedFiles, Set<String> componentNames) throws IOException {
public static void logStateDiffInfo(Set<Pair<VirtualFile, StateStorage>> changedFiles, Set<String> componentNames) {
if (componentNames.isEmpty() || !(DUMP_COMPONENT_STATES || ApplicationManager.getApplication().isInternal())) {
return;
}
@@ -468,7 +468,7 @@ public abstract class XmlElementStorage implements StateStorage, Disposable {
}
try {
boolean result = StorageUtil.sendContent(streamProvider, myFileSpec, actualDocument, roamingType, true);
boolean result = StorageUtil.doSendContent(streamProvider, myFileSpec, actualDocument, roamingType, true);
TObjectLongHashMap<String> versions = loadVersions(actualDocument.getRootElement().getChildren(StorageData.COMPONENT));
if (!versions.isEmpty()) {
Document versionDoc = new Document(StateStorageManagerImpl.createComponentVersionsXml(versions));
@@ -47,8 +47,8 @@ public class SchemesManagerFactoryImpl extends SchemesManagerFactory implements
if (baseDirPath != null) {
StreamProvider[] providers = ((ApplicationImpl)ApplicationManager.getApplication()).getStateStore().getStateStorageManager().getStreamProviders();
SchemesManagerImpl<T, E> manager = new SchemesManagerImpl<T, E>(fileSpec, processor, roamingType, providers, new File(baseDirPath));
StreamProvider provider = ((ApplicationImpl)ApplicationManager.getApplication()).getStateStore().getStateStorageManager().getStreamProvider();
SchemesManagerImpl<T, E> manager = new SchemesManagerImpl<T, E>(fileSpec, processor, roamingType, provider, new File(baseDirPath));
myRegisteredManagers.add(manager);
return manager;
}
@@ -69,7 +69,7 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
@NonNls private static final String SCHEME_LOCAL_COPY = "scheme-local-copy";
@NonNls private static final String DELETED_XML = "__deleted.xml";
private final StreamProvider[] myProviders;
private final StreamProvider myProvider;
private final File myBaseDir;
private VirtualFile myVFSBaseDir;
private static final String DESCRIPTION = "description";
@@ -85,13 +85,13 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
public SchemesManagerImpl(@NotNull String fileSpec,
@NotNull SchemeProcessor<E> processor,
@NotNull RoamingType roamingType,
@NotNull StreamProvider[] providers,
@Nullable StreamProvider provider,
@NotNull File baseDir) {
myFileSpec = fileSpec;
myProcessor = processor;
myRoamingType = roamingType;
myProviders = providers;
myProvider = provider;
myBaseDir = baseDir;
if (processor instanceof SchemeExtensionProvider) {
mySchemeExtension = ((SchemeExtensionProvider)processor).getSchemeExtension();
@@ -267,26 +267,27 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
}
private Collection<String> readDeletedSchemeNames() {
Collection<String> result = new HashSet<String>();
for (StreamProvider provider : getEnabledProviders()) {
try {
Document deletedNameDoc = StorageUtil.loadDocument(provider.loadContent(getFileFullPath(DELETED_XML), myRoamingType));
if (deletedNameDoc != null) {
for (Object child : deletedNameDoc.getRootElement().getChildren()) {
String deletedSchemeName = ((Element)child).getAttributeValue("name");
if (deletedSchemeName != null) {
result.add(deletedSchemeName);
}
Collection<String> result = new THashSet<String>();
if (myProvider == null || !myProvider.isEnabled()) {
return result;
}
try {
Document deletedNameDoc = StorageUtil.loadDocument(myProvider.loadContent(getFileFullPath(DELETED_XML), myRoamingType));
if (deletedNameDoc != null) {
for (Element child : deletedNameDoc.getRootElement().getChildren()) {
String deletedSchemeName = child.getAttributeValue("name");
if (deletedSchemeName != null) {
result.add(deletedSchemeName);
}
}
}
catch (Exception e) {
LOG.debug(e);
}
}
catch (Exception e) {
LOG.debug(e);
}
return result;
}
private void initLoadedSchemes(final Collection<E> read) {
@@ -298,43 +299,44 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
private Collection<E> readSchemesFromProviders() {
Collection<E> result = new ArrayList<E>();
for (StreamProvider provider : getEnabledProviders()) {
for (String subPath : provider.listSubFiles(myFileSpec, myRoamingType)) {
if (!subPath.equals(DELETED_XML)) {
try {
final Document subDocument = StorageUtil.loadDocument(provider.loadContent(getFileFullPath(subPath), myRoamingType));
if (subDocument != null) {
E scheme = readScheme(subDocument);
boolean fileRenamed = false;
T existing = findSchemeByName(scheme.getName());
if (existing != null && existing instanceof ExternalizableScheme) {
String currentFileName = ((ExternalizableScheme)existing).getExternalInfo().getCurrentFileName();
if (currentFileName != null && !currentFileName.equals(subPath)) {
deleteServerFiles(subPath);
subPath = currentFileName;
fileRenamed = true;
}
if (myProvider == null || !myProvider.isEnabled()) {
return result;
}
}
String fileName = checkFileNameIsFree(subPath, scheme.getName());
if (!fileRenamed && !fileName.equals(subPath)) {
for (String subPath : myProvider.listSubFiles(myFileSpec, myRoamingType)) {
if (!subPath.equals(DELETED_XML)) {
try {
final Document subDocument = StorageUtil.loadDocument(myProvider.loadContent(getFileFullPath(subPath), myRoamingType));
if (subDocument != null) {
E scheme = readScheme(subDocument);
boolean fileRenamed = false;
T existing = findSchemeByName(scheme.getName());
if (existing != null && existing instanceof ExternalizableScheme) {
String currentFileName = ((ExternalizableScheme)existing).getExternalInfo().getCurrentFileName();
if (currentFileName != null && !currentFileName.equals(subPath)) {
deleteServerFiles(subPath);
subPath = currentFileName;
fileRenamed = true;
}
}
String fileName = checkFileNameIsFree(subPath, scheme.getName());
if (scheme != null) {
loadScheme(scheme, false, fileName);
result.add(scheme);
}
if (!fileRenamed && !fileName.equals(subPath)) {
deleteServerFiles(subPath);
}
if (scheme != null) {
loadScheme(scheme, false, fileName);
result.add(scheme);
}
}
catch (Exception e) {
LOG.info("Cannot load data from IDEAServer: " + e.getLocalizedMessage());
}
}
catch (Exception e) {
LOG.info("Cannot load data from IDEAServer: " + e.getLocalizedMessage());
}
}
}
return result;
}
@@ -556,15 +558,8 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
@Nullable
private static Document loadGlobalScheme(final String schemePath) throws IOException {
final StreamProvider[] providers = getProvidersForGlobal();
for (StreamProvider provider : providers) {
if (provider.isEnabled()) {
Document document = StorageUtil.loadDocument(provider.loadContent(schemePath, RoamingType.GLOBAL));
if (document != null) return document;
}
}
return null;
StreamProvider provider = getProvider();
return provider != null && provider.isEnabled() ? StorageUtil.loadDocument(provider.loadContent(schemePath, RoamingType.GLOBAL)) : null;
}
private void saveFileName(String fileName, final E schemeKey) {
@@ -621,38 +616,32 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
@Override
@NotNull
public Collection<SharedScheme<E>> loadSharedSchemes(Collection<T> currentSchemeList) {
StreamProvider[] providers = getProvidersForGlobal();
if (providers.length == 0) {
StreamProvider provider = getProvider();
if (provider == null || !provider.isEnabled()) {
return Collections.emptyList();
}
Collection<String> names = new THashSet<String>(getAllSchemeNames(currentSchemeList));
Map<String, SharedScheme<E>> result = new THashMap<String, SharedScheme<E>>();
for (StreamProvider provider : providers) {
if (!provider.isEnabled()) {
continue;
}
for (String subPath : provider.listSubFiles(myFileSpec, RoamingType.GLOBAL)) {
try {
final Document subDocument = StorageUtil.loadDocument(provider.loadContent(getFileFullPath(subPath), RoamingType.GLOBAL));
if (subDocument != null) {
SharedSchemeData original = unwrap(subDocument);
final E scheme = myProcessor.readScheme(original.original);
if (!alreadyShared(subPath, currentSchemeList)) {
String schemeName = original.name;
String uniqueName = UniqueNameGenerator.generateUniqueName("[shared] " + schemeName, names);
renameScheme(scheme, uniqueName);
schemeName = uniqueName;
scheme.getExternalInfo().setOriginalPath(getFileFullPath(subPath));
scheme.getExternalInfo().setIsImported(true);
result.put(schemeName, new SharedScheme<E>(original.user == null ? "unknown" : original.user, original.description, scheme));
}
for (String subPath : provider.listSubFiles(myFileSpec, RoamingType.GLOBAL)) {
try {
final Document subDocument = StorageUtil.loadDocument(provider.loadContent(getFileFullPath(subPath), RoamingType.GLOBAL));
if (subDocument != null) {
SharedSchemeData original = unwrap(subDocument);
final E scheme = myProcessor.readScheme(original.original);
if (!alreadyShared(subPath, currentSchemeList)) {
String schemeName = original.name;
String uniqueName = UniqueNameGenerator.generateUniqueName("[shared] " + schemeName, names);
renameScheme(scheme, uniqueName);
schemeName = uniqueName;
scheme.getExternalInfo().setOriginalPath(getFileFullPath(subPath));
scheme.getExternalInfo().setIsImported(true);
result.put(schemeName, new SharedScheme<E>(original.user == null ? "unknown" : original.user, original.description, scheme));
}
}
catch (Exception e) {
LOG.debug("Cannot load data from IDEAServer: " + e.getLocalizedMessage());
}
}
catch (Exception e) {
LOG.debug("Cannot load data from IDEAServer: " + e.getLocalizedMessage());
}
}
@@ -699,25 +688,23 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
@Override
public void exportScheme(final E scheme, final String name, final String description) throws WriteExternalException, IOException {
StreamProvider[] providers = getProvidersForGlobal();
if (providers.length == 0) {
StreamProvider provider = getProvider();
if (provider == null) {
return;
}
Document document = myProcessor.writeScheme(scheme);
if (document != null) {
Document wrapped = wrap(document, name, description);
for (StreamProvider provider : providers) {
if (provider instanceof CurrentUserHolder) {
wrapped = wrapped.clone();
String userName = ((CurrentUserHolder)provider).getCurrentUserName();
if (userName != null) {
wrapped.getRootElement().setAttribute(USER, userName);
}
if (provider instanceof CurrentUserHolder) {
wrapped = wrapped.clone();
String userName = ((CurrentUserHolder)provider).getCurrentUserName();
if (userName != null) {
wrapped.getRootElement().setAttribute(USER, userName);
}
StorageUtil.sendContent(provider, getFileFullPath(UniqueFileNamesProvider.convertName(scheme.getName())) + mySchemeExtension,
wrapped, RoamingType.GLOBAL, false);
}
StorageUtil.doSendContent(provider, getFileFullPath(UniqueFileNamesProvider.convertName(scheme.getName())) + mySchemeExtension,
wrapped, RoamingType.GLOBAL, false);
}
}
@@ -731,22 +718,13 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
@Override
public boolean isImportAvailable() {
StreamProvider[] providers = getProvidersForGlobal();
if (providers.length == 0) {
return false;
}
for (StreamProvider provider : providers) {
if (provider.isEnabled()) {
return true;
}
}
return false;
return getProvider() != null;
}
private static StreamProvider[] getProvidersForGlobal() {
return ((ApplicationImpl)ApplicationManager.getApplication()).getStateStore().getStateStorageManager().getStreamProviders();
@Nullable
private static StreamProvider getProvider() {
StreamProvider provider = ((ApplicationImpl)ApplicationManager.getApplication()).getStateStore().getStateStorageManager().getStreamProvider();
return provider == null || !provider.isEnabled() ? null : provider;
}
@Override
@@ -811,7 +789,6 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
myBaseDir.mkdirs();
final UniqueFileNamesProvider fileNameProvider = new UniqueFileNamesProvider();
reserveUsingFileNames(schemes, fileNameProvider);
final WriteExternalException[] ex = new WriteExternalException[1];
@@ -827,21 +804,15 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
}
}
});
if (ex[0] != null) throw ex[0];
if (ex[0] != null) {
throw ex[0];
}
if (myDeletedNames.isEmpty()) {
deleteServerFiles(DELETED_XML);
}
else {
for (StreamProvider provider : getEnabledProviders()) {
try {
StorageUtil.sendContent(provider, getFileFullPath(DELETED_XML), createDeletedDocument(), myRoamingType, true);
}
catch (IOException e) {
LOG.debug(e);
}
}
else if (myProvider != null && myProvider.isEnabled()) {
StorageUtil.sendContent(myProvider, getFileFullPath(DELETED_XML), createDeletedDocument(), myRoamingType, true);
}
}
finally {
@@ -878,9 +849,11 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
}
private void deleteServerFiles(final String fileName) {
for (StreamProvider provider : getEnabledProviders()) {
provider.deleteFile(getFileFullPath(fileName), myRoamingType);
if (myProvider == null || !myProvider.isEnabled()) {
return;
}
myProvider.deleteFile(getFileFullPath(fileName), myRoamingType);
}
private void saveSchemes(final Collection<T> schemes, final UniqueFileNamesProvider fileNameProvider) throws WriteExternalException {
@@ -941,26 +914,11 @@ public class SchemesManagerImpl<T extends Scheme, E extends ExternalizableScheme
}
private void saveOnServer(final String fileName, final Document document) {
for (StreamProvider provider : getEnabledProviders()) {
try {
StorageUtil.sendContent(provider, getFileFullPath(fileName), document, myRoamingType, true);
}
catch (IOException e) {
LOG.debug(e);
}
if (myProvider != null && myProvider.isEnabled()) {
StorageUtil.sendContent(myProvider, getFileFullPath(fileName), document, myRoamingType, true);
}
}
private Collection<StreamProvider> getEnabledProviders() {
ArrayList<StreamProvider> result = new ArrayList<StreamProvider>();
for (StreamProvider provider : myProviders) {
if (provider.isEnabled()) {
result.add(provider);
}
}
return result;
}
private void reserveUsingFileNames(final Collection<T> schemes, final UniqueFileNamesProvider fileNameProvider) {
fileNameProvider.reserveFileName(DELETED_XML);
@@ -16,6 +16,7 @@
package com.intellij.util;
import com.intellij.openapi.util.Condition;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -38,7 +39,7 @@ public class ObjectUtils {
return t;
}
@Nullable
@Contract("null, null -> null")
public static <T> T chooseNotNull(@Nullable T t1, @Nullable T t2) {
return t1 == null? t2 : t1;
}
@@ -25,18 +25,18 @@ import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.vcs.changes.CommitContext;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Collections;
import java.util.List;
public class CompoundShelfFileProcessor {
private final String mySubdirName;
private final StreamProvider[] myServerStreamProviders;
private final StreamProvider myServerStreamProvider;
private final String FILE_SPEC;
private final String myShelfPath;
@@ -44,20 +44,19 @@ public class CompoundShelfFileProcessor {
public CompoundShelfFileProcessor(final String subdirName) {
mySubdirName = subdirName;
myServerStreamProviders = ((ApplicationImpl)ApplicationManager.getApplication()).getStateStore().getStateStorageManager().getStreamProviders();
myServerStreamProvider = ((ApplicationImpl)ApplicationManager.getApplication()).getStateStore().getStateStorageManager().getStreamProvider();
FILE_SPEC = "$ROOT_CONFIG$/" + subdirName + "/";
myShelfPath = PathManager.getConfigPath() + File.separator + mySubdirName;
}
public CompoundShelfFileProcessor(final StreamProvider[] serverStreamProviders, final String shelfPath) {
myServerStreamProviders = serverStreamProviders;
public CompoundShelfFileProcessor(@Nullable StreamProvider serverStreamProvider, String shelfPath) {
myServerStreamProvider = serverStreamProvider;
myShelfPath = shelfPath;
mySubdirName = new File(myShelfPath).getName();
FILE_SPEC = "$ROOT_CONFIG$/" + mySubdirName + "/";
}
/*
public void onWriteExternal() {
if (myShelfPath != null) {
@@ -98,39 +97,33 @@ public class CompoundShelfFileProcessor {
}
public Collection<String> getServerFiles() {
Collection<String> result = new LinkedHashSet<String>();
for (StreamProvider serverStreamProvider : myServerStreamProviders) {
if (serverStreamProvider.isEnabled()) {
ContainerUtil.addAll(result, serverStreamProvider.listSubFiles(FILE_SPEC, RoamingType.PER_USER));
}
if (myServerStreamProvider == null || !myServerStreamProvider.isEnabled()) {
return Collections.emptyList();
}
return result;
return myServerStreamProvider.listSubFiles(FILE_SPEC, RoamingType.PER_USER);
}
public String copyFileFromServer(final String serverFileName, final List<String> localFileNames) {
for (StreamProvider serverStreamProvider : myServerStreamProviders) {
if (serverStreamProvider.isEnabled()) {
try {
File file = new File(new File(myShelfPath), serverFileName);
if (!file.exists()) {
InputStream stream = serverStreamProvider.loadContent(FILE_SPEC + serverFileName, RoamingType.PER_USER);
if (stream != null) {
//noinspection ResultOfMethodCallIgnored
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
try {
FileUtil.copy(stream, out);
}
finally {
out.close();
stream.close();
}
if (myServerStreamProvider != null && myServerStreamProvider.isEnabled()) {
try {
File file = new File(new File(myShelfPath), serverFileName);
if (!file.exists()) {
InputStream stream = myServerStreamProvider.loadContent(FILE_SPEC + serverFileName, RoamingType.PER_USER);
if (stream != null) {
//noinspection ResultOfMethodCallIgnored
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
try {
FileUtil.copy(stream, out);
}
finally {
out.close();
stream.close();
}
}
}
catch (IOException e) {
//ignore
}
}
catch (IOException ignored) {
}
}
localFileNames.add(serverFileName);
@@ -139,12 +132,9 @@ public class CompoundShelfFileProcessor {
public String renameFileOnServer(final String serverFileName, final Collection<String> serverFileNames, final Collection<String> localFileNames) {
String newName = getNewFileName(serverFileName, serverFileNames, localFileNames);
String oldFilePath = FILE_SPEC + serverFileName;
String newFilePath = FILE_SPEC + newName;
for (StreamProvider serverStreamProvider : myServerStreamProviders) {
renameFileOnProvider(newName, oldFilePath, newFilePath, serverStreamProvider);
if (myServerStreamProvider != null && myServerStreamProvider.isEnabled()) {
renameFileOnProvider(newName, FILE_SPEC + serverFileName, FILE_SPEC + newName, myServerStreamProvider);
}
return newName;
}
@@ -205,7 +195,6 @@ public class CompoundShelfFileProcessor {
}
public void savePathFile(ContentProvider contentProvider, final File patchPath, CommitContext commitContext) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(patchPath), CharsetToolkit.UTF8_CHARSET);
try {
contentProvider.writeContentTo(writer, commitContext);
@@ -214,13 +203,9 @@ public class CompoundShelfFileProcessor {
writer.close();
}
for (StreamProvider serverStreamProvider : myServerStreamProviders) {
copyFileContentToProviders(FILE_SPEC + patchPath.getName(), serverStreamProvider, patchPath);
if (myServerStreamProvider != null && myServerStreamProvider.isEnabled()) {
copyFileContentToProviders(FILE_SPEC + patchPath.getName(), myServerStreamProvider, patchPath);
}
}
public File getBaseIODir() {
@@ -228,23 +213,16 @@ public class CompoundShelfFileProcessor {
}
public void saveFile(final File from, final File to) throws IOException {
for (StreamProvider serverStreamProvider : myServerStreamProviders) {
copyFileContentToProviders(FILE_SPEC + to.getName(), serverStreamProvider, from);
if (myServerStreamProvider != null && myServerStreamProvider.isEnabled()) {
copyFileContentToProviders(FILE_SPEC + to.getName(), myServerStreamProvider, from);
}
FileUtil.copy(from, to);
}
public void delete(final String name) {
FileUtil.delete(new File(getBaseIODir(), name));
for (StreamProvider serverStreamProvider : myServerStreamProviders) {
if (serverStreamProvider.isEnabled()) {
serverStreamProvider.deleteFile(FILE_SPEC + name, RoamingType.PER_USER);
}
if (myServerStreamProvider != null && myServerStreamProvider.isEnabled()) {
myServerStreamProvider.deleteFile(FILE_SPEC + name, RoamingType.PER_USER);
}
}
}
}
@@ -23,6 +23,7 @@ import com.intellij.openapi.util.WriteExternalException;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import java.io.File;
@@ -38,9 +39,9 @@ public class ShelfManagerConfigurationMerger implements XmlConfigurationMerger {
}
@TestOnly
public ShelfManagerConfigurationMerger(final String configPath, final StreamProvider[] streamProviders) {
public ShelfManagerConfigurationMerger(final String configPath, @Nullable StreamProvider streamProvider) {
myConfigPath = configPath;
myFileProcessor = new CompoundShelfFileProcessor(streamProviders, configPath);
myFileProcessor = new CompoundShelfFileProcessor(streamProvider, configPath);
}
@Override
@@ -26,7 +26,6 @@ import com.intellij.lifecycle.PeriodicalTasksCloser;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.components.AbstractProjectComponent;
import com.intellij.openapi.components.impl.stores.StreamProvider;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.diff.impl.patch.*;
import com.intellij.openapi.diff.impl.patch.apply.ApplyFilePatchBase;
@@ -87,7 +86,7 @@ public class ShelveChangesManager extends AbstractProjectComponent implements JD
super(project);
myBus = bus;
if (project.isDefault()) {
myFileProcessor = new CompoundShelfFileProcessor(StreamProvider.EMPTY_ARRAY, PathManager.getConfigPath() + File.separator + "shelf");
myFileProcessor = new CompoundShelfFileProcessor(null, PathManager.getConfigPath() + File.separator + "shelf");
}
else {
myFileProcessor = new CompoundShelfFileProcessor("shelf");