154

How would you implement a Plugin-system for your Java application?

Is it possible to have an easy to use (for the developer) system which achieves the following:

  • Users put their plugins into a subdirectory of the app
  • The Plugin can provide a configuration screen
  • If you use a framework, is the license compatible with commercial developement?
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
Sven Lilienthal
  • 6,396
  • 4
  • 29
  • 25

8 Answers8

114

First you need an interface that all plugins need to implement, e.g.

public interface Plugin {
    public void load(PluginConfiguration pluginConfiguration);
    public void run();
    public void unload();
    public JComponent getConfigurationPage();
}

Plugin authors should then bundle their plugins into JAR files. Your applications opens the JAR file and could then use an attribute from JAR manifest or the list of all files in the JAR file to find the class that implements your Plugin interface. Instantiate that class, the plugin is ready to go.

Of course you may also want to implement some kind of sandboxing so that the plugin is restricted in what it can and can not do. I have created a small test application (and blogged about it) that consists of two plugins, one of which is denied access to local resources.

Bombe
  • 81,643
  • 20
  • 123
  • 127
  • 2
    the sandbox you mention is actually the most difficult part! but not to fret - osgi already does it for you as mentioned above. – Chii Jan 22 '09 at 02:03
  • 1
    Sandboxing isn’t that difficult. Took me about two weeks to find out how to do it correctly but once you know that it’s pretty simple. :) – Bombe Jan 23 '09 at 07:46
  • @Bombe is this example application still live anywhere? – ataulm Nov 28 '12 at 21:51
  • * using the attachment found at https://bugs.freenetproject.org/print_bug_page.php?bug_id=1900 – ataulm Nov 28 '12 at 22:07
  • @timberwo7ves I don’t understand what you mean. The file test application can still be downloaded from the location given in the post, and from the page you mentioned. – Bombe Nov 30 '12 at 11:55
  • sorry Bombe, I tried it a couple days ago and it timed out repeatedly - both links work now. Thanks for the worked example :) – ataulm Dec 01 '12 at 13:28
  • I just wanted to say thanks so much for this, and thanks for licensing under GPL! I'm going to add this to my code right now. – Piccolo Apr 18 '13 at 04:14
  • @Bombe what do you mean by load() and unload()? – capovawi Mar 17 '14 at 14:19
  • @capovawi I don’t mean anything by that. If you don’t need those methods, don’t include them in your plugin interface. – Bombe Jan 14 '16 at 08:58
42

Use OSGi.

It is the foundation of the Eclipse plug-in system. Equinox is Eclipse's implementation (licensed EPL) and Felix is the Apache Project's implementation (licensed Apache Public License).

Eclipse provides a concrete example that OSGi can cover the points you mentioned (or you could just build your application on top of Eclipse RCP if you want a full Eclipse/SWT/JFace stack).

chatellier
  • 179
  • 14
Aaron Maenpaa
  • 119,832
  • 11
  • 95
  • 108
  • 4
    I have dabbled with OSGi but never found a really good primer for it. It would be great if somebody could recommend some links here. – Brian Matthews Jan 21 '09 at 14:30
  • 1
    I've embedded equinox into my application and used standard OSGi practices to do exactly what the OP wants. In swing too, not SWT. Webstarted as well. good starting resource: http://neilbartlett.name/blog/ – basszero Jan 21 '09 at 14:42
  • 4
    OSGi is ideally the de facto plugin system. However, the leap from standard Java to OSGi programming model is pretty wide... – Hendy Irawan Jul 17 '10 at 12:40
31

Since 1.6, there's been java.util.ServiceLoader which can be used if you want to code your own simple system.

But if you want anything more than basic features, use one of the existing frameworks.

Steen
  • 6,573
  • 3
  • 39
  • 56
Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
20

Use PF4J. It has support for Web, Spring and Wicket. Easy to use and build the applications

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • i find this to be what i need and it seems to be quite simple but i need some help on it – nonybrighto Mar 31 '17 at 18:25
  • try to raise questions to Decebal on github. he will help you – Daniel Jipa May 27 '17 at 10:41
  • see https://meta.stackoverflow.com/questions/376686/cant-there-be-a-direct-lift-of-the-ban-or-the-option-to-specifically-answer-onl on Decebals account status here – Wolfgang Fahl Nov 17 '18 at 07:29
  • 1
    If you are looking at pf4j, you might also take a look at https://github.com/hank-cp/sbp. It is built on top of pf4j to support Spring Boot to build a complete web application. – Hank Dec 11 '19 at 12:44
