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 7.0KB

11 years ago
7 years ago
6 years ago
11 years ago
6 years ago
10 years ago
11 years ago
11 years ago
10 years ago
4 years ago
10 years ago
10 years ago
6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
5 years ago
7 years ago
11 years ago
7 years ago
11 years ago
11 years ago
5 years ago
5 years ago
5 years ago
11 years ago
6 years ago
11 years ago
5 years ago
11 years ago
11 years ago
7 years ago
11 years ago
11 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
11 years ago
5 years ago
11 years ago
5 years ago
11 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
5 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
11 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. [![GitHub Actions Status](https://github.com/pf4j/pf4j/actions/workflows/build.yml/badge.svg)](https://github.com/pf4j/pf4j/actions/workflows/build.yml)
  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 __100 KB__) plugin framework for java, with minimal dependencies (only slf4j-api) and very extensible (see `PluginDescriptorFinder` and `ExtensionFinder`).
  15. Practically, PF4J is a microframework that aims to keep the core simple but extensible. We also have a community-driven ecosystem of extensions.
  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. Components
  24. -------------------
  25. - **Plugin** is the base class for all plugins types. Each plugin is loaded into a separate class loader to avoid conflicts.
  26. - **PluginManager** is used for all aspects of plugins management (loading, starting, stopping). You can use a built-in implementation as `JarPluginManager`, `ZipPluginManager`, `DefaultPluginManager` (it's a `JarPluginManager` + `ZipPluginManager`) or you can implement a custom plugin manager starting from `AbstractPluginManager` (implement only factory methods).
  27. - **PluginLoader** loads all information (classes) needed by a plugin.
  28. - **ExtensionPoint** is a point in the application where custom code can be invoked. It's a java interface marker.
  29. Any java interface or abstract class can be marked as an extension point (implements `ExtensionPoint` interface).
  30. - **Extension** is an implementation of an extension point. It's a java annotation on a class.
  31. **PLUGIN** = a container for **EXTENSION POINTS** and **EXTENSIONS** + lifecycle methods (start, stop, delete)
  32. A **PLUGIN** is similar with a **MODULE** from other systems. If you don't need lifecycle methods (hook methods for start, stop, delete) you are not forced to supply a plugin class (the `PluginClass` property from the plugin descriptor is optional). You only need to supply some description of plugin (id, version, author, ...) for a good tracking (your application wants to know who supplied the extensions or extensions points).
  33. How to use
  34. -------------------
  35. It's very simple to add pf4j in your application.
  36. Define an extension point in your application/plugin using **ExtensionPoint** interface marker:
  37. ```java
  38. public interface Greeting extends ExtensionPoint {
  39. String getGreeting();
  40. }
  41. ```
  42. Create an extension using `@Extension` annotation:
  43. ```java
  44. @Extension
  45. public class WelcomeGreeting implements Greeting {
  46. public String getGreeting() {
  47. return "Welcome";
  48. }
  49. }
  50. ```
  51. Create (it's __optional__) a `Plugin` class if you are interested in plugin's lifecycle events (start, stop, ...):
  52. ```java
  53. public class WelcomePlugin extends Plugin {
  54. @Override
  55. public void start() {
  56. System.out.println("WelcomePlugin.start()");
  57. }
  58. @Override
  59. public void stop() {
  60. System.out.println("WelcomePlugin.stop()");
  61. }
  62. @Override
  63. public void delete() {
  64. System.out.println("WelcomePlugin.delete()");
  65. }
  66. }
  67. ```
  68. In above code we've created a plugin (welcome) that comes with one extension for the `Greeting` extension point.
  69. You can distribute your plugin as a jar file (the simple solution). In this case add the plugin's metadata in `MANIFEST.MF` file of jar:
  70. ```
  71. Manifest-Version: 1.0
  72. Archiver-Version: Plexus Archiver
  73. Created-By: Apache Maven
  74. Built-By: decebal
  75. Build-Jdk: 1.6.0_17
  76. Plugin-Class: org.pf4j.demo.welcome.WelcomePlugin
  77. Plugin-Dependencies: x, y, z
  78. Plugin-Id: welcome-plugin
  79. Plugin-Provider: Decebal Suiu
  80. Plugin-Version: 0.0.1
  81. ```
  82. In the manifest above, we've described a plugin with the id of `welcome-plugin` (mandatory attribute). We've also defined a class `org.pf4j.demo.welcome.WelcomePlugin` (optional attribute), with version `0.0.1` (mandatory attribute) and with dependencies to plugins `x, y, z` (optional attribute).
  83. Now you can play with plugins and extensions in your code:
  84. ```java
  85. public static void main(String[] args) {
  86. ...
  87. // create the plugin manager
  88. PluginManager pluginManager = new JarPluginManager(); // or "new ZipPluginManager() / new DefaultPluginManager()"
  89. // start and load all plugins of application
  90. pluginManager.loadPlugins();
  91. pluginManager.startPlugins();
  92. // retrieve all extensions for "Greeting" extension point
  93. List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
  94. for (Greeting greeting : greetings) {
  95. System.out.println(">>> " + greeting.getGreeting());
  96. }
  97. // stop and unload all plugins
  98. pluginManager.stopPlugins();
  99. pluginManager.unloadPlugins();
  100. ...
  101. }
  102. ```
  103. The output is:
  104. ```
  105. >>> Welcome
  106. ```
  107. PF4J is very customizable and comes with a lot of goodies. Please read the documentation to discover yourself the power of this library.
  108. Documentation
  109. ---------------
  110. Documentation is available on [pf4j.org](http://pf4j.org)
  111. Demo
  112. ---------------
  113. Demo applications are available in [demo](https://github.com/pf4j/pf4j/tree/master/demo) folder
  114. Quickstart (call to action)
  115. ---------------
  116. 1. Read this file to have an overview about what this project does
  117. 2. Read [Getting started](https://pf4j.org/doc/getting-started.html) section of documentation to understand the basic concepts
  118. 3. Read [Quickstart](https://pf4j.org/dev/quickstart.html) section of documentation to create your first PF4J-based modular application