Sometimes in a RCP application, you may need to uninstall OSGi bundle in the runtime. With BundleContext, it’s doable. Here is a sample.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/** * Uninstall specific bundle by name. * * @param context the bundle context. * @param bundleName the name of bundle. * @return true means the bundle has been uninstalled. */ public static boolean uninstallBundle(BundleContext context, String bundleName) { if (context == null || Strings.isNullOrEmpty(bundleName)) { LOGGER.debug("some argument is null. do nothing."); return false; } Bundle[] bundles = context.getBundles(); if (bundles != null) { for (Bundle bundle : bundles) { if (bundle.getSymbolicName().equals(bundleName)) { LOGGER.debug("Found bundle " + bundleName + ", uninstall it"); try { bundle.uninstall(); return true; } catch (BundleException e) { LOGGER.debug(e.getMessage(), e); return false; } } } } LOGGER.debug("no bundle named " + bundleName + " was found"); return false; } |
As you can see, you need to specify the bundle/plugin name.