}
private static void installPackageFromRepository(String packageName, String version,
boolean force) throws Exception {
Package toInstall = null;
try {
toInstall = getRepositoryPackageInfo(packageName, version);
} catch (Exception ex) {
System.err.println("[WekaPackageManager] Package " + packageName + " at version " + version
+ " doesn't seem to exist!");
//System.exit(1);
return;
}
// First check to see if this package is compatible with the base system
if (!force) {
boolean ok = toInstall.isCompatibleBaseSystem();
if (!ok) {
List<Dependency> baseSysDep = toInstall.getBaseSystemDependency();
StringBuffer depList = new StringBuffer();
for (Dependency bd : baseSysDep) {
depList.append(bd.getTarget().toString() + " ");
}
System.err.println("Can't install package " + packageName
+ " because it requires " + depList.toString());
return;
}
// true if this package is already installed. In which case we need to check
// if changing this package will impact other already installed packages
boolean upOrDowngrading = false;
if (toInstall.isInstalled()) {
Package installedVersion = getInstalledPackageInfo(packageName);
if (!toInstall.equals(installedVersion)) {
System.out.println("Package " + packageName + "[" + installedVersion
+ "] is already installed. Replace with " + toInstall +" [y/n]?");
String response = queryUser();
if (response != null && (response.equalsIgnoreCase("n") ||
response.equalsIgnoreCase("no"))) {
return; // bail out here
}
upOrDowngrading = true;
} else {
System.out.println("Package " + packageName + "[" + installedVersion
+ "] is already installed. Install again [y/n]?");
String response = queryUser();
if (response != null && (response.equalsIgnoreCase("n") ||
response.equalsIgnoreCase("no"))) {
return; // bail out here
}
}
}
// Now get a full list of dependencies for this package and
// check for any conflicts
Map<String, List<Dependency>> conflicts = new HashMap<String, List<Dependency>>();
List<Dependency> dependencies = getAllDependenciesForPackage(toInstall, conflicts);
if (conflicts.size() > 0) {
System.err.println("Package " + packageName + " requires the following packages:\n");
Iterator<Dependency> depI = dependencies.iterator();
while (depI.hasNext()) {
Dependency d = depI.next();
System.err.println("\t" + d);
}
System.err.println("\nThere are conflicting dependencies:\n");
Set<String> pNames = conflicts.keySet();
Iterator<String> pNameI = pNames.iterator();
while (pNameI.hasNext()) {
String pName = pNameI.next();
System.err.println("Conflicts for " + pName);
List<Dependency> confsForPackage = conflicts.get(pName);
Iterator<Dependency> confs = confsForPackage.iterator();
while (confs.hasNext()) {
Dependency problem = confs.next();
System.err.println("\t" + problem);
}
}
System.err.println("Unable to continue with installation.");
return; // bail out here.
}
// Next check all dependencies against what is installed and
// inform the user about which installed packages will be altered. Also
// build the list of only those packages that need to be installed or
// upgraded (excluding those that are already installed and are OK).
List<PackageConstraint> needsUpgrade = new ArrayList<PackageConstraint>();
List<Package> finalListToInstall = new ArrayList<Package>();
Iterator<Dependency> depI = dependencies.iterator();
while (depI.hasNext()) {
Dependency toCheck = depI.next();
if (toCheck.getTarget().getPackage().isInstalled()) {
String toCheckName =
toCheck.getTarget().getPackage().
getPackageMetaDataElement("PackageName").toString();
Package installedVersion = PACKAGE_MANAGER.getInstalledPackageInfo(toCheckName);
if (!toCheck.getTarget().checkConstraint(installedVersion)) {
needsUpgrade.add(toCheck.getTarget());
Package mostRecent = toCheck.getTarget().getPackage();
if (toCheck.getTarget() instanceof
org.pentaho.packageManagement.VersionPackageConstraint) {
mostRecent =
WekaPackageManager.mostRecentVersionWithRespectToConstraint(toCheck.getTarget());
}
finalListToInstall.add(mostRecent);
}
} else {
Package mostRecent = toCheck.getTarget().getPackage();
if (toCheck.getTarget() instanceof
org.pentaho.packageManagement.VersionPackageConstraint) {
mostRecent =
WekaPackageManager.mostRecentVersionWithRespectToConstraint(toCheck.getTarget());
}
finalListToInstall.add(mostRecent);
}
}
if (needsUpgrade.size() > 0) {
System.out.println("The following packages will be upgraded in order to install "
+ packageName);
Iterator<PackageConstraint> upI = needsUpgrade.iterator();
while (upI.hasNext()) {
PackageConstraint tempC = upI.next();
System.out.println("\t" + tempC);
}
System.out.print("\nOK to continue [y/n]? > ");
String response = queryUser();
if (response != null && (response.equalsIgnoreCase("n") ||
response.equalsIgnoreCase("no"))) {
return; // bail out here
}
// now take a look at the other installed packages and see if
// any would have a problem when these ones are upgraded
boolean conflictsAfterUpgrade = false;
List<Package> installed = getInstalledPackages();
List<Package> toUpgrade = new ArrayList<Package>();
upI = needsUpgrade.iterator();
while (upI.hasNext()) {
toUpgrade.add(upI.next().getPackage());
}
// add the actual package the user is wanting to install if it
// is going to be an up/downgrade rather than a first install since
// other installed packages may depend on the currently installed version
// and thus could be affected after the up/downgrade
toUpgrade.add(toInstall);
for (int i = 0; i < installed.size(); i++) {
Package tempP = installed.get(i);
String tempPName = tempP.getName();
boolean checkIt = true;
for (int j = 0; j < needsUpgrade.size(); j++) {
if (tempPName.equals(needsUpgrade.get(j).getPackage().getName())) {
checkIt = false;
break;
}
}
if (checkIt) {
List<Dependency> problem = tempP.getIncompatibleDependencies(toUpgrade);
if (problem.size() > 0) {
conflictsAfterUpgrade = true;
System.err.println("Package " + tempP.getName() + " will have a compatibility" +
"problem with the following packages after upgrading them:");
Iterator<Dependency> dI = problem.iterator();
while (dI.hasNext()) {
System.err.println("\t" + dI.next().getTarget().getPackage());
}