You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.6KB

11 years ago
7 years ago
6 years ago
6 years ago
11 years ago
6 years ago
10 years ago
11 years ago
11 years ago
10 years ago
7 years ago
10 years ago
10 years ago
6 years ago
11 years ago
11 years ago
11 years ago
6 years ago
11 years ago
11 years ago
11 years ago
7 years ago
11 years ago
7 years ago
11 years ago
11 years ago
6 years ago
11 years ago
6 years ago
11 years ago
11 years ago
7 years ago
11 years ago
11 years ago
6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
6 years ago
11 years ago
6 years ago
11 years ago
8 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
8 years ago
6 years ago
8 years ago
6 years ago
6 years ago
11 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <img src="pf4j-logo.svg" width="250"/>
  2. Plugin Framework for Java (PF4J)
  3. =====================
  4. [![Join the chat at https://gitter.im/decebals/pf4j](https://badges.gitter.im/decebals/pf4j.svg)](https://gitter.im/decebals/pf4j?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
  5. [![Travis CI Build Status](https://travis-ci.org/pf4j/pf4j.png)](https://travis-ci.org/pf4j/pf4j)
  6. [![Coverage Status](https://coveralls.io/repos/pf4j/pf4j/badge.svg?branch=master&service=github)](https://coveralls.io/github/pf4j/pf4j?branch=master)
  7. [![Maven Central](http://img.shields.io/maven-central/v/org.pf4j/pf4j.svg)](http://search.maven.org/#search|ga|1|pf4j)
  8. A plugin is a way for a third party to extend the functionality of an application. A plugin implements extension points
  9. declared by application or other plugins. Also a plugin can define extension points.
  10. **NOTE:** Starting with version 0.9 you can define an extension directly in the application jar (you're not obligated to put the extension in a plugin - you can see this extension as a default/system extension). See [WhazzupGreeting](https://github.com/pf4j/pf4j/blob/master/demo/app/src/main/java/org/pf4j/demo/WhazzupGreeting.java) for a real example.
  11. Features/Benefits
  12. -------------------
  13. With PF4J you can easily transform a monolithic java application in a modular application.
  14. PF4J is an open source (Apache license) lightweight (around __50 KB__) plugin framework for java, with minimal dependencies (only slf4j-api) and very extensible (see PluginDescriptorFinder and ExtensionFinder).
  15. Practically PF4J is a microframework and the aim is to keep the core simple but extensible. I try to create a little ecosystem (extensions) based on this core with the help of the comunity.
  16. For now are available these extensions:
  17. - [pf4j-update](https://github.com/pf4j/pf4j-update) (update mechanism for PF4J)
  18. - [pf4j-spring](https://github.com/pf4j/pf4j-spring) (PF4J - Spring Framework integration)
  19. - [pf4j-wicket](https://github.com/pf4j/pf4j-wicket) (PF4J - Wicket integration)
  20. - [pf4j-web](https://github.com/pf4j/pf4j-web) (PF4J in web applications)
  21. No XML, only Java.
  22. You can mark any interface or abstract class as an extension point (with marker interface ExtensionPoint) and you specified that an class is an extension with @Extension annotation.
  23. Also, PF4J can be used in web applications. For my web applications when I want modularity I use [pf4j-wicket](https://github.com/pf4j/pf4j-wicket).
  24. Components
  25. -------------------
  26. - **Plugin** is the base class for all plugins types. Each plugin is loaded into a separate class loader to avoid conflicts.
  27. - **PluginManager** is used for all aspects of plugins management (loading, starting, stopping). You can use a built-in implementation as `DefaultPluginManager`, `JarPluginManager` or you can implement a custom plugin manager starting from `AbstractPluginManager` (implement only factory methods).
  28. - **PluginLoader** loads all information (classes) needed by a plugin.
  29. - **ExtensionPoint** is a point in the application where custom code can be invoked. It's a java interface marker.
  30. Any java interface or abstract class can be marked as an extension point (implements `ExtensionPoint` interface).
  31. - **Extension** is an implementation of an extension point. It's a java annotation on a class.
  32. How to use
  33. -------------------
  34. It's very simple to add pf4j in your application.
  35. Define an extension point in your application using **ExtensionPoint** interface marker:
  36. ```java
  37. public interface Greeting extends ExtensionPoint {
  38. String getGreeting();
  39. }
  40. ```
  41. Create a plugin that contribute with an extension:
  42. ```java
  43. public class WelcomePlugin extends Plugin {
  44. public WelcomePlugin(PluginWrapper wrapper) {
  45. super(wrapper);
  46. }
  47. @Extension
  48. public static class WelcomeGreeting implements Greeting {
  49. public String getGreeting() {
  50. return "Welcome";
  51. }
  52. }
  53. }
  54. ```
  55. In above code I created a plugin that comes with one extension for the `Greeting` extension point.
  56. You can distribute you plugin as a jar file (the simple solution). In this case add the plugin's metadata in `MANIFEST.MF` file of jar:
  57. ```
  58. Manifest-Version: 1.0
  59. Archiver-Version: Plexus Archiver
  60. Created-By: Apache Maven
  61. Built-By: decebal
  62. Build-Jdk: 1.6.0_17
  63. Plugin-Class: org.pf4j.demo.welcome.WelcomePlugin
  64. Plugin-Dependencies: x, y, z
  65. Plugin-Id: welcome-plugin
  66. Plugin-Provider: Decebal Suiu
  67. Plugin-Version: 0.0.1
  68. ```
  69. In above manifest I described a plugin with id `welcome-plugin`, with class `org.pf4j.demo.welcome.WelcomePlugin`, with version `0.0.1` and with dependencies
  70. to plugins `x, y, z`.
  71. Now you can play with plugins and extensions in your code:
  72. ```java
  73. public static void main(String[] args) {
  74. ...
  75. // create the plugin manager
  76. PluginManager pluginManager = new DefaultPluginManager();
  77. // start and load all plugins of application
  78. pluginManager.loadPlugins();
  79. pluginManager.startPlugins();
  80. // retrieve all extensions for "Greeting" extension point
  81. List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
  82. for (Greeting greeting : greetings) {
  83. System.out.println(">>> " + greeting.getGreeting());
  84. }
  85. // stop and unload all plugins
  86. pluginManager.stopPlugins();
  87. pluginManager.unloadPlugins();
  88. ...
  89. }
  90. ```
  91. The output is:
  92. ```
  93. >>> Welcome
  94. ```
  95. PF4J is very customizable and comes with a lot of goodies. Please read the documentation to discover yourself the power of this library.
  96. Documentation
  97. ---------------
  98. Documentation is available on [pf4j.org](http://www.pf4j.org)
  99. Demo
  100. ---------------
  101. Demo applications are available in [demo](https://github.com/pf4j/pf4j/tree/master/demo) folder