Jahia 7.3 Jahia 8

Get latest/started version of a module

Question

How to get the latest version or stated version of a module?

Answer

To get the latest bundle, follow code snippet could be used (example with event module):

    String symbolicName = "event";
        org.osgi.framework.Bundle latestBundle = null;
        for (org.osgi.framework.Bundle bundle : org.jahia.osgi.FrameworkService.getBundleContext().getBundles()) {
            String n = bundle.getSymbolicName();
            if (org.apache.commons.lang.StringUtils.equals(n, symbolicName)) {
             if (latestBundle == null) {
              latestBundle = bundle;
             } else if (latestBundle.getVersion().compareTo(bundle.getVersion()) < 0) {
              latestBundle = bundle;
             }
            }
        }
        log.info("Latest bundle found: " + latestBundle.getSymbolicName() + " in version " + latestBundle.getVersion() + " " + latestBundle.getLocation());
 

If you want to get the started bundle you can use follow code (example with event module):

        String symbolicName = "event";
      org.osgi.framework.Bundle startedBundle = null;
     for (org.osgi.framework.Bundle bundle : org.jahia.osgi.FrameworkService.getBundleContext().getBundles()) {
            String n = bundle.getSymbolicName();
            if (org.apache.commons.lang.StringUtils.equals(n, symbolicName)) {
                if (bundle.getState() == org.osgi.framework.Bundle.ACTIVE) {
                    startedBundle = bundle;
                    break;
                }
            }
        }
     if (startedBundle != null) {
      log.info("Started bundle found: " + startedBundle.getSymbolicName() + " in version " + startedBundle.getVersion() + " " + startedBundle.getLocation());
     } else {
      log.info("No started " + symbolicName + " bundle found!");
     }

 

The code can be executed directly in the JCR Console.