GithubHelp home page GithubHelp logo

rahjesh / pf4j Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pf4j/pf4j

0.0 1.0 0.0 1.1 MB

Plugin Framework for Java (PF4J)

Home Page: http://www.pf4j.org

License: Apache License 2.0

Java 99.14% Kotlin 0.42% Batchfile 0.23% Shell 0.21%

pf4j's Introduction

Plugin Framework for Java (PF4J)

Join the chat at https://gitter.im/decebals/pf4j Travis CI Build Status Coverage Status Maven Central

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 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 50 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:

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.

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 DefaultPluginManager, JarPluginManager 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.

How to use

It's very simple to add pf4j in your application.

Define an extension point in your application using ExtensionPoint interface marker:

public interface Greeting extends ExtensionPoint {

    String getGreeting();

}

Create a plugin that contribute with an extension:

public class WelcomePlugin extends Plugin {

    public WelcomePlugin(PluginWrapper wrapper) {
        super(wrapper);
    }

    @Extension
    public static class WelcomeGreeting implements Greeting {

        public String getGreeting() {
            return "Welcome";
        }

    }

}

In above code I created a plugin 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, with class org.pf4j.demo.welcome.WelcomePlugin, with version 0.0.1 and with dependencies to plugins x, y, z.

Now you can play with plugins and extensions in your code:

public static void main(String[] args) {
    ...

    // create the plugin manager
    PluginManager pluginManager = new DefaultPluginManager();
    
    // start and load all plugins of application
    pluginManager.loadPlugins();
    pluginManager.startPlugins();

    // retrieve all extensions for "Greeting" extension point
    List<Greeting> 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

Demo

Demo applications are available in demo folder

pf4j's People

Contributors

decebals avatar lightglitch avatar janhoy avatar gitblit avatar pinhead84 avatar nextreports avatar dmitry-timofeev avatar ktdynamic avatar stixits avatar harbulot avatar kkriske avatar styurin avatar tylerhawkes avatar ecumene avatar anidotnet avatar srbala avatar tcollignon avatar acesar avatar hxzhao527 avatar josiahhaswell avatar julieng50 avatar reddraggone9 avatar michaelruocco avatar nddipiazza avatar robfrank avatar ggsjyoon avatar ssherwood avatar shirishpandharikar avatar gitter-badger avatar dustcontributor avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.