diff --git a/main/commons/src/main/java/org/cryptomator/common/vaults/AbstractVolume.java b/main/commons/src/main/java/org/cryptomator/common/vaults/AbstractVolume.java new file mode 100644 index 000000000..69fdfc8af --- /dev/null +++ b/main/commons/src/main/java/org/cryptomator/common/vaults/AbstractVolume.java @@ -0,0 +1,29 @@ +package org.cryptomator.common.vaults; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableSet; +import org.cryptomator.common.mountpoint.InvalidMountPointException; +import org.cryptomator.common.mountpoint.MountPointChooser; +import org.cryptomator.cryptofs.CryptoFileSystem; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Optional; + +public abstract class AbstractVolume implements Volume { + + public Path determineMountPoint() throws InvalidMountPointException { + for (MountPointChooser chooser : this.choosers) { + Optional chosenPath = chooser.chooseMountPoint(); + if (chosenPath.isEmpty()) { + //Chooser was applicable, but couldn't find a feasible mountpoint + continue; + } + this.cleanupRequired = chooser.prepare(chosenPath.get()); //Fail entirely if an Exception occurs + this.usedChooser = chooser; + return chosenPath.get(); + } + String tried = Joiner.on(", ").join(this.choosers.stream().map((mpc) -> mpc.getClass().getTypeName()).collect(ImmutableSet.toImmutableSet())); + throw new InvalidMountPointException(String.format("No feasible MountPoint found! Tried %s", tried)); + } +} diff --git a/main/commons/src/main/java/org/cryptomator/common/vaults/DokanyVolume.java b/main/commons/src/main/java/org/cryptomator/common/vaults/DokanyVolume.java index becd3d7e8..f1f595429 100644 --- a/main/commons/src/main/java/org/cryptomator/common/vaults/DokanyVolume.java +++ b/main/commons/src/main/java/org/cryptomator/common/vaults/DokanyVolume.java @@ -1,7 +1,5 @@ package org.cryptomator.common.vaults; -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableSet; import org.cryptomator.common.mountpoint.InvalidMountPointException; import org.cryptomator.common.mountpoint.MountPointChooser; import org.cryptomator.common.settings.VaultSettings; @@ -19,7 +17,7 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.ExecutorService; -public class DokanyVolume implements Volume { +public class DokanyVolume extends AbstractVolume { private static final Logger LOG = LoggerFactory.getLogger(DokanyVolume.class); @@ -44,11 +42,6 @@ public class DokanyVolume implements Volume { this.choosers = choosers; } - @Override - public boolean isSupported() { - return DokanyVolume.isSupportedStatic(); - } - @Override public void mount(CryptoFileSystem fs, String mountFlags) throws InvalidMountPointException, VolumeException { this.mountPoint = determineMountPoint(); @@ -63,23 +56,6 @@ public class DokanyVolume implements Volume { } } - private Path determineMountPoint() throws InvalidMountPointException { - for (MountPointChooser chooser : this.choosers) { - Optional chosenPath = chooser.chooseMountPoint(); - if (chosenPath.isEmpty()) { - //Chooser was applicable, but couldn't find a feasible mountpoint - continue; - } - this.cleanupRequired = chooser.prepare(chosenPath.get()); //Fail entirely if an Exception occurs - this.usedChooser = chooser; - return chosenPath.get(); - } - String tried = Joiner.on(", ").join(this.choosers.stream() - .map((mpc) -> mpc.getClass().getTypeName()) - .collect(ImmutableSet.toImmutableSet())); - throw new InvalidMountPointException(String.format("No feasible MountPoint found! Tried %s", tried)); - } - @Override public void reveal() throws VolumeException { boolean success = mount.reveal(); @@ -100,6 +76,11 @@ public class DokanyVolume implements Volume { } } + @Override + public boolean isSupported() { + return DokanyVolume.isSupportedStatic(); + } + @Override public Optional getMountPoint() { return Optional.ofNullable(mountPoint); diff --git a/main/commons/src/main/java/org/cryptomator/common/vaults/FuseVolume.java b/main/commons/src/main/java/org/cryptomator/common/vaults/FuseVolume.java index ad5128570..eac288bcc 100644 --- a/main/commons/src/main/java/org/cryptomator/common/vaults/FuseVolume.java +++ b/main/commons/src/main/java/org/cryptomator/common/vaults/FuseVolume.java @@ -1,8 +1,6 @@ package org.cryptomator.common.vaults; -import com.google.common.base.Joiner; import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableSet; import org.apache.commons.lang3.SystemUtils; import org.cryptomator.common.mountpoint.InvalidMountPointException; import org.cryptomator.common.mountpoint.MountPointChooser; @@ -22,7 +20,7 @@ import java.nio.file.Path; import java.util.Optional; import java.util.Set; -public class FuseVolume implements Volume { +public class FuseVolume extends AbstractVolume { private static final Logger LOG = LoggerFactory.getLogger(FuseVolume.class); @@ -47,23 +45,6 @@ public class FuseVolume implements Volume { mount(fs.getPath("/"), mountFlags); } - private Path determineMountPoint() throws InvalidMountPointException { - for (MountPointChooser chooser : this.choosers) { - Optional chosenPath = chooser.chooseMountPoint(); - if (chosenPath.isEmpty()) { - //Chooser was applicable, but couldn't find a feasible mountpoint - continue; - } - this.cleanupRequired = chooser.prepare(chosenPath.get()); //Fail entirely if an Exception occurs - this.usedChooser = chooser; - return chosenPath.get(); - } - String tried = Joiner.on(", ").join(this.choosers.stream() - .map((mpc) -> mpc.getClass().getTypeName()) - .collect(ImmutableSet.toImmutableSet())); - throw new InvalidMountPointException(String.format("No feasible MountPoint found! Tried %s", tried)); - } - private void mount(Path root, String mountFlags) throws VolumeException { try { Mounter mounter = FuseMountFactory.getMounter();