Package org.pf4j

Class AbstractPluginManager

java.lang.Object
org.pf4j.AbstractPluginManager
All Implemented Interfaces:
PluginManager
Direct Known Subclasses:
DefaultPluginManager

public abstract class AbstractPluginManager extends Object implements PluginManager
This class implements the boilerplate plugin code that any PluginManager implementation would have to support. It helps cut the noise out of the subclass that handles plugin management.

This class is not thread-safe.

  • Field Details

    • log

      private static final org.slf4j.Logger log
    • PLUGINS_DIR_PROPERTY_NAME

      public static final String PLUGINS_DIR_PROPERTY_NAME
      See Also:
    • MODE_PROPERTY_NAME

      public static final String MODE_PROPERTY_NAME
      See Also:
    • DEFAULT_PLUGINS_DIR

      public static final String DEFAULT_PLUGINS_DIR
      See Also:
    • DEVELOPMENT_PLUGINS_DIR

      public static final String DEVELOPMENT_PLUGINS_DIR
      See Also:
    • pluginsRoot

      protected Path pluginsRoot
    • extensionFinder

      protected ExtensionFinder extensionFinder
    • pluginDescriptorFinder

      protected PluginDescriptorFinder pluginDescriptorFinder
    • plugins

      protected Map<String,PluginWrapper> plugins
      A map of plugins this manager is responsible for (the key is the 'pluginId').
    • pluginClassLoaders

      protected Map<String,ClassLoader> pluginClassLoaders
      A map of plugin class loaders (the key is the 'pluginId').
    • unresolvedPlugins

      protected List<PluginWrapper> unresolvedPlugins
      A list with unresolved plugins (unresolved dependency).
    • resolvedPlugins

      protected List<PluginWrapper> resolvedPlugins
      A list with all resolved plugins (resolved dependency).
    • startedPlugins

      protected List<PluginWrapper> startedPlugins
      A list with started plugins.
    • pluginStateListeners

      protected List<PluginStateListener> pluginStateListeners
      The registered PluginStateListeners.
    • runtimeMode

      protected RuntimeMode runtimeMode
      Cache value for the runtime mode. No need to re-read it because it wont change at runtime.
    • systemVersion

      protected String systemVersion
      The system version used for comparisons to the plugin requires attribute.
    • pluginRepository

      protected PluginRepository pluginRepository
    • pluginFactory

      protected PluginFactory pluginFactory
    • extensionFactory

      protected ExtensionFactory extensionFactory
    • pluginStatusProvider

      protected PluginStatusProvider pluginStatusProvider
    • dependencyResolver

      protected DependencyResolver dependencyResolver
    • pluginLoader

      protected PluginLoader pluginLoader
    • exactVersionAllowed

      protected boolean exactVersionAllowed
    • versionManager

      protected VersionManager versionManager
  • Constructor Details

    • AbstractPluginManager

      public AbstractPluginManager()
      The plugins root is supplied by System.getProperty("pf4j.pluginsDir", "plugins").
    • AbstractPluginManager

      public AbstractPluginManager(Path pluginsRoot)
      Constructs AbstractPluginManager with the given plugins root.
      Parameters:
      pluginsRoot - the root to search for plugins
  • Method Details

    • setSystemVersion

      public void setSystemVersion(String version)
      Description copied from interface: PluginManager
      Set the system version. This is used to compare against the plugin requires attribute. The default system version is 0.0.0 which disables all version checking.
      Specified by:
      setSystemVersion in interface PluginManager
      Parameters:
      version -
    • getSystemVersion

      public String getSystemVersion()
      Description copied from interface: PluginManager
      Returns the system version.
      Specified by:
      getSystemVersion in interface PluginManager
      Returns:
      the system version
    • getPlugins

      public List<PluginWrapper> getPlugins()
      Returns a copy of plugins.
      Specified by:
      getPlugins in interface PluginManager
    • getPlugins

      public List<PluginWrapper> getPlugins(PluginState pluginState)
      Returns a copy of plugins with that state.
      Specified by:
      getPlugins in interface PluginManager
    • getResolvedPlugins

      public List<PluginWrapper> getResolvedPlugins()
      Description copied from interface: PluginManager
      Retrieves all resolved plugins (with resolved dependency).
      Specified by:
      getResolvedPlugins in interface PluginManager
    • getUnresolvedPlugins

      public List<PluginWrapper> getUnresolvedPlugins()
      Description copied from interface: PluginManager
      Retrieves all unresolved plugins (with unresolved dependency).
      Specified by:
      getUnresolvedPlugins in interface PluginManager
    • getStartedPlugins

      public List<PluginWrapper> getStartedPlugins()
      Description copied from interface: PluginManager
      Retrieves all started plugins.
      Specified by:
      getStartedPlugins in interface PluginManager
    • getPlugin

      public PluginWrapper getPlugin(String pluginId)
      Description copied from interface: PluginManager
      Retrieves the plugin with this id, or null if the plugin does not exist.
      Specified by:
      getPlugin in interface PluginManager
      Parameters:
      pluginId - the unique plugin identifier, specified in its metadata
      Returns:
      A PluginWrapper object for this plugin, or null if it does not exist.
    • loadPlugin

      public String loadPlugin(Path pluginPath)
      Description copied from interface: PluginManager
      Load a plugin.
      Specified by:
      loadPlugin in interface PluginManager
      Parameters:
      pluginPath - the plugin location
      Returns:
      the pluginId of the installed plugin as specified in its metadata
    • loadPlugins

      public void loadPlugins()
      Load plugins.
      Specified by:
      loadPlugins in interface PluginManager
    • unloadPlugin

      public boolean unloadPlugin(String pluginId)
      Unload the specified plugin and it's dependents.
      Specified by:
      unloadPlugin in interface PluginManager
      Parameters:
      pluginId - the unique plugin identifier, specified in its metadata
      Returns:
      true if the plugin was unloaded
    • unloadPlugin

      private boolean unloadPlugin(String pluginId, boolean unloadDependents)
    • deletePlugin

      public boolean deletePlugin(String pluginId)
      Description copied from interface: PluginManager
      Deletes a plugin.
      Specified by:
      deletePlugin in interface PluginManager
      Parameters:
      pluginId - the unique plugin identifier, specified in its metadata
      Returns:
      true if the plugin was deleted
    • startPlugins

      public void startPlugins()
      Start all active plugins.
      Specified by:
      startPlugins in interface PluginManager
    • startPlugin

      public PluginState startPlugin(String pluginId)
      Start the specified plugin and its dependencies.
      Specified by:
      startPlugin in interface PluginManager
      Returns:
      the plugin state
    • stopPlugins

      public void stopPlugins()
      Stop all active plugins.
      Specified by:
      stopPlugins in interface PluginManager
    • stopPlugin

      public PluginState stopPlugin(String pluginId)
      Stop the specified plugin and it's dependents.
      Specified by:
      stopPlugin in interface PluginManager
      Returns:
      the plugin state
    • stopPlugin

      private PluginState stopPlugin(String pluginId, boolean stopDependents)
    • checkPluginId

      private void checkPluginId(String pluginId)
    • disablePlugin

      public boolean disablePlugin(String pluginId)
      Description copied from interface: PluginManager
      Disables a plugin from being loaded.
      Specified by:
      disablePlugin in interface PluginManager
      Parameters:
      pluginId - the unique plugin identifier, specified in its metadata
      Returns:
      true if plugin is disabled
    • enablePlugin

      public boolean enablePlugin(String pluginId)
      Description copied from interface: PluginManager
      Enables a plugin that has previously been disabled.
      Specified by:
      enablePlugin in interface PluginManager
      Parameters:
      pluginId - the unique plugin identifier, specified in its metadata
      Returns:
      true if plugin is enabled
    • getPluginClassLoader

      public ClassLoader getPluginClassLoader(String pluginId)
      Get the ClassLoader for plugin.
      Specified by:
      getPluginClassLoader in interface PluginManager
    • getExtensionClasses

      public List<Class<?>> getExtensionClasses(String pluginId)
      Specified by:
      getExtensionClasses in interface PluginManager
    • getExtensionClasses

      public <T> List<Class<? extends T>> getExtensionClasses(Class<T> type)
      Specified by:
      getExtensionClasses in interface PluginManager
    • getExtensionClasses

      public <T> List<Class<? extends T>> getExtensionClasses(Class<T> type, String pluginId)
      Specified by:
      getExtensionClasses in interface PluginManager
    • getExtensions

      public <T> List<T> getExtensions(Class<T> type)
      Specified by:
      getExtensions in interface PluginManager
    • getExtensions

      public <T> List<T> getExtensions(Class<T> type, String pluginId)
      Specified by:
      getExtensions in interface PluginManager
    • getExtensions

      public List getExtensions(String pluginId)
      Specified by:
      getExtensions in interface PluginManager
    • getExtensionClassNames

      public Set<String> getExtensionClassNames(String pluginId)
      Specified by:
      getExtensionClassNames in interface PluginManager
    • getExtensionFactory

      public ExtensionFactory getExtensionFactory()
      Specified by:
      getExtensionFactory in interface PluginManager
    • getPluginLoader

      public PluginLoader getPluginLoader()
    • getPluginsRoot

      public Path getPluginsRoot()
      Description copied from interface: PluginManager
      Gets the path of the folder where plugins are installed.
      Specified by:
      getPluginsRoot in interface PluginManager
      Returns:
      Path of plugins root
    • getRuntimeMode

      public RuntimeMode getRuntimeMode()
      Description copied from interface: PluginManager
      The runtime mode. Must currently be either DEVELOPMENT or DEPLOYMENT.
      Specified by:
      getRuntimeMode in interface PluginManager
    • whichPlugin

      public PluginWrapper whichPlugin(Class<?> clazz)
      Description copied from interface: PluginManager
      Retrieves the PluginWrapper that loaded the given class 'clazz'.
      Specified by:
      whichPlugin in interface PluginManager
    • addPluginStateListener

      public void addPluginStateListener(PluginStateListener listener)
      Specified by:
      addPluginStateListener in interface PluginManager
    • removePluginStateListener

      public void removePluginStateListener(PluginStateListener listener)
      Specified by:
      removePluginStateListener in interface PluginManager
    • getVersion

      public String getVersion()
    • createPluginRepository

      protected abstract PluginRepository createPluginRepository()
    • createPluginFactory

      protected abstract PluginFactory createPluginFactory()
    • createExtensionFactory

      protected abstract ExtensionFactory createExtensionFactory()
    • createPluginDescriptorFinder

      protected abstract PluginDescriptorFinder createPluginDescriptorFinder()
    • createExtensionFinder

      protected abstract ExtensionFinder createExtensionFinder()
    • createPluginStatusProvider

      protected abstract PluginStatusProvider createPluginStatusProvider()
    • createPluginLoader

      protected abstract PluginLoader createPluginLoader()
    • createVersionManager

      protected abstract VersionManager createVersionManager()
    • getPluginDescriptorFinder

      protected PluginDescriptorFinder getPluginDescriptorFinder()
    • getPluginFactory

      protected PluginFactory getPluginFactory()
    • getPluginClassLoaders

      protected Map<String,ClassLoader> getPluginClassLoaders()
    • initialize

      protected void initialize()
    • createPluginsRoot

      protected Path createPluginsRoot()
      Add the possibility to override the plugins root. If a PLUGINS_DIR_PROPERTY_NAME system property is defined than this method returns that root. If getRuntimeMode() returns RuntimeMode.DEVELOPMENT than DEVELOPMENT_PLUGINS_DIR is returned else this method returns DEFAULT_PLUGINS_DIR.
      Returns:
      the plugins root
    • isPluginValid

      protected boolean isPluginValid(PluginWrapper pluginWrapper)
      Check if this plugin is valid (satisfies "requires" param) for a given system version.
      Parameters:
      pluginWrapper - the plugin to check
      Returns:
      true if plugin satisfies the "requires" or if requires was left blank
    • isPluginDisabled

      protected boolean isPluginDisabled(String pluginId)
    • resolvePlugins

      protected void resolvePlugins()
    • firePluginStateEvent

      protected void firePluginStateEvent(PluginStateEvent event)
    • loadPluginFromPath

      protected PluginWrapper loadPluginFromPath(Path pluginPath)
    • idForPath

      protected String idForPath(Path pluginPath)
      Tests for already loaded plugins on given path.
      Parameters:
      pluginPath - the path to investigate
      Returns:
      id of plugin or null if not loaded
    • validatePluginDescriptor

      protected void validatePluginDescriptor(PluginDescriptor descriptor)
      Override this to change the validation criteria.
      Parameters:
      descriptor - the plugin descriptor to validate
      Throws:
      PluginRuntimeException - if validation fails
    • isExactVersionAllowed

      public boolean isExactVersionAllowed()
      Returns:
      true if exact versions in requires is allowed
    • setExactVersionAllowed

      public void setExactVersionAllowed(boolean exactVersionAllowed)
      Set to true to allow requires expression to be exactly x.y.z. The default is false, meaning that using an exact version x.y.z will implicitly mean the same as >=x.y.z
      Parameters:
      exactVersionAllowed - set to true or false
    • getVersionManager

      public VersionManager getVersionManager()
      Specified by:
      getVersionManager in interface PluginManager
    • getPluginLabel

      protected String getPluginLabel(PluginDescriptor pluginDescriptor)
      The plugin label is used in logging and it's a string in format pluginId@pluginVersion.
    • getExtensionClasses

      private <T> List<Class<? extends T>> getExtensionClasses(List<ExtensionWrapper<T>> extensionsWrapper)
    • getExtensions

      private <T> List<T> getExtensions(List<ExtensionWrapper<T>> extensionsWrapper)