/** * Install a local package. * * @param pkgId Package ID or Name * @return The installed LocalPackage or null if failed */ public LocalPackage pkgInstall(String pkgId) { if (env.getProperty(LAUNCHER_CHANGED_PROPERTY, "false").equals("true")) { System.exit(LAUNCHER_CHANGED_EXIT_CODE); } CommandInfo cmdInfo = cset.newCommandInfo(CommandInfo.CMD_INSTALL); cmdInfo.param = pkgId; try { LocalPackage pkg = getLocalPackage(pkgId); if (pkg == null) { // We don't know this package, try to add it first pkg = pkgAdd(pkgId); } if (pkg == null) { // Nothing worked - can't find the package anywhere throw new PackageException("Package not found: " + pkgId); } pkgId = pkg.getId(); cmdInfo.param = pkgId; log.info("Installing " + pkgId); Task installTask = pkg.getInstallTask(); try { performTask(installTask); } catch (PackageException e) { installTask.rollback(); throw e; } // Refresh state pkg = service.getPackage(pkgId); newPackageInfo(cmdInfo, pkg); return pkg; } catch (Exception e) { log.error(String.format("Failed to install package: %s (%s)", pkgId, e.getMessage())); cmdInfo.exitCode = 1; cmdInfo.newMessage(e); return null; } }
/** * Uninstall a local package. The package is not removed from cache. * * @param pkgId Package ID or Name * @return The uninstalled LocalPackage or null if failed */ public LocalPackage pkgUninstall(String pkgId) { if (env.getProperty(LAUNCHER_CHANGED_PROPERTY, "false").equals("true")) { System.exit(LAUNCHER_CHANGED_EXIT_CODE); } CommandInfo cmdInfo = cset.newCommandInfo(CommandInfo.CMD_UNINSTALL); cmdInfo.param = pkgId; try { LocalPackage pkg = service.getPackage(pkgId); if (pkg == null) { // Check whether this is the name of an installed package String realPkgId = getInstalledPackageIdFromName(pkgId); if (realPkgId != null) { pkgId = realPkgId; pkg = service.getPackage(realPkgId); } } if (pkg == null) { throw new PackageException("Package not found: " + pkgId); } log.info("Uninstalling " + pkgId); Task uninstallTask = pkg.getUninstallTask(); try { performTask(uninstallTask); } catch (PackageException e) { uninstallTask.rollback(); throw e; } // Refresh state pkg = service.getPackage(pkgId); newPackageInfo(cmdInfo, pkg); return pkg; } catch (Exception e) { log.error("Failed to uninstall package: " + pkgId, e); cmdInfo.exitCode = 1; return null; } }