16

There is also JPF (Java Plugin Framework).

рüффп
  • 5,172
  • 34
  • 67
  • 113
jan
  • 273
  • 1
  • 6
  • Has anyone here used JPF? Sounds interesting – Sven Lilienthal Jan 21 '09 at 15:48
  • 1
    JabRef uses JPF for its plugins. Works quite well. – koppor Jan 02 '13 at 14:55
  • I'm using JPF, but it is tightly coupled with Ant. I want to get rid of it but I don't know how much impact it will have on my application. – MartinL Jun 16 '13 at 22:58
  • I created a JPF plugin for an open source product (OpenEMM); I was able to make it with maven (maven-assembly plugin); For sure I really appreciate the simplicity of the development even now it is probably OSGI which is more popular. – рüффп May 09 '14 at 09:16
  • Is there any restrictions on java versions for using JPF? – Madhav Jun 17 '16 at 19:03
14

I worked on OSGi for a week--an intense, nothing but OSGi week. At the end it was like a bad dream but I learned a lot.

I was able to get OSGi working (not easy, all examples are out of date, everything on the net is at least three years old if not five), but I had serious trouble getting it integrated into an existing project because of issues with the jar manifests.

In short, there are only a few obscure tools used for building manifests and they are not well documented (BND Tools is hardly obscure, but it is designed for a certain process in Eclipse). Also, most of the OSGi information available is not targeted towards application developers who have an existing desktop application.

This makes a lot of the context for the information foggy or inappropriate. Neil Bartlett's blog posts were the biggest help, but even those failed to get a working system (I grabbed some code from the Felix tutorial and pieced it together to get the embedded framework rolling). I found his book draft that he posted for free years ago, which is excellent, but the examples in Eclipse do not work because of changes in Eclipse OSGi support.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Sean Anderson
  • 614
  • 8
  • 20
  • 20
    How is this an answer? It's more of a rant about OSGi, and you're not even explaining what OSGi is. – Stealth Rabbi Oct 06 '15 at 12:37
  • 2
    @StealthRabbi At the time of this "answer," and for some time afterward, it was good information for someone trying to work with OSGi out in the wild. OSGi was already part of the discussion--so there was no need to define it. It is an answer in that it might have saved people a week of work--the whole reason for SO. – Sean Anderson Sep 11 '18 at 13:08
11

I think that recommending OSGi for solving the above stated problem is extremely poor advice. OSGi is "the right choice" but for a scenario as the one above, I think either JPF or some homegrown minimalistic framework is sufficient.

Steen
  • 6,573
  • 3
  • 39
  • 56
3

Years ago I started a project like that and I hope soon will be ready.I got inspired by projects like NetBeans and Eclipse but meanwhile it changed to something a little bit different. OSGi looks like a good choice now, but I didn't had a chance to compare it with my project.It is similar with JPF mentioned above, but in the same time different in many ways.

The basic idea which motivated me is to be as easy as possible to build Java application, with no separation between web applications, desktop applications or applet/JWS applications(of course this doesn't cover the UI - yet) as a core functionality.

I built the project with a few goals in my mind :

  • it doesn't matter if you build a web application or a desktop application you should start the application in the same way, a plain main method, No fancy web.xml declaration(not that I'm against having a standard web descriptor, but it doesn't go well with a plug-in system, where you add "servlets" - I call them RequestHandler(s) - dynamic at your will).
  • easy to plug in "extensions" around an "extension point" - something from Eclipse but a different approach.
  • self-deployable, since all the plugins are registered(XML files) the application must be self-deployable independent of the build system - of course there is an Ant task and a Maven MOJO which are the links with the ourside world, but in the end it calls the application and instruct it to self-deploy itself at a specific location.
  • borrowed from Maven, it can download code from repositories(including Maven 1 & 2 repositories) so your application can be deployed as a single small jar as long as you have access to the repositories(useful sometime, and basically this provides support for auto-updates - don't you love the idea to be notified by your web application that there is a newer version, it was downloaded and it just needs your permission to install it? I know I love that).
  • basic application monitoring about system health, email notifications in case of failures
adrian.tarau
  • 3,124
  • 2
  • 26
  • 29