Best way to build a Plugin system with Java

JavaDesign PatternsPluginsFrameworksPlugin Architecture

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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).

Solution 3 - Java

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.

Solution 4 - Java

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

[1]: https://pf4j.org/ "PF4J"

Solution 5 - Java

Solution 6 - Java

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.

Solution 7 - Java

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.

Solution 8 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSven LilienthalView Question on Stackoverflow
Solution 1 - JavaBombeView Answer on Stackoverflow
Solution 2 - JavaAaron MaenpaaView Answer on Stackoverflow
Solution 3 - JavaPete KirkhamView Answer on Stackoverflow
Solution 4 - JavaMallikarjuna SangisettyView Answer on Stackoverflow
Solution 5 - JavajanView Answer on Stackoverflow
Solution 6 - JavaSean AndersonView Answer on Stackoverflow
Solution 7 - JavaSteenView Answer on Stackoverflow
Solution 8 - Javaadrian.tarauView Answer on Stackoverflow