dealing with deprecation

This commit is contained in:
Sebastian Stenzel
2022-04-05 08:14:14 +02:00
parent 5a4f714c93
commit 4c363a9abc
3 changed files with 19 additions and 72 deletions
@@ -1,67 +0,0 @@
package org.cryptomator.common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
@Deprecated // to be moved to integrations-api 1.1.0
@Singleton
public class PluginClassLoader extends URLClassLoader {
private static final Logger LOG = LoggerFactory.getLogger(PluginClassLoader.class);
private static final String NAME = "PluginClassLoader";
private static final String JAR_SUFFIX = ".jar";
@Inject
public PluginClassLoader(Environment env) {
super(NAME, env.getPluginDir().map(PluginClassLoader::findJars).orElse(new URL[0]), PluginClassLoader.class.getClassLoader());
}
private static URL[] findJars(Path path) {
if (!Files.isDirectory(path)) {
return new URL[0];
} else {
try {
var visitor = new JarVisitor();
Files.walkFileTree(path, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, visitor);
return visitor.urls.toArray(URL[]::new);
} catch (IOException e) {
LOG.warn("Failed to scan plugin dir " + path, e);
return new URL[0];
}
}
}
private static final class JarVisitor extends SimpleFileVisitor<Path> {
private final List<URL> urls = new ArrayList<>();
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (attrs.isRegularFile() && file.getFileName().toString().toLowerCase().endsWith(JAR_SUFFIX)) {
try {
urls.add(file.toUri().toURL());
} catch (MalformedURLException e) {
LOG.warn("Failed to create URL for jar file {}", file);
}
}
return FileVisitResult.CONTINUE;
}
}
}
@@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test;
import javafx.application.Platform;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -23,7 +24,7 @@ public class KeychainManagerTest {
@Test
public void testStoreAndLoad() throws KeychainAccessException {
KeychainManager keychainManager = new KeychainManager(new SimpleObjectProperty<>(new MapKeychainAccess()));
keychainManager.storePassphrase("test", "asd");
keychainManager.storePassphrase("test", "Test", "asd");
Assertions.assertArrayEquals("asd".toCharArray(), keychainManager.loadPassphrase("test"));
}
@@ -42,7 +43,7 @@ public class KeychainManagerTest {
public void testPropertyChangesWhenStoringPassword() throws KeychainAccessException, InterruptedException {
KeychainManager keychainManager = new KeychainManager(new SimpleObjectProperty<>(new MapKeychainAccess()));
ReadOnlyBooleanProperty property = keychainManager.getPassphraseStoredProperty("test");
Assertions.assertEquals(false, property.get());
Assertions.assertFalse(property.get());
keychainManager.storePassphrase("test", "bar");
@@ -52,8 +53,8 @@ public class KeychainManagerTest {
result.set(property.get());
latch.countDown();
});
latch.await(1, TimeUnit.SECONDS);
Assertions.assertEquals(true, result.get());
Assertions.assertTimeoutPreemptively(Duration.ofSeconds(1), () -> latch.await());
Assertions.assertTrue(result.get());
}
}
@@ -5,6 +5,7 @@
*******************************************************************************/
package org.cryptomator.common.keychain;
import org.cryptomator.integrations.keychain.KeychainAccessException;
import org.cryptomator.integrations.keychain.KeychainAccessProvider;
import java.util.HashMap;
@@ -20,7 +21,13 @@ class MapKeychainAccess implements KeychainAccessProvider {
}
@Override
@Deprecated
public void storePassphrase(String key, CharSequence passphrase) {
throw new NoSuchMethodError("not implemented");
}
@Override
public void storePassphrase(String key, String displayName,CharSequence passphrase) {
char[] pw = new char[passphrase.length()];
for (int i = 0; i < passphrase.length(); i++) {
pw[i] = passphrase.charAt(i);
@@ -39,7 +46,13 @@ class MapKeychainAccess implements KeychainAccessProvider {
}
@Override
public void changePassphrase(String key, CharSequence passphrase) {
@Deprecated
public void changePassphrase(String key, CharSequence passphrase) throws KeychainAccessException {
throw new NoSuchMethodError("not implemented");
}
@Override
public void changePassphrase(String key, String displayName, CharSequence passphrase) {
map.get(key);
storePassphrase(key, passphrase);
}