Class PluginManager<T extends ApplicationComponent>
- java.lang.Object
-
- com.tagtraum.core.app.PluginManager<T>
-
- All Implemented Interfaces:
ApplicationComponent<T>
public class PluginManager<T extends ApplicationComponent> extends Object implements ApplicationComponent<T>
Loads, starts and makes plugins available.
Note, that most actions and a number of other major components are implemented as plugins. Plugins are either jars in a plugin directory or zips in the plugin directory that are expanded. Native libraries or jars in
/lib
are loaded or added to the classpath, respectively./classes
is also added to the classpath.beaTlets are also loaded by this manager.
Unless you code your plugin as beaTlet, each plugin has to contain at least one plugin descriptor. The descriptor is a simple XML document that resides in the file
/META-INF/plugin.xml
. It is needed to tell thePluginManager
which classes to instantiate.Here's a simple example:
<?xml version="1.0" encoding="UTF-8" ?> <!-- contains a list of plugins to be instantiated by the PluginManager --> <plugins> <name>Awesome</name> <id>projectawesome</id> <version>0.9.0</version> <description>Project Awesome</description> <!-- your license --> <license> <name>GNU Lesser General Public License 2.1</name> <url>http://www.gnu.org/licenses/lgpl.txt</url> </license> <!-- icons contained in your plugin jar --> <icon> <small>/com/yourdomain/project/[email protected]</small> <large>/com/yourdomain/project/[email protected]</large> </icon> <change-notes> 0.9.0 - first version </change-notes> <!-- info about your organization --> <vendor> <name>YourCompany Inc</name> <url>http://www.yourdomain.com/</url> <email>[email protected]</email> <logo>http://www.yourdomain.com/images/yourcompany-logo.png</logo> </vendor> <!-- versions of beaTunes this plugin works with --> <beatunes-version> <min>3.0.0</min> <max>3.9.9</max> </beatunes-version> <!-- the actual plugin classes that need to be instantiated --> <plugin class="com.yourdomain.project.Awesome"/> <plugin class="com.yourdomain.project.Awesome2"/> <plugin class="com.yourdomain.project.Awesome3"/> <!-- add more plugins here ... --> </plugins>
Should you want to write your code in a simple JRuby/Jython/Groovy script, you need to write it as beaTlet. This means you simply drop the source file into the plugin directory. If your beaTlet wants to also expose information about itself, it needs to implement the
Plugin
interface.- Author:
- Hendrik Schreiber
- See Also:
Plugin
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
PluginManager.PluginReference<P>
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_CONNECT_TIMEOUT
static int
DEFAULT_READ_TIMEOUT
-
Constructor Summary
Constructors Constructor Description PluginManager(T application)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addClassLoader(ClassLoader classLoader)
Add a classloader to the plugin classloader.void
addPluginRepository(PluginRepository pluginRepository)
void
addPluginRepository(URL pluginRepositoryURL)
void
addPropertyChangeListener(PropertyChangeListener listener)
T
getApplication()
Returns the main application component.Version
getApplicationVersion()
List<RuntimePluginDescriptor>
getEnabledPluginDescriptors()
Currently enabled plugin descriptors.String
getId()
Unique id for each instance of this object.<A> A
getImplementation(Class<A> interfaceClass)
Lets you obtain a plugin implementations for the given interface.<A> List<A>
getImplementations(Class<A> interfaceClass)
Lets you obtain a list of plugin implementations for the given interface.List<?>
getImplementationsAnnotatedWith(Class<? extends Annotation> annotationClass)
Lets you obtain a list of plugin implementations for the given interface.int
getInstallablePluginDescriptorCount()
List<RuntimePluginDescriptor>
getInstallablePluginDescriptors()
Lists plugins that are not installed, but could be installed.Object
getPlugin(Object id)
Allows you to retrieve a plugin by id.ClassLoader
getPluginClassLoader()
At this point all plugins share one classloader.int
getPluginDescriptorCount()
List<RuntimePluginDescriptor>
getPluginDescriptors()
All currently installed plugin descriptors.Path
getPluginDir()
int
getRemotePluginDescriptorCount()
List<RuntimePluginDescriptor>
getRemotePluginDescriptors()
String
getUserAgent()
void
init()
Loads all plugins.void
initPlugins()
CallsApplicationComponent.init()
on all plugins that implement that method.<A> void
injectApplication(A plugin)
void
install(RuntimePluginDescriptor pluginDescriptor)
boolean
isSuitableForCurrentApplicationVersion(PluginDescriptor pluginDescriptor)
void
loadRemotePluginDescriptors()
void
loadRemotePluginDescriptors(boolean forceReload)
void
registerPlugin(PluginManager.PluginReference<?> pluginReference)
void
registerRuntimePluginDescriptor(RuntimePluginDescriptor runtimePluginDescriptor)
void
setApplication(T t)
Sets the main application component.void
setApplicationVersion(Version applicationVersion)
void
setPluginDir(File pluginDir)
void
setPluginDir(Path pluginDir)
void
setUserAgent(String userAgent)
void
shutdown()
CallsApplicationComponent.shutdown()
()} on all plugins that implement that method.void
uninstall(RuntimePluginDescriptor pluginDescriptor)
Uninstalls a plugin described by its descriptor.
-
-
-
Field Detail
-
DEFAULT_READ_TIMEOUT
public static final int DEFAULT_READ_TIMEOUT
- See Also:
- Constant Field Values
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUT
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
PluginManager
public PluginManager(T application)
-
-
Method Detail
-
getApplication
public T getApplication()
Description copied from interface:ApplicationComponent
Returns the main application component.- Specified by:
getApplication
in interfaceApplicationComponent<T extends ApplicationComponent>
- Returns:
- application object
-
setApplication
public void setApplication(T t)
Description copied from interface:ApplicationComponent
Sets the main application component.- Specified by:
setApplication
in interfaceApplicationComponent<T extends ApplicationComponent>
- Parameters:
t
- main application component
-
getId
public String getId()
Description copied from interface:ApplicationComponent
Unique id for each instance of this object.- Specified by:
getId
in interfaceApplicationComponent<T extends ApplicationComponent>
- Returns:
- id
-
getUserAgent
public String getUserAgent()
-
setUserAgent
public void setUserAgent(String userAgent)
-
addPluginRepository
public void addPluginRepository(PluginRepository pluginRepository)
-
addPluginRepository
public void addPluginRepository(URL pluginRepositoryURL)
-
loadRemotePluginDescriptors
public void loadRemotePluginDescriptors() throws IOException
- Throws:
IOException
-
loadRemotePluginDescriptors
public void loadRemotePluginDescriptors(boolean forceReload) throws IOException
- Throws:
IOException
-
getRemotePluginDescriptorCount
public int getRemotePluginDescriptorCount()
-
getRemotePluginDescriptors
public List<RuntimePluginDescriptor> getRemotePluginDescriptors()
-
getInstallablePluginDescriptors
public List<RuntimePluginDescriptor> getInstallablePluginDescriptors()
Lists plugins that are not installed, but could be installed.- Returns:
- list of plugin descriptors
-
getInstallablePluginDescriptorCount
public int getInstallablePluginDescriptorCount()
-
getPluginDir
public Path getPluginDir()
-
setPluginDir
public void setPluginDir(Path pluginDir)
-
setPluginDir
public void setPluginDir(File pluginDir)
-
getApplicationVersion
public Version getApplicationVersion()
-
setApplicationVersion
public void setApplicationVersion(Version applicationVersion)
-
uninstall
public void uninstall(RuntimePluginDescriptor pluginDescriptor) throws IOException
Uninstalls a plugin described by its descriptor.- Parameters:
pluginDescriptor
- plugin descriptor- Throws:
IOException
- if something goes wrong
-
install
public void install(RuntimePluginDescriptor pluginDescriptor) throws IOException
- Throws:
IOException
-
addPropertyChangeListener
public void addPropertyChangeListener(PropertyChangeListener listener)
-
getImplementationsAnnotatedWith
public List<?> getImplementationsAnnotatedWith(Class<? extends Annotation> annotationClass)
Lets you obtain a list of plugin implementations for the given interface. E.g. to get all registered Entities, call:getBeaTunes().getPluginManager().getImplementationsAnnotatedWith(Entity.class)
- Parameters:
annotationClass
- annotation the plugin has to be annotated with- Returns:
- list of implementations
-
getImplementations
public <A> List<A> getImplementations(Class<A> interfaceClass)
Lets you obtain a list of plugin implementations for the given interface. E.g. to get all registered com.tagtraum.beatunes.songtable.SongContextComponents, call:getBeaTunes().getPluginManager().getImplementations(SongContextComponent.class)
- Parameters:
interfaceClass
- interface the plugin has to implement- Returns:
- list of implementations
-
getImplementation
public <A> A getImplementation(Class<A> interfaceClass)
Lets you obtain a plugin implementations for the given interface. E.g. to get a registered com.tagtraum.beatunes.songtable.SongContextComponents, call:getBeaTunes().getPluginManager().getImplementation(SongContextComponent.class)
- Parameters:
interfaceClass
- interface the plugin has to implement- Returns:
- one implementation or null, if none are registered
-
getPlugin
public Object getPlugin(Object id)
Allows you to retrieve a plugin by id. For this to work, the plugin has to implement aString getId()
method.- Parameters:
id
- id string- Returns:
- the plugin with the given id or null.
-
getPluginClassLoader
public ClassLoader getPluginClassLoader()
At this point all plugins share one classloader. This may change in the future to allow different plugins to use different versions of third party libraries.- Returns:
- plugin class loader
-
addClassLoader
public void addClassLoader(ClassLoader classLoader)
Add a classloader to the plugin classloader.- Parameters:
classLoader
- classloader
-
registerRuntimePluginDescriptor
public void registerRuntimePluginDescriptor(RuntimePluginDescriptor runtimePluginDescriptor)
-
getPluginDescriptors
public List<RuntimePluginDescriptor> getPluginDescriptors()
All currently installed plugin descriptors.- Returns:
- plugin descriptors
-
getEnabledPluginDescriptors
public List<RuntimePluginDescriptor> getEnabledPluginDescriptors()
Currently enabled plugin descriptors.- Returns:
- enabled descriptors
-
getPluginDescriptorCount
public int getPluginDescriptorCount()
-
init
public void init()
Loads all plugins.- Specified by:
init
in interfaceApplicationComponent<T extends ApplicationComponent>
-
initPlugins
public void initPlugins()
CallsApplicationComponent.init()
on all plugins that implement that method. This is done after all plugins have been instantiated. The initialization order is the same as the declaration order in the plugin descriptorplugin.xml
.
-
shutdown
public void shutdown()
CallsApplicationComponent.shutdown()
()} on all plugins that implement that method. The shutdown order is the reverse declaration order of the plugin descriptorplugin.xml
.- Specified by:
shutdown
in interfaceApplicationComponent<T extends ApplicationComponent>
-
registerPlugin
public void registerPlugin(PluginManager.PluginReference<?> pluginReference)
-
injectApplication
public <A> void injectApplication(A plugin) throws IllegalAccessException, InvocationTargetException
-
isSuitableForCurrentApplicationVersion
public boolean isSuitableForCurrentApplicationVersion(PluginDescriptor pluginDescriptor)
-
-