KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > resolver > Eclipse30SelectionPolicy


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.internal.resolver;
13
14 import java.util.*;
15 import org.eclipse.core.internal.dependencies.*;
16
17 /**
18  * This is a variant of Eclipse 2.1 resolution policy that takes into account the current state
19  * of the element in a sense that resolved elements tend to stay resolved (the "least perturbation mode").
20  */

21 public class Eclipse30SelectionPolicy implements ISelectionPolicy {
22     public Set selectMultiple(ElementSet elementSet) {
23         Set selected = new HashSet();
24         // pre-select all elements that were previously resolved and are satisfied
25
Collection previouslyResolvedSet = elementSet.getResolved();
26         for (Iterator iter = previouslyResolvedSet.iterator(); iter.hasNext();) {
27             Element previouslyResolved = (Element) iter.next();
28             // if the previously resolved element is satisfied, select it
29
if (elementSet.getSatisfied().contains(previouslyResolved))
30                 selected.add(previouslyResolved);
31         }
32         // may want to select others
33
for (Iterator requiringIter = elementSet.getRequiring().iterator(); requiringIter.hasNext();) {
34             ElementSet requiringNode = (ElementSet) requiringIter.next();
35             Collection requiringNodeSelectedVersions = requiringNode.getSelected();
36             // loop through the selected versions (one if it is a non-library plug-in) of this requiring plug-in
37
for (Iterator requiringVersionsIter = requiringNodeSelectedVersions.iterator(); requiringVersionsIter.hasNext();) {
38                 Element requiringSelectedVersion = (Element) requiringVersionsIter.next();
39                 // the selected version may not require this element set (but it still can be selected)
40
Dependency requiringSelectedVersionDependency = requiringSelectedVersion.getDependency(elementSet.getId());
41                 if (requiringSelectedVersionDependency == null)
42                     continue;
43                 // find the best version for this pre-requisite
44
Element bestVersion = null;
45                 for (Iterator satisfiedIter = elementSet.getSatisfied().iterator(); satisfiedIter.hasNext();) {
46                     Element satisfiedVersion = (Element) satisfiedIter.next();
47                     boolean satisfiesDependency = requiringSelectedVersionDependency.getMatchRule().isSatisfied(requiringSelectedVersionDependency.getUserObject(), satisfiedVersion.getVersionId());
48                     if (satisfiesDependency) {
49                         boolean betterThanBest = bestVersion == null || elementSet.getSystem().compare(satisfiedVersion.getVersionId(), bestVersion.getVersionId()) > 0;
50                         if (betterThanBest)
51                             bestVersion = satisfiedVersion;
52                     }
53                 }
54                 if (bestVersion != null)
55                     selected.add(bestVersion);
56             }
57         }
58         // if none of the versions are required (or satisfy any selected re-requisites), pick the highest
59
if (selected.isEmpty()) {
60             Element bestVersion = null;
61             for (Iterator satisfiedIter = elementSet.getSatisfied().iterator(); satisfiedIter.hasNext();) {
62                 Element satisfiedVersion = (Element) satisfiedIter.next();
63                 boolean betterThanBest = bestVersion == null || elementSet.getSystem().compare(satisfiedVersion.getVersionId(), bestVersion.getVersionId()) > 0;
64                 if (betterThanBest)
65                     bestVersion = satisfiedVersion;
66             }
67             selected = Collections.singleton(bestVersion);
68         }
69         return selected;
70     }
71
72     public Element selectSingle(ElementSet elementSet) {
73         Collection previouslyResolvedSet = elementSet.getResolved();
74         // we already have one element resolved
75
if (!previouslyResolvedSet.isEmpty()) {
76             // TODO: need a cheaper way to obtain the single element
77
Element previouslyResolved = (Element) previouslyResolvedSet.iterator().next();
78             // if it is satisfied, just return it
79
if (elementSet.getSatisfied().contains(previouslyResolved))
80                 return previouslyResolved;
81         }
82
83         // none of its versions are required by other element sets - so just pick the highest
84
if (elementSet.getRequiring().isEmpty()) {
85             // otherwise, pick the highest version
86
Element highest = null;
87             for (Iterator satisfiedIter = elementSet.getSatisfied().iterator(); satisfiedIter.hasNext();) {
88                 Element satisfiedVersion = (Element) satisfiedIter.next();
89                 if (highest == null || elementSet.getSystem().compare(satisfiedVersion.getVersionId(), highest.getVersionId()) > 0)
90                     highest = satisfiedVersion;
91             }
92             return highest;
93         }
94
95         // let's pick the highest that satisfies all or the highest required
96
Element highest = null;
97         int highestStatus = 0; // 0 - not required, 1 - satisfy some, 2 - satisfy all mandatory, 3 - satisfy all
98
for (Iterator satisfiedIter = elementSet.getSatisfied().iterator(); satisfiedIter.hasNext();) {
99             boolean satisfiesAllMandatory = true;
100             boolean satisfiesAll = true;
101             boolean isRequired = false;
102             Element satisfiedVersion = (Element) satisfiedIter.next();
103             for (Iterator requiringIter = elementSet.getRequiring().iterator(); requiringIter.hasNext();) {
104                 ElementSet requiringNode = (ElementSet) requiringIter.next();
105                 Collection requiringNodeSelectedVersions = requiringNode.getSelected();
106                 // loop through the selected versions (one if it is a non-library plug-in) of this requiring plug-in
107
for (Iterator requiringVersionsIter = requiringNodeSelectedVersions.iterator(); requiringVersionsIter.hasNext();) {
108                     Element requiringSelectedVersion = (Element) requiringVersionsIter.next();
109                     // the selected version may not require this element set (but it still can be selected)
110
Dependency requiringSelectedVersionDep = requiringSelectedVersion.getDependency(elementSet.getId());
111                     if (requiringSelectedVersionDep != null) {
112                         boolean satisfiesDependency = requiringSelectedVersionDep.getMatchRule().isSatisfied(requiringSelectedVersionDep.getUserObject(), satisfiedVersion.getVersionId());
113                         isRequired |= satisfiesDependency;
114                         satisfiesAll &= satisfiesDependency;
115                         satisfiesAllMandatory &= (satisfiesDependency | requiringSelectedVersionDep.isOptional());
116                     }
117                 }
118             }
119             int status = satisfiesAll ? 3 : (satisfiesAllMandatory ? 2 : (isRequired ? 1 : 0));
120             boolean higherThanHighest = highest == null || elementSet.getSystem().compare(satisfiedVersion.getVersionId(), highest.getVersionId()) > 0;
121             if (status > highestStatus || (status == highestStatus && higherThanHighest)) {
122                 highest = satisfiedVersion;
123                 highestStatus = status;
124             }
125         }
126         return highest;
127     }
128
129 }
Popular Tags