exception handling during mount/unmount/reveal operations

This commit is contained in:
Sebastian Stenzel
2016-01-27 14:46:48 +01:00
parent c56d0b7d4a
commit 091a44e65d
20 changed files with 94 additions and 56 deletions

View File

@@ -0,0 +1,8 @@
package org.cryptomator.common;
@FunctionalInterface
public interface ConsumerThrowingException<T, E extends Exception> {
void accept(T t) throws E;
}

View File

@@ -0,0 +1,17 @@
package org.cryptomator.common;
import java.util.Optional;
public final class Optionals {
private Optionals() {
}
public static <T, E extends Exception> void ifPresent(Optional<T> optional, ConsumerThrowingException<T, E> consumer) throws E {
final T t = optional.orElse(null);
if (t != null) {
consumer.accept(t);
}
}
}

View File

@@ -7,7 +7,7 @@
* Sebastian Stenzel - initial API and implementation
* Markus Kreusch - Refactored WebDavMounter to use strategy pattern
******************************************************************************/
package org.cryptomator.frontend.webdav.mount;
package org.cryptomator.frontend;
public class CommandFailedException extends Exception {

View File

@@ -9,10 +9,10 @@ public interface Frontend extends AutoCloseable {
MOUNT_NAME, WIN_DRIVE_LETTER
}
boolean mount(Map<MountParam, Optional<String>> map);
void mount(Map<MountParam, Optional<String>> map) throws CommandFailedException;
void unmount();
void unmount() throws CommandFailedException;
void reveal();
void reveal() throws CommandFailedException;
}

View File

@@ -4,9 +4,9 @@ import java.net.URI;
import java.util.Map;
import java.util.Optional;
import org.cryptomator.frontend.CommandFailedException;
import org.cryptomator.frontend.Frontend;
import org.cryptomator.frontend.FrontendCreationFailedException;
import org.cryptomator.frontend.webdav.mount.CommandFailedException;
import org.cryptomator.frontend.webdav.mount.WebDavMount;
import org.cryptomator.frontend.webdav.mount.WebDavMounterProvider;
import org.eclipse.jetty.servlet.ServletContextHandler;
@@ -36,36 +36,21 @@ class WebDavFrontend implements Frontend {
}
@Override
public boolean mount(Map<MountParam, Optional<String>> mountParams) {
try {
mount = webdavMounterProvider.get().mount(uri, mountParams);
return true;
} catch (CommandFailedException e) {
return false;
public void mount(Map<MountParam, Optional<String>> mountParams) throws CommandFailedException {
mount = webdavMounterProvider.get().mount(uri, mountParams);
}
@Override
public void unmount() throws CommandFailedException {
if (mount != null) {
mount.unmount();
}
}
@Override
public void unmount() {
public void reveal() throws CommandFailedException {
if (mount != null) {
try {
mount.unmount();
} catch (CommandFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void reveal() {
if (mount != null) {
try {
mount.reveal();
} catch (CommandFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mount.reveal();
}
}

View File

@@ -12,6 +12,7 @@ import java.net.URI;
import java.util.Map;
import java.util.Optional;
import org.cryptomator.frontend.CommandFailedException;
import org.cryptomator.frontend.Frontend.MountParam;
/**

View File

@@ -18,6 +18,7 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.frontend.CommandFailedException;
import org.cryptomator.frontend.Frontend.MountParam;
import org.cryptomator.frontend.webdav.mount.command.Script;

View File

@@ -20,6 +20,7 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.frontend.CommandFailedException;
import org.cryptomator.frontend.Frontend.MountParam;
import org.cryptomator.frontend.webdav.mount.command.Script;

View File

@@ -8,6 +8,8 @@
******************************************************************************/
package org.cryptomator.frontend.webdav.mount;
import org.cryptomator.frontend.CommandFailedException;
/**
* A mounted webdav share.
*

View File

@@ -13,6 +13,7 @@ import java.net.URI;
import java.util.Map;
import java.util.Optional;
import org.cryptomator.frontend.CommandFailedException;
import org.cryptomator.frontend.Frontend.MountParam;
public interface WebDavMounter {

View File

@@ -23,6 +23,7 @@ import javax.inject.Singleton;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.frontend.CommandFailedException;
import org.cryptomator.frontend.Frontend.MountParam;
import org.cryptomator.frontend.webdav.mount.command.CommandResult;
import org.cryptomator.frontend.webdav.mount.command.Script;

View File

@@ -15,7 +15,7 @@ import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.util.Strings;
import org.cryptomator.frontend.webdav.mount.CommandFailedException;
import org.cryptomator.frontend.CommandFailedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@@ -24,7 +24,7 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.frontend.webdav.mount.CommandFailedException;
import org.cryptomator.frontend.CommandFailedException;
/**
* <p>

View File

@@ -17,7 +17,7 @@ import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.cryptomator.frontend.webdav.mount.CommandFailedException;
import org.cryptomator.frontend.CommandFailedException;
final class FutureCommandResult implements Future<CommandResult>, Runnable {

View File

@@ -12,7 +12,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.cryptomator.frontend.webdav.mount.CommandFailedException;
import org.cryptomator.frontend.CommandFailedException;
public final class Script {

View File

@@ -163,7 +163,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
<version>19.0</version>
</dependency>
<!-- DI -->

View File

@@ -18,6 +18,10 @@
<name>Cryptomator GUI</name>
<dependencies>
<dependency>
<groupId>org.cryptomator</groupId>
<artifactId>commons</artifactId>
</dependency>
<dependency>
<groupId>org.cryptomator</groupId>
<artifactId>filesystem-api</artifactId>

View File

@@ -19,6 +19,7 @@ import javax.inject.Inject;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.crypto.engine.InvalidPassphraseException;
import org.cryptomator.frontend.CommandFailedException;
import org.cryptomator.frontend.FrontendCreationFailedException;
import org.cryptomator.frontend.webdav.mount.WindowsDriveLetters;
import org.cryptomator.ui.controls.SecPasswordField;
@@ -291,7 +292,13 @@ public class UnlockController extends AbstractFXMLViewController {
if (vault.isUnlocked() && !mountSuccess) {
exec.submit(vault::deactivateFrontend);
} else if (vault.isUnlocked() && mountSuccess) {
exec.submit(vault::reveal);
exec.submit(() -> {
try {
vault.reveal();
} catch (CommandFailedException e) {
LOG.error("Failed to reveal mounted vault", e);
}
});
}
if (mountSuccess && listener != null) {
listener.didUnlock(this);

View File

@@ -15,6 +15,7 @@ import java.util.concurrent.ExecutorService;
import javax.inject.Inject;
import javax.inject.Provider;
import org.cryptomator.frontend.CommandFailedException;
import org.cryptomator.ui.model.Vault;
import org.cryptomator.ui.util.ActiveWindowStyleSupport;
@@ -78,20 +79,28 @@ public class UnlockedController extends AbstractFXMLViewController {
@FXML
private void didClickRevealVault(ActionEvent event) {
exec.submit(vault::reveal);
exec.submit(() -> {
try {
vault.reveal();
} catch (CommandFailedException e) {
Platform.runLater(() -> {
messageLabel.setText(resourceBundle.getString("unlocked.label.revealFailed"));
});
}
});
}
@FXML
private void didClickCloseVault(ActionEvent event) {
exec.submit(() -> {
// try {
vault.unmount();
// } catch (CommandFailedException e) {
// Platform.runLater(() -> {
// messageLabel.setText(resourceBundle.getString("unlocked.label.unmountFailed"));
// });
// return;
// }
try {
vault.unmount();
} catch (CommandFailedException e) {
Platform.runLater(() -> {
messageLabel.setText(resourceBundle.getString("unlocked.label.unmountFailed"));
});
return;
}
vault.deactivateFrontend();
if (listener != null) {
Platform.runLater(() -> {

View File

@@ -15,10 +15,12 @@ import java.util.Set;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.cryptomator.common.Optionals;
import org.cryptomator.filesystem.FileSystem;
import org.cryptomator.filesystem.crypto.CryptoFileSystemDelegate;
import org.cryptomator.filesystem.crypto.CryptoFileSystemFactory;
import org.cryptomator.filesystem.nio.NioFileSystem;
import org.cryptomator.frontend.CommandFailedException;
import org.cryptomator.frontend.Frontend;
import org.cryptomator.frontend.Frontend.MountParam;
import org.cryptomator.frontend.FrontendCreationFailedException;
@@ -120,23 +122,22 @@ public class Vault implements Serializable, CryptoFileSystemDelegate {
}
public Boolean mount() {
// TODO exception handling
Frontend frontend = filesystemFrontend.get().orElse(null);
if (frontend == null) {
try {
Optionals.ifPresent(filesystemFrontend.get(), f -> {
f.mount(getMountParams());
});
return true;
} catch (CommandFailedException e) {
return false;
} else {
return frontend.mount(getMountParams());
}
}
public void reveal() {
// TODO exception handling
filesystemFrontend.get().ifPresent(Frontend::reveal);
public void reveal() throws CommandFailedException {
Optionals.ifPresent(filesystemFrontend.get(), Frontend::reveal);
}
public void unmount() {
// TODO exception handling
filesystemFrontend.get().ifPresent(Frontend::unmount);
public void unmount() throws CommandFailedException {
Optionals.ifPresent(filesystemFrontend.get(), Frontend::unmount);
}
/* Delegate Methods */