PluginLoader { public PluginRegistry load() throws PluginException { List<Path> pluginFiles = getPluginsProvider().provide(getPluginsDirectory()); PluginRegistry pluginRegistry = new PluginRegistryContainer(); for (Path pluginFile : pluginFiles) { PluginMetadata pluginMetadata = getManifestReader().read(pluginFile); pluginRegistry.addPlugin(pluginMetadata); } for (String pluginName : pluginRegistry.getPluginNames()) { Optional<PluginMetadata> pluginMetadata = pluginRegistry.getPlugin(pluginName); if (pluginMetadata.isPresent()) { getDependencyChecker().check(pluginRegistry, pluginMetadata.get()); ScanResult scanResult = getClassesScanner().scan( pluginMetadata.get().getPath(), getExtensionPoints() ); for (Class extensionPoint : scanResult.getContents().getExtensionPoints()) { pluginRegistry.addImplementations( pluginMetadata.get(), extensionPoint, scanResult.getContents().getImplementations(extensionPoint) ); } pluginRegistry.addClassLoader(pluginMetadata.get(), scanResult.getClassLoader()); List<Path> resources = getResourcesScanner().scan(pluginMetadata.get().getPath()); pluginRegistry.addResources(pluginMetadata.get(), resources); } } return pluginRegistry; } private  PluginLoader(Path pluginsDirectory); static PluginLoader withPluginDirectory(Path pluginsDirectory); PluginLoader withFileGlob(String fileGlob); PluginLoader withPluginsProvider(PluginsProvider pluginsProvider); PluginLoader withCacheDirectory(Path cacheDirectory); PluginLoader withExtensionPoints(Class... extensionPoints); PluginLoader withManifestReader(ManifestReader manifestReader); PluginLoader withDependencyChecker(DependencyChecker dependencyChecker); PluginLoader withClassesScanner(ClassesScanner classesScanner); PluginLoader withResourcesScanner(ResourcesScanner resourcesScanner); PluginLoader withResourcesPatterns(String... resourcesPatterns); Path getPluginsDirectory(); String getFileGlob(); PluginsProvider getPluginsProvider(); Path getCacheDirectory(); ManifestReader getManifestReader(); DependencyChecker getDependencyChecker(); ClassesScanner getClassesScanner(); ResourcesScanner getResourcesScanner(); String[] getResourcesPatterns(); List<Class> getExtensionPoints(); PluginRegistry load(); static final String DEFAULT_CACHE_DIRECTORY; }
@Test public void testLoad() throws Exception { final String PLUGIN_NAME = "plugin-name"; final String PLUGIN_VERSION = "plugin-version"; Manifest manifest = createTestLoadManifest(PLUGIN_NAME, PLUGIN_VERSION); Path tempDirectory = temporaryDirectory.getDirectory(); assertNotNull(tempDirectory); assertTrue(Files.exists(tempDirectory)); JarHelper.createTestPluginFile( "some-plugin", tempDirectory, Optional.of(manifest) ); PluginRegistry pluginRegistry = PluginLoader .withPluginDirectory(tempDirectory) .withExtensionPoints(TestExtensionPoint.class, TestAnnotation.class) .withResourcesPatterns("glob:**/*.resource") .load(); assertThat(pluginRegistry.getPluginNames(), hasSize(1)); assertThat(pluginRegistry.getPluginNames(), contains(PLUGIN_NAME)); assertTrue(pluginRegistry.getPlugin(PLUGIN_NAME).isPresent()); assertThat(pluginRegistry.getPlugin(PLUGIN_NAME).get().getVersion(), equalTo(PLUGIN_VERSION)); assertThat(pluginRegistry.getExtensionPoints(), hasSize(2)); assertThat(pluginRegistry.getExtensionPoints(), containsInAnyOrder(TestExtensionPoint.class, TestAnnotation.class)); assertThat(pluginRegistry.getImplementations(TestAnnotation.class), hasSize(1)); assertThat(pluginRegistry.getImplementations(TestAnnotation.class), contains(AnnotatedImpl.class)); assertThat(pluginRegistry.getImplementations(TestExtensionPoint.class), hasSize(1)); assertThat(pluginRegistry.getImplementations(TestExtensionPoint.class), contains(TestExtensionPointImpl.class)); assertThat(pluginRegistry.getResources(), hasSize(1)); assertThat(pluginRegistry.getResources("missing-plugin"), hasSize(0)); assertThat(pluginRegistry.getResources(PLUGIN_NAME), hasSize(1)); Path resourcePath = pluginRegistry.getResources(PLUGIN_NAME).get(0); assertTrue(resourcePath.endsWith(JarHelper.TEST_RESOURCE_NAME)); assertTrue(pluginRegistry.getClassLoader(PLUGIN_NAME).isPresent()); assertFalse(pluginRegistry.getClassLoader("missing-plugin").isPresent()); try (InputStream inputStream = Files.newInputStream(resourcePath)) { assertNotNull(inputStream); } }