Programmatically install a module in a NetBeans Platform Application

Since we developed a new feature for my company software, I was asked to automatically install the help module for this one. The only way to do it was to install the module programmatically because we couldn’t add a dependency since all the help modules sit in a different suite.

You can find a copy of the code on my Github repository.

First of all, I added an Installer to my module because I wanted to check if the module was installed or not at the when the application starts.

To add an Installer, just right click on your module in the Project View and select New/Other…, in the popup window that appears select Module Development on the left and Installer / Activator on the right, continue with the wizard and NetBeans will create one file called Installer.java with a restore() method that will be called every time the application starts.

The first thing I do is to refresh the application module list because it might happen that the application does know new modules are available yet.

// I refresh the module list
for (UpdateUnitProvider provider : UpdateUnitProviderFactory.getDefault().getUpdateUnitProviders(false)) {
    try {
        provider.refresh(handle, true);
    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
    }
}

Then I search for my module UpdateUnit, an UpdateUnits represents something we want to install or update.

  • List<UpdateUnit> updateUnits = UpdateManager.getDefault().getUpdateUnits();
    UpdateUnit moduleUpdateUnit = null;
    for (UpdateUnit updateUnit : updateUnits) {
         final String codeName = updateUnit.getCodeName();
         if (MODULE_CNB.equals(codeName)) {
             moduleUpdateUnit = updateUnit;
            break;
        }
    }
    

    I ask if there are any updates available, in this case, the module is not installed so the platform will install it.

    List<UpdateElement> availableUpdates = moduleUpdateUnit.getAvailableUpdates();
    UpdateElement update = pickLatest(availableUpdates);
    

    Finally, I can start the real installation: I create an OperationContainer which will take care of the operation.
    Some notes here:

  • The InstallSupport doDownload() methods take three parameters: the ProgressHandle,  a boolean to install the module for all users or only for the current one and a second boolean that allows setting the user dir as a fallback. I’d highly recommend setting the last argument to true.
  • The Restarter allows restarting the platform immediately or setting it to being restarted later, I prefer the former because is less invasive. In my company application, all modules require restarting before being installed for safety reasons.
    OperationContainer<InstallSupport> container = createForInstall();
    container.add(Collections.singleton(update));
    
     InstallSupport support = container.getSupport();
     try {
         Validator v = support.doDownload(handle, true, true);
         InstallSupport.Installer i = support.doValidate(v, handle);
         Restarter r = support.doInstall(i, handle);
         if (r != null) {
             support.doRestartLater(r);
             setModuleInstalled();
         }
     } catch (OperationException ex) {
         LOGGER.log(Level.SEVERE, null, ex);
     }
    

    Here there are some useful links:

  • NetBeans Update Centers content management process
  • NetBeans Architecture Answers for Auto Update Services module
  • Class OperationContainer<Support>

 


The image was designed by FreePik

Leave a Comment

Your email address will not be published. Required fields are marked *