Debian Codegraph @

bundles/org.eclipse.equinox.p2.operations/src/org/eclipse/equinox/p2/operations/UpdateOperation.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2009, 2018 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * Sonatype, Inc. - ongoing development
14 ******************************************************************************/
15 package org.eclipse.equinox.p2.operations;
16
17 import java.util.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.equinox.internal.p2.director.ProfileChangeRequest;
21 import org.eclipse.equinox.internal.p2.operations.*;
22 import org.eclipse.equinox.p2.engine.IProfile;
23 import org.eclipse.equinox.p2.engine.query.UserVisibleRootQuery;
24 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
25 import org.eclipse.equinox.p2.planner.ProfileInclusionRules;
26 import org.eclipse.equinox.p2.query.*;
27
28 /**
29 * An UpdateOperation describes an operation that updates {@link IInstallableUnit}s in
30 * a profile.
31 *
32 * The following snippet shows how one might use an UpdateOperation to check for updates
33 * to the profile and then install them in the background.
34 *
35 * <pre>
36 * UpdateOperation op = new UpdateOperation(session);
37 * IStatus result = op.resolveModal(monitor);
38 * if (result.isOK()) {
39 * op.getProvisioningJob(monitor).schedule();
40 * }
41 * </pre>
42 *
43 * The life cycle of an UpdateOperation is different than that of the other
44 * operations. Since assembling the list of possible updates may be costly,
45 * clients should not have to create a new update operation if the desired updates
46 * to be applied need to change. In this case, the client can set a new set of
47 * chosen updates on the update operation and resolve again.
48 *
49 * <pre>
50 * UpdateOperation op = new UpdateOperation(session);
51 * IStatus result = op.resolveModal(monitor);
52 * if (result.isOK()) {
53 * op.getProvisioningJob(monitor).schedule();
54 * } else if (result.getSeverity() == IStatus.ERROR) {
55 * Update [] chosenUpdates = letUserPickFrom(op.getPossibleUpdates());
56 * op.setSelectedUpdates(chosenUpdates);
57 * IStatus result = op.resolveModal(monitor);
58 * }
59 * </pre>
60 *
61 * @noextend This class is not intended to be subclassed by clients.
62 * @since 2.0
63 */
64 public class UpdateOperation extends ProfileChangeOperation {
65
66 /**
67 * A status code used to indicate that there were no updates found when
68 * looking for updates.
69 */
70 public static final int STATUS_NOTHING_TO_UPDATE = IStatusCodes.NOTHING_TO_UPDATE;
71
72 private Collection<IInstallableUnit> iusToUpdate;
73 private HashMap<IInstallableUnit, List<Update>> possibleUpdatesByIU = new HashMap<>();
74 private List<Update> defaultUpdates;
75
76 /**
77 * Create an update operation on the specified provisioning session that updates
78 * the specified IInstallableUnits. Unless otherwise specified, the operation will
79 * be associated with the currently running profile.
80 *
81 * @param session the session to use for obtaining provisioning services
82 * @param toBeUpdated the IInstallableUnits to be updated.
83 */
84 public UpdateOperation(ProvisioningSession session, Collection<IInstallableUnit> toBeUpdated) {
85 super(session);
86 this.iusToUpdate = toBeUpdated;
87 }
88
89 /**
90 * Create an update operation that will update all of the user-visible installable
91 * units in the profile (the profile roots).
92 *
93 * @param session the session providing the provisioning services
94 */
95 public UpdateOperation(ProvisioningSession session) {
96 this(session, null);
97 }
98
99 /**
100 * Set the updates that should be selected from the set of available updates.
101 * If the selected updates are not specified, then the latest available update
102 * for each IInstallableUnit with updates will be chosen.
103 *
104 * @param defaultUpdates the updates that should be chosen from all of the available
105 * updates.
106 */
107 public void setSelectedUpdates(Update[] defaultUpdates) {
108 this.defaultUpdates = new ArrayList<>(Arrays.asList(defaultUpdates));
109 }
110
111 /**
112 * Get the updates that have been selected from the set of available updates.
113 * If none have been specified by the client, then the latest available update
114 * for each IInstallableUnit with updates will be chosen.
115 *
116 * @return the updates that should be chosen from all of the available updates
117 */
118 public Update[] getSelectedUpdates() {
119 if (defaultUpdates == null)
120 return new Update[0];
121 return defaultUpdates.toArray(new Update[defaultUpdates.size()]);
122 }
123
124 /**
125 * Get the list of all possible updates. This list may include multiple versions
126 * of updates for the same IInstallableUnit, as well as patches to the IInstallableUnit.
127 *
128 * @return an array of all possible updates
129 */
130 public Update[] getPossibleUpdates() {
131 ArrayList<Update> all = new ArrayList<>();
132 for (List<Update> updates : possibleUpdatesByIU.values())
133 all.addAll(updates);
134 return all.toArray(new Update[all.size()]);
135 }
136
137 private Update[] updatesFor(IInstallableUnit iu, IProfile profile, IProgressMonitor monitor) {
138 List<Update> updates;
139 if (possibleUpdatesByIU.containsKey(iu)) {
140 // We've already looked them up in the planner, use the cache
141 updates = possibleUpdatesByIU.get(iu);
142 } else {
143 // We must consult the planner
144 IQueryResult<IInstallableUnit> replacements = session.getPlanner().updatesFor(iu, context, monitor);
145 updates = new ArrayList<>();
146 for (IInstallableUnit replacementIU : replacements) {
147 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=273967
148 // In the case of patches, it's possible that a patch is returned as an
149 // available update
150 // even though it is already installed, because we are querying each IU for
151 // updates individually.
152 // For now, we ignore any proposed update that is already installed.
153 IQueryResult<IInstallableUnit> alreadyInstalled = profile.query(QueryUtil.createIUQuery(replacementIU), null);
154 if (alreadyInstalled.isEmpty()) {
155 Update update = new Update(iu, replacementIU);
156 updates.add(update);
157 }
158 }
159 possibleUpdatesByIU.put(iu, updates);
160 }
161 return updates.toArray(new Update[updates.size()]);
162 }
163
164 @Override
165 protected void computeProfileChangeRequest(MultiStatus status, IProgressMonitor monitor) {
166 // Here we create a profile change request by finding the latest version available for any replacement, unless
167 // otherwise specified in the selections.
168 // We have to consider the scenario where the only updates available are patches, in which case the original
169 // IU should not be removed as part of the update.
170 Set<IInstallableUnit> toBeUpdated = new HashSet<>();
171 HashSet<Update> elementsToPlan = new HashSet<>();
172 boolean selectionSpecified = defaultUpdates != null;
173 IProfile profile = session.getProfileRegistry().getProfile(profileId);
174 if (profile == null)
175 return;
176
177 SubMonitor sub = SubMonitor.convert(monitor, Messages.UpdateOperation_ProfileChangeRequestProgress, 100 * iusToUpdate.size());
178 for (IInstallableUnit iuToUpdate : iusToUpdate) {
179 SubMonitor iuMon = sub.newChild(100);
180 Update[] updates = updatesFor(iuToUpdate, profile, iuMon);
181 for (Update update : updates) {
182 toBeUpdated.add(iuToUpdate);
183 if (defaultUpdates != null && defaultUpdates.contains(update)) {
184 elementsToPlan.add(update);
185 }
186 }
187 if (!selectionSpecified) {
188 // If no selection was specified, we must figure out the latest version to apply.
189 // The rules are that a true update will always win over a patch, but if only
190 // patches are available, they should all be selected.
191 // We first gather the latest versions of everything proposed.
192 // Patches are keyed by their id because they are unique and should not be compared to
193 // each other. Updates are keyed by the IU they are updating so we can compare the
194 // versions and select the latest one
195 HashMap<String, Update> latestVersions = new HashMap<>();
196 boolean foundUpdate = false;
197 boolean foundPatch = false;
198 for (Update update : updates) {
199 String key;
200 if (QueryUtil.isPatch(update.replacement)) {
201 foundPatch = true;
202 key = update.replacement.getId();
203 } else {
204 foundUpdate = true;
205 key = update.toUpdate.getId();
206 }
207 Update latestUpdate = latestVersions.get(key);
208 IInstallableUnit latestIU = latestUpdate == null ? null : latestUpdate.replacement;
209 if (latestIU == null || update.replacement.getVersion().compareTo(latestIU.getVersion()) > 0) {
210 latestVersions.put(key, update);
211 }
212 }
213 // If there is a true update available, ignore any patches found
214 // Patches are keyed by their own id
215 if (foundPatch && foundUpdate) {
216 Set<String> keys = new HashSet<>();
217 keys.addAll(latestVersions.keySet());
218 for (String id : keys) {
219 // Get rid of things keyed by a different id. We've already made sure
220 // that updates with a different id are keyed under the original id
221 if (!id.equals(iuToUpdate.getId())) {
222 latestVersions.remove(id);
223 }
224 }
225 }
226 elementsToPlan.addAll(latestVersions.values());
227 }
228 sub.worked(100);
229 }
230
231 if (toBeUpdated.size() <= 0 || elementsToPlan.isEmpty()) {
232 sub.done();
233 status.add(PlanAnalyzer.getStatus(IStatusCodes.NOTHING_TO_UPDATE, null));
234 return;
235 }
236
237 request = ProfileChangeRequest.createByProfileId(session.getProvisioningAgent(), profileId);
238 for (Update update : elementsToPlan) {
239 IInstallableUnit theUpdate = update.replacement;
240 if (defaultUpdates == null) {
241 defaultUpdates = new ArrayList<>();
242 defaultUpdates.add(update);
243 } else {
244 if (!defaultUpdates.contains(update))
245 defaultUpdates.add(update);
246 }
247 request.add(theUpdate);
248 request.setInstallableUnitProfileProperty(theUpdate, IProfile.PROP_PROFILE_ROOT_IU, Boolean.toString(true));
249 if (QueryUtil.isPatch(theUpdate)) {
250 request.setInstallableUnitInclusionRules(theUpdate, ProfileInclusionRules.createOptionalInclusionRule(theUpdate));
251 } else {
252 request.remove(update.toUpdate);
253 }
254
255 }
256 sub.done();
257 }
258
259 @Override
260 protected String getProvisioningJobName() {
261 return Messages.UpdateOperation_UpdateJobName;
262 }
263
264 @Override
265 protected String getResolveJobName() {
266 return Messages.UpdateOperation_ResolveJobName;
267 }
268
269 @Override
270 protected void prepareToResolve() {
271 super.prepareToResolve();
272 if (iusToUpdate == null) {
273 iusToUpdate = getInstalledIUs();
274 }
275 }
276
277 /*
278 * Get the IInstallable units for the specified profile
279 *
280 * @param profileId the profile in question
281 * @param all <code>true</code> if all IInstallableUnits in the profile should
282 * be returned, <code>false</code> only those IInstallableUnits marked as (user visible) roots
283 * should be returned.
284 *
285 * @return an array of IInstallableUnits installed in the profile.
286 */
287 private Collection<IInstallableUnit> getInstalledIUs() {
288 IProfile profile = session.getProfileRegistry().getProfile(profileId);
289 if (profile == null)
290 return Collections.emptyList();
291 IQuery<IInstallableUnit> query = new UserVisibleRootQuery();
292 IQueryResult<IInstallableUnit> queryResult = profile.query(query, null);
293 return queryResult.toUnmodifiableSet();
294 }
295
296 /*
297 * Overridden to delay computation of the profile change request until the resolution
298 * occurs. This is done because computing the request is expensive (it involves searching
299 * for updates).
300 */
301 @Override
302 void makeResolveJob(IProgressMonitor monitor) {
303 // throw away any previous requests
304 request = null;
305 noChangeRequest = PlanAnalyzer.getProfileChangeAlteredStatus();
306 // the requestHolder is a hack to work around the fact that there is no public API
307 // for the resolution job to get the request from the operation after it has been
308 // computed.
309 final ProfileChangeRequest[] requestHolder = new ProfileChangeRequest[1];
310 job = new SearchForUpdatesResolutionJob(getResolveJobName(), session, profileId, request, getFirstPassProvisioningContext(), getSecondPassEvaluator(), noChangeRequest, mon -> {
311 // We only check for other jobs running if this job is *not* scheduled
312 if (job.getState() == Job.NONE && session.hasScheduledOperationsFor(profileId)) {
313 noChangeRequest.add(PlanAnalyzer.getStatus(IStatusCodes.OPERATION_ALREADY_IN_PROGRESS, null));
314 } else {
315 computeProfileChangeRequest(noChangeRequest, mon);
316 requestHolder[0] = UpdateOperation.this.request;
317 }
318 }, requestHolder, this);
319 }
320
321 /*
322 * Overridden because our resolution job life cycle is different. We have a job
323 * before we've computed the profile change request, so we must ensure that we
324 * have already computed the profile change request.
325 *
326 */
327 @Override
328 public boolean hasResolved() {
329 return request != null && super.hasResolved();
330 }
331 }