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
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
4 years ago
7 years ago
11 years ago
7 years ago
11 years ago
11 years ago
4 years ago
4 years ago
4 years ago
11 years ago
6 years ago
11 years ago
4 years ago
11 years ago
11 years ago
7 years ago
11 years ago
11 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
11 years ago
4 years ago
11 years ago
4 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
4 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
7 years ago
6 years ago
11 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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](https://sonarcloud.io/api/project_badges/measure?project=pf4j_pf4j&metric=coverage)](https://sonarcloud.io/summary/new_code?id=pf4j_pf4j)
  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-shell](https://github.com/pf4j/pf4j-shell) (interactive shell for PF4J)
  18. - [pf4j-update](https://github.com/pf4j/pf4j-update) (update mechanism for PF4J)
  19. - [pf4j-spring](https://github.com/pf4j/pf4j-spring) (PF4J - Spring Framework integration)
  20. - [pf4j-wicket](https://github.com/pf4j/pf4j-wicket) (PF4J - Wicket integration)
  21. - [pf4j-web](https://github.com/pf4j/pf4j-web) (PF4J in web applications)
  22. No XML, only Java.
  23. 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.
  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 `JarPluginManager`, `ZipPluginManager`, `DefaultPluginManager` (it's a `JarPluginManager` + `ZipPluginManager`) 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. **PLUGIN** = a container for **EXTENSION POINTS** and **EXTENSIONS** + lifecycle methods (start, stop, delete)
  33. 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).
  34. How to use
  35. -------------------
  36. It's very simple to add pf4j in your application.
  37. Define an extension point in your application/plugin using **ExtensionPoint** interface marker:
  38. ```java
  39. public interface Greeting extends ExtensionPoint {
  40. String getGreeting();
  41. }
  42. ```
  43. Create an extension using `@Extension` annotation:
  44. ```java
  45. @Extension
  46. public class WelcomeGreeting implements Greeting {
  47. public String getGreeting() {
  48. return "Welcome";
  49. }
  50. }
  51. ```
  52. Create (it's __optional__) a `Plugin` class if you are interested in plugin's lifecycle events (start, stop, ...):
  53. ```java
  54. public class WelcomePlugin extends Plugin {
  55. @Override
  56. public void start() {
  57. System.out.println("WelcomePlugin.start()");
  58. }
  59. @Override
  60. public void stop() {
  61. System.out.println("WelcomePlugin.stop()");
  62. }
  63. @Override
  64. public void delete() {
  65. System.out.println("WelcomePlugin.delete()");
  66. }
  67. }
  68. ```
  69. In above code we've created a plugin (welcome) that comes with one extension for the `Greeting` extension point.
  70. 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:
  71. ```
  72. Manifest-Version: 1.0
  73. Archiver-Version: Plexus Archiver
  74. Created-By: Apache Maven
  75. Built-By: decebal
  76. Build-Jdk: 1.6.0_17
  77. Plugin-Class: org.pf4j.demo.welcome.WelcomePlugin
  78. Plugin-Dependencies: x, y, z
  79. Plugin-Id: welcome-plugin
  80. Plugin-Provider: Decebal Suiu
  81. Plugin-Version: 0.0.1
  82. ```
  83. 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).
  84. Now you can play with plugins and extensions in your code:
  85. ```java
  86. public static void main(String[] args) {
  87. ...
  88. // create the plugin manager
  89. PluginManager pluginManager = new JarPluginManager(); // or "new ZipPluginManager() / new DefaultPluginManager()"
  90. // start and load all plugins of application
  91. pluginManager.loadPlugins();
  92. pluginManager.startPlugins();
  93. // retrieve all extensions for "Greeting" extension point
  94. List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
  95. for (Greeting greeting : greetings) {
  96. System.out.println(">>> " + greeting.getGreeting());
  97. }
  98. // stop and unload all plugins
  99. pluginManager.stopPlugins();
  100. pluginManager.unloadPlugins();
  101. ...
  102. }
  103. ```
  104. The output is:
  105. ```
  106. >>> Welcome
  107. ```
  108. PF4J is very customizable and comes with a lot of goodies. Please read the documentation to discover yourself the power of this library.
  109. Documentation
  110. ---------------
  111. Documentation is available on [pf4j.org](http://pf4j.org)
  112. Demo
  113. ---------------
  114. Demo applications are available in [demo](https://github.com/pf4j/pf4j/tree/master/demo) folder
  115. Quickstart (call to action)
  116. ---------------
  117. 1. Read this file to have an overview about what this project does
  118. 2. Read [Getting started](https://pf4j.org/doc/getting-started.html) section of documentation to understand the basic concepts
  119. 3. Read [Quickstart](https://pf4j.org/dev/quickstart.html) section of documentation to create your first PF4J-based modular application