Class PluginManager


  • public class PluginManager
    extends Object
    Manages plugins. The plugins directory is monitored for any new plugins, and they are dynamically loaded. An instance of this class can be obtained using: XMPPServer.getInstance().getPluginManager() These states are defined for plugin management:
    • installed - the plugin archive file is present in the plugins directory.
    • extracted - the plugin archive file has been extracted.
    • loaded - the plugin has (successfully) been initialized.
    Note that an installed plugin is not per definition an extracted plugin, and an extracted plugin is not per definition a loaded plugin. A plugin that's extracted might, for instance, fail to load, due to restrictions imposed by its minServerVersion definition.
    Author:
    Matt Tucker
    See Also:
    Plugin, XMPPServer.getPluginManager()
    • Constructor Detail

      • PluginManager

        public PluginManager​(Path pluginDir)
        Constructs a new plugin manager.
        Parameters:
        pluginDir - the directory containing all Openfire plugins, typically OPENFIRE_HOME/plugins/
    • Method Detail

      • start

        public void start()
        Starts plugins and the plugin monitoring service.
      • shutdown

        public void shutdown()
        Shuts down all running plugins.
      • getPluginsDirectory

        public Path getPluginsDirectory()
        Returns the directory that contains all plugins. This typically is OPENFIRE_HOME/plugins.
        Returns:
        The directory that contains all plugins.
      • installPlugin

        public boolean installPlugin​(InputStream in,
                                     String pluginFilename)
        Installs or updates an existing plugin.
        Parameters:
        in - the input stream that contains the new plugin definition.
        pluginFilename - the filename of the plugin to create or update.
        Returns:
        true if the plugin was successfully installed or updated.
      • isInstalled

        public boolean isInstalled​(String canonicalName)
        Returns true if the plugin by the specified name is installed. Specifically, this checks if the plugin archive file is present in the plugins directory. Note that an installed plugin is not per definition an extracted plugin, and an extracted plugin is not per definition a loaded plugin. A plugin that's extracted might, for instance, fail to load, due to restrictions imposed by its minServerVersion definition.
        Parameters:
        canonicalName - the canonical filename of the plugin (cannot be null).
        Returns:
        true if the plugin is installed, otherwise false.
      • isExtracted

        public boolean isExtracted​(String canonicalName)
        Returns true if the plugin by the specified name is extracted. Specifically, this checks if the plugins directory contains a subdirectory that matches the canonical name of the plugin. Note that an installed plugin is not per definition an extracted plugin, and an extracted plugin is not per definition a loaded plugin. A plugin that's extracted might, for instance, fail to load, due to restrictions imposed by its minServerVersion definition.
        Parameters:
        canonicalName - the canonical filename of the plugin (cannot be null).
        Returns:
        true if the plugin is extracted, otherwise false.
      • isLoaded

        public boolean isLoaded​(String canonicalName)
        Returns true if the plugin by the specified name is loaded. Specifically, this checks if an instance was created for the plugin class file. Note that an installed plugin is not per definition an extracted plugin, and an extracted plugin is not per definition a loaded plugin. A plugin that's extracted might, for instance, fail to load, due to restrictions imposed by its minServerVersion definition.
        Parameters:
        canonicalName - the canonical filename of the plugin (cannot be null).
        Returns:
        true if the plugin is extracted, otherwise false.
      • getMetadataExtractedPlugins

        public Map<String,​PluginMetadata> getMetadataExtractedPlugins()
        Returns metadata for all extracted plugins, mapped by their canonical name. The collection is alphabetically sorted, by plugin name. Note that an installed plugin is not per definition an extracted plugin, and an extracted plugin is not per definition a loaded plugin. A plugin that's extracted might, for instance, fail to load, due to restrictions imposed by its minServerVersion definition.
        Returns:
        A collection of metadata (possibly empty, never null).
      • getMetadata

        public PluginMetadata getMetadata​(String canonicalName)
        Returns metadata for an extracted plugin, or null when the plugin is extracted nor loaded. Note that an installed plugin is not per definition an extracted plugin, and an extracted plugin is not per definition a loaded plugin. A plugin that's extracted might, for instance, fail to load, due to restrictions imposed by its minServerVersion definition.
        Parameters:
        canonicalName - the canonical name (lower case JAR/WAR file without exception) of the plugin
        Returns:
        A collection of metadata (possibly empty, never null).
      • getPlugins

        public Collection<Plugin> getPlugins()
        Returns a Collection of all loaded plugins. The returned collection will not include plugins that have been downloaded, but not loaded.
        Returns:
        a Collection of all loaded plugins.
      • getCanonicalName

        public String getCanonicalName​(Plugin plugin)
        Returns the canonical name for a loaded plugin.
        Parameters:
        plugin - A plugin (cannot be null).
        Returns:
        The canonical name for the plugin (never null).
      • getPlugin

        @Deprecated(since="4.4",
                    forRemoval=true)
        public Plugin getPlugin​(String canonicalName)
        Deprecated, for removal: This API element is subject to removal in a future version.
        in Openfire 4.4 in favour of getPluginByName(String)
        Returns a loaded plugin by its canonical name or null if a plugin with that name does not exist. The canonical name is the lowercase-name of the plugin archive, without the file extension. For example: "broadcast".
        Parameters:
        canonicalName - the name of the plugin.
        Returns:
        the plugin.
      • getPluginByName

        public Optional<Plugin> getPluginByName​(String pluginName)
        Returns a loaded plugin by the name contained in the plugin.xml <name/> tag, ignoring case. For example: "broadcast".
        Parameters:
        pluginName - the name of the plugin.
        Returns:
        the plugin, if found
        Since:
        Openfire 4.4
      • getPluginPath

        public Path getPluginPath​(Plugin plugin)
        Returns the plugin's directory.
        Parameters:
        plugin - the plugin.
        Returns:
        the plugin's directory.
        Since:
        Openfire 4.1
      • isExecuted

        public boolean isExecuted()
        Returns true if at least one attempt to load plugins has been done. A true value does not mean that available plugins have been loaded nor that plugins to be added in the future are already loaded. :)

        Returns:
        true if at least one attempt to load plugins has been done.
      • deletePlugin

        public void deletePlugin​(String pluginName)
        Delete a plugin, which removes the plugin.jar/war file after which the plugin is unloaded.
        Parameters:
        pluginName - the plugin to delete
      • reloadPlugin

        public boolean reloadPlugin​(String pluginName)
      • getLoadWarning

        public String getLoadWarning​(String canonicalPluginName)
        Returns a human-readable, localized message related to a failure while trying to load a plugin. When the last time that this plugin was loaded was successful (or when a plugin of this name was never attempted to be loaded at all), this method returns null.
        Parameters:
        canonicalPluginName - The canonical name of a plugin
        Returns:
        An optional human-readable, localized failure message.
      • loadClass

        public Class loadClass​(Plugin plugin,
                               String className)
                        throws ClassNotFoundException
        Loads a class from the classloader of a plugin.
        Parameters:
        plugin - the plugin.
        className - the name of the class to load.
        Returns:
        the class.
        Throws:
        ClassNotFoundException - if the class was not found.
      • getPluginClassloader

        public PluginClassLoader getPluginClassloader​(Plugin plugin)
        Returns the classloader of a plugin.
        Parameters:
        plugin - the plugin.
        Returns:
        the classloader of the plugin.
      • validMagicNumbers

        public static boolean validMagicNumbers​(BufferedInputStream bin)
                                         throws IOException
        Verifies that the first few bytes of the input stream correspond to any of the known 'magic numbers' that are known to represent a JAR archive. This method uses the mark/reset functionality of InputStream. This ensures that the input stream is reset back to its original position after execution of this method.
        Parameters:
        bin - The input to read (cannot be null).
        Returns:
        true if the stream first few bytes are equal to any of the known magic number sequences, otherwise false.
        Throws:
        IOException
      • addPluginListener

        public void addPluginListener​(PluginListener listener)
        Registers a PluginListener, which will now start receiving events regarding plugin creation and destruction. When the listener was already registered, this method will have no effect.
        Parameters:
        listener - the listener to be notified (cannot be null).
      • removePluginListener

        public void removePluginListener​(PluginListener listener)
        Deregisters a PluginListener, which will no longer receive events. When the listener was never added, this method will have no effect.
        Parameters:
        listener - the listener to be removed (cannot be null).
      • addPluginManagerListener

        public void addPluginManagerListener​(PluginManagerListener listener)
        Registers a PluginManagerListener, which will now start receiving events regarding plugin management.
        Parameters:
        listener - the listener to be notified (cannot be null).
      • removePluginManagerListener

        public void removePluginManagerListener​(PluginManagerListener listener)
        Deregisters a PluginManagerListener, which will no longer receive events. When the listener was never added, this method will have no effect.
        Parameters:
        listener - the listener to be notified (cannot be null).
      • isMonitorTaskRunning

        public boolean isMonitorTaskRunning()