Creating a moddable application with OSGi

225 Views Asked by At

I have been searching the web for a link to a good tutorial about what I am about to describe, but I haven't found it yet. My hope is that someone here already has an idea where to find it and can post a link to it here.

I want to know how to use OSGi/Equinox to build an application (game, business, or otherwise) that allows for libraries to be added later of unknown content.

This idea fascinated me from the first time I used Minecraft Forge and all I had to do was drop JARs into a mods directory that was probably identified as a classpath or something.

I first researched the topic in .NET and found a suitable response in "System.AddIn" and "System.AddIn.Contract" libraries. There is a good tutorial here: MSDN - Creating an Extensible Application. This worked pretty well. You create a pipeline of interfaces and contracts that define the types of AddIns and then AddIns can be added later simply by dropping the class libraries in the correct directory.

I am now actively investigating how to do this same thing in Java and I am currently looking at OSGi plug ins. However, all of the examples I have come across so far have focused on bundles that are known at design time. Moreover, there is the conversation of "DynamicImport-Package", but the resources I have seen so far frown on this.

So the question is does anyone know of a resource that discusses a method that is "smiled upon" for the dynamic discovery and loading of an unknown bundly (class library) that meets a specific interface?

2

There are 2 best solutions below

4
On BEST ANSWER

You are looking for OSGi services. In OSGi you can register services in one bundle and use those services in another bundle. You can also track services that appear in an OSGi container and use them if they meet your needs.

There are many tutorials on the NET that explains how you can use OSGi services. E.g.: http://www.knopflerfish.org/osgi_service_tutorial.html

An example how you could use it:

  • You specify an interface
  • Each plug-in bundle must have an implementation of the interface and the bundle registers the instantiated object as an OSGi service.
  • Your technology bundle (that can have plugins) tracks all OSGi services that implement the interface.
0
On

The main OSGi API to look at to embed an OSGi framework into your application and then dynamically load OSGi bundle plugin libraries to be loaded is the org.osgi.framework.launch.Framework. You may find poking around the source code of my https://github.com/vorburger/ch.vorburger.minecraft.osgi/ project of interest in this context: It embeds the Apache Felix OSGi framework into a Minecraft Sponge mod, and loads additional Minecraft mods as OSGi bundles dynamically - pretty much what you are asking for? ;-) Have a look specifically at the OSGiFrameworkWrapper class. You COULD alternatively also do this without OSGi and having fun with java.lang.ClassLoader yourself, or use my https://github.com/vorburger/HoTea project.