org.pf4j.DefaultPluginManager Java Examples

The following examples show how to use org.pf4j.DefaultPluginManager. 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: DefaultToolchainProviderTest.java    From buck with Apache License 2.0 6 votes vote down vote up
private <T extends ThrowingToolchainFactory> DefaultToolchainProvider createProvider(
    Class<T> factoryClass) {
  ToolchainSupplier supplier =
      () ->
          Collections.singleton(
              ToolchainDescriptor.of(
                  NoopToolchain.DEFAULT_NAME, NoopToolchain.class, factoryClass));

  return new DefaultToolchainProvider(
      new DefaultPluginManager() {
        @Override
        public <T> List<T> getExtensions(Class<T> type) {
          if (ToolchainSupplier.class.equals(type)) {
            return (List<T>) Collections.singletonList(supplier);
          }
          return super.getExtensions(type);
        }
      },
      ImmutableMap.of(),
      FakeBuckConfig.builder().build(),
      new FakeProjectFilesystem(),
      new FakeProcessExecutor(),
      new ExecutableFinder(),
      TestRuleKeyConfigurationFactory.create());
}
 
Example #2
Source File: TestApplication.java    From pf4j-update with Apache License 2.0 5 votes vote down vote up
public TestApplication() {
    Path pluginsPath;
    try {
        pluginsPath = Files.createTempDirectory("plugins");
    } catch (IOException e) {
        throw new RuntimeException("Failed creating temp plugins directory", e);
    }

    pluginManager = new DefaultPluginManager(pluginsPath);
    updateManager = new UpdateManager(pluginManager);
}
 
Example #3
Source File: Container.java    From pf4j-spring-tutorial with MIT License 4 votes vote down vote up
public static void main(String[] args){
    final PluginManager pm = new DefaultPluginManager();

    pm.loadPlugins();

    pm.startPlugins();

    final List<PluginInterface> plugins = pm.getExtensions(PluginInterface.class);

    log.info(MessageFormat.format(" {0} Plugin(s) found ", String.valueOf(plugins.size())));

    plugins.forEach(g ->
            log.info(MessageFormat.format(" {0}: {1}",
                    g.getClass().getCanonicalName(),
                    g.identify())));

    pm.stopPlugins();

}
 
Example #4
Source File: AuthcPluginTest.java    From spring-boot-starter-samples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	
	System.setProperty("pf4j.mode", RuntimeMode.DEPLOYMENT.toString());
	System.setProperty("pf4j.pluginsDir", "plugins");
	
	if(RuntimeMode.DEPLOYMENT.compareTo(RuntimeMode.DEPLOYMENT) == 0) {
		//System.setProperty("pf4j.pluginsDir", System.getProperty("app.home","e:/root") + "/plugins");
	}
	
	/**
	 * 创建PluginManager对象,此处根据生产环境选择合适的实现,或者自定义实现
	 */
	PluginManager pluginManager = new DefaultPluginManager();
	// PluginManager pluginManager = new Pf4jPluginManager();
	// PluginManager pluginManager = new SpringPluginManager();
	// PluginManager pluginManager = new Pf4jJarPluginManager();
	// PluginManager pluginManager = new Pf4jJarPluginWhitSpringManager();

	/**
	 * 加载插件到JVM
	 */
    pluginManager.loadPlugins();

    /**
     * 调用Plugin实现类的start()方法: 
     */
    pluginManager.startPlugins();
    
    List<PluginWrapper> list = pluginManager.getPlugins();
    for (PluginWrapper pluginWrapper : list) {
		System.out.println(pluginWrapper.getPluginId());
	}
    
    
    List<AuthcExtensionPoint> extensions = pluginManager.getExtensions(AuthcExtensionPoint.class);
    
    for (AuthcExtensionPoint point : extensions) {
    	
    	ExtensionMapping m = point.getClass().getAnnotation(ExtensionMapping.class);
    	System.out.println(m.title());
    	
	}
    
    /**
     * 调用Plugin实现类的stop()方法
     */
    pluginManager.stopPlugins();
    
    System.out.println("=============");
	
}
 
Example #5
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);
}
 
Example #6
Source File: Pf4jServiceLoaderWithDefaultIntegrationTest.java    From exonum-java-binding with Apache License 2.0 4 votes vote down vote up
@Override
PluginManager createPluginManager() {
  return new DefaultPluginManager();
}