Creating an application that uses an optional module when it is available
Create a platform suite application – for this demonstration I called it MyAppWithOptionalModule
Add Module A
Create an Installer/Activator
This is for quick demonstration purposes, you don’t need to use an Intaller/Activator this code could be placed in your own class!
Add a simple System.out.println(“Hello From Module A”); message so we can see something happening in ModuleA.
If you run this application you should see “Hello From Module A” in the output window (as seen above).
In order to make ModuleB optional we need a shared interface that we’ll define in another Module (Module C).
Create a new Module Suite:
Create a new Module called ModuleC within the ModuleC Module Suite.
Create a new Interface I’ve called it ModuleBInterface.
Define a method for this example I’ve called it sayHelloFromModuleB.
(I’ve not shown all these steps but you should already know how to create a new interface or class within a project)
You should now have something like this…
Goto ModuleC’s Properties and select API Versioning. Make Module C public:
Build the ModuleC Module Suite.
Goto the first Application Suite that we created
I called mine MyAppWithOptionalModule.
Right click and select Properties.
Goto Libraries and press the “Add Project Button”
Select the ModuleC Module Suite we created above:
Press OK, you should now see your new shared module in the list of available libraries:
Go back to our Installer (or your class) in ModuleA and we’ll add some code that uses ModuleB if it exists (which at this point it does not):
We are going to use the Lookup API so right click on Module A, select Libraries, Add Dependency and Select Lookup API.
We are also going to need ModuleC so lets add that too!
Add some more code to our Installer class as shown below:
This uses the ModuleB interface we defined (in ModuleC).
We fetch an instance of that class if there is one defined in the global lookup.
If we get back an instance (from Module B) we use it – otherwise we display a message that module B isn’t installed (just for demonstration purposes).
If you run the application you should see the new output (as seen in the image above).
Hello from Module A
Module B isn’t installed but that’s OK, we just won’t use it
Finally create ModuleB which will provide an implementation of the (optional) class we wan’t to use in ModuleA.
Create a new Module Suite:
I created a new Module Suite called ModuleB and added a new Module (also called ModuleB) to it.
Right Click on the ModuleB ModuleSuite properties and select Libraries. Add Module C as we did earlier.
Right Click on the ModuleB ModuleB and add a dependency on ModuleC
We are also going to need the Lookup API so lets add that too!
In ModuleB create a new Class – I’ve called mine ModuleBImplementation.
You should now have the following structure:
Modify the code so that we implement the ModuleBInterface interface we defined in ModuleC and add a ServiceProvider annotation as shown below:
Compile/Build all 3 projects.
Right Click on the ModuleB ModuleSuite and select Package as NBMs.
Make a note of the directory where the NBMs are created (shown in the output window as seen below):
Right Click on our Application and select Libraries.
Add Auto Update Services and Auto Update UI. These will allow use to install our optional Module B. It will add a Plugins menu item under the Tools menu of our GUI.
If we run our application again we notice it returns the same information as before – that Module B isn’t installed and therefore not used.
When we run – we also get a GUI window. In our GUI application select the Tools/Plugins menu item.
Goto the Downloaded Tab. Press the Add Plugins… button and navigate to the directory where our NBM is located.
(It was shown in the output when we performed the Package as NBM step above)
Press the install button and follow the wizard to install the plugin.
If we run the application again we’ll see the following in the output window:
Yay! Module A just used our optional Module B class.
Go back to the Tools/Plugins dialog and uninstall Module B.
Run the application again…
Once again we see Module A is running but it didn’t use the optional class defined in Module B because Module B is no longer installed!