public void pkgList(List<? extends Package> packagesList) {
   CommandInfo cmdInfo = cset.newCommandInfo(CommandInfo.CMD_LIST);
   try {
     if (packagesList.isEmpty()) {
       log.info("None");
     } else {
       NuxeoConnectClient.getPackageManager().sort(packagesList);
       StringBuilder sb = new StringBuilder();
       for (Package pkg : packagesList) {
         newPackageInfo(cmdInfo, pkg);
         String packageDescription = PackageState.getByValue(pkg.getState()).getLabel();
         packageDescription = String.format("%6s %11s\t", pkg.getType(), packageDescription);
         if (pkg.getState() == PackageState.REMOTE.getValue()
             && pkg.getType() != PackageType.STUDIO
             && pkg.getVisibility() != PackageVisibility.PUBLIC
             && !LogicalInstanceIdentifier.isRegistered()) {
           packageDescription += "Registration required for ";
         }
         packageDescription += String.format("%s (id: %s)\n", pkg.getName(), pkg.getId());
         sb.append(packageDescription);
       }
       log.info(sb.toString());
     }
   } catch (Exception e) {
     log.error(e);
     cmdInfo.exitCode = 1;
   }
 }
 protected boolean pkgUpgradeByType(PackageType type) {
   List<DownloadablePackage> upgrades =
       NuxeoConnectClient.getPackageManager().listUpdatePackages(type, targetPlatform);
   List<String> upgradeIds = new ArrayList<String>();
   for (DownloadablePackage upgrade : upgrades) {
     upgradeIds.add(upgrade.getId());
   }
   return pkgRequest(null, upgradeIds, null, null);
 }
 protected boolean isRemotePackageId(String pkgId) {
   List<DownloadablePackage> remotePackages =
       NuxeoConnectClient.getPackageManager().listAllPackages();
   boolean foundId = false;
   for (DownloadablePackage pkg : remotePackages) {
     if (pkg.getId().equals(pkgId)) {
       foundId = true;
       break;
     }
   }
   return foundId;
 }
 public ConnectBroker(Environment env) throws IOException, PackageException {
   this.env = env;
   service = new StandaloneUpdateService(env);
   service.initialize();
   cbHolder = new StandaloneCallbackHolder(env, service);
   NuxeoConnectClient.setCallBackHolder(cbHolder);
   targetPlatform =
       env.getProperty(Environment.DISTRIBUTION_NAME)
           + "-"
           + env.getProperty(Environment.DISTRIBUTION_VERSION);
   distributionMPDir = env.getProperty(PARAM_MP_DIR, DISTRIBUTION_MP_DIR_DEFAULT);
 }
  // Wrap connect client services as Nuxeo Services
  public <T> T getAdapter(Class<T> adapter) {

    if (adapter.getCanonicalName().equals(ConnectConnector.class.getCanonicalName())) {
      return adapter.cast(NuxeoConnectClient.getConnectConnector());
    }

    if (adapter.getCanonicalName().equals(ConnectRegistrationService.class.getCanonicalName())) {
      return adapter.cast(NuxeoConnectClient.getConnectRegistrationService());
    }

    if (adapter.getCanonicalName().equals(ConnectDownloadManager.class.getCanonicalName())) {
      return adapter.cast(NuxeoConnectClient.getDownloadManager());
    }

    if (adapter.getCanonicalName().equals(PackageManager.class.getCanonicalName())) {
      return adapter.cast(NuxeoConnectClient.getPackageManager());
    }

    if (adapter.getCanonicalName().equals(PackageUpdateService.class.getCanonicalName())) {
      return adapter.cast(NuxeoConnectClient.getPackageUpdateService());
    }

    return adapter.cast(this);
  }
 protected Map<String, PackageDefinition> getDistributionDefinitions(List<String> md5Filenames) {
   Map<String, PackageDefinition> allDefinitions = new HashMap<String, PackageDefinition>();
   if (md5Filenames == null) {
     return allDefinitions;
   }
   for (String md5Filename : md5Filenames) {
     File md5File = new File(distributionMPDir, md5Filename);
     if (!md5File.exists()) {
       // distribution file has been deleted
       continue;
     }
     ZipFile zipFile;
     try {
       zipFile = new ZipFile(md5File);
     } catch (ZipException e) {
       log.warn("Unzip error reading file " + md5File, e);
       continue;
     } catch (IOException e) {
       log.warn("Could not read file " + md5File, e);
       continue;
     }
     try {
       ZipEntry zipEntry = zipFile.getEntry("package.xml");
       InputStream in = zipFile.getInputStream(zipEntry);
       PackageDefinition pd = NuxeoConnectClient.getPackageUpdateService().loadPackage(in);
       allDefinitions.put(md5Filename, pd);
     } catch (Exception e) {
       log.error("Could not read package description", e);
       continue;
     } finally {
       try {
         zipFile.close();
       } catch (IOException e) {
         log.warn("Unexpected error closing file " + md5File, e);
       }
     }
   }
   return allDefinitions;
 }
 @Override
 public void activate(ComponentContext context) throws Exception {
   NuxeoConnectClient.setCallBackHolder(new NuxeoCallbackHolder());
 }
 public void pkgListAll() {
   log.info("All packages:");
   pkgList(NuxeoConnectClient.getPackageManager().listAllPackages());
 }
 protected String getRemotePackageIdFromName(String pkgName) {
   return getBestIdForNameInList(
       pkgName, NuxeoConnectClient.getPackageManager().listAllPackages());
 }
 public void refreshCache() {
   getPackageManager().flushCache();
   NuxeoConnectClient.getPackageManager().listAllPackages();
 }
 public PackageManager getPackageManager() {
   return NuxeoConnectClient.getPackageManager();
 }
  @SuppressWarnings("unchecked")
  public boolean pkgRequest(
      List<String> pkgsToAdd,
      List<String> pkgsToInstall,
      List<String> pkgsToUninstall,
      List<String> pkgsToRemove) {
    boolean cmdOk = true;
    // Add local files
    cmdOk = pkgAdd(pkgsToAdd);
    // Build solver request
    List<String> solverInstall = new ArrayList<String>();
    List<String> solverRemove = new ArrayList<String>();
    List<String> solverUpgrade = new ArrayList<String>();
    if (pkgsToInstall != null) {
      // If install request is a file name, add to cache and get the id
      List<String> namesOrIdsToInstall = new ArrayList<String>();
      for (String pkgToInstall : pkgsToInstall) {
        if (isLocalPackageFile(pkgToInstall)) {
          LocalPackage addedPkg = pkgAdd(pkgToInstall);
          if (addedPkg != null) {
            namesOrIdsToInstall.add(addedPkg.getId());
          } else {
            cmdOk = false;
          }
          // TODO: set flag to prefer local package
        } else {
          namesOrIdsToInstall.add(pkgToInstall);
        }
      }
      // Check whether we have new installs or upgrades
      for (String pkgToInstall : namesOrIdsToInstall) {
        Map<String, DownloadablePackage> allPackagesByID =
            NuxeoConnectClient.getPackageManager().getAllPackagesByID();
        DownloadablePackage pkg = allPackagesByID.get(pkgToInstall);
        if (pkg != null) {
          // This is a known ID
          if (isInstalledPackageName(pkg.getName())) {
            // The package is installed in another version
            solverUpgrade.add(pkgToInstall);
          } else {
            // The package isn't installed yet
            solverInstall.add(pkgToInstall);
          }
        } else {
          // This is a name (or a non-existing ID)
          String id = getInstalledPackageIdFromName(pkgToInstall);
          if (id != null) {
            // The package is installed in another version
            solverUpgrade.add(id);
          } else {
            // The package isn't installed yet
            solverInstall.add(pkgToInstall);
          }
        }
      }
    }
    if (pkgsToUninstall != null) {
      solverRemove.addAll(pkgsToUninstall);
    }
    if (pkgsToRemove != null) {
      // Add packages to remove to uninstall list
      solverRemove.addAll(pkgsToRemove);
    }
    if ((solverInstall.size() != 0) || (solverRemove.size() != 0) || (solverUpgrade.size() != 0)) {
      // Check whether we need to relax restriction to targetPlatform
      String requestPlatform = targetPlatform;
      List<String> requestPackages = new ArrayList<String>();
      requestPackages.addAll(solverInstall);
      requestPackages.addAll(solverRemove);
      requestPackages.addAll(solverUpgrade);
      try {
        String nonCompliantPkg =
            getPackageManager().getNonCompliant(requestPackages, targetPlatform);
        if (nonCompliantPkg != null) {
          requestPlatform = null;
          if ("ask".equalsIgnoreCase(relax)) {
            relax =
                readConsole(
                    "Package %s is not available on platform version %s.\n"
                        + "Do you want to relax the constraint (yes/no)? [no] ",
                    "no", nonCompliantPkg, targetPlatform);
          }

          if (Boolean.parseBoolean(relax)) {
            log.warn(
                String.format(
                    "Relax restriction to target platform %s because of package %s",
                    targetPlatform, nonCompliantPkg));
          } else {
            throw new PackageException(
                String.format(
                    "Package %s is not available on platform version %s (relax is not allowed)",
                    nonCompliantPkg, targetPlatform));
          }
        }
      } catch (PackageException e) {
        log.error(e);
        return false;
      }

      log.debug("solverInstall: " + solverInstall);
      log.debug("solverRemove: " + solverRemove);
      log.debug("solverUpgrade: " + solverUpgrade);
      DependencyResolution resolution =
          getPackageManager()
              .resolveDependencies(solverInstall, solverRemove, solverUpgrade, requestPlatform);
      log.info(resolution);
      if (resolution.isFailed()) {
        return false;
      }
      if (resolution.isEmpty()) {
        pkgRemove(pkgsToRemove);
        return cmdOk;
      }
      if ("ask".equalsIgnoreCase(accept)) {
        accept = readConsole("Do you want to continue (yes/no)? [yes] ", "yes");
      }
      if (!Boolean.parseBoolean(accept)) {
        log.warn("Exit");
        return false;
      }

      List<String> packageIdsToRemove = resolution.getOrderedPackageIdsToRemove();
      List<String> packageIdsToUpgrade = resolution.getUpgradePackageIds();
      List<String> packageIdsToInstall = resolution.getOrderedPackageIdsToInstall();
      List<String> packagesIdsToReInstall = new ArrayList<String>();

      // Download remote packages
      if (!downloadPackages(resolution.getDownloadPackageIds())) {
        log.error("Aborting packages change request");
        return false;
      }

      // Uninstall
      if (!packageIdsToUpgrade.isEmpty()) {
        // Add packages to upgrade to uninstall list
        // Don't use IDs to avoid downgrade instead of uninstall
        packageIdsToRemove.addAll(resolution.getLocalPackagesToUpgrade().keySet());
        DependencyResolution uninstallResolution =
            getPackageManager()
                .resolveDependencies(null, packageIdsToRemove, null, requestPlatform);
        log.debug("Sub-resolution (uninstall) " + uninstallResolution);
        if (uninstallResolution.isFailed()) {
          return false;
        }
        List<String> newPackageIdsToRemove = uninstallResolution.getOrderedPackageIdsToRemove();
        packagesIdsToReInstall = ListUtils.subtract(newPackageIdsToRemove, packageIdsToRemove);
        packagesIdsToReInstall.removeAll(packageIdsToUpgrade);
        packageIdsToRemove = newPackageIdsToRemove;
      }
      if (!pkgUninstall(packageIdsToRemove)) {
        return false;
      }

      // Install
      if (!packagesIdsToReInstall.isEmpty()) {
        // Add list of packages uninstalled because of upgrade
        packageIdsToInstall.addAll(packagesIdsToReInstall);
        DependencyResolution installResolution =
            getPackageManager()
                .resolveDependencies(packageIdsToInstall, null, null, requestPlatform);
        log.debug("Sub-resolution (install) " + installResolution);
        if (installResolution.isFailed()) {
          return false;
        }
        packageIdsToInstall = installResolution.getOrderedPackageIdsToInstall();
      }
      if (!pkgInstall(packageIdsToInstall)) {
        return false;
      }

      pkgRemove(pkgsToRemove);
    }
    return cmdOk;
  }