GithubHelp home page GithubHelp logo

opwvhk / plugin-loader Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 99 KB

A simple plugin loader, loading isolated plugins with their own classpath without a proxy or reflection.

License: Apache License 2.0

Java 100.00%

plugin-loader's Introduction

Build Status license Maven Central

Plugin Loader

A simple, minimal plugin system that does not require reflection.

To use it, you'll need a separate jar (dependency) with your service interfaces and required classes (or enums/records), providing interfaces for the Java ServiceLoader<T>. This jar is a compile-time dependency for plugins, and that it is a separate jar is a requirement for classloader isolation (see below).

Second, at runtime, you'll need one or more directories where plugins can be stored (for the plugin structure, see below).

Once your project structure is in place, you can simply create an accessor for the plugins and use it:

// One-time setup; you can specify all your SPIs here
Plugins plugins = new Plugins(pluginFolders, FooService.class);

// Use services
Iterable<FooService> fooServicesFromAllPlugins = plugins.getServices(FooService.class);
fooServicesFromAllPlugins.iterator().next().foo();

// Direct plugin access (needed to access metadata)
Collection<Plugin> allPlugins = plugins.getPlugins();
Plugin randomPlugin = plugins.getPlugins().iterator().next();
String metadata = randomPlugin.loadTextMetadata("metadata-file.txt");
System.out.printf("Metadata for plugin %s: %s%n", randomPlugin.getName(), metadata);

Plugin structure

The code above loads plugins from your plugin path, which is a set of directories like this:

plugin-dir/
├─ single-fat-jar.jar
├─ folder-structure/
│  ├─ META-INF/
│  │  ├─ MANIFEST.MF
│  │  └─ services/
│  │     └─ some.service.FooService
│  ├─ directory/
│  │  └─ my/
│  │     └─ plugin/
│  │        │─ Utility.class
│  │        └─ Service.class
│  ├─ dependency.jar
│  └─ metadata-file.txt
├─ ...

In this directory, we see two plugins: "single-fat-jar" and "folder-structure". The plugin names are taken from the names in the directory, and will only be unique if you're using a single directory as plugin path.

The plugins themselves:

  • have their own classloader, with a classpath consisting of JRE & SPI classes and either the plugin jar itself, or the jars and directories in the plugin directory
  • provide services using the Java {@link java.util.ServiceLoader ServiceLoader<T>} facility
  • can have metadata (for directory plugins, all files in the plugin directory that are not part of the classpath)

Plugin isolation

Each plugin has an isolated classpath. This is achieved with a FilteringClassLoader, that acts as a firewall: it allows access to the platform classes and to specified classpath entries, but nothing else. The classloader for each plugin adds the jars and directories packaged with the plugin classpath.

Class Loader Structure

Note that the classloaders use the default JRE delegation method: parent first. Combined with the firewall provided by the FilteringClassLoader, this ensures that the same service classes are used both by your application and the plugins. The practical result of this is that neither proxies nor reflection are needed to use the services provided by plugins.

As a bonus, the parent-first model hides any platform and service classes included in the plugin. So if a plugin erroneously includes the service classes as well (e.g., by marking them as compile/implementation dependencies instead of provided/compileOnly dependencies), these are hidden.

Logging

When loading plugins, this library uses the Java 9 System.Logger framework to log progress. Although this falls back to java.util.logging, modern frameworks like SLF4J and Log4J also provide platform loggers.

plugin-loader's People

Contributors

opwvhk avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

 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.