Plugin mechanism for GWT application

Cotrix, the RIA I’m working on, is packaged differently depending on the target environment. For each environment we want some features enabled or not, both client and server side. From the client POV I’ve to enable or disable some GWT components, I will call those components plugins. In our case each plugin is bounded in a separate Java library.

In GWT to enable a component you need to:

  • include it in the module file:
  • call it somewhere.

The first point can’t be automatized, or you can but you need to manipulate the module file during the packaging phase. This can be done using Maven facilities, but I will avoid it. Each packaging in the Cotrix application has his own GWT module, all of them inheriting from a common super module. The module for a specific environment adds the components (plugins) that I want to add.

The second point is a little tricky. Let’s assume that all the plugin implement a Plugin interface:

Once we have all the required plugin included in the main GWT module we need to discover them. In GWT you can’t use reflection or Service locator mechanism. With GIN I don’t have found any way to retrieve all the interface implementations (as you can do win CDI).

Therefore I’ve decided to go with a generator solution. In this solution the generator will be responsible to collect all the Plugin interface implementations and to generate the code that will instantiate them.

First I’ve declared a PluginProvider which will return all the Plugin instances:

Then how to retrieve the Plugin interface implementations? One way is using reflections during the compilation phase. Another one is to use the module configuration properties. A GWT module can declare a configuration property, both single value and multi-value. A third module can extend the property adding it more values.

I’ve used the property mechanism in order to let each module containing a Plugin implementation to extends a particular property with the Plugin implementation class name. The code generator will read this property values in order to know the Plugin interface implementations.

In the main GWT module we will declare the property used to declare the plugins:

Then each module containing a Plugin interface implementations will extend the property with the name of the implementation class:

Let see the PluginProvider generator code:

The generator retrieves the “plugins” property values, then use each value to instantiate the Plugin implementation, returning it in a list.

In the main GWT module we need to declare the generator :

Finally we will use the generated PluginProvider in this way:

I’ve created a sample project in GitHub: