org.pf4j.PluginState Java Examples

The following examples show how to use org.pf4j.PluginState. 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: PluginManagerController.java    From sbp with Apache License 2.0 6 votes vote down vote up
@PostMapping(value = "${spring.sbp.controller.base-path:/sbp}/restart-all")
public int restartAll() {
    List<String> startedPluginIds = new ArrayList<>();
    List<PluginWrapper> loadedPlugins = pluginManager.getPlugins();
    loadedPlugins.forEach(plugin -> {
        if (plugin.getPluginState() == PluginState.STARTED) {
            startedPluginIds.add(plugin.getPluginId());
        }
        pluginManager.unloadPlugin(plugin.getPluginId());
    });
    pluginManager.loadPlugins();
    startedPluginIds.forEach(pluginId -> {
        // restart started plugin
        if (pluginManager.getPlugin(pluginId) != null) {
            pluginManager.startPlugin(pluginId);
        }
    });
    return 0;
}
 
Example #2
Source File: PluginInfo.java    From sbp with Apache License 2.0 6 votes vote down vote up
public static PluginInfo build(PluginDescriptor descriptor,
                               PluginState pluginState,
                               String newVersion,
                               boolean removed) {
    PluginInfo pluginInfo = new PluginInfo();
    pluginInfo.pluginId = descriptor.getPluginId();
    pluginInfo.pluginDescription = descriptor.getPluginDescription();
    pluginInfo.pluginClass = descriptor.getPluginClass();
    pluginInfo.version = descriptor.getVersion();
    pluginInfo.requires = descriptor.getRequires();
    pluginInfo.provider = descriptor.getProvider();
    pluginInfo.license = descriptor.getLicense();
    if (descriptor.getDependencies() != null) {
        pluginInfo.dependencies = new ArrayList<>(descriptor.getDependencies());
    }
    pluginInfo.pluginState = pluginState;
    pluginInfo.newVersion = newVersion;
    pluginInfo.removed = removed;
    return pluginInfo;
}
 
Example #3
Source File: UpdateManager.java    From pf4j-update with Apache License 2.0 6 votes vote down vote up
/**
 * Installs a plugin by id and version.
 *
 * @param id the id of plugin to install
 * @param version the version of plugin to install, on SemVer format, or null for latest
 * @return true if installation successful and plugin started
 * @exception PluginRuntimeException if plugin does not exist in repos or problems during
 */
public synchronized boolean installPlugin(String id, String version) {
    // Download to temporary location
    Path downloaded = downloadPlugin(id, version);

    Path pluginsRoot = pluginManager.getPluginsRoot();
    Path file = pluginsRoot.resolve(downloaded.getFileName());
    try {
        Files.move(downloaded, file, REPLACE_EXISTING);
    } catch (IOException e) {
        throw new PluginRuntimeException(e, "Failed to write file '{}' to plugins folder", file);
    }

    String pluginId = pluginManager.loadPlugin(file);
    PluginState state = pluginManager.startPlugin(pluginId);

    return PluginState.STARTED.equals(state);
}
 
Example #4
Source File: SpringBootPlugin.java    From sbp with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
    if (getWrapper().getPluginState() == PluginState.STARTED) return;

    applicationContext = springBootstrap.run();
    getMainRequestMapping().registerControllers(this);

    // register Extensions
    Set<String> extensionClassNames = getWrapper().getPluginManager()
            .getExtensionClassNames(getWrapper().getPluginId());
    for (String extensionClassName : extensionClassNames) {
        try {
            log.debug("Register extension <{}> to main ApplicationContext", extensionClassName);
            Class<?> extensionClass = getWrapper().getPluginClassLoader().loadClass(extensionClassName);
            SpringExtensionFactory extensionFactory = (SpringExtensionFactory) getWrapper()
                    .getPluginManager().getExtensionFactory();
            Object bean = extensionFactory.create(extensionClass);
            String beanName = extensionFactory.getExtensionBeanName(extensionClass);
            registerBeanToMainContext(beanName, bean);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }

    ApplicationContextProvider.registerApplicationContext(applicationContext);

    if (getPluginManager().isMainApplicationStarted()) {
        applicationContext.publishEvent(new SbpPluginRestartedEvent(applicationContext));
    }
}
 
Example #5
Source File: SpringBootPlugin.java    From sbp with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() {
    if (getWrapper().getPluginState() != PluginState.STARTED) return;

    log.debug("Stopping plugin {} ......", getWrapper().getPluginId());
    releaseResource();
    // register Extensions
    Set<String> extensionClassNames = getWrapper().getPluginManager()
            .getExtensionClassNames(getWrapper().getPluginId());
    for (String extensionClassName : extensionClassNames) {
        try {
            log.debug("Register extension <{}> to main ApplicationContext", extensionClassName);
            Class<?> extensionClass = getWrapper().getPluginClassLoader().loadClass(extensionClassName);
            SpringExtensionFactory extensionFactory = (SpringExtensionFactory) getWrapper()
                    .getPluginManager().getExtensionFactory();
            String beanName = extensionFactory.getExtensionBeanName(extensionClass);
            unregisterBeanFromMainContext(beanName);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }

    getMainRequestMapping().unregisterControllers(this);
    applicationContext.publishEvent(new SbpPluginStoppedEvent(applicationContext));
    ApplicationContextProvider.unregisterApplicationContext(applicationContext);
    ((ConfigurableApplicationContext) applicationContext).close();

    log.debug("Plugin {} is stopped", getWrapper().getPluginId());
}
 
Example #6
Source File: MainAppStartedListener.java    From sbp with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
    pluginManager.getPlugins(PluginState.STARTED).forEach(pluginWrapper -> {
        SpringBootPlugin springBootPlugin = (SpringBootPlugin) pluginWrapper.getPlugin();
        ApplicationContext pluginAppCtx = springBootPlugin.getApplicationContext();
        pluginAppCtx.publishEvent(new SbpMainAppStartedEvent(applicationContext));
    });
    pluginManager.setMainApplicationStarted(true);
}
 
Example #7
Source File: MainAppReadyListener.java    From sbp with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
    pluginManager.getPlugins(PluginState.STARTED).forEach(pluginWrapper -> {
        SpringBootPlugin springBootPlugin = (SpringBootPlugin) pluginWrapper.getPlugin();
        ApplicationContext pluginAppCtx = springBootPlugin.getApplicationContext();
        pluginAppCtx.publishEvent(new SbpMainAppReadyEvent(applicationContext));
    });
}
 
Example #8
Source File: Pf4jServiceLoader.java    From exonum-java-binding with Apache License 2.0 5 votes vote down vote up
private void startPlugin(String pluginId) throws ServiceLoadingException {
  try {
    PluginState pluginState = pluginManager.startPlugin(pluginId);
    checkState(pluginState == PluginState.STARTED,
        "Failed to start the plugin %s, its state=%s", pluginId, pluginState);
  } catch (Exception e) {
    // Catch any exception, as it may originate either from PluginManager code or
    // from Plugin#start (= service code).
    throw new ServiceLoadingException("Failed to start the plugin " + pluginId, e);
  }
}
 
Example #9
Source File: JarPluginManagerSmokeIntegrationTest.java    From exonum-java-binding with Apache License 2.0 5 votes vote down vote up
@Test
void loadsUnloadsJarPlugins(@TempDir Path tmp) throws IOException {
  Path pluginPath = tmp.resolve("test-plugin.jar");

  String pluginId = "test-plugin";
  String version = "1.0.1";
  new ServiceArtifactBuilder()
      .setPluginId(pluginId)
      .setPluginVersion(version)
      .addExtensionClass(TestServiceModule1.class)
      .writeTo(pluginPath);

  PluginManager pluginManager = new JarPluginManager();

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

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

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

  // Try to stop and unload
  pluginState = pluginManager.stopPlugin(pluginId);
  assertThat(pluginState).isEqualTo(PluginState.STOPPED);
  boolean unloadResult = pluginManager.unloadPlugin(pluginId);
  assertTrue(unloadResult);
}
 
Example #10
Source File: UpdateManager.java    From pf4j-update with Apache License 2.0 5 votes vote down vote up
/**
 * Updates a plugin id to given version or to latest version if {@code version == null}.
 *
 * @param id the id of plugin to update
 * @param version the version to update to, on SemVer format, or null for latest
 * @return true if update successful
 * @exception PluginRuntimeException in case the given version is not available, plugin id not already installed etc
*/
public boolean updatePlugin(String id, String version) {
    if (pluginManager.getPlugin(id) == null) {
        throw new PluginRuntimeException("Plugin {} cannot be updated since it is not installed", id);
    }

    PluginInfo pluginInfo = getPluginsMap().get(id);
    if (pluginInfo == null) {
        throw new PluginRuntimeException("Plugin {} does not exist in any repository", id);
    }

    if (!hasPluginUpdate(id)) {
        log.warn("Plugin {} does not have an update available which is compatible with system version {}", id, systemVersion);
        return false;
    }

    // Download to temp folder
    Path downloaded = downloadPlugin(id, version);

    if (!pluginManager.deletePlugin(id)) {
        return false;
    }

    Path pluginsRoot = pluginManager.getPluginsRoot();
    Path file = pluginsRoot.resolve(downloaded.getFileName());
    try {
        Files.move(downloaded, file, REPLACE_EXISTING);
    } catch (IOException e) {
        throw new PluginRuntimeException("Failed to write plugin file {} to plugin folder", file);
    }

    String newPluginId = pluginManager.loadPlugin(file);
    PluginState state = pluginManager.startPlugin(newPluginId);

    return PluginState.STARTED.equals(state);
}
 
Example #11
Source File: PluginInfo.java    From springboot-plugin-framework-parent with Apache License 2.0 4 votes vote down vote up
public PluginInfo(PluginDescriptor pluginDescriptor, PluginState pluginState, String path) {
    this.pluginDescriptor = pluginDescriptor;
    this.pluginState = pluginState;
    this.path = path;
}
 
Example #12
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);
}