-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#413: Implement new plugin service loader
- Loading branch information
kaklakariada
committed
Jun 16, 2024
1 parent
bfcb923
commit 887a5ca
Showing
6 changed files
with
241 additions
and
13 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...src/main/java/org/itsallcode/openfasttrace/core/serviceloader/ClassPathServiceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.itsallcode.openfasttrace.core.serviceloader; | ||
|
||
import java.util.ServiceLoader; | ||
import java.util.ServiceLoader.Provider; | ||
import java.util.stream.Stream; | ||
|
||
final class ClassPathServiceLoader<T> implements Loader<T> | ||
{ | ||
private final ServiceLoader<T> serviceLoader; | ||
|
||
private ClassPathServiceLoader(final ServiceLoader<T> serviceLoader) | ||
{ | ||
this.serviceLoader = serviceLoader; | ||
} | ||
|
||
static <T> Loader<T> load(final Class<T> serviceType, final ServiceOrigin serviceOrigin) | ||
{ | ||
return new ClassPathServiceLoader<>(ServiceLoader.load(serviceType, serviceOrigin.getClassLoader())); | ||
} | ||
|
||
@Override | ||
public Stream<T> load() | ||
{ | ||
return this.serviceLoader.stream().map(Provider::get); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
core/src/main/java/org/itsallcode/openfasttrace/core/serviceloader/DelegatingLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.itsallcode.openfasttrace.core.serviceloader; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
class DelegatingLoader<T> implements Loader<T> | ||
{ | ||
private final List<Loader<T>> delegates; | ||
|
||
DelegatingLoader(final List<Loader<T>> delegates) | ||
{ | ||
this.delegates = delegates; | ||
} | ||
|
||
@Override | ||
public Stream<T> load() | ||
{ | ||
return delegates.stream().flatMap(Loader::load); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
core/src/main/java/org/itsallcode/openfasttrace/core/serviceloader/Loader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.itsallcode.openfasttrace.core.serviceloader; | ||
|
||
import java.util.stream.Stream; | ||
|
||
interface Loader<T> | ||
{ | ||
Stream<T> load(); | ||
} |
106 changes: 106 additions & 0 deletions
106
core/src/main/java/org/itsallcode/openfasttrace/core/serviceloader/PluginLoaderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.itsallcode.openfasttrace.core.serviceloader; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.*; | ||
|
||
class PluginLoaderFactory | ||
{ | ||
private final Path pluginsDirectory; | ||
|
||
PluginLoaderFactory(final Path pluginsDirectory) | ||
{ | ||
this.pluginsDirectory = pluginsDirectory; | ||
} | ||
|
||
static PluginLoaderFactory createDefault() | ||
{ | ||
return new PluginLoaderFactory(getHomeDirectory().resolve(".oft").resolve("plugins")); | ||
} | ||
|
||
private static Path getHomeDirectory() | ||
{ | ||
return Path.of(System.getProperty("user.home")); | ||
} | ||
|
||
<T> Loader<T> createLoader(final Class<T> serviceType) | ||
{ | ||
return createLoader(serviceType, findServiceOrigins()); | ||
} | ||
|
||
private <T> Loader<T> createLoader(final Class<T> serviceType, final List<ServiceOrigin> origins) | ||
{ | ||
final List<Loader<T>> loaders = origins.stream().map(origin -> ClassPathServiceLoader.load(serviceType, origin)) | ||
.toList(); | ||
return new DelegatingLoader<>(loaders); | ||
} | ||
|
||
private List<ServiceOrigin> findServiceOrigins() | ||
{ | ||
final List<ServiceOrigin> origins = new ArrayList<>(); | ||
origins.add(ServiceOrigin.forCurrentClassPath()); | ||
origins.addAll(findPluginOrigins()); | ||
return origins; | ||
} | ||
|
||
Collection<ServiceOrigin> findPluginOrigins() | ||
{ | ||
if (!Files.isDirectory(pluginsDirectory)) | ||
{ | ||
return Collections.emptyList(); | ||
} | ||
|
||
try | ||
{ | ||
return Files.list(pluginsDirectory) | ||
.map(this::originForPath) | ||
.flatMap(Optional::stream) | ||
.toList(); | ||
} | ||
catch (final IOException exception) | ||
{ | ||
throw new UncheckedIOException( | ||
"Failed to list plugin directories in '" + this.pluginsDirectory + "': " + exception.getMessage(), | ||
exception); | ||
} | ||
} | ||
|
||
private Optional<ServiceOrigin> originForPath(final Path path) | ||
{ | ||
if (isJarFile(path)) | ||
{ | ||
return Optional.of(ServiceOrigin.forJars(List.of(path))); | ||
} | ||
if (!Files.isDirectory(path)) | ||
{ | ||
return Optional.empty(); | ||
} | ||
final List<Path> jars = findJarsInDir(path); | ||
if (jars.isEmpty()) | ||
{ | ||
return Optional.empty(); | ||
} | ||
return Optional.of(ServiceOrigin.forJars(jars)); | ||
} | ||
|
||
private static List<Path> findJarsInDir(final Path path) | ||
{ | ||
try | ||
{ | ||
return Files.list(path).filter(PluginLoaderFactory::isJarFile).toList(); | ||
} | ||
catch (final IOException exception) | ||
{ | ||
throw new UncheckedIOException( | ||
"Failed to list files in plugin directory '" + path + "': " + exception.getMessage(), | ||
exception); | ||
} | ||
} | ||
|
||
private static boolean isJarFile(final Path path) | ||
{ | ||
return path.getFileName().toString().toLowerCase(Locale.ENGLISH).endsWith(".jar"); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
core/src/main/java/org/itsallcode/openfasttrace/core/serviceloader/ServiceOrigin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.itsallcode.openfasttrace.core.serviceloader; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
import java.net.*; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
final class ServiceOrigin | ||
{ | ||
private final ClassLoader classLoader; | ||
|
||
private ServiceOrigin(final ClassLoader classLoader) | ||
{ | ||
this.classLoader = classLoader; | ||
} | ||
|
||
public static ServiceOrigin forCurrentClassPath() | ||
{ | ||
return new ServiceOrigin(getBaseClassLoader()); | ||
} | ||
|
||
private static ClassLoader getBaseClassLoader() | ||
{ | ||
return Thread.currentThread().getContextClassLoader(); | ||
} | ||
|
||
public static ServiceOrigin forJar(final Path jar) | ||
{ | ||
return forJars(List.of(jar)); | ||
} | ||
|
||
public static ServiceOrigin forJars(final List<Path> jars) | ||
{ | ||
return new ServiceOrigin(createClassLoader(jars)); | ||
} | ||
|
||
private static ClassLoader createClassLoader(final List<Path> jars) | ||
{ | ||
final String name = "ServiceClassLoader-" | ||
+ jars.stream().map(Path::getFileName).map(Path::toString).collect(joining(",")); | ||
final URL[] urls = jars.stream().map(ServiceOrigin::toUrl).toArray(URL[]::new); | ||
return new URLClassLoader(name, urls, getBaseClassLoader()); | ||
} | ||
|
||
private static URL toUrl(final Path path) | ||
{ | ||
try | ||
{ | ||
return path.toUri().toURL(); | ||
} | ||
catch (final MalformedURLException e) | ||
{ | ||
throw new IllegalStateException("Error converting path " + path + " to url", e); | ||
} | ||
} | ||
|
||
public ClassLoader getClassLoader() | ||
{ | ||
return classLoader; | ||
} | ||
} |