save — simplify error handling

This commit is contained in:
Vladimir Krivosheev
2014-09-26 18:32:35 +02:00
parent 6713517f3d
commit bbd2f6f6b1
7 changed files with 61 additions and 73 deletions
@@ -25,13 +25,13 @@ import com.intellij.openapi.module.impl.ModuleImpl;
import com.intellij.openapi.project.impl.ProjectImpl;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.SmartList;
import com.intellij.util.containers.SmartHashSet;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -90,7 +90,7 @@ public class ProjectWithModulesStoreImpl extends ProjectStoreImpl {
}
private class ProjectWithModulesSaveSession extends ProjectSaveSession {
List<SaveSession> myModuleSaveSessions = new ArrayList<SaveSession>();
List<SaveSession> myModuleSaveSessions = new SmartList<SaveSession>();
public ProjectWithModulesSaveSession() {
for (Module module : getPersistentModules()) {
@@ -169,7 +169,7 @@ public class ProjectWithModulesStoreImpl extends ProjectStoreImpl {
}
@Override
protected void beforeSave() throws IOException {
protected void beforeSave() {
super.beforeSave();
for (SaveSession moduleSaveSession : myModuleSaveSessions) {
moduleSaveSession.save();
@@ -177,7 +177,7 @@ public class ProjectWithModulesStoreImpl extends ProjectStoreImpl {
}
@Override
protected void collectSubFilesToSave(final List<File> result) throws IOException {
protected void collectSubFilesToSave(@NotNull List<File> result) {
for (SaveSession moduleSaveSession : myModuleSaveSessions) {
result.addAll(moduleSaveSession.getAllStorageFilesToSave(true));
}
@@ -1390,18 +1390,18 @@ public class ApplicationImpl extends PlatformComponentManagerImpl implements App
try {
StoreUtil.doSave(getStateStore());
}
catch (final Throwable ex) {
catch (final Throwable e) {
if (isUnitTestMode()) {
System.out.println("Saving application settings failed");
ex.printStackTrace();
e.printStackTrace();
}
else {
LOG.info("Saving application settings failed", ex);
LOG.info("Saving application settings failed", e);
invokeLater(new Runnable() {
@Override
public void run() {
if (ex instanceof PluginException) {
final PluginException pluginException = (PluginException)ex;
if (e instanceof PluginException) {
final PluginException pluginException = (PluginException)e;
PluginManagerCore.disablePlugin(pluginException.getPluginId().getIdString());
Messages.showMessageDialog("The plugin " +
pluginException.getPluginId() +
@@ -1410,7 +1410,7 @@ public class ApplicationImpl extends PlatformComponentManagerImpl implements App
Messages.getErrorIcon());
}
else {
Messages.showMessageDialog(ApplicationBundle.message("application.save.settings.error", ex.getLocalizedMessage()),
Messages.showMessageDialog(ApplicationBundle.message("application.save.settings.error", e.getLocalizedMessage()),
CommonBundle.getErrorTitle(), Messages.getErrorIcon());
}
@@ -36,7 +36,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
@@ -346,42 +345,25 @@ public abstract class ComponentStoreImpl implements IComponentStore {
@NotNull
@Override
public List<File> getAllStorageFilesToSave(final boolean includingSubStructures) throws IOException {
try {
return myStorageManagerSaveSession.getAllStorageFilesToSave();
}
catch (StateStorageException e) {
throw new IOException(e.getMessage());
}
public List<File> getAllStorageFilesToSave(final boolean includingSubStructures) {
return myStorageManagerSaveSession.getAllStorageFilesToSave();
}
@NotNull
@Override
public SaveSession save() throws IOException {
try {
final SettingsSavingComponent[] settingsComponents =
mySettingsSavingComponents.toArray(new SettingsSavingComponent[mySettingsSavingComponents.size()]);
for (SettingsSavingComponent settingsSavingComponent : settingsComponents) {
try {
settingsSavingComponent.save();
}
catch (StateStorageException e) {
LOG.info(e);
throw new IOException(e.getMessage());
}
catch (Exception e) {
LOG.error(e);
}
public SaveSession save() {
SettingsSavingComponent[] settingsComponents =
mySettingsSavingComponents.toArray(new SettingsSavingComponent[mySettingsSavingComponents.size()]);
for (SettingsSavingComponent settingsSavingComponent : settingsComponents) {
try {
settingsSavingComponent.save();
}
catch (Throwable e) {
LOG.error(e);
}
myStorageManagerSaveSession.save();
}
catch (StateStorageException e) {
LOG.info(e);
throw new IOException(e.getMessage(), e);
}
myStorageManagerSaveSession.save();
return this;
}
@@ -45,8 +45,7 @@ public interface IComponentStore {
@NotNull
StateStorageManager getStateStorageManager();
class SaveCancelledException extends IOException {
class SaveCancelledException extends RuntimeException {
public SaveCancelledException() {
}
@@ -60,10 +59,10 @@ public interface IComponentStore {
interface SaveSession {
@NotNull
List<File> getAllStorageFilesToSave(final boolean includingSubStructures) throws IOException;
List<File> getAllStorageFilesToSave(boolean includingSubStructures);
@NotNull
SaveSession save() throws IOException;
SaveSession save();
void finishSave();
@@ -474,27 +474,36 @@ class ProjectStoreImpl extends BaseFileConfigurableStoreImpl implements IProject
@NotNull
@Override
public List<File> getAllStorageFilesToSave(final boolean includingSubStructures) throws IOException {
List<File> result = new SmartList<File>();
public List<File> getAllStorageFilesToSave(boolean includingSubStructures) {
List<File> result = null;
if (includingSubStructures) {
result = new SmartList<File>();
collectSubFilesToSave(result);
}
result.addAll(super.getAllStorageFilesToSave(false));
List<File> filesToSave = super.getAllStorageFilesToSave(false);
if (result == null) {
return filesToSave;
}
result.addAll(filesToSave);
return result;
}
protected void collectSubFilesToSave(final List<File> result) throws IOException { }
protected void collectSubFilesToSave(@NotNull List<File> result) {
}
@NotNull
@Override
public SaveSession save() throws IOException {
final ProjectImpl.UnableToSaveProjectNotification[] notifications =
public SaveSession save() {
ProjectImpl.UnableToSaveProjectNotification[] notifications =
NotificationsManager.getNotificationsManager().getNotificationsOfType(ProjectImpl.UnableToSaveProjectNotification.class, myProject);
if (notifications.length > 0) throw new SaveCancelledException();
if (notifications.length > 0) {
throw new SaveCancelledException();
}
final ReadonlyStatusHandler.OperationStatus operationStatus = ensureConfigFilesWritable();
ReadonlyStatusHandler.OperationStatus operationStatus = ensureConfigFilesWritable();
if (operationStatus == null) {
throw new IOException();
throw new StateStorageException();
}
else if (operationStatus.hasReadonlyFiles()) {
ProjectImpl.dropUnableToSaveProjectNotification(myProject, operationStatus.getReadonlyFiles());
@@ -502,30 +511,28 @@ class ProjectStoreImpl extends BaseFileConfigurableStoreImpl implements IProject
}
beforeSave();
super.save();
return this;
}
protected void beforeSave() throws IOException {
protected void beforeSave() {
}
private ReadonlyStatusHandler.OperationStatus ensureConfigFilesWritable() {
return ApplicationManager.getApplication().runReadAction(new Computable<ReadonlyStatusHandler.OperationStatus>() {
@Override
public ReadonlyStatusHandler.OperationStatus compute() {
final List<File> filesToSave;
List<File> filesToSave;
try {
filesToSave = getAllStorageFilesToSave(true);
final Iterator<File> iterator = filesToSave.iterator();
Iterator<File> iterator = filesToSave.iterator();
while (iterator.hasNext()) {
if (!iterator.next().exists()) {
iterator.remove();
}
}
}
catch (IOException e) {
catch (Exception e) {
LOG.error(e);
return null;
}
@@ -549,7 +556,7 @@ class ProjectStoreImpl extends BaseFileConfigurableStoreImpl implements IProject
}
}
if (readonlyFiles.size() == 0) {
if (readonlyFiles.isEmpty()) {
final VirtualFile projectBaseDir = getProjectBaseDir();
if (projectBaseDir != null && projectBaseDir.isValid()) {
if (!projectBaseDir.isWritable()) {
@@ -581,7 +588,6 @@ class ProjectStoreImpl extends BaseFileConfigurableStoreImpl implements IProject
}
}
private final StateStorageChooser<PersistentStateComponent<?>> myStateStorageChooser = new StateStorageChooser<PersistentStateComponent<?>>() {
@Override
public Storage[] selectStorages(final Storage[] storages, final PersistentStateComponent<?> component, final StateStorageOperation operation) {
@@ -17,8 +17,6 @@ package com.intellij.openapi.components.impl.stores;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
/**
* @author yole
*/
@@ -26,7 +24,7 @@ public class StoreUtil {
private StoreUtil() {
}
public static void doSave(@NotNull IComponentStore stateStore) throws IOException {
public static void doSave(@NotNull IComponentStore stateStore) {
IComponentStore.SaveSession session = null;
try {
session = stateStore.startSave();
@@ -327,19 +327,21 @@ public class ProjectImpl extends PlatformComponentManagerImpl implements Project
@Override
public void save() {
if (ApplicationManagerEx.getApplicationEx().isDoNotSave()) return; //no need to save
if (ApplicationManagerEx.getApplicationEx().isDoNotSave()) {
return; //no need to save
}
if (!mySavingInProgress.compareAndSet(false, true)) {
return;
}
try {
if (isToSaveProjectName()) {
final IProjectStore stateStore = getStateStore();
final VirtualFile baseDir = stateStore.getProjectBaseDir();
VirtualFile baseDir = getStateStore().getProjectBaseDir();
if (baseDir != null && baseDir.isValid()) {
final VirtualFile ideaDir = baseDir.findChild(DIRECTORY_STORE_FOLDER);
VirtualFile ideaDir = baseDir.findChild(DIRECTORY_STORE_FOLDER);
if (ideaDir != null && ideaDir.isValid() && ideaDir.isDirectory()) {
final File nameFile = new File(ideaDir.getPath(), NAME_FILE);
File nameFile = new File(ideaDir.getPath(), NAME_FILE);
try {
FileUtil.writeToFile(nameFile, getName().getBytes("UTF-8"), false);
myOldName = null;
@@ -367,12 +369,13 @@ public class ProjectImpl extends PlatformComponentManagerImpl implements Project
(ApplicationManagerEx.getApplicationEx().isInternal() ? "<p>" + StringUtil.getThrowableText(e) + "</p>" : ""),
NotificationType.ERROR);
Notifications.Bus.notify(notification, this);
LOG.info("Unable to save plugin settings",e);
LOG.info("Unable to save plugin settings", e);
}
catch (IOException e) {
MessagesEx.error(this, ProjectBundle.message("project.save.error", e.getMessage())).showLater();
catch (Throwable e) {
LOG.info("Error saving project", e);
} finally {
MessagesEx.error(this, ProjectBundle.message("project.save.error", e.getMessage())).showLater();
}
finally {
mySavingInProgress.set(false);
ApplicationManager.getApplication().getMessageBus().syncPublisher(ProjectSaved.TOPIC).saved(this);
}