Skip to content

Commit 71bcf11

Browse files
committed
Cache classloader
and lazily initialize it
1 parent e833e27 commit 71bcf11

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/main/java/org/cryptomator/integrations/common/ClassLoaderFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ClassLoaderFactory {
2323

2424
/**
2525
* Attempts to find {@code .jar} files in the path specified in {@value #PLUGIN_DIR_KEY} system property.
26-
* A new class loader instance is returned that loads classes from the given classes.
26+
* A new class loader instance is returned that loads classes from the given directory.
2727
*
2828
* @return A new URLClassLoader that is aware of all {@code .jar} files in the plugin dir
2929
*/

src/main/java/org/cryptomator/integrations/common/IntegrationsLoader.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public class IntegrationsLoader {
2323
private IntegrationsLoader() {
2424
}
2525

26+
private static class PluginClassLoaderHolder {
27+
private static final ClassLoader CLASS_LOADER = ClassLoaderFactory.forPluginDir();
28+
}
29+
30+
private static ClassLoader getClassLoader() {
31+
return PluginClassLoaderHolder.CLASS_LOADER;
32+
}
33+
2634
/**
2735
* Loads the best suited service provider, i.e. the one with the highest priority that is supported.
2836
* <p>
@@ -44,7 +52,7 @@ public static <T> Optional<T> load(Class<T> clazz) {
4452
* @param <T> Type of the service
4553
*/
4654
public static <T> Optional<T> loadSpecific(Class<T> clazz, String implementationClassName) {
47-
return ServiceLoader.load(clazz, ClassLoaderFactory.forPluginDir()).stream()
55+
return ServiceLoader.load(clazz, getClassLoader()).stream()
4856
.filter(provider -> provider.type().getName().equals(implementationClassName))
4957
.map(ServiceLoader.Provider::get)
5058
.findAny();
@@ -61,7 +69,7 @@ public static <T> Optional<T> loadSpecific(Class<T> clazz, String implementation
6169
* @return An ordered stream of all suited service providers
6270
*/
6371
public static <T> Stream<T> loadAll(Class<T> clazz) {
64-
return loadAll(ServiceLoader.load(clazz, ClassLoaderFactory.forPluginDir()), clazz);
72+
return loadAll(ServiceLoader.load(clazz, getClassLoader()), clazz);
6573
}
6674

6775
/**

0 commit comments

Comments
 (0)