| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009, 2017 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 |
* Rapicorp, Inc (Pascal Rapicault) - Bug 394156 - Add support for updates from one namespace to another |
| 15 |
******************************************************************************/ |
| 16 |
package org.eclipse.equinox.p2.operations; |
| 17 |
|
| 18 |
import java.util.Collection; |
| 19 |
import java.util.Iterator; |
| 20 |
import org.eclipse.core.runtime.*; |
| 21 |
import org.eclipse.equinox.internal.p2.director.Explanation; |
| 22 |
import org.eclipse.equinox.internal.p2.director.ProfileChangeRequest; |
| 23 |
import org.eclipse.equinox.internal.p2.operations.*; |
| 24 |
import org.eclipse.equinox.internal.provisional.p2.director.PlannerStatus; |
| 25 |
import org.eclipse.equinox.internal.provisional.p2.director.RequestStatus; |
| 26 |
import org.eclipse.equinox.p2.engine.*; |
| 27 |
import org.eclipse.equinox.p2.engine.query.UserVisibleRootQuery; |
| 28 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 29 |
import org.eclipse.equinox.p2.planner.ProfileInclusionRules; |
| 30 |
import org.eclipse.equinox.p2.query.IQueryResult; |
| 31 |
import org.eclipse.equinox.p2.query.QueryUtil; |
| 32 |
|
| 33 |
/** |
| 34 |
* An InstallOperation describes an operation that installs IInstallableUnits into |
| 35 |
* a profile. |
| 36 |
* |
| 37 |
* The following snippet shows how one might use an InstallOperation to perform a synchronous resolution and |
| 38 |
* then kick off an install in the background: |
| 39 |
* |
| 40 |
* <pre> |
| 41 |
* InstallOperation op = new InstallOperation(session, new IInstallableUnit [] { myIU }); |
| 42 |
* IStatus result = op.resolveModal(monitor); |
| 43 |
* if (result.isOK()) { |
| 44 |
* op.getProvisioningJob(monitor).schedule(); |
| 45 |
* } |
| 46 |
* </pre> |
| 47 |
* |
| 48 |
* @since 2.0 |
| 49 |
* @see ProfileChangeOperation |
| 50 |
* @noextend This class is not intended to be subclassed by clients. |
| 51 |
*/ |
| 52 |
public class InstallOperation extends ProfileChangeOperation { |
| 53 |
|
| 54 |
/** |
| 55 |
* @since 2.1 |
| 56 |
*/ |
| 57 |
protected Collection<IInstallableUnit> toInstall; |
| 58 |
|
| 59 |
/** |
| 60 |
* Create an install operation on the specified provisioning session that installs |
| 61 |
* the supplied IInstallableUnits. Unless otherwise specified, the operation will |
| 62 |
* be associated with the currently running profile. |
| 63 |
* |
| 64 |
* @param session the session to use for obtaining provisioning services |
| 65 |
* @param toInstall the IInstallableUnits to be installed into the profile. |
| 66 |
*/ |
| 67 |
public InstallOperation(ProvisioningSession session, Collection<IInstallableUnit> toInstall) { |
| 68 |
super(session); |
| 69 |
this.toInstall = toInstall; |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
protected void computeProfileChangeRequest(MultiStatus status, IProgressMonitor monitor) { |
| 74 |
request = ProfileChangeRequest.createByProfileId(session.getProvisioningAgent(), profileId); |
| 75 |
IProfile profile = session.getProfileRegistry().getProfile(profileId); |
| 76 |
SubMonitor sub = SubMonitor.convert(monitor, Messages.InstallOperation_ComputeProfileChangeProgress, toInstall.size()); |
| 77 |
for (IInstallableUnit entryToInstall : toInstall) { |
| 78 |
// If the user is installing a patch, we mark it optional. This allows |
| 79 |
// the patched IU to be updated later by removing the patch. |
| 80 |
if (QueryUtil.isPatch(entryToInstall)) |
| 81 |
request.setInstallableUnitInclusionRules(entryToInstall, ProfileInclusionRules.createOptionalInclusionRule(entryToInstall)); |
| 82 |
|
| 83 |
// Check to see if it is already installed. This may alter the request. |
| 84 |
IQueryResult<IInstallableUnit> alreadyInstalled = profile.query(QueryUtil.createIUQuery(entryToInstall.getId()), null); |
| 85 |
// TODO ideally we should only do this check if the iu is a singleton, but in practice many iu's that should |
| 86 |
// be singletons are not, so we don't check this (yet) |
| 87 |
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=230878 |
| 88 |
if (!alreadyInstalled.isEmpty()) { // && installedIU.isSingleton() |
| 89 |
IInstallableUnit installedIU = alreadyInstalled.iterator().next(); |
| 90 |
int compareTo = entryToInstall.getVersion().compareTo(installedIU.getVersion()); |
| 91 |
// If the iu is a newer version of something already installed, consider this an update request |
| 92 |
if (compareTo > 0) { |
| 93 |
boolean lockedForUpdate = false; |
| 94 |
String value = profile.getInstallableUnitProperty(installedIU, IProfile.PROP_PROFILE_LOCKED_IU); |
| 95 |
if (value != null) |
| 96 |
lockedForUpdate = (Integer.parseInt(value) & IProfile.LOCK_UPDATE) == IProfile.LOCK_UPDATE; |
| 97 |
if (lockedForUpdate) { |
| 98 |
// Add a status telling the user that this implies an update, but the iu should not be updated |
| 99 |
status.merge(PlanAnalyzer.getStatus(IStatusCodes.ALTERED_IGNORED_IMPLIED_UPDATE, entryToInstall)); |
| 100 |
} else { |
| 101 |
dealWithUpdates(status, profile, entryToInstall, installedIU); |
| 102 |
} |
| 103 |
} else if (compareTo < 0) { |
| 104 |
// An implied downgrade. We will not put this in the plan, add a status informing the user |
| 105 |
status.merge(PlanAnalyzer.getStatus(IStatusCodes.ALTERED_IGNORED_IMPLIED_DOWNGRADE, entryToInstall)); |
| 106 |
} else { |
| 107 |
if (UserVisibleRootQuery.isUserVisible(installedIU, profile)) |
| 108 |
// It is already a root, nothing to do. We tell the user it was already installed |
| 109 |
status.merge(PlanAnalyzer.getStatus(IStatusCodes.ALTERED_IGNORED_ALREADY_INSTALLED, entryToInstall)); |
| 110 |
else { |
| 111 |
// It was already installed but not as a root. Tell the user that parts of it are already installed and mark |
| 112 |
// it as a root. |
| 113 |
status.merge(PlanAnalyzer.getStatus(IStatusCodes.ALTERED_PARTIAL_INSTALL, entryToInstall)); |
| 114 |
request.setInstallableUnitProfileProperty(entryToInstall, IProfile.PROP_PROFILE_ROOT_IU, Boolean.toString(true)); |
| 115 |
} |
| 116 |
} |
| 117 |
} else { |
| 118 |
//Deal with the case of updates with renames |
| 119 |
boolean handled = false; |
| 120 |
Iterator<IInstallableUnit> allIUsIterator = profile.query(QueryUtil.ALL_UNITS, null).iterator(); |
| 121 |
while (allIUsIterator.hasNext()) { |
| 122 |
IInstallableUnit iuAlreadyInstalled = allIUsIterator.next(); |
| 123 |
if (entryToInstall.getUpdateDescriptor() != null && entryToInstall.getUpdateDescriptor().isUpdateOf(iuAlreadyInstalled)) { |
| 124 |
dealWithUpdates(status, profile, entryToInstall, iuAlreadyInstalled); |
| 125 |
handled = true; |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
if (!handled) |
| 130 |
// Install it and mark as a root |
| 131 |
request.add(entryToInstall); |
| 132 |
request.setInstallableUnitProfileProperty(entryToInstall, IProfile.PROP_PROFILE_ROOT_IU, Boolean.toString(true)); |
| 133 |
} |
| 134 |
sub.worked(1); |
| 135 |
} |
| 136 |
sub.done(); |
| 137 |
} |
| 138 |
|
| 139 |
private void dealWithUpdates(MultiStatus status, IProfile profile, IInstallableUnit entryToInstall, IInstallableUnit installedIU) { |
| 140 |
request.add(entryToInstall); |
| 141 |
request.remove(installedIU); |
| 142 |
// Add a status informing the user that the update has been inferred |
| 143 |
status.merge(PlanAnalyzer.getStatus(IStatusCodes.ALTERED_IMPLIED_UPDATE, entryToInstall)); |
| 144 |
// Mark it as a root if it hasn't been already |
| 145 |
if (!UserVisibleRootQuery.isUserVisible(installedIU, profile)) |
| 146 |
request.setInstallableUnitProfileProperty(entryToInstall, IProfile.PROP_PROFILE_ROOT_IU, Boolean.toString(true)); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
protected String getResolveJobName() { |
| 151 |
return Messages.InstallOperation_ResolveJobName; |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
protected String getProvisioningJobName() { |
| 156 |
return Messages.InstallOperation_InstallJobName; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
ProvisioningContext getFirstPassProvisioningContext() { |
| 162 |
// Set it back to no referencing for first pass in case we reuse this context. |
| 163 |
context.setProperty(ProvisioningContext.FOLLOW_REPOSITORY_REFERENCES, null); |
| 164 |
return context; |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
IFailedStatusEvaluator getSecondPassEvaluator() { |
| 169 |
return failedPlan -> { |
| 170 |
// Follow metadata repository references if the first try fails |
| 171 |
// There should be real API for this! |
| 172 |
if (missingRequirement(failedPlan)) |
| 173 |
context.setProperty(ProvisioningContext.FOLLOW_REPOSITORY_REFERENCES, Boolean.toString(true)); |
| 174 |
return context; |
| 175 |
}; |
| 176 |
} |
| 177 |
|
| 178 |
// this is very reachy |
| 179 |
boolean missingRequirement(IProvisioningPlan failedPlan) { |
| 180 |
IStatus status = failedPlan.getStatus(); |
| 181 |
RequestStatus requestStatus = null; |
| 182 |
if (status instanceof PlannerStatus) |
| 183 |
requestStatus = ((PlannerStatus) status).getRequestStatus(); |
| 184 |
return requestStatus != null && requestStatus.getShortExplanation() == Explanation.MISSING_REQUIREMENT; |
| 185 |
} |
| 186 |
} |