void uninstallBundle(UserBundleState userBundle, int options) {
    if (userBundle.aquireBundleLock(LockMethod.UNINSTALL)) {
      try {
        int state = userBundle.getState();
        if (state == Bundle.UNINSTALLED) return;

        // #2 If the bundle's state is ACTIVE, STARTING or STOPPING, the bundle is stopped
        if (userBundle.isFragment() == false) {
          if (state == Bundle.ACTIVE || state == Bundle.STARTING || state == Bundle.STOPPING) {
            try {
              userBundle.stopInternal(options);
            } catch (Exception ex) {
              // If Bundle.stop throws an exception, a Framework event of type FrameworkEvent.ERROR
              // is fired
              fireFrameworkError(userBundle, "stopping bundle: " + userBundle, ex);
            }
          }
        }

        // #3 This bundle's state is set to UNINSTALLED
        FrameworkEventsPlugin eventsPlugin =
            userBundle.getFrameworkState().getFrameworkEventsPlugin();
        userBundle.changeState(Bundle.UNINSTALLED, 0);

        // Remove the bundle services
        userBundle.removeServices();

        // Check if the bundle has still active wires
        boolean hasActiveWires = userBundle.hasActiveWires();
        if (hasActiveWires == false) {
          // #5 This bundle and any persistent storage area provided for this bundle by the
          // Framework are removed
          removeBundle(userBundle, options);
        }

        // #4 A bundle event of type BundleEvent.UNINSTALLED is fired
        eventsPlugin.fireBundleEvent(userBundle, BundleEvent.UNINSTALLED);

        LOGGER.infoBundleUninstalled(userBundle);

        // Remove other uninstalled bundles that now also have no active wires any more
        Set<XBundle> uninstalled = getBundles(Bundle.UNINSTALLED);
        for (Bundle auxState : uninstalled) {
          UserBundleState auxUser = UserBundleState.assertBundleState(auxState);
          if (auxUser.hasActiveWires() == false) {
            removeBundle(auxUser, options);
          }
        }
      } finally {
        userBundle.releaseBundleLock(LockMethod.UNINSTALL);
      }
    }
  }