Search in sources :

Example 1 with UnitCategory

use of org.netbeans.modules.autoupdate.ui.UnitCategory in project netbeans-rcp-lite by outersky.

the class AutoupdateCheckScheduler method checkUpdateElements.

public static Collection<UpdateElement> checkUpdateElements(OperationType type, Collection<String> problems, boolean forceReload, Collection<UpdateElement> visibleUpdateElement) {
    // check
    err.log(Level.FINEST, "Check UpdateElements for " + type);
    if (forceReload) {
        // NOI18N
        ProgressHandle dummyHandler = ProgressHandleFactory.createHandle("dummy-check-for-updates");
        ProgressHandleFactory.createProgressComponent(dummyHandler);
        dummyHandler.start();
        Collection<String> updateProblems = refreshUpdateCenters(dummyHandler);
        if (problems != null && updateProblems != null) {
            problems.addAll(updateProblems);
        }
    }
    List<UpdateUnit> units = UpdateManager.getDefault().getUpdateUnits(UpdateManager.TYPE.MODULE);
    boolean handleUpdates = OperationType.UPDATE == type;
    Collection<UnitCategory> cats = handleUpdates ? Utilities.makeUpdateCategories(units, false) : Utilities.makeAvailableCategories(units, false);
    if (cats == null || cats.isEmpty()) {
        err.log(Level.FINE, "findUpdateElements(" + type + ") doesn't find any elements.");
        return null;
    }
    // 1. try to install all available updates
    Collection<UpdateElement> updates = new HashSet<UpdateElement>();
    boolean somePendingElements = false;
    boolean someBrokenDependencies = false;
    OperationContainer<InstallSupport> container = handleUpdates ? OperationContainer.createForUpdate() : OperationContainer.createForInstall();
    List<UpdateElement> elements = new ArrayList<UpdateElement>();
    for (UnitCategory cat : cats) {
        for (Unit u : cat.getUnits()) {
            if (u instanceof Unit.Available) {
                elements.add(((Unit.Available) u).getRelevantElement());
            } else if (u instanceof Unit.CompoundUpdate) {
                for (UpdateUnit uu : ((Unit.CompoundUpdate) u).getUpdateUnits()) {
                    elements.add(uu.getAvailableUpdates().get(0));
                }
                if (((Unit.CompoundUpdate) u).getRealUpdate() != null) {
                    elements.add(((Unit.CompoundUpdate) u).getRealUpdate());
                }
            } else if (u instanceof Unit.Update) {
                elements.add(((Unit.Update) u).getRelevantElement());
            }
        }
    }
    for (UpdateElement element : elements) {
        if (!somePendingElements) {
            if (container.canBeAdded(element.getUpdateUnit(), element)) {
                OperationInfo<InstallSupport> operationInfo = container.add(element);
                if (operationInfo == null) {
                    updates.add(element);
                    continue;
                }
                Collection<UpdateElement> reqs = new HashSet<UpdateElement>(operationInfo.getRequiredElements());
                Collection<String> brokenDeps = operationInfo.getBrokenDependencies();
                if (!brokenDeps.isEmpty()) {
                    err.log(Level.WARNING, // NOI18N
                    "Plugin " + operationInfo + " cannot be installed because some dependencies cannot be satisfied: " + // NOI18N
                    brokenDeps);
                    someBrokenDependencies = true;
                    break;
                }
                for (UpdateElement tmpEl : reqs) {
                    if (tmpEl.getUpdateUnit().isPending()) {
                        err.log(Level.WARNING, // NOI18N
                        "Plugin " + operationInfo.getUpdateElement() + " depends on " + tmpEl + " in pending state.");
                        somePendingElements = true;
                        updates = Collections.emptySet();
                        break;
                    }
                }
                if (!somePendingElements) {
                    container.add(reqs);
                    updates.add(element);
                }
            }
        }
    }
    if (!somePendingElements && !container.listInvalid().isEmpty()) {
        err.log(Level.WARNING, // NOI18N
        "Plugins " + updates + " cannot be installed, Install Container contains invalid elements " + // NOI18N
        container.listInvalid());
    }
    if (!somePendingElements && someBrokenDependencies) {
        // 2. if some problem then try one by one
        updates = new HashSet<UpdateElement>();
        for (UpdateElement element : elements) {
            OperationContainer<InstallSupport> oc = handleUpdates ? OperationContainer.createForUpdate() : OperationContainer.createForInstall();
            UpdateUnit unit = element.getUpdateUnit();
            if (oc.canBeAdded(unit, element)) {
                OperationInfo<InstallSupport> operationInfo = oc.add(element);
                if (operationInfo == null) {
                    updates.add(element);
                    continue;
                }
                boolean skip = false;
                Collection<UpdateElement> reqs = new HashSet<UpdateElement>(operationInfo.getRequiredElements());
                for (UpdateElement tmpEl : reqs) {
                    if (tmpEl.getUpdateUnit().isPending()) {
                        err.log(Level.WARNING, // NOI18N
                        "Plugin " + element + " depends on " + tmpEl + " in pending state.");
                        skip = true;
                    }
                }
                if (skip) {
                    continue;
                }
                oc.add(reqs);
                Collection<String> brokenDeps = new HashSet<String>();
                for (OperationInfo<InstallSupport> info : oc.listAll()) {
                    brokenDeps.addAll(info.getBrokenDependencies());
                }
                if (brokenDeps.isEmpty() && oc.listInvalid().isEmpty()) {
                    updates.add(element);
                } else {
                    oc.removeAll();
                    if (!brokenDeps.isEmpty()) {
                        err.log(Level.WARNING, // NOI18N
                        "Plugin " + element + " cannot be installed because some dependencies cannot be satisfied: " + // NOI18N
                        brokenDeps);
                    } else {
                        err.log(Level.WARNING, // NOI18N
                        "Plugin " + element + " cannot be installed, Install Container contains invalid elements " + // NOI18N
                        oc.listInvalid());
                    }
                }
            }
        }
    }
    // if any then notify updates
    if (visibleUpdateElement == null) {
        err.log(Level.FINE, "findUpdateElements(" + type + ") returns " + updates.size() + " elements.");
        return updates;
    } else {
        for (UnitCategory cat : cats) {
            for (Unit u : cat.getUnits()) {
                assert u instanceof Unit.Update : u + " has to be instanceof Unit.Update";
                if (u instanceof Unit.Update) {
                    visibleUpdateElement.add(((Unit.Update) u).getRelevantElement());
                }
            }
        }
        err.log(Level.FINE, "findUpdateElements(" + type + ") returns " + visibleUpdateElement.size() + " visible elements (" + updates.size() + " in all)");
        return updates;
    }
}
Also used : InstallSupport(org.netbeans.api.autoupdate.InstallSupport) UpdateUnit(org.netbeans.api.autoupdate.UpdateUnit) UpdateElement(org.netbeans.api.autoupdate.UpdateElement) ArrayList(java.util.ArrayList) UpdateUnit(org.netbeans.api.autoupdate.UpdateUnit) LazyUnit(org.netbeans.modules.autoupdate.ui.wizards.LazyInstallUnitWizardIterator.LazyUnit) Unit(org.netbeans.modules.autoupdate.ui.Unit) ProgressHandle(org.netbeans.api.progress.ProgressHandle) UnitCategory(org.netbeans.modules.autoupdate.ui.UnitCategory) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 InstallSupport (org.netbeans.api.autoupdate.InstallSupport)1 UpdateElement (org.netbeans.api.autoupdate.UpdateElement)1 UpdateUnit (org.netbeans.api.autoupdate.UpdateUnit)1 ProgressHandle (org.netbeans.api.progress.ProgressHandle)1 Unit (org.netbeans.modules.autoupdate.ui.Unit)1 UnitCategory (org.netbeans.modules.autoupdate.ui.UnitCategory)1 LazyUnit (org.netbeans.modules.autoupdate.ui.wizards.LazyInstallUnitWizardIterator.LazyUnit)1