@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;
  }
 @Override
 public ServiceName getServiceName(Deployment dep, int state) {
   // Currently the bundleId is needed for uniqueness because of
   // [MSC-97] Cannot re-install service with same name
   Long bundleId = dep.getAttachment(Long.class);
   ServiceName serviceName =
       ServiceName.of(
           BUNDLE_BASE_NAME, "" + bundleId, "" + dep.getSymbolicName(), "" + dep.getVersion());
   if (state == Bundle.INSTALLED || state == Bundle.RESOLVED || state == Bundle.ACTIVE) {
     serviceName = serviceName.append(ConstantsHelper.bundleState(state));
   }
   return serviceName;
 }
  @Override
  protected InvocationContext getInvocationContext(Bundle bundle) {
    if (bundle == null) throw MESSAGES.illegalArgumentNull("bundle");

    UserBundleState userBundle = UserBundleState.assertBundleState(bundle);
    Deployment dep = userBundle.getDeployment();

    InvocationContext inv = dep.getAttachment(INVOCATION_CONTEXT_KEY);
    if (inv == null) {
      // TODO: support multiple roots defined in Bundle-ClassPath
      VirtualFile rootFile = userBundle.getDeployment().getRoot();
      Attachable att = new AttachableSupport() {};
      inv = new AbstractInvocationContext(systemContext, userBundle, rootFile, att);
      dep.putAttachment(INVOCATION_CONTEXT_KEY, inv);
    }
    return inv;
  }
 @Override
 public void uninstallBundle(Deployment dep) {
   Bundle bundle = dep.getAttachment(Bundle.class);
   UserBundleState userBundle = UserBundleState.assertBundleState(bundle);
   uninstallBundle(userBundle, 0);
 }