(String[] args)
| 17 | |
| 18 | class PluginLoader { |
| 19 | public static void run(String[] args) { |
| 20 | List<Path> pluginPaths = new ArrayList<>(); |
| 21 | pluginPaths.add(Paths.get("plugins")); |
| 22 | |
| 23 | for (int i = 0; i < args.length; i++) { |
| 24 | switch (args[i]) { |
| 25 | case "--additional-plugins": |
| 26 | while (i+1 < args.length && !args[i+1].startsWith("--")) { |
| 27 | pluginPaths.add(Path.of(args[++i])); |
| 28 | } |
| 29 | |
| 30 | break; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | List<URL> urls = new ArrayList<>(); |
| 35 | |
| 36 | for (Path path : pluginPaths) { |
| 37 | try { |
| 38 | if (Files.isDirectory(path)) { |
| 39 | Stream<Path> stream = Files.list(path); |
| 40 | urls.addAll(stream |
| 41 | .filter(p -> p.getFileName().toString().toLowerCase(Locale.ENGLISH).endsWith(".jar")) |
| 42 | .map(p -> { |
| 43 | try { |
| 44 | return p.toUri().toURL(); |
| 45 | } catch (MalformedURLException e) { |
| 46 | throw new RuntimeException(e); |
| 47 | } |
| 48 | }) |
| 49 | .collect(Collectors.toList())); |
| 50 | stream.close(); |
| 51 | } else if (path.getFileName().toString().toLowerCase(Locale.ENGLISH).endsWith(".jar")) { |
| 52 | urls.add(path.toUri().toURL()); |
| 53 | } else { |
| 54 | System.err.println("No plugin(s) found at " + path.toFile().getCanonicalPath()); |
| 55 | } |
| 56 | } catch (IOException e) { |
| 57 | throw new UncheckedIOException(e); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | URLClassLoader cl = new URLClassLoader(urls.toArray(new URL[0])); |
| 62 | |
| 63 | ServiceLoader<Plugin> pluginLoader = ServiceLoader.load(Plugin.class, cl); |
| 64 | |
| 65 | for (Plugin p : pluginLoader) { |
| 66 | p.init(apiVersion); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private static final int apiVersion = 0; |
| 71 | } |
no test coverage detected