Package org.pf4j
Class AbstractPluginManager
- java.lang.Object
-
- org.pf4j.AbstractPluginManager
-
- All Implemented Interfaces:
PluginManager
- Direct Known Subclasses:
DefaultPluginManager
public abstract class AbstractPluginManager extends java.lang.Object implements PluginManager
This class implements the boilerplate plugin code that anyPluginManagerimplementation would have to support. It helps cut the noise out of the subclass that handles plugin management.This class is not thread-safe.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_PLUGINS_DIRprotected DependencyResolverdependencyResolverstatic java.lang.StringDEVELOPMENT_PLUGINS_DIRprotected booleanexactVersionAllowedprotected ExtensionFactoryextensionFactoryprotected ExtensionFinderextensionFinderprivate static org.slf4j.Loggerlogstatic java.lang.StringMODE_PROPERTY_NAMEprotected java.util.Map<java.lang.String,java.lang.ClassLoader>pluginClassLoadersA map of plugin class loaders (the key is the 'pluginId').protected PluginDescriptorFinderpluginDescriptorFinderprotected PluginFactorypluginFactoryprotected PluginLoaderpluginLoaderprotected PluginRepositorypluginRepositoryprotected java.util.Map<java.lang.String,PluginWrapper>pluginsA map of plugins this manager is responsible for (the key is the 'pluginId').static java.lang.StringPLUGINS_DIR_PROPERTY_NAMEprotected java.nio.file.PathpluginsRootprotected java.util.List<PluginStateListener>pluginStateListenersThe registeredPluginStateListeners.protected PluginStatusProviderpluginStatusProviderprotected java.util.List<PluginWrapper>resolvedPluginsA list with all resolved plugins (resolved dependency).protected RuntimeModeruntimeModeCache value for the runtime mode.protected java.util.List<PluginWrapper>startedPluginsA list with started plugins.protected java.lang.StringsystemVersionThe system version used for comparisons to the plugin requires attribute.protected java.util.List<PluginWrapper>unresolvedPluginsA list with unresolved plugins (unresolved dependency).protected VersionManagerversionManager
-
Constructor Summary
Constructors Constructor Description AbstractPluginManager()The plugins root is supplied bySystem.getProperty("pf4j.pluginsDir", "plugins").AbstractPluginManager(java.nio.file.Path pluginsRoot)ConstructsAbstractPluginManagerwith the given plugins root.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddPluginStateListener(PluginStateListener listener)private voidcheckPluginId(java.lang.String pluginId)protected abstract ExtensionFactorycreateExtensionFactory()protected abstract ExtensionFindercreateExtensionFinder()protected abstract PluginDescriptorFindercreatePluginDescriptorFinder()protected abstract PluginFactorycreatePluginFactory()protected abstract PluginLoadercreatePluginLoader()protected abstract PluginRepositorycreatePluginRepository()protected java.nio.file.PathcreatePluginsRoot()Add the possibility to override the plugins root.protected abstract PluginStatusProvidercreatePluginStatusProvider()protected abstract VersionManagercreateVersionManager()booleandeletePlugin(java.lang.String pluginId)Deletes a plugin.booleandisablePlugin(java.lang.String pluginId)Disables a plugin from being loaded.booleanenablePlugin(java.lang.String pluginId)Enables a plugin that has previously been disabled.protected voidfirePluginStateEvent(PluginStateEvent event)<T> java.util.List<java.lang.Class<? extends T>>getExtensionClasses(java.lang.Class<T> type)<T> java.util.List<java.lang.Class<? extends T>>getExtensionClasses(java.lang.Class<T> type, java.lang.String pluginId)java.util.List<java.lang.Class<?>>getExtensionClasses(java.lang.String pluginId)private <T> java.util.List<java.lang.Class<? extends T>>getExtensionClasses(java.util.List<ExtensionWrapper<T>> extensionsWrapper)java.util.Set<java.lang.String>getExtensionClassNames(java.lang.String pluginId)ExtensionFactorygetExtensionFactory()<T> java.util.List<T>getExtensions(java.lang.Class<T> type)<T> java.util.List<T>getExtensions(java.lang.Class<T> type, java.lang.String pluginId)java.util.ListgetExtensions(java.lang.String pluginId)private <T> java.util.List<T>getExtensions(java.util.List<ExtensionWrapper<T>> extensionsWrapper)PluginWrappergetPlugin(java.lang.String pluginId)Retrieves the plugin with this id, or null if the plugin does not exist.java.lang.ClassLoadergetPluginClassLoader(java.lang.String pluginId)Get theClassLoaderfor plugin.protected java.util.Map<java.lang.String,java.lang.ClassLoader>getPluginClassLoaders()protected PluginDescriptorFindergetPluginDescriptorFinder()protected PluginFactorygetPluginFactory()protected java.lang.StringgetPluginLabel(PluginDescriptor pluginDescriptor)The plugin label is used in logging and it's a string in formatpluginId@pluginVersion.PluginLoadergetPluginLoader()java.util.List<PluginWrapper>getPlugins()Returns a copy of plugins.java.util.List<PluginWrapper>getPlugins(PluginState pluginState)Returns a copy of plugins with that state.java.nio.file.PathgetPluginsRoot()Gets the path of the folder where plugins are installed.java.util.List<PluginWrapper>getResolvedPlugins()Retrieves all resolved plugins (with resolved dependency).RuntimeModegetRuntimeMode()The runtime mode.java.util.List<PluginWrapper>getStartedPlugins()Retrieves all started plugins.java.lang.StringgetSystemVersion()Returns the system version.java.util.List<PluginWrapper>getUnresolvedPlugins()Retrieves all unresolved plugins (with unresolved dependency).java.lang.StringgetVersion()VersionManagergetVersionManager()protected java.lang.StringidForPath(java.nio.file.Path pluginPath)Tests for already loaded plugins on given path.protected voidinitialize()booleanisExactVersionAllowed()protected booleanisPluginDisabled(java.lang.String pluginId)protected booleanisPluginValid(PluginWrapper pluginWrapper)Check if this plugin is valid (satisfies "requires" param) for a given system version.java.lang.StringloadPlugin(java.nio.file.Path pluginPath)Load a plugin.protected PluginWrapperloadPluginFromPath(java.nio.file.Path pluginPath)voidloadPlugins()Load plugins.voidremovePluginStateListener(PluginStateListener listener)protected voidresolvePlugins()voidsetExactVersionAllowed(boolean exactVersionAllowed)Set to true to allow requires expression to be exactly x.y.z.voidsetSystemVersion(java.lang.String version)Set the system version.PluginStatestartPlugin(java.lang.String pluginId)Start the specified plugin and its dependencies.voidstartPlugins()Start all active plugins.PluginStatestopPlugin(java.lang.String pluginId)Stop the specified plugin and it's dependents.private PluginStatestopPlugin(java.lang.String pluginId, boolean stopDependents)voidstopPlugins()Stop all active plugins.booleanunloadPlugin(java.lang.String pluginId)Unload the specified plugin and it's dependents.private booleanunloadPlugin(java.lang.String pluginId, boolean unloadDependents)protected voidvalidatePluginDescriptor(PluginDescriptor descriptor)Override this to change the validation criteria.PluginWrapperwhichPlugin(java.lang.Class<?> clazz)Retrieves thePluginWrapperthat loaded the given class 'clazz'.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.pf4j.PluginManager
isDevelopment, isNotDevelopment
-
-
-
-
Field Detail
-
log
private static final org.slf4j.Logger log
-
PLUGINS_DIR_PROPERTY_NAME
public static final java.lang.String PLUGINS_DIR_PROPERTY_NAME
- See Also:
- Constant Field Values
-
MODE_PROPERTY_NAME
public static final java.lang.String MODE_PROPERTY_NAME
- See Also:
- Constant Field Values
-
DEFAULT_PLUGINS_DIR
public static final java.lang.String DEFAULT_PLUGINS_DIR
- See Also:
- Constant Field Values
-
DEVELOPMENT_PLUGINS_DIR
public static final java.lang.String DEVELOPMENT_PLUGINS_DIR
- See Also:
- Constant Field Values
-
pluginsRoot
protected java.nio.file.Path pluginsRoot
-
extensionFinder
protected ExtensionFinder extensionFinder
-
pluginDescriptorFinder
protected PluginDescriptorFinder pluginDescriptorFinder
-
plugins
protected java.util.Map<java.lang.String,PluginWrapper> plugins
A map of plugins this manager is responsible for (the key is the 'pluginId').
-
pluginClassLoaders
protected java.util.Map<java.lang.String,java.lang.ClassLoader> pluginClassLoaders
A map of plugin class loaders (the key is the 'pluginId').
-
unresolvedPlugins
protected java.util.List<PluginWrapper> unresolvedPlugins
A list with unresolved plugins (unresolved dependency).
-
resolvedPlugins
protected java.util.List<PluginWrapper> resolvedPlugins
A list with all resolved plugins (resolved dependency).
-
startedPlugins
protected java.util.List<PluginWrapper> startedPlugins
A list with started plugins.
-
pluginStateListeners
protected java.util.List<PluginStateListener> pluginStateListeners
The registeredPluginStateListeners.
-
runtimeMode
protected RuntimeMode runtimeMode
Cache value for the runtime mode. No need to re-read it because it wont change at runtime.
-
systemVersion
protected java.lang.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 Detail
-
AbstractPluginManager
public AbstractPluginManager()
The plugins root is supplied bySystem.getProperty("pf4j.pluginsDir", "plugins").
-
AbstractPluginManager
public AbstractPluginManager(java.nio.file.Path pluginsRoot)
ConstructsAbstractPluginManagerwith the given plugins root.- Parameters:
pluginsRoot- the root to search for plugins
-
-
Method Detail
-
setSystemVersion
public void setSystemVersion(java.lang.String version)
Description copied from interface:PluginManagerSet 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:
setSystemVersionin interfacePluginManager
-
getSystemVersion
public java.lang.String getSystemVersion()
Description copied from interface:PluginManagerReturns the system version.- Specified by:
getSystemVersionin interfacePluginManager- Returns:
- the system version
-
getPlugins
public java.util.List<PluginWrapper> getPlugins()
Returns a copy of plugins.- Specified by:
getPluginsin interfacePluginManager
-
getPlugins
public java.util.List<PluginWrapper> getPlugins(PluginState pluginState)
Returns a copy of plugins with that state.- Specified by:
getPluginsin interfacePluginManager
-
getResolvedPlugins
public java.util.List<PluginWrapper> getResolvedPlugins()
Description copied from interface:PluginManagerRetrieves all resolved plugins (with resolved dependency).- Specified by:
getResolvedPluginsin interfacePluginManager
-
getUnresolvedPlugins
public java.util.List<PluginWrapper> getUnresolvedPlugins()
Description copied from interface:PluginManagerRetrieves all unresolved plugins (with unresolved dependency).- Specified by:
getUnresolvedPluginsin interfacePluginManager
-
getStartedPlugins
public java.util.List<PluginWrapper> getStartedPlugins()
Description copied from interface:PluginManagerRetrieves all started plugins.- Specified by:
getStartedPluginsin interfacePluginManager
-
getPlugin
public PluginWrapper getPlugin(java.lang.String pluginId)
Description copied from interface:PluginManagerRetrieves the plugin with this id, or null if the plugin does not exist.- Specified by:
getPluginin interfacePluginManager- 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 java.lang.String loadPlugin(java.nio.file.Path pluginPath)
Description copied from interface:PluginManagerLoad a plugin.- Specified by:
loadPluginin interfacePluginManager- 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:
loadPluginsin interfacePluginManager
-
unloadPlugin
public boolean unloadPlugin(java.lang.String pluginId)
Unload the specified plugin and it's dependents.- Specified by:
unloadPluginin interfacePluginManager- Parameters:
pluginId- the unique plugin identifier, specified in its metadata- Returns:
- true if the plugin was unloaded
-
unloadPlugin
private boolean unloadPlugin(java.lang.String pluginId, boolean unloadDependents)
-
deletePlugin
public boolean deletePlugin(java.lang.String pluginId)
Description copied from interface:PluginManagerDeletes a plugin.- Specified by:
deletePluginin interfacePluginManager- 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:
startPluginsin interfacePluginManager
-
startPlugin
public PluginState startPlugin(java.lang.String pluginId)
Start the specified plugin and its dependencies.- Specified by:
startPluginin interfacePluginManager- Returns:
- the plugin state
-
stopPlugins
public void stopPlugins()
Stop all active plugins.- Specified by:
stopPluginsin interfacePluginManager
-
stopPlugin
public PluginState stopPlugin(java.lang.String pluginId)
Stop the specified plugin and it's dependents.- Specified by:
stopPluginin interfacePluginManager- Returns:
- the plugin state
-
stopPlugin
private PluginState stopPlugin(java.lang.String pluginId, boolean stopDependents)
-
checkPluginId
private void checkPluginId(java.lang.String pluginId)
-
disablePlugin
public boolean disablePlugin(java.lang.String pluginId)
Description copied from interface:PluginManagerDisables a plugin from being loaded.- Specified by:
disablePluginin interfacePluginManager- Parameters:
pluginId- the unique plugin identifier, specified in its metadata- Returns:
- true if plugin is disabled
-
enablePlugin
public boolean enablePlugin(java.lang.String pluginId)
Description copied from interface:PluginManagerEnables a plugin that has previously been disabled.- Specified by:
enablePluginin interfacePluginManager- Parameters:
pluginId- the unique plugin identifier, specified in its metadata- Returns:
- true if plugin is enabled
-
getPluginClassLoader
public java.lang.ClassLoader getPluginClassLoader(java.lang.String pluginId)
Get theClassLoaderfor plugin.- Specified by:
getPluginClassLoaderin interfacePluginManager
-
getExtensionClasses
public java.util.List<java.lang.Class<?>> getExtensionClasses(java.lang.String pluginId)
- Specified by:
getExtensionClassesin interfacePluginManager
-
getExtensionClasses
public <T> java.util.List<java.lang.Class<? extends T>> getExtensionClasses(java.lang.Class<T> type)
- Specified by:
getExtensionClassesin interfacePluginManager
-
getExtensionClasses
public <T> java.util.List<java.lang.Class<? extends T>> getExtensionClasses(java.lang.Class<T> type, java.lang.String pluginId)- Specified by:
getExtensionClassesin interfacePluginManager
-
getExtensions
public <T> java.util.List<T> getExtensions(java.lang.Class<T> type)
- Specified by:
getExtensionsin interfacePluginManager
-
getExtensions
public <T> java.util.List<T> getExtensions(java.lang.Class<T> type, java.lang.String pluginId)- Specified by:
getExtensionsin interfacePluginManager
-
getExtensions
public java.util.List getExtensions(java.lang.String pluginId)
- Specified by:
getExtensionsin interfacePluginManager
-
getExtensionClassNames
public java.util.Set<java.lang.String> getExtensionClassNames(java.lang.String pluginId)
- Specified by:
getExtensionClassNamesin interfacePluginManager
-
getExtensionFactory
public ExtensionFactory getExtensionFactory()
- Specified by:
getExtensionFactoryin interfacePluginManager
-
getPluginLoader
public PluginLoader getPluginLoader()
-
getPluginsRoot
public java.nio.file.Path getPluginsRoot()
Description copied from interface:PluginManagerGets the path of the folder where plugins are installed.- Specified by:
getPluginsRootin interfacePluginManager- Returns:
- Path of plugins root
-
getRuntimeMode
public RuntimeMode getRuntimeMode()
Description copied from interface:PluginManagerThe runtime mode. Must currently be either DEVELOPMENT or DEPLOYMENT.- Specified by:
getRuntimeModein interfacePluginManager
-
whichPlugin
public PluginWrapper whichPlugin(java.lang.Class<?> clazz)
Description copied from interface:PluginManagerRetrieves thePluginWrapperthat loaded the given class 'clazz'.- Specified by:
whichPluginin interfacePluginManager
-
addPluginStateListener
public void addPluginStateListener(PluginStateListener listener)
- Specified by:
addPluginStateListenerin interfacePluginManager
-
removePluginStateListener
public void removePluginStateListener(PluginStateListener listener)
- Specified by:
removePluginStateListenerin interfacePluginManager
-
getVersion
public java.lang.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 java.util.Map<java.lang.String,java.lang.ClassLoader> getPluginClassLoaders()
-
initialize
protected void initialize()
-
createPluginsRoot
protected java.nio.file.Path createPluginsRoot()
Add the possibility to override the plugins root. If aPLUGINS_DIR_PROPERTY_NAMEsystem property is defined than this method returns that root. IfgetRuntimeMode()returnsRuntimeMode.DEVELOPMENTthanDEVELOPMENT_PLUGINS_DIRis returned else this method returnsDEFAULT_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(java.lang.String pluginId)
-
resolvePlugins
protected void resolvePlugins()
-
firePluginStateEvent
protected void firePluginStateEvent(PluginStateEvent event)
-
loadPluginFromPath
protected PluginWrapper loadPluginFromPath(java.nio.file.Path pluginPath)
-
idForPath
protected java.lang.String idForPath(java.nio.file.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:
getVersionManagerin interfacePluginManager
-
getPluginLabel
protected java.lang.String getPluginLabel(PluginDescriptor pluginDescriptor)
The plugin label is used in logging and it's a string in formatpluginId@pluginVersion.
-
getExtensionClasses
private <T> java.util.List<java.lang.Class<? extends T>> getExtensionClasses(java.util.List<ExtensionWrapper<T>> extensionsWrapper)
-
getExtensions
private <T> java.util.List<T> getExtensions(java.util.List<ExtensionWrapper<T>> extensionsWrapper)
-
-