| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 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 |
* Landmark Graphics Corporation - bug 397183 |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.equinox.internal.p2.engine; |
| 16 |
|
| 17 |
import java.util.*; |
| 18 |
import org.eclipse.core.runtime.*; |
| 19 |
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; |
| 20 |
import org.eclipse.equinox.internal.provisional.p2.core.eventbus.IProvisioningEventBus; |
| 21 |
import org.eclipse.equinox.p2.engine.IProfile; |
| 22 |
import org.eclipse.equinox.p2.engine.ProvisioningContext; |
| 23 |
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction; |
| 24 |
import org.eclipse.equinox.p2.engine.spi.Touchpoint; |
| 25 |
import org.eclipse.osgi.util.NLS; |
| 26 |
|
| 27 |
public abstract class Phase { |
| 28 |
protected static final String PARM_OPERAND = "operand"; //$NON-NLS-1$ |
| 29 |
protected static final String PARM_PHASE_ID = "phaseId"; //$NON-NLS-1$ |
| 30 |
protected static final String PARM_PROFILE = "profile"; //$NON-NLS-1$ |
| 31 |
protected static final String PARM_PROFILE_DATA_DIRECTORY = "profileDataDirectory"; //$NON-NLS-1$ |
| 32 |
protected static final String PARM_CONTEXT = "context"; //$NON-NLS-1$ |
| 33 |
/** |
| 34 |
* Internal property. |
| 35 |
*/ |
| 36 |
protected static final String PARM_AGENT = "agent"; //$NON-NLS-1$ |
| 37 |
protected static final String PARM_FORCED = "forced"; //$NON-NLS-1$ |
| 38 |
protected static final String PARM_TOUCHPOINT = "touchpoint"; //$NON-NLS-1$ |
| 39 |
protected static final String LAST_RESULT_INTERNAL_NAME = "_p2_internal_last_result_variable_"; //$NON-NLS-1$ // |
| 40 |
protected static final String LAST_RESULT_PUBLIC_NAME = "lastResult"; //$NON-NLS-1$ |
| 41 |
|
| 42 |
protected final String phaseId; |
| 43 |
protected final int weight; |
| 44 |
protected final boolean forced; |
| 45 |
protected int prePerformWork = 1000; |
| 46 |
protected int mainPerformWork = 10000; |
| 47 |
protected int postPerformWork = 1000; |
| 48 |
private Map<String, Object> operandParameters = null; |
| 49 |
private Map<String, Object> phaseParameters = new HashMap<>(); |
| 50 |
private Map<Touchpoint, Map<String, Object>> touchpointToTouchpointPhaseParameters = new HashMap<>(); |
| 51 |
private Map<Touchpoint, Map<String, Object>> touchpointToTouchpointOperandParameters = new HashMap<>(); |
| 52 |
ActionManager actionManager; // injected from phaseset |
| 53 |
protected boolean isPaused = false; |
| 54 |
|
| 55 |
protected Phase(String phaseId, int weight, boolean forced) { |
| 56 |
if (phaseId == null || phaseId.length() == 0) |
| 57 |
throw new IllegalArgumentException(Messages.phaseid_not_set); |
| 58 |
if (weight <= 0) |
| 59 |
throw new IllegalArgumentException(Messages.phaseid_not_positive); |
| 60 |
this.weight = weight; |
| 61 |
this.phaseId = phaseId; |
| 62 |
this.forced = forced; |
| 63 |
} |
| 64 |
|
| 65 |
protected Phase(String phaseId, int weight) { |
| 66 |
this(phaseId, weight, false); |
| 67 |
} |
| 68 |
|
| 69 |
final protected ActionManager getActionManager() { |
| 70 |
return actionManager; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public String toString() { |
| 75 |
return getClass().getName() + " - " + this.weight; //$NON-NLS-1$ |
| 76 |
} |
| 77 |
|
| 78 |
private void broadcastPhaseEvent(EngineSession session, Operand[] operands, int type) { |
| 79 |
IProvisioningEventBus bus = session.getAgent().getService(IProvisioningEventBus.class); |
| 80 |
if (bus != null) { |
| 81 |
bus.publishEvent(getPhaseEvent(operands, type)); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
protected PhaseEvent getPhaseEvent(final Operand[] operands, int type) { |
| 86 |
return new PhaseEvent(phaseId, operands, type); |
| 87 |
} |
| 88 |
|
| 89 |
void perform(MultiStatus status, EngineSession session, Operand[] operands, IProgressMonitor monitor) { |
| 90 |
SubMonitor subMonitor = SubMonitor.convert(monitor, prePerformWork + mainPerformWork + postPerformWork); |
| 91 |
session.recordPhaseEnter(this); |
| 92 |
broadcastPhaseEvent(session, operands, PhaseEvent.TYPE_START); |
| 93 |
prePerform(status, session, subMonitor.newChild(prePerformWork)); |
| 94 |
if (status.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 95 |
return; |
| 96 |
session.recordPhaseStart(this); |
| 97 |
|
| 98 |
subMonitor.setWorkRemaining(mainPerformWork + postPerformWork); |
| 99 |
mainPerform(status, session, operands, subMonitor.newChild(mainPerformWork)); |
| 100 |
if (status.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 101 |
return; |
| 102 |
|
| 103 |
session.recordPhaseEnd(this); |
| 104 |
subMonitor.setWorkRemaining(postPerformWork); |
| 105 |
postPerform(status, session, subMonitor.newChild(postPerformWork)); |
| 106 |
phaseParameters.clear(); |
| 107 |
if (status.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 108 |
return; |
| 109 |
broadcastPhaseEvent(session, operands, PhaseEvent.TYPE_END); |
| 110 |
session.recordPhaseExit(this); |
| 111 |
subMonitor.done(); |
| 112 |
} |
| 113 |
|
| 114 |
void prePerform(MultiStatus status, EngineSession session, IProgressMonitor monitor) { |
| 115 |
IProfile profile = session.getProfile(); |
| 116 |
phaseParameters.put(PARM_PROFILE, profile); |
| 117 |
phaseParameters.put(PARM_PROFILE_DATA_DIRECTORY, session.getProfileDataDirectory()); |
| 118 |
phaseParameters.put(PARM_CONTEXT, session.getProvisioningContext()); |
| 119 |
phaseParameters.put(PARM_PHASE_ID, phaseId); |
| 120 |
phaseParameters.put(PARM_FORCED, Boolean.toString(forced)); |
| 121 |
phaseParameters.put(PARM_AGENT, session.getAgent()); |
| 122 |
mergeStatus(status, initializePhase(monitor, profile, phaseParameters)); |
| 123 |
} |
| 124 |
|
| 125 |
private void mainPerform(MultiStatus status, EngineSession session, Operand[] operands, SubMonitor subMonitor) { |
| 126 |
IProfile profile = session.getProfile(); |
| 127 |
subMonitor.beginTask(null, operands.length); |
| 128 |
for (int i = 0; i < operands.length; i++) { |
| 129 |
subMonitor.setWorkRemaining(operands.length - i); |
| 130 |
if (subMonitor.isCanceled()) |
| 131 |
throw new OperationCanceledException(); |
| 132 |
while (isPaused) { |
| 133 |
try { |
| 134 |
Thread.sleep(1000); |
| 135 |
} catch (InterruptedException e) { |
| 136 |
mergeStatus(status, new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.phase_thread_interrupted_error, phaseId), e)); |
| 137 |
return; |
| 138 |
} |
| 139 |
if (subMonitor.isCanceled()) |
| 140 |
throw new OperationCanceledException(); |
| 141 |
} |
| 142 |
Operand operand = operands[i]; |
| 143 |
if (!isApplicable(operand)) |
| 144 |
continue; |
| 145 |
|
| 146 |
session.recordOperandStart(operand); |
| 147 |
List<ProvisioningAction> actions = getActions(operand); |
| 148 |
operandParameters = new HashMap<>(phaseParameters); |
| 149 |
operandParameters.put(PARM_OPERAND, operand); |
| 150 |
mergeStatus(status, initializeOperand(profile, operand, operandParameters, subMonitor)); |
| 151 |
if (status.matches(IStatus.ERROR | IStatus.CANCEL)) { |
| 152 |
operandParameters = null; |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
Touchpoint operandTouchpoint = (Touchpoint) operandParameters.get(PARM_TOUCHPOINT); |
| 157 |
if (operandTouchpoint != null) { |
| 158 |
mergeStatus(status, initializeTouchpointParameters(profile, operand, operandTouchpoint, subMonitor)); |
| 159 |
if (status.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 160 |
return; |
| 161 |
|
| 162 |
operandParameters = touchpointToTouchpointOperandParameters.get(operandTouchpoint); |
| 163 |
} |
| 164 |
|
| 165 |
if (actions != null) { |
| 166 |
Object lastResult = null; |
| 167 |
for (ProvisioningAction action : actions) { |
| 168 |
Map<String, Object> parameters = operandParameters; |
| 169 |
Touchpoint touchpoint = action.getTouchpoint(); |
| 170 |
if (touchpoint != null) { |
| 171 |
mergeStatus(status, initializeTouchpointParameters(profile, operand, touchpoint, subMonitor)); |
| 172 |
if (status.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 173 |
return; |
| 174 |
|
| 175 |
parameters = touchpointToTouchpointOperandParameters.get(touchpoint); |
| 176 |
} |
| 177 |
if (lastResult != null) { |
| 178 |
parameters = new HashMap<>(parameters); |
| 179 |
parameters.put(LAST_RESULT_INTERNAL_NAME, lastResult); |
| 180 |
} |
| 181 |
parameters = Collections.unmodifiableMap(parameters); |
| 182 |
|
| 183 |
IStatus actionStatus = null; |
| 184 |
try { |
| 185 |
session.recordActionExecute(action, parameters); |
| 186 |
actionStatus = action.execute(parameters); |
| 187 |
lastResult = action.getResult(); |
| 188 |
} catch (RuntimeException e) { |
| 189 |
if (!forced) |
| 190 |
throw e; |
| 191 |
// "action.execute" calls user code and might throw an unchecked exception |
| 192 |
// we catch the error here to gather information on where the problem occurred. |
| 193 |
actionStatus = new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.forced_action_execute_error, action.getClass().getName()), e); |
| 194 |
} catch (LinkageError e) { |
| 195 |
if (!forced) |
| 196 |
throw e; |
| 197 |
// Catch linkage errors as these are generally recoverable but let other Errors propagate (see bug 222001) |
| 198 |
actionStatus = new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.forced_action_execute_error, action.getClass().getName()), e); |
| 199 |
} |
| 200 |
if (forced && actionStatus != null && actionStatus.matches(IStatus.ERROR)) { |
| 201 |
MultiStatus result = new MultiStatus(EngineActivator.ID, IStatus.ERROR, getProblemMessage(), null); |
| 202 |
result.add(new Status(IStatus.ERROR, EngineActivator.ID, session.getContextString(this, operand, action), null)); |
| 203 |
LogHelper.log(result); |
| 204 |
actionStatus = Status.OK_STATUS; |
| 205 |
} |
| 206 |
mergeStatus(status, actionStatus); |
| 207 |
if (status.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 208 |
return; |
| 209 |
} |
| 210 |
} |
| 211 |
mergeStatus(status, touchpointCompleteOperand(profile, operand, operandParameters, subMonitor)); |
| 212 |
mergeStatus(status, completeOperand(profile, operand, operandParameters, subMonitor)); |
| 213 |
if (status.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 214 |
return; |
| 215 |
operandParameters = null; |
| 216 |
session.recordOperandEnd(operand); |
| 217 |
subMonitor.worked(1); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
private IStatus initializeTouchpointParameters(IProfile profile, Operand operand, Touchpoint touchpoint, IProgressMonitor monitor) { |
| 222 |
if (touchpointToTouchpointOperandParameters.containsKey(touchpoint)) |
| 223 |
return Status.OK_STATUS; |
| 224 |
|
| 225 |
Map<String, Object> touchpointPhaseParameters = touchpointToTouchpointPhaseParameters.get(touchpoint); |
| 226 |
if (touchpointPhaseParameters == null) { |
| 227 |
touchpointPhaseParameters = new HashMap<>(phaseParameters); |
| 228 |
IStatus status = touchpoint.initializePhase(monitor, profile, phaseId, touchpointPhaseParameters); |
| 229 |
if (status != null && status.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 230 |
return status; |
| 231 |
touchpointToTouchpointPhaseParameters.put(touchpoint, touchpointPhaseParameters); |
| 232 |
} |
| 233 |
|
| 234 |
Map<String, Object> touchpointOperandParameters = new HashMap<>(touchpointPhaseParameters); |
| 235 |
touchpointOperandParameters.putAll(operandParameters); |
| 236 |
IStatus status = touchpoint.initializeOperand(profile, touchpointOperandParameters); |
| 237 |
if (status != null && status.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 238 |
return status; |
| 239 |
touchpointToTouchpointOperandParameters.put(touchpoint, touchpointOperandParameters); |
| 240 |
return Status.OK_STATUS; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Merges a given IStatus into a MultiStatus |
| 245 |
*/ |
| 246 |
protected static void mergeStatus(MultiStatus multi, IStatus status) { |
| 247 |
if (status != null && !status.isOK()) |
| 248 |
multi.merge(status); |
| 249 |
} |
| 250 |
|
| 251 |
void postPerform(MultiStatus status, EngineSession session, IProgressMonitor monitor) { |
| 252 |
IProfile profile = session.getProfile(); |
| 253 |
mergeStatus(status, touchpointCompletePhase(monitor, profile, phaseParameters)); |
| 254 |
mergeStatus(status, completePhase(monitor, profile, phaseParameters)); |
| 255 |
} |
| 256 |
|
| 257 |
void undo(MultiStatus status, EngineSession session, IProfile profile, Operand operand, ProvisioningAction[] actions, ProvisioningContext context) { |
| 258 |
if (operandParameters == null) { |
| 259 |
operandParameters = new HashMap<>(phaseParameters); |
| 260 |
operandParameters.put(PARM_OPERAND, operand); |
| 261 |
mergeStatus(status, initializeOperand(profile, operand, operandParameters, new NullProgressMonitor())); |
| 262 |
Touchpoint operandTouchpoint = (Touchpoint) operandParameters.get(PARM_TOUCHPOINT); |
| 263 |
if (operandTouchpoint != null) { |
| 264 |
mergeStatus(status, initializeTouchpointParameters(profile, operand, operandTouchpoint, new NullProgressMonitor())); |
| 265 |
if (status.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 266 |
return; |
| 267 |
|
| 268 |
operandParameters = touchpointToTouchpointOperandParameters.get(operandTouchpoint); |
| 269 |
} |
| 270 |
operandParameters = Collections.unmodifiableMap(operandParameters); |
| 271 |
} |
| 272 |
for (ProvisioningAction action : actions) { |
| 273 |
Map<String, Object> parameters = operandParameters; |
| 274 |
Touchpoint touchpoint = action.getTouchpoint(); |
| 275 |
if (touchpoint != null) { |
| 276 |
mergeStatus(status, initializeTouchpointParameters(profile, operand, touchpoint, new NullProgressMonitor())); |
| 277 |
if (status.matches(IStatus.ERROR)) |
| 278 |
return; |
| 279 |
|
| 280 |
parameters = touchpointToTouchpointOperandParameters.get(touchpoint); |
| 281 |
} |
| 282 |
IStatus actionStatus = null; |
| 283 |
try { |
| 284 |
session.recordActionUndo(action, parameters); |
| 285 |
actionStatus = action.undo(parameters); |
| 286 |
} catch (RuntimeException e) { |
| 287 |
// "action.undo" calls user code and might throw an unchecked exception |
| 288 |
// we catch the error here to gather information on where the problem occurred. |
| 289 |
actionStatus = new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.action_undo_error, action.getClass().getName()), e); |
| 290 |
} catch (LinkageError e) { |
| 291 |
// Catch linkage errors as these are generally recoverable but let other Errors propagate (see bug 222001) |
| 292 |
actionStatus = new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.action_undo_error, action.getClass().getName()), e); |
| 293 |
} |
| 294 |
if (actionStatus != null && actionStatus.matches(IStatus.ERROR)) { |
| 295 |
MultiStatus result = new MultiStatus(EngineActivator.ID, IStatus.ERROR, getProblemMessage(), null); |
| 296 |
result.add(new Status(IStatus.ERROR, EngineActivator.ID, session.getContextString(this, operand, action), null)); |
| 297 |
result.merge(actionStatus); |
| 298 |
} |
| 299 |
} |
| 300 |
mergeStatus(status, touchpointCompleteOperand(profile, operand, operandParameters, new NullProgressMonitor())); |
| 301 |
mergeStatus(status, completeOperand(profile, operand, operandParameters, new NullProgressMonitor())); |
| 302 |
operandParameters = null; |
| 303 |
} |
| 304 |
|
| 305 |
public boolean isApplicable(Operand operand) { |
| 306 |
return true; |
| 307 |
} |
| 308 |
|
| 309 |
protected IStatus initializePhase(IProgressMonitor monitor, IProfile profile, Map<String, Object> parameters) { |
| 310 |
return Status.OK_STATUS; |
| 311 |
} |
| 312 |
|
| 313 |
protected IStatus completePhase(IProgressMonitor monitor, IProfile profile, Map<String, Object> parameters) { |
| 314 |
return Status.OK_STATUS; |
| 315 |
} |
| 316 |
|
| 317 |
IStatus touchpointCompletePhase(IProgressMonitor monitor, IProfile profile, Map<String, Object> parameters) { |
| 318 |
if (touchpointToTouchpointPhaseParameters.isEmpty()) |
| 319 |
return Status.OK_STATUS; |
| 320 |
|
| 321 |
MultiStatus status = new MultiStatus(EngineActivator.ID, IStatus.OK, null, null); |
| 322 |
for (Map.Entry<Touchpoint, Map<String, Object>> entry : touchpointToTouchpointPhaseParameters.entrySet()) { |
| 323 |
Touchpoint touchpoint = entry.getKey(); |
| 324 |
Map<String, Object> touchpointParameters = entry.getValue(); |
| 325 |
mergeStatus(status, touchpoint.completePhase(monitor, profile, phaseId, touchpointParameters)); |
| 326 |
} |
| 327 |
touchpointToTouchpointPhaseParameters.clear(); |
| 328 |
return status; |
| 329 |
} |
| 330 |
|
| 331 |
protected IStatus completeOperand(IProfile profile, Operand operand, Map<String, Object> parameters, IProgressMonitor monitor) { |
| 332 |
return Status.OK_STATUS; |
| 333 |
} |
| 334 |
|
| 335 |
IStatus touchpointCompleteOperand(IProfile profile, Operand operand, Map<String, Object> parameters, IProgressMonitor monitor) { |
| 336 |
if (touchpointToTouchpointOperandParameters.isEmpty()) |
| 337 |
return Status.OK_STATUS; |
| 338 |
|
| 339 |
MultiStatus status = new MultiStatus(EngineActivator.ID, IStatus.OK, null, null); |
| 340 |
for (Map.Entry<Touchpoint, Map<String, Object>> entry : touchpointToTouchpointOperandParameters.entrySet()) { |
| 341 |
Touchpoint touchpoint = entry.getKey(); |
| 342 |
Map<String, Object> touchpointParameters = entry.getValue(); |
| 343 |
mergeStatus(status, touchpoint.completeOperand(profile, touchpointParameters)); |
| 344 |
} |
| 345 |
touchpointToTouchpointOperandParameters.clear(); |
| 346 |
return status; |
| 347 |
} |
| 348 |
|
| 349 |
protected IStatus initializeOperand(IProfile profile, Operand operand, Map<String, Object> parameters, IProgressMonitor monitor) { |
| 350 |
return Status.OK_STATUS; |
| 351 |
} |
| 352 |
|
| 353 |
protected abstract List<ProvisioningAction> getActions(Operand operand); |
| 354 |
|
| 355 |
/** |
| 356 |
* Returns a human-readable message to be displayed in case of an error performing |
| 357 |
* this phase. Subclasses should override. |
| 358 |
*/ |
| 359 |
protected String getProblemMessage() { |
| 360 |
return NLS.bind(Messages.phase_error, getClass().getName()); |
| 361 |
} |
| 362 |
|
| 363 |
protected void setPaused(boolean isPaused) { |
| 364 |
this.isPaused = isPaused; |
| 365 |
} |
| 366 |
} |