com.alee.managers.plugin
Class PluginManager<T extends Plugin>

java.lang.Object
  extended by com.alee.managers.plugin.PluginManager<T>

public abstract class PluginManager<T extends Plugin>
extends java.lang.Object

Base class for any plugin manager you might want to create.

Author:
Mikle Garin
See Also:
How to use PluginManager, Plugin

Field Summary
protected  boolean allowSimilarPlugins
          Whether should allow loading multiply plugins with the same ID or not.
protected  java.util.List<T> availablePlugins
          Loaded and running plugins list.
protected  java.util.Map<java.lang.Class<? extends Plugin>,T> availablePluginsByClass
          Map of plugins cached by their classes.
protected  java.util.Map<java.lang.String,T> availablePluginsById
          Map of plugins cached by their IDs.
protected  java.lang.Object checkLock
          Plugin checks lock object.
protected  boolean checkRecursively
          Whether plugins directory subfolders should be checked recursively or not.
protected  boolean createNewClassLoader
          Whether should create new class loader for each loaded plugin or not.
protected  java.util.List<DetectedPlugin<T>> detectedPlugins
          Detected plugins list.
protected  java.io.FileFilter fileFilter
          Plugin directory files filter.
protected  java.util.List<PluginsListener<T>> listeners
          Plugins listeners.
protected  Filter<DetectedPlugin<T>> pluginFilter
          Special filter to filter out unwanted plugins before their initialization.
protected  java.lang.String pluginsDirectoryPath
          Plugins directory path.
protected  java.util.List<DetectedPlugin<T>> recentlyDetected
          Recently detected plugins list.
protected  java.util.List<T> recentlyInitialized
          Recently initialized plugins list.
 
Constructor Summary
PluginManager()
          Constructs new plugin manager.
PluginManager(java.lang.String pluginsDirectoryPath)
          Constructs new plugin manager.
PluginManager(java.lang.String pluginsDirectoryPath, boolean checkRecursively)
          Constructs new plugin manager.
 
Method Summary
 void addPluginsListener(PluginsListener<T> listener)
          Adds plugins listener.
protected  void applyInitializationStrategy()
          Sorting plugins according to their initialization strategies.
 void checkPlugins()
          Performs plugins search within the specified plugins directory.
 void checkPlugins(boolean checkRecursively)
          Performs plugins search within the specified plugins directory.
 void checkPlugins(java.lang.String pluginsDirectoryPath)
          Performs plugins search within the specified plugins directory.
 void checkPlugins(java.lang.String pluginsDirectoryPath, boolean checkRecursively)
          Performs plugins search within the specified plugins directory.
protected  void collectPluginInformation(java.io.File file)
          Tries to collect plugin information from the specified file.
protected  boolean collectPluginsInformation(java.lang.String pluginsDirectoryPath, boolean checkRecursively)
          Collects information about available plugins.
protected  boolean collectPluginsInformationImpl(java.io.File dir, boolean checkRecursively)
          Collects information about available plugins.
 void firePluginsCheckEnded(java.lang.String directory, boolean recursive)
          Informs about plugins check operation end.
 void firePluginsCheckStarted(java.lang.String directory, boolean recursive)
          Informs about plugins check operation start.
 void firePluginsDetected(java.util.List<DetectedPlugin<T>> plugins)
          Informs about newly detected plugins.
 void firePluginsInitialized(java.util.List<T> plugins)
          Informs about newly initialized plugins.
protected  java.lang.String getAcceptedPluginType()
          Returns accepted by this manager plugin type.
 java.util.List<T> getAvailablePlugins()
          Returns list of available loaded plugins.
 java.util.List<DetectedPlugin<T>> getDetectedPlugins()
          Returns list of detected plugins.
 int getDetectedPluginsAmount()
          Returns amount of detected plugins.
 int getFailedPluginsAmount()
          Returns amount of plugins which have failed to load.
 java.io.FileFilter getFileFilter()
          Returns plugins directory file filter.
 int getLoadedPluginsAmount()
          Returns amount of successfully loaded plugins.
<P extends T>
P
getPlugin(java.lang.Class<P> pluginClass)
          Returns available plugin instance by its class.
<P extends T>
P
getPlugin(java.lang.String pluginId)
          Returns available plugin instance by its ID.
protected  java.lang.String getPluginDescriptorFile()
          Returns name of the plugin descriptor file.
 Filter<DetectedPlugin<T>> getPluginFilter()
          Returns special filter that filters out unwanted plugins before their initialization.
protected  java.lang.String getPluginLogoFile()
          Returns name of the plugin logo file.
 java.lang.String getPluginsDirectoryPath()
          Returns plugins directory path.
protected  void initializePlugins()
          Initializes detected earlier plugins.
 boolean isAllowSimilarPlugins()
          Returns whether should allow loading multiply plugins with the same ID or not.
 boolean isCheckRecursively()
          Returns whether plugins directory subfolders should be checked recursively or not.
 boolean isCreateNewClassLoader()
          Returns whether should create new class loader for each loaded plugin or not.
 boolean isDeprecatedVersion(DetectedPlugin<T> plugin)
          Returns whether the list of detected plugins contain a newer version of the specified plugin or not.
 boolean isDeprecatedVersion(DetectedPlugin<T> plugin, java.util.List<DetectedPlugin<T>> detectedPlugins)
          Returns whether the list of detected plugins contain a newer version of the specified plugin or not.
protected  boolean isSameVersionAlreadyLoaded(DetectedPlugin<T> plugin, java.util.List<DetectedPlugin<T>> detectedPlugins)
          Returns whether the list of detected plugins contain the same version of the specified plugin or not.
 void registerPlugin(T plugin)
          Registers programmatically loaded plugin within this PluginManager.
 void registerPlugin(T plugin, PluginInformation information, javax.swing.ImageIcon logo)
          Registers programmatically loaded plugin within this PluginManager.
 void removePluginsListener(PluginsListener<T> listener)
          Removes plugins listener.
 void setAllowSimilarPlugins(boolean allow)
          Sets whether should allow loading multiply plugins with the same ID or not.
 void setCheckRecursively(boolean checkRecursively)
          Sets whether plugins directory subfolders should be checked recursively or not.
 void setCreateNewClassLoader(boolean createNewClassLoader)
          Sets whether should create new class loader for each loaded plugin or not.
 void setFileFilter(java.io.FileFilter filter)
          Sets plugins directory file filter.
 void setPluginFilter(Filter<DetectedPlugin<T>> pluginFilter)
          Sets special filter that filters out unwanted plugins before their initialization.
 void setPluginsDirectoryPath(java.lang.String path)
          Sets plugins directory path.
protected  boolean wasDetected(java.lang.String pluginFolder, java.lang.String pluginFile)
          Returns whether this plugin file was already detected before or not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listeners

protected java.util.List<PluginsListener<T extends Plugin>> listeners
Plugins listeners.


checkLock

protected final java.lang.Object checkLock
Plugin checks lock object.


detectedPlugins

protected java.util.List<DetectedPlugin<T extends Plugin>> detectedPlugins
Detected plugins list. All plugins with available descriptions will get into this list.


recentlyDetected

protected java.util.List<DetectedPlugin<T extends Plugin>> recentlyDetected
Recently detected plugins list. Contains plugins detected while last plugins check.


pluginFilter

protected Filter<DetectedPlugin<T extends Plugin>> pluginFilter
Special filter to filter out unwanted plugins before their initialization. It is up to developer to specify this filter and its conditions.


allowSimilarPlugins

protected boolean allowSimilarPlugins
Whether should allow loading multiply plugins with the same ID or not. In case this is set to false only the newest version of the same plugin will be loaded if more than one provided.


availablePlugins

protected java.util.List<T extends Plugin> availablePlugins
Loaded and running plugins list. This might be less than list of detected plugins in the end due to lots of different reasons. Only those plugins which are actually loaded successfully are getting added here.


availablePluginsById

protected java.util.Map<java.lang.String,T extends Plugin> availablePluginsById
Map of plugins cached by their IDs.


availablePluginsByClass

protected java.util.Map<java.lang.Class<? extends Plugin>,T extends Plugin> availablePluginsByClass
Map of plugins cached by their classes.


recentlyInitialized

protected java.util.List<T extends Plugin> recentlyInitialized
Recently initialized plugins list. Contains plugins initialized while last plugins check.


pluginsDirectoryPath

protected java.lang.String pluginsDirectoryPath
Plugins directory path. It is either absolute path or relative to working directory path.


checkRecursively

protected boolean checkRecursively
Whether plugins directory subfolders should be checked recursively or not.


fileFilter

protected java.io.FileFilter fileFilter
Plugin directory files filter. By defauly "*.jar" and "*.plugin" files are accepted.


createNewClassLoader

protected boolean createNewClassLoader
Whether should create new class loader for each loaded plugin or not. Be aware that you might experience various issues with separate class loaders.

Constructor Detail

PluginManager

public PluginManager()
Constructs new plugin manager.


PluginManager

public PluginManager(java.lang.String pluginsDirectoryPath)
Constructs new plugin manager.

Parameters:
pluginsDirectoryPath - plugins directory path

PluginManager

public PluginManager(java.lang.String pluginsDirectoryPath,
                     boolean checkRecursively)
Constructs new plugin manager.

Parameters:
pluginsDirectoryPath - plugins directory path
checkRecursively - whether plugins directory subfolders should be checked recursively or not
Method Detail

getPluginDescriptorFile

protected java.lang.String getPluginDescriptorFile()
Returns name of the plugin descriptor file. This file should contain serialized PluginInformation.

Returns:
name of the plugin descriptor file

getPluginLogoFile

protected java.lang.String getPluginLogoFile()
Returns name of the plugin logo file. Logo should be placed near the plugin descriptor file.

Returns:
name of the plugin logo file

getAcceptedPluginType

protected java.lang.String getAcceptedPluginType()
Returns accepted by this manager plugin type. In case null is returned this manager accepts any plugin type.

Returns:
accepted by this manager plugin type

registerPlugin

public void registerPlugin(T plugin)
Registers programmatically loaded plugin within this PluginManager. This call will add the specified plugin into available plugins list. It will also create a custom DetectedPlugin data based on provided information.

Parameters:
plugin - plugin to register

registerPlugin

public void registerPlugin(T plugin,
                           PluginInformation information,
                           javax.swing.ImageIcon logo)
Registers programmatically loaded plugin within this PluginManager. This call will add the specified plugin into available plugins list. It will also create a custom DetectedPlugin data based on provided information.

Parameters:
plugin - plugin to register
information - about this plugin
logo - plugin logo

checkPlugins

public void checkPlugins()
Performs plugins search within the specified plugins directory. This call might be performed as many times as you like. It will simply ignore plugins detected before and will process newly found plugins appropriately.


checkPlugins

public void checkPlugins(boolean checkRecursively)
Performs plugins search within the specified plugins directory. This call might be performed as many times as you like. It will simply ignore plugins detected before and will process newly found plugins appropriately.

Parameters:
checkRecursively - whether plugins directory subfolders should be checked recursively or not

checkPlugins

public void checkPlugins(java.lang.String pluginsDirectoryPath)
Performs plugins search within the specified plugins directory. This call might be performed as many times as you like. It will simply ignore plugins detected before and will process newly found plugins appropriately.

Parameters:
pluginsDirectoryPath - plugins directory path

checkPlugins

public void checkPlugins(java.lang.String pluginsDirectoryPath,
                         boolean checkRecursively)
Performs plugins search within the specified plugins directory. This call might be performed as many times as you like. It will simply ignore plugins detected before and will process newly found plugins appropriately.

Parameters:
pluginsDirectoryPath - plugins directory path
checkRecursively - whether plugins directory subfolders should be checked recursively or not

collectPluginsInformation

protected boolean collectPluginsInformation(java.lang.String pluginsDirectoryPath,
                                            boolean checkRecursively)
Collects information about available plugins.

Returns:
true if operation succeeded, false otherwise

collectPluginsInformationImpl

protected boolean collectPluginsInformationImpl(java.io.File dir,
                                                boolean checkRecursively)
Collects information about available plugins.

Parameters:
dir - plugins directory
Returns:
true if operation succeeded, false otherwise

collectPluginInformation

protected void collectPluginInformation(java.io.File file)
Tries to collect plugin information from the specified file. This call will simply be ignored if this is not a plugin file or if something goes wrong.

Parameters:
file - plugin file to process

wasDetected

protected boolean wasDetected(java.lang.String pluginFolder,
                              java.lang.String pluginFile)
Returns whether this plugin file was already detected before or not.

Parameters:
pluginFolder - plugin directory
pluginFile - plugin file
Returns:
true if this plugin file was already detected before, false otherwise

initializePlugins

protected void initializePlugins()
Initializes detected earlier plugins.


isDeprecatedVersion

public boolean isDeprecatedVersion(DetectedPlugin<T> plugin)
Returns whether the list of detected plugins contain a newer version of the specified plugin or not.

Parameters:
plugin - plugin to compare with other detected plugins
Returns:
true if the list of detected plugins contain a newer version of the specified plugin, false otherwise

isDeprecatedVersion

public boolean isDeprecatedVersion(DetectedPlugin<T> plugin,
                                   java.util.List<DetectedPlugin<T>> detectedPlugins)
Returns whether the list of detected plugins contain a newer version of the specified plugin or not.

Parameters:
plugin - plugin to compare with other detected plugins
detectedPlugins - list of detected plugins
Returns:
true if the list of detected plugins contain a newer version of the specified plugin, false otherwise

isSameVersionAlreadyLoaded

protected boolean isSameVersionAlreadyLoaded(DetectedPlugin<T> plugin,
                                             java.util.List<DetectedPlugin<T>> detectedPlugins)
Returns whether the list of detected plugins contain the same version of the specified plugin or not.

Parameters:
plugin - plugin to compare with other detected plugins
detectedPlugins - list of detected plugins
Returns:
true if the list of detected plugins contain the same version of the specified plugin, false otherwise

applyInitializationStrategy

protected void applyInitializationStrategy()
Sorting plugins according to their initialization strategies.


getDetectedPlugins

public java.util.List<DetectedPlugin<T>> getDetectedPlugins()
Returns list of detected plugins.

Returns:
list of detected plugins

getAvailablePlugins

public java.util.List<T> getAvailablePlugins()
Returns list of available loaded plugins.

Returns:
list of available loaded plugins

getPlugin

public <P extends T> P getPlugin(java.lang.String pluginId)
Returns available plugin instance by its ID.

Parameters:
pluginId - plugin ID
Returns:
available plugin instance by its ID

getPlugin

public <P extends T> P getPlugin(java.lang.Class<P> pluginClass)
Returns available plugin instance by its class.

Parameters:
pluginClass - plugin class
Returns:
available plugin instance by its class

getDetectedPluginsAmount

public int getDetectedPluginsAmount()
Returns amount of detected plugins.

Returns:
amount of detected loaded plugins

getLoadedPluginsAmount

public int getLoadedPluginsAmount()
Returns amount of successfully loaded plugins.

Returns:
amount of successfully loaded plugins

getFailedPluginsAmount

public int getFailedPluginsAmount()
Returns amount of plugins which have failed to load. There might be a lot of reasons why they failed to load - exception, broken JAR, missing libraries etc. Simply check the log or retrieve failure cause from DetectedPlugin to understand what happened.

Returns:
amount of plugins which have failed to load

getPluginsDirectoryPath

public java.lang.String getPluginsDirectoryPath()
Returns plugins directory path.

Returns:
plugins directory path

setPluginsDirectoryPath

public void setPluginsDirectoryPath(java.lang.String path)
Sets plugins directory path.

Parameters:
path - new plugins directory path

isCheckRecursively

public boolean isCheckRecursively()
Returns whether plugins directory subfolders should be checked recursively or not.

Returns:
true if plugins directory subfolders should be checked recursively, false otherwise

setCheckRecursively

public void setCheckRecursively(boolean checkRecursively)
Sets whether plugins directory subfolders should be checked recursively or not.

Parameters:
checkRecursively - whether plugins directory subfolders should be checked recursively or not

getFileFilter

public java.io.FileFilter getFileFilter()
Returns plugins directory file filter.

Returns:
plugins directory file filter

setFileFilter

public void setFileFilter(java.io.FileFilter filter)
Sets plugins directory file filter. Note that setting this filter will not have any effect on plugins which are already initialized.

Parameters:
filter - plugins directory file filter

isCreateNewClassLoader

public boolean isCreateNewClassLoader()
Returns whether should create new class loader for each loaded plugin or not.

Returns:
true if should create new class loader for each loaded plugin, false otherwise

setCreateNewClassLoader

public void setCreateNewClassLoader(boolean createNewClassLoader)
Sets whether should create new class loader for each loaded plugin or not.

Parameters:
createNewClassLoader - whether should create new class loader for each loaded plugin or not

getPluginFilter

public Filter<DetectedPlugin<T>> getPluginFilter()
Returns special filter that filters out unwanted plugins before their initialization.

Returns:
special filter that filters out unwanted plugins before their initialization

setPluginFilter

public void setPluginFilter(Filter<DetectedPlugin<T>> pluginFilter)
Sets special filter that filters out unwanted plugins before their initialization.

Parameters:
pluginFilter - special filter that filters out unwanted plugins before their initialization

isAllowSimilarPlugins

public boolean isAllowSimilarPlugins()
Returns whether should allow loading multiply plugins with the same ID or not.

Returns:
true if should allow loading multiply plugins with the same ID, false otherwise

setAllowSimilarPlugins

public void setAllowSimilarPlugins(boolean allow)
Sets whether should allow loading multiply plugins with the same ID or not.

Parameters:
allow - whether should allow loading multiply plugins with the same ID or not

addPluginsListener

public void addPluginsListener(PluginsListener<T> listener)
Adds plugins listener.

Parameters:
listener - new plugins listener

removePluginsListener

public void removePluginsListener(PluginsListener<T> listener)
Removes plugins listener.

Parameters:
listener - plugins listener to remove

firePluginsCheckStarted

public void firePluginsCheckStarted(java.lang.String directory,
                                    boolean recursive)
Informs about plugins check operation start.

Parameters:
directory - checked plugins directory path
recursive - whether plugins directory subfolders are checked recursively or not

firePluginsCheckEnded

public void firePluginsCheckEnded(java.lang.String directory,
                                  boolean recursive)
Informs about plugins check operation end.

Parameters:
directory - checked plugins directory path
recursive - whether plugins directory subfolders are checked recursively or not

firePluginsDetected

public void firePluginsDetected(java.util.List<DetectedPlugin<T>> plugins)
Informs about newly detected plugins.

Parameters:
plugins - newly detected plugins list

firePluginsInitialized

public void firePluginsInitialized(java.util.List<T> plugins)
Informs about newly initialized plugins.

Parameters:
plugins - newly initialized plugins list