Plugin Framework for Java (PF4J) ===================== [![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) [![Travis CI Build Status](https://travis-ci.org/pf4j/pf4j.png)](https://travis-ci.org/pf4j/pf4j) [![Coverage Status](https://coveralls.io/repos/pf4j/pf4j/badge.svg?branch=master&service=github)](https://coveralls.io/github/pf4j/pf4j?branch=master) [![Maven Central](http://img.shields.io/maven-central/v/org.pf4j/pf4j.svg)](http://search.maven.org/#search|ga|1|pf4j) A plugin is a way for a third party to extend the functionality of an application. A plugin implements extension points declared by application or other plugins. Also a plugin can define extension points. **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. Features/Benefits ------------------- With PF4J you can easily transform a monolithic java application in a modular application. 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`). 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. For now are available these extensions: - [pf4j-update](https://github.com/pf4j/pf4j-update) (update mechanism for PF4J) - [pf4j-spring](https://github.com/pf4j/pf4j-spring) (PF4J - Spring Framework integration) - [pf4j-wicket](https://github.com/pf4j/pf4j-wicket) (PF4J - Wicket integration) - [pf4j-web](https://github.com/pf4j/pf4j-web) (PF4J in web applications) No XML, only Java. 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. 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). Components ------------------- - **Plugin** is the base class for all plugins types. Each plugin is loaded into a separate class loader to avoid conflicts. - **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). - **PluginLoader** loads all information (classes) needed by a plugin. - **ExtensionPoint** is a point in the application where custom code can be invoked. It's a java interface marker. Any java interface or abstract class can be marked as an extension point (implements `ExtensionPoint` interface). - **Extension** is an implementation of an extension point. It's a java annotation on a class. **PLUGIN** = a container for **EXTENSION POINTS** and **EXTENSIONS** + lifecycle methods (start, stop, delete) 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). How to use ------------------- It's very simple to add pf4j in your application. Define an extension point in your application/plugin using **ExtensionPoint** interface marker: ```java public interface Greeting extends ExtensionPoint { String getGreeting(); } ``` Create an extension using `@Extension` annotation: ```java @Extension public class WelcomeGreeting implements Greeting { public String getGreeting() { return "Welcome"; } } ``` Create (it's optional) a `Plugin` class if you are interested for plugin's lifecycle events (start, stop, ...): ```java public class WelcomePlugin extends Plugin { public WelcomePlugin(PluginWrapper wrapper) { super(wrapper); // you can use "wrapper" to have access to the plugin context (plugin manager, descriptor, ...) } @Override public void start() { System.out.println("WelcomePlugin.start()"); } @Override public void stop() { System.out.println("WelcomePlugin.stop()"); } @Override public void delete() { System.out.println("WelcomePlugin.delete()"); } } ``` In above code I created a plugin (welcome) that comes with one extension for the `Greeting` extension point. 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: ``` Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: decebal Build-Jdk: 1.6.0_17 Plugin-Class: org.pf4j.demo.welcome.WelcomePlugin Plugin-Dependencies: x, y, z Plugin-Id: welcome-plugin Plugin-Provider: Decebal Suiu Plugin-Version: 0.0.1 ``` 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 to plugins `x, y, z` (optional attribute). Now you can play with plugins and extensions in your code: ```java public static void main(String[] args) { ... // create the plugin manager PluginManager pluginManager = new JarPluginManager(); // or "new ZipPluginManager() / new DefaultPluginManager()" // start and load all plugins of application pluginManager.loadPlugins(); pluginManager.startPlugins(); // retrieve all extensions for "Greeting" extension point List greetings = pluginManager.getExtensions(Greeting.class); for (Greeting greeting : greetings) { System.out.println(">>> " + greeting.getGreeting()); } // stop and unload all plugins pluginManager.stopPlugins(); pluginManager.unloadPlugins(); ... } ``` The output is: ``` >>> Welcome ``` PF4J is very customizable and comes with a lot of goodies. Please read the documentation to discover yourself the power of this library. Documentation --------------- Documentation is available on [pf4j.org](http://www.pf4j.org) Demo --------------- Demo applications are available in [demo](https://github.com/pf4j/pf4j/tree/master/demo) folder Quickstart (call to action) --------------- 1. Read this file to have an overview about what this project does 2. Read [Getting started](https://pf4j.org/doc/getting-started.html) section of documentation to understand the basic concepts 3. Read [Quickstart](https://pf4j.org/dev/quickstart.html) section of documentation to create your first PF4J-based modular application 6 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360