Browse Source

exception handling during mount/unmount/reveal operations

Sebastian Stenzel 9 years ago
parent
commit
091a44e65d
20 changed files with 90 additions and 52 deletions
  1. 8 0
      main/commons/src/main/java/org/cryptomator/common/ConsumerThrowingException.java
  2. 17 0
      main/commons/src/main/java/org/cryptomator/common/Optionals.java
  3. 1 1
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/CommandFailedException.java
  4. 3 3
      main/frontend-api/src/main/java/org/cryptomator/frontend/Frontend.java
  5. 7 22
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/WebDavFrontend.java
  6. 1 0
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/FallbackWebDavMounter.java
  7. 1 0
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/LinuxGvfsWebDavMounter.java
  8. 1 0
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/MacOsXWebDavMounter.java
  9. 2 0
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/WebDavMount.java
  10. 1 0
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/WebDavMounter.java
  11. 1 0
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/WindowsWebDavMounter.java
  12. 1 1
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/command/CommandResult.java
  13. 1 1
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/command/CommandRunner.java
  14. 1 1
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/command/FutureCommandResult.java
  15. 1 1
      main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/command/Script.java
  16. 1 1
      main/pom.xml
  17. 4 0
      main/ui/pom.xml
  18. 8 1
      main/ui/src/main/java/org/cryptomator/ui/controllers/UnlockController.java
  19. 18 9
      main/ui/src/main/java/org/cryptomator/ui/controllers/UnlockedController.java
  20. 12 11
      main/ui/src/main/java/org/cryptomator/ui/model/Vault.java

+ 8 - 0
main/commons/src/main/java/org/cryptomator/common/ConsumerThrowingException.java

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

+ 17 - 0
main/commons/src/main/java/org/cryptomator/common/Optionals.java

@@ -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);
+		}
+	}
+
+}

+ 1 - 1
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/CommandFailedException.java

@@ -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 {
 

+ 3 - 3
main/frontend-api/src/main/java/org/cryptomator/frontend/Frontend.java

@@ -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;
 
 }

+ 7 - 22
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/WebDavFrontend.java

@@ -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() {
+	public void unmount() throws CommandFailedException {
 		if (mount != null) {
-			try {
-				mount.unmount();
-			} catch (CommandFailedException e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
-			}
+			mount.unmount();
 		}
 	}
 
 	@Override
-	public void reveal() {
+	public void reveal() throws CommandFailedException {
 		if (mount != null) {
-			try {
-				mount.reveal();
-			} catch (CommandFailedException e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
-			}
+			mount.reveal();
 		}
 	}
 

+ 1 - 0
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/FallbackWebDavMounter.java

@@ -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;
 
 /**

+ 1 - 0
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/LinuxGvfsWebDavMounter.java

@@ -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;
 

+ 1 - 0
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/MacOsXWebDavMounter.java

@@ -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;
 

+ 2 - 0
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/WebDavMount.java

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

+ 1 - 0
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/WebDavMounter.java

@@ -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 {

+ 1 - 0
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/WindowsWebDavMounter.java

@@ -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;

+ 1 - 1
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/command/CommandResult.java

@@ -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;
 

+ 1 - 1
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/command/CommandRunner.java

@@ -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>

+ 1 - 1
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/command/FutureCommandResult.java

@@ -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 {
 	

+ 1 - 1
main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/mount/command/Script.java

@@ -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 {
 

+ 1 - 1
main/pom.xml

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

+ 4 - 0
main/ui/pom.xml

@@ -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>

+ 8 - 1
main/ui/src/main/java/org/cryptomator/ui/controllers/UnlockController.java

@@ -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);

+ 18 - 9
main/ui/src/main/java/org/cryptomator/ui/controllers/UnlockedController.java

@@ -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(() -> {

+ 12 - 11
main/ui/src/main/java/org/cryptomator/ui/model/Vault.java

@@ -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 */