void removeBundle(UserBundleState userBundle, int options) {
    LOGGER.tracef("Start removing bundle: %s", userBundle);

    if ((options & Bundle.STOP_TRANSIENT) == 0) {
      BundleStoragePlugin storagePlugin = getFrameworkState().getBundleStoragePlugin();
      storagePlugin.deleteStorageState(userBundle.getStorageState());
    }

    XEnvironment env = getFrameworkState().getEnvironment();
    for (XBundleRevision abr : userBundle.getAllBundleRevisions()) {
      env.uninstallResources(abr);
    }

    FrameworkEventsPlugin eventsPlugin = getFrameworkState().getFrameworkEventsPlugin();
    eventsPlugin.fireBundleEvent(userBundle, BundleEvent.UNRESOLVED);

    ModuleManagerPlugin moduleManager = getFrameworkState().getModuleManagerPlugin();
    for (XBundleRevision brev : userBundle.getAllBundleRevisions()) {
      UserBundleRevision userRev = (UserBundleRevision) brev;
      if (userRev.isFragment() == false) {
        ModuleIdentifier identifier = moduleManager.getModuleIdentifier(brev);
        moduleManager.removeModule(brev, identifier);
      }
      userRev.close();
    }

    LOGGER.debugf("Removed bundle: %s", userBundle);
  }
  @Override
  public ServiceName installBundle(
      Deployment dep, ServiceTarget serviceTarget, ServiceListener<XBundle> listener)
      throws BundleException {
    if (dep == null) throw MESSAGES.illegalArgumentNull("deployment");

    ServiceName serviceName;

    // If a bundle containing the same location identifier is already installed,
    // the Bundle object for that bundle is returned.
    XBundle bundle = getBundleByLocation(dep.getLocation());
    if (bundle instanceof AbstractBundleState) {
      LOGGER.debugf("Installing an already existing bundle: %s", dep);
      AbstractBundleState bundleState = AbstractBundleState.assertBundleState(bundle);
      serviceName = bundleState.getServiceName(Bundle.INSTALLED);
      VFSUtils.safeClose(dep.getRoot());
    } else {
      try {
        Long bundleId;
        String symbolicName = dep.getSymbolicName();
        XEnvironment env = injectedEnvironment.getValue();

        // The storage state exists when we re-create the bundle from persistent storage
        StorageState storageState = dep.getAttachment(StorageState.class);
        if (storageState != null) {
          LOGGER.debugf("Found storage state: %s", storageState);
          bundleId = env.nextResourceIdentifier(storageState.getBundleId(), symbolicName);
        } else {
          bundleId = env.nextResourceIdentifier(null, symbolicName);
        }
        dep.addAttachment(Long.class, bundleId);

        // Check that we have valid metadata
        OSGiMetaData metadata = dep.getAttachment(OSGiMetaData.class);
        if (metadata == null) {
          DeploymentFactoryPlugin plugin = getFrameworkState().getDeploymentFactoryPlugin();
          metadata = plugin.createOSGiMetaData(dep);
        }

        // Create the bundle services
        if (metadata.getFragmentHost() == null) {
          serviceName =
              HostBundleInstalledService.addService(
                  serviceTarget, getFrameworkState(), dep, listener);
        } else {
          serviceName =
              FragmentBundleInstalledService.addService(
                  serviceTarget, getFrameworkState(), dep, listener);
        }
      } catch (RuntimeException rte) {
        VFSUtils.safeClose(dep.getRoot());
        throw rte;
      } catch (BundleException ex) {
        VFSUtils.safeClose(dep.getRoot());
        throw ex;
      }
    }
    dep.addAttachment(ServiceName.class, serviceName);
    return serviceName;
  }
 Set<XBundle> getBundles() {
   Set<XBundle> result = new HashSet<XBundle>();
   XEnvironment env = injectedEnvironment.getValue();
   for (Resource aux : env.getResources(XEnvironment.ALL_IDENTITY_TYPES)) {
     XBundle bundle = ((XBundleRevision) aux).getBundle();
     if (bundle.getState() != Bundle.UNINSTALLED) result.add(bundle);
   }
   return Collections.unmodifiableSet(result);
 }
 @Override
 public Set<XBundle> getBundles(Integer states) {
   Set<XBundle> result = new HashSet<XBundle>();
   XEnvironment env = injectedEnvironment.getValue();
   for (Resource aux : env.getResources(XEnvironment.ALL_IDENTITY_TYPES)) {
     XBundle bundle = ((XBundleRevision) aux).getBundle();
     if (states == null || (bundle.getState() & states.intValue()) != 0) result.add(bundle);
   }
   return Collections.unmodifiableSet(result);
 }
 @Override
 public XBundle getBundleById(long bundleId) {
   if (bundleId == 0) {
     return getFrameworkState().getSystemBundle();
   }
   XEnvironment env = injectedEnvironment.getValue();
   Collection<XResource> resources = env.getResources(XEnvironment.ALL_IDENTITY_TYPES);
   for (Resource aux : resources) {
     XBundle bundle = ((XBundleRevision) aux).getBundle();
     if (bundle.getBundleId() == bundleId) {
       return bundle;
     }
   }
   return null;
 }