Debian Codegraph @

bundles/org.eclipse.equinox.p2.operations/src/org/eclipse/equinox/internal/p2/operations/PlanAnalyzer.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2008, 2016 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.internal.p2.operations;
16
17 import java.util.Collection;
18 import java.util.Map.Entry;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.equinox.internal.p2.director.ProfileChangeRequest;
21 import org.eclipse.equinox.internal.provisional.p2.director.PlannerStatus;
22 import org.eclipse.equinox.internal.provisional.p2.director.RequestStatus;
23 import org.eclipse.equinox.p2.engine.IProvisioningPlan;
24 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
25 import org.eclipse.osgi.util.NLS;
26
27 /**
28 * This class analyzes a profile change request and the resultant provisioning plan,
29 * and reports problems in a way that can be communicated to a user.
30 *
31 * @since 3.5
32 */
33 public class PlanAnalyzer {
34
35 public static IStatus getStatus(int statusCode, IInstallableUnit affectedIU) {
36 switch (statusCode) {
37 case IStatusCodes.NOTHING_TO_UPDATE :
38 return new Status(IStatus.INFO, Constants.BUNDLE_ID, statusCode, Messages.PlanAnalyzer_NoUpdates, null);
39 case IStatusCodes.PROFILE_CHANGE_ALTERED :
40 return new MultiStatus(Constants.BUNDLE_ID, statusCode, Messages.PlanAnalyzer_RequestAltered, null);
41 case IStatusCodes.ALTERED_IMPLIED_UPDATE :
42 return new Status(IStatus.INFO, Constants.BUNDLE_ID, statusCode, NLS.bind(Messages.PlanAnalyzer_ImpliedUpdate, getIUString(affectedIU)), null);
43 case IStatusCodes.ALTERED_IGNORED_IMPLIED_UPDATE :
44 return new Status(IStatus.WARNING, Constants.BUNDLE_ID, statusCode, NLS.bind(Messages.PlanAnalyzer_LockedImpliedUpdate0, getIUString(affectedIU)), null);
45 case IStatusCodes.ALTERED_IGNORED_IMPLIED_DOWNGRADE :
46 return new Status(IStatus.WARNING, Constants.BUNDLE_ID, statusCode, NLS.bind(Messages.PlanAnalyzer_IgnoringImpliedDowngrade, getIUString(affectedIU)), null);
47 case IStatusCodes.ALTERED_IGNORED_ALREADY_INSTALLED :
48 return new Status(IStatus.WARNING, Constants.BUNDLE_ID, statusCode, NLS.bind(Messages.PlanAnalyzer_AlreadyInstalled, getIUString(affectedIU)), null);
49 case IStatusCodes.ALTERED_PARTIAL_INSTALL :
50 return new Status(IStatus.INFO, Constants.BUNDLE_ID, statusCode, NLS.bind(Messages.PlanAnalyzer_PartialInstall, getIUString(affectedIU)), null);
51 case IStatusCodes.ALTERED_PARTIAL_UNINSTALL :
52 return new Status(IStatus.INFO, Constants.BUNDLE_ID, statusCode, NLS.bind(Messages.PlanAnalyzer_PartialUninstall, getIUString(affectedIU)), null);
53 case IStatusCodes.UNEXPECTED_NOTHING_TO_DO :
54 return new Status(IStatus.ERROR, Constants.BUNDLE_ID, statusCode, NLS.bind(Messages.PlanAnalyzer_NothingToDo, getIUString(affectedIU)), null);
55 case IStatusCodes.OPERATION_ALREADY_IN_PROGRESS :
56 return new Status(IStatus.ERROR, Constants.BUNDLE_ID, statusCode, Messages.PlanAnalyzer_AnotherOperationInProgress, null);
57 default :
58 return new Status(IStatus.ERROR, Constants.BUNDLE_ID, statusCode, NLS.bind(Messages.PlanAnalyzer_UnexpectedError, Integer.valueOf(statusCode), getIUString(affectedIU)), null);
59 }
60 }
61
62 public static MultiStatus getProfileChangeAlteredStatus() {
63 return (MultiStatus) getStatus(IStatusCodes.PROFILE_CHANGE_ALTERED, null);
64 }
65
66 public static ResolutionResult computeResolutionResult(ProfileChangeRequest originalRequest, IProvisioningPlan plan, MultiStatus originalStatus) {
67 Assert.isNotNull(originalRequest);
68 Assert.isNotNull(plan);
69 Assert.isNotNull(originalStatus);
70
71 ResolutionResult report = new ResolutionResult();
72
73 // If the plan was canceled, no further analysis is needed
74 if (plan.getStatus().getSeverity() == IStatus.CANCEL) {
75 report.addSummaryStatus(plan.getStatus());
76 return report;
77 }
78
79 if (nothingToDo(originalRequest)) {
80 report.addSummaryStatus(getStatus(IStatusCodes.UNEXPECTED_NOTHING_TO_DO, null));
81 IStatus[] details = originalStatus.getChildren();
82 for (IStatus detail : details) {
83 report.addSummaryStatus(detail);
84 }
85 return report;
86 }
87
88 // If there was already some status supplied before resolution, this should get included
89 // with the report. For example, this might contain information about the profile request
90 // being altered before resolution began.
91 if (originalStatus != null && originalStatus.getChildren().length > 0) {
92 report.addSummaryStatus(originalStatus);
93 }
94
95 // If the overall plan had a non-OK status, capture that in the report.
96 if (!plan.getStatus().isOK())
97 report.addSummaryStatus(plan.getStatus());
98
99 // Now we compare what was requested with what is going to happen.
100 // In the long run, when a RequestStatus can provide actual explanation/status
101 // about failures, we might want to add this information to the overall status.
102 // As it stands now, if the provisioning plan is in error, that info is more detailed
103 // than the request status. So we will only add request status info to the overall
104 // status when the overall status is not in error.
105
106 PlannerStatus plannerStatus = plan.getStatus() instanceof PlannerStatus ? (PlannerStatus) plan.getStatus() : null;
107 // If there is no additional plannerStatus details just return the report
108 if (plannerStatus == null)
109 return report;
110
111 if (plan.getStatus().getSeverity() != IStatus.ERROR) {
112 Collection<IInstallableUnit> iusAdded = originalRequest.getAdditions();
113 for (IInstallableUnit added : iusAdded) {
114 RequestStatus rs = plannerStatus.getRequestChanges().get(added);
115 if (rs.getSeverity() == IStatus.ERROR) {
116 // This is a serious error so it must also appear in the overall status
117 IStatus fail = new Status(IStatus.ERROR, Constants.BUNDLE_ID, IStatusCodes.ALTERED_IGNORED_INSTALL_REQUEST, NLS.bind(Messages.PlanAnalyzer_IgnoringInstall, getIUString(added)), null);
118 report.addStatus(added, fail);
119 report.addSummaryStatus(fail);
120 }
121 }
122 Collection<IInstallableUnit> iusRemoved = originalRequest.getRemovals();
123 for (IInstallableUnit removed : iusRemoved) {
124 RequestStatus rs = plannerStatus.getRequestChanges().get(removed);
125 if (rs.getSeverity() == IStatus.ERROR) {
126 // TODO see https://bugs.eclipse.org/bugs/show_bug.cgi?id=255984
127 // We are making assumptions here about why the planner chose to ignore an uninstall.
128 // Assume it could not be uninstalled because of some other dependency, yet the planner did not view
129 // this as an error. So we inform the user that we can only uninstall parts of it. The root property will be
130 // removed per the original change request.
131 IStatus fail = new Status(IStatus.INFO, Constants.BUNDLE_ID, IStatusCodes.ALTERED_PARTIAL_UNINSTALL, NLS.bind(Messages.PlanAnalyzer_PartialUninstall, getIUString(removed)), null);
132 report.addStatus(removed, fail);
133 report.addSummaryStatus(fail);
134 }
135 }
136 } else {
137 if (plannerStatus.getCode() == IStatusCodes.MISSING_REQUIREMENTS) {
138 IStatus existingSummaryStatus = report.getSummaryStatus();
139 MultiStatus newSummaryStatus = new MultiStatus(Constants.BUNDLE_ID, IStatusCodes.MISSING_REQUIREMENTS, Messages.ResolutionResult_SummaryStatus, null);
140 if (!existingSummaryStatus.isOK()) {
141 newSummaryStatus.addAll(existingSummaryStatus);
142 report.setSummaryStatus(newSummaryStatus);
143 }
144 }
145 }
146
147 // Now process the side effects
148 if (plannerStatus.getRequestSideEffects() != null) {
149 for (Entry<IInstallableUnit, RequestStatus> entry : plannerStatus.getRequestSideEffects().entrySet()) {
150 IInstallableUnit iu = entry.getKey();
151 RequestStatus rs = entry.getValue();
152 if (rs.getInitialRequestType() == RequestStatus.ADDED) {
153 report.addStatus(iu, new Status(rs.getSeverity(), Constants.BUNDLE_ID, IStatusCodes.ALTERED_SIDE_EFFECT_INSTALL, NLS.bind(Messages.PlanAnalyzer_SideEffectInstall, getIUString(iu)), null));
154 } else {
155 report.addStatus(iu, new Status(rs.getSeverity(), Constants.BUNDLE_ID, IStatusCodes.ALTERED_SIDE_EFFECT_REMOVE, NLS.bind(Messages.PlanAnalyzer_SideEffectUninstall, getIUString(iu)), null));
156 }
157 }
158 }
159
160 return report;
161
162 }
163
164 private static String getIUString(IInstallableUnit iu) {
165 if (iu == null)
166 return Messages.PlanAnalyzer_Items;
167 // Get the iu name in the default locale
168 String name = iu.getProperty(IInstallableUnit.PROP_NAME, null);
169 if (name != null)
170 return name;
171 return iu.getId();
172 }
173
174 private static boolean nothingToDo(ProfileChangeRequest request) {
175 return request.getAdditions().size() == 0 && request.getRemovals().size() == 0 && request.getInstallableUnitProfilePropertiesToAdd().size() == 0 && request.getInstallableUnitProfilePropertiesToRemove().size() == 0;
176 }
177 }