@Override
  public void deploy(final DeploymentPhaseContext phaseContext)
      throws DeploymentUnitProcessingException {

    // Check if we already have an OSGi deployment
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    Deployment deployment = OSGiDeploymentAttachment.getDeployment(deploymentUnit);

    ServiceRegistry serviceRegistry = phaseContext.getServiceRegistry();
    String location =
        DeploymentHolderService.getLocation(serviceRegistry, deploymentUnit.getName());
    VirtualFile virtualFile = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT).getRoot();

    // Check for attached BundleInfo
    BundleInfo info = BundleInfoAttachment.getBundleInfo(deploymentUnit);
    if (deployment == null && info != null) {
      deployment = DeploymentFactory.createDeployment(info);
      deployment.addAttachment(BundleInfo.class, info);
      OSGiDeploymentAttachment.attachDeployment(deploymentUnit, deployment);
    }

    // Check for attached OSGiMetaData
    OSGiMetaData metadata = OSGiMetaDataAttachment.getOSGiMetaData(deploymentUnit);
    if (deployment == null && metadata != null) {
      String symbolicName = metadata.getBundleSymbolicName();
      Version version = metadata.getBundleVersion();
      deployment =
          DeploymentFactory.createDeployment(
              AbstractVFS.adapt(virtualFile), location, symbolicName, version);
      deployment.addAttachment(OSGiMetaData.class, metadata);
      OSGiDeploymentAttachment.attachDeployment(deploymentUnit, deployment);
    }

    // Check for attached XModule
    XModule resModule = XModuleAttachment.getXModuleAttachment(deploymentUnit);
    if (deployment == null && resModule != null) {
      String symbolicName = resModule.getName();
      Version version = resModule.getVersion();
      deployment =
          DeploymentFactory.createDeployment(
              AbstractVFS.adapt(virtualFile), location, symbolicName, version);
      deployment.addAttachment(XModule.class, resModule);
      OSGiDeploymentAttachment.attachDeployment(deploymentUnit, deployment);
    }

    // Create the {@link OSGiDeploymentService}
    if (deployment != null) {

      // Prevent garbage collection of the MountHandle which will close the file
      MountHandle mount =
          deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT).getMountHandle();
      deployment.addAttachment(MountHandle.class, mount);

      // Mark the bundle to start automatically
      deployment.setAutoStart(true);

      OSGiDeploymentService.addService(phaseContext);
    }
  }
  @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;
  }