@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;
  }
  private void assertStorageState(StorageState storageState) {
    assertNotNull("BundleStorageState not null", storageState);

    File storageDir = storageState.getStorageDir();
    assertNotNull("Storage dir not null", storageDir);
    assertNotNull("Location not null", storageState.getLocation());
    assertTrue("Storage dir exists", storageDir.exists());

    File propertiesFile = new File(storageDir + "/" + StorageState.BUNDLE_PERSISTENT_PROPERTIES);
    assertTrue("Properties file exists", propertiesFile.exists());
  }
 void close() {
   VFSUtils.safeClose(storageState.getRootFile());
 }
 int getRevisionId() {
   return storageState.getRevisionId();
 }