@Override
 public BundleInformation installBundle(MultipartFile bundleFile, boolean startBundle) {
   File savedBundleFile = null;
   try {
     savedBundleFile = bundleDirectoryManager.saveBundleFile(bundleFile);
     return new BundleInformation(installBundleFromFile(savedBundleFile, startBundle, true));
   } catch (Exception e) {
     if (savedBundleFile != null) {
       LOG.error("Removing bundle due to exception", e);
       savedBundleFile.delete();
     }
     throw new MotechException("Cannot install file", e);
   }
 }
  @Override
  public void uninstallBundle(long bundleId) throws BundleException {
    Bundle bundle = getBundle(bundleId);
    bundle.uninstall();

    try {
      boolean deleted = bundleDirectoryManager.removeBundle(bundle);
      if (!deleted) {
        LOG.warn("Failed to delete bundle file: " + bundle.getLocation());
      }
    } catch (IOException e) {
      throw new MotechException("Error while removing bundle file", e);
    }
  }
  @Override
  public BundleInformation installFromRepository(String featureId, boolean start) {

    org.apache.maven.repository.internal.DefaultServiceLocator locator =
        new org.apache.maven.repository.internal.DefaultServiceLocator();
    locator.addService(RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class);
    locator.setServices(WagonProvider.class, new HttpWagonProvider());

    RepositorySystem system = locator.getService(RepositorySystem.class);

    // RepositorySystem system = null;
    try {
      // system = new DefaultPlexusContainer().lookup(RepositorySystem.class);

      MavenRepositorySystemSession mvnRepository = new MavenRepositorySystemSession();

      mvnRepository.setLocalRepositoryManager(
          system.newLocalRepositoryManager(
              new LocalRepository(System.getProperty("java.io.tmpdir") + "/repo")));

      CollectRequest collectRequest = new CollectRequest();
      collectRequest.setRoot(new Dependency(new DefaultArtifact(featureId), JavaScopes.RUNTIME));
      collectRequest.addRepository(
          new RemoteRepository(
              "central", "default", "http://nexus.motechproject.org/content/repositories/public"));

      DependencyRequest dependencyRequest =
          new DependencyRequest(
              collectRequest, DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME));

      BundleInformation bundleInformation = null;
      List<ArtifactResult> artifactResults =
          system.resolveDependencies(mvnRepository, dependencyRequest).getArtifactResults();

      List<Bundle> bundlesInstalled = new ArrayList<>();

      for (ArtifactResult artifact : artifactResults) {
        LOG.info("Installing " + artifact);
        final File bundleFile = artifact.getArtifact().getFile();
        FileInputStream fileInputStream = null;
        try {
          fileInputStream = new FileInputStream(bundleFile);
          final File bundleFileToInstall =
              bundleDirectoryManager.saveBundleStreamToFile(bundleFile.getName(), fileInputStream);
          final Bundle bundle = installBundleFromFile(bundleFileToInstall, false, false);
          if (bundle != null) {
            bundlesInstalled.add(bundle);
          }
        } finally {
          IOUtils.closeQuietly(fileInputStream);
        }
      }

      // start bundles after all bundles installed to avoid any dependency resolution problems.
      if (start) {
        for (Bundle bundle : bundlesInstalled) {
          if (!isFragmentBundle(bundle)) {
            bundle.start();
          }
        }
      }

      return bundleInformation;
    } catch (Exception e) {
      LOG.error("Error while installing bundle and dependencies " + featureId, e);
      throw new MotechException("Cannot install file", e);
    }
  }