Java Code Examples for org.pf4j.PluginManager#getPlugin()

The following examples show how to use org.pf4j.PluginManager#getPlugin() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModuleClassTest.java    From buck with Apache License 2.0 5 votes vote down vote up
private static PluginWrapper getPluginWrapperForClass(PluginManager pluginManager, Class<?> cls)
    throws Exception {
  Field pluginDescriptorField = PluginClassLoader.class.getDeclaredField("pluginDescriptor");
  pluginDescriptorField.setAccessible(true);
  PluginDescriptor pluginDescriptor =
      (PluginDescriptor) pluginDescriptorField.get(cls.getClassLoader());

  return pluginManager.getPlugin(pluginDescriptor.getPluginId());
}
 
Example 2
Source File: ServiceArtifactBuilderSmokeIntegrationTest.java    From exonum-java-binding with Apache License 2.0 4 votes vote down vote up
@Test
@DisplayName("Created plugin must be successfully loaded and unloaded by the PluginManager. "
    + "If this test does not work, subsequent use of ServiceArtifactBuilder in other ITs makes "
    + "no sense.")
void createdArtifactCanBeLoaded(@TempDir Path tmp) throws IOException {
  Path pluginPath = tmp.resolve("test-plugin.jar");

  String pluginId = "test-plugin";
  String version = "1.0.1";
  Class<?> pluginClass = TestPlugin.class;
  new ServiceArtifactBuilder()
      .setPluginId(pluginId)
      .setPluginVersion(version)
      .setManifestEntry("Plugin-Class", pluginClass.getName())
      .addClass(pluginClass)
      .addExtensionClass(TestServiceExtensionImpl.class)
      .writeTo(pluginPath);

  PluginManager pluginManager = new DefaultPluginManager();

  // Try to load the plugin
  String loadedPluginId = pluginManager.loadPlugin(pluginPath);
  assertThat(loadedPluginId).isEqualTo(pluginId);

  // Check it has correct version
  PluginWrapper plugin = pluginManager.getPlugin(pluginId);
  assertThat(plugin.getDescriptor().getVersion()).isEqualTo(version);
  assertNamesEqual(plugin.getPlugin().getClass(), pluginClass);

  // Try to start
  PluginState pluginState = pluginManager.startPlugin(pluginId);
  assertThat(pluginState).isEqualTo(PluginState.STARTED);

  // Check the extensions
  List<Class<? extends TestServiceExtension>> extensionClasses = pluginManager
      .getExtensionClasses(TestServiceExtension.class, pluginId);
  assertThat(extensionClasses).hasSize(1);
  Class<?> extensionType = extensionClasses.get(0);
  assertNamesEqual(extensionType, TestServiceExtensionImpl.class);

  // Try to stop and unload
  pluginState = pluginManager.stopPlugin(pluginId);
  assertThat(pluginState).isEqualTo(PluginState.STOPPED);
  boolean unloadResult = pluginManager.unloadPlugin(pluginId);
  assertTrue(unloadResult);
}