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.3KB

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
4 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
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
11 years ago
5 years ago
11 years ago
5 years ago
11 years ago
5 years ago
5 years ago
11 years ago
6 years ago
11 years ago
8 years ago
6 years ago
8 years ago
5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 __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 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 `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 for plugin's lifecycle events (start, stop, ...):
  53. ```java
  54. public class WelcomePlugin extends Plugin {
  55. public WelcomePlugin(PluginWrapper wrapper) {
  56. super(wrapper);
  57. // you can use "wrapper" to have access to the plugin context (plugin manager, descriptor, ...)
  58. }
  59. @Override
  60. public void start() {
  61. System.out.println("WelcomePlugin.start()");
  62. }
  63. @Override
  64. public void stop() {
  65. System.out.println("WelcomePlugin.stop()");
  66. }
  67. @Override
  68. public void delete() {
  69. System.out.println("WelcomePlugin.delete()");
  70. }
  71. }
  72. ```
  73. In above code I created a plugin (welcome) that comes with one extension for the `Greeting` extension point.
  74. 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:
  75. ```
  76. Manifest-Version: 1.0
  77. Archiver-Version: Plexus Archiver
  78. Created-By: Apache Maven
  79. Built-By: decebal
  80. Build-Jdk: 1.6.0_17
  81. Plugin-Class: org.pf4j.demo.welcome.WelcomePlugin
  82. Plugin-Dependencies: x, y, z
  83. Plugin-Id: welcome-plugin
  84. Plugin-Provider: Decebal Suiu
  85. Plugin-Version: 0.0.1
  86. ```
  87. In above manifest I described a plugin with id `welcome-plugin` (mandatory attribute), with class `org.pf4j.demo.welcome.WelcomePlugin` (optional attribute), with version `0.0.1` (mandatory attribute) and with dependencies
  88. to plugins `x, y, z` (optional attribute).
  89. Now you can play with plugins and extensions in your code:
  90. ```java
  91. public static void main(String[] args) {
  92. ...
  93. // create the plugin manager
  94. PluginManager pluginManager = new JarPluginManager(); // or "new ZipPluginManager() / new DefaultPluginManager()"
  95. // start and load all plugins of application
  96. pluginManager.loadPlugins();
  97. pluginManager.startPlugins();
  98. // retrieve all extensions for "Greeting" extension point
  99. List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
  100. for (Greeting greeting : greetings) {
  101. System.out.println(">>> " + greeting.getGreeting());
  102. }
  103. // stop and unload all plugins
  104. pluginManager.stopPlugins();
  105. pluginManager.unloadPlugins();
  106. ...
  107. }
  108. ```
  109. The output is:
  110. ```
  111. >>> Welcome
  112. ```
  113. PF4J is very customizable and comes with a lot of goodies. Please read the documentation to discover yourself the power of this library.
  114. Documentation
  115. ---------------
  116. Documentation is available on [pf4j.org](http://pf4j.org)
  117. Demo
  118. ---------------
  119. Demo applications are available in [demo](https://github.com/pf4j/pf4j/tree/master/demo) folder
  120. Quickstart (call to action)
  121. ---------------
  122. 1. Read this file to have an overview about what this project does
  123. 2. Read [Getting started](https://pf4j.org/doc/getting-started.html) section of documentation to understand the basic concepts
  124. 3. Read [Quickstart](https://pf4j.org/dev/quickstart.html) section of documentation to create your first PF4J-based modular application