Debian Codegraph @

bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/EngineSession.java

raw
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 *******************************************************************************/
14 package org.eclipse.equinox.internal.p2.engine;
15
16 import java.io.File;
17 import java.util.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.equinox.p2.core.IProvisioningAgent;
20 import org.eclipse.equinox.p2.engine.*;
21 import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
22 import org.eclipse.equinox.p2.engine.spi.Touchpoint;
23 import org.eclipse.osgi.util.NLS;
24
25 /**
26 * TODO: not API
27 */
28 public class EngineSession {
29 private static final String ENGINE_SESSION = "enginesession"; //$NON-NLS-1$
30
31 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
32
33 private static class ActionsRecord {
34 Operand operand;
35 List<ProvisioningAction> actions = new ArrayList<>();
36
37 ActionsRecord(Operand operand) {
38 this.operand = operand;
39 }
40 }
41
42 private List<Object[]> phaseActionRecordsPairs = new ArrayList<>();
43
44 private Phase currentPhase;
45 boolean currentPhaseActive;
46
47 private List<ActionsRecord> currentActionRecords;
48 private ActionsRecord currentRecord;
49
50 private IProfile profile;
51
52 private ProvisioningContext context;
53
54 private final HashMap<String, Object> sessionServices = new HashMap<>();
55
56 private Set<Touchpoint> touchpoints = new HashSet<>();
57
58 private final IProvisioningAgent agent;
59
60 public EngineSession(IProvisioningAgent agent, IProfile profile, ProvisioningContext context) {
61 super();
62 this.agent = agent;
63 this.profile = profile;
64 this.context = context;
65 }
66
67 public IProfile getProfile() {
68 return profile;
69 }
70
71 public IProvisioningAgent getAgent() {
72 return agent;
73 }
74
75 public ProvisioningContext getProvisioningContext() {
76 return context;
77 }
78
79 public File getProfileDataDirectory() {
80 SimpleProfileRegistry profileRegistry = (SimpleProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
81 return profileRegistry.getProfileDataDirectory(profile.getProfileId());
82 }
83
84 /**
85 * This is the interface through which parts of the engine obtain the services they need
86 * @param serviceName The name of the service to obtain
87 * @return The service instance, or <code>null</code> if no such service is available
88 */
89 public Object getxService(String serviceName) {
90 Object result = sessionServices.get(serviceName);
91 if (result != null)
92 return result;
93 return agent.getService(serviceName);
94 }
95
96 IStatus prepare(IProgressMonitor monitor) {
97 monitor.subTask(Messages.preparing);
98 MultiStatus status = new MultiStatus(EngineActivator.ID, IStatus.OK, null, null);
99 for (Touchpoint touchpoint : touchpoints) {
100 try {
101 status.add(touchpoint.prepare(profile));
102 } catch (RuntimeException e) {
103 // "touchpoint.prepare" calls user code and might throw an unchecked exception
104 // we catch the error here to gather information on where the problem occurred.
105 status.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.touchpoint_prepare_error, touchpoint.getClass().getName()), e));
106 } catch (LinkageError e) {
107 // Catch linkage errors as these are generally recoverable but let other Errors propagate (see bug 222001)
108 status.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.touchpoint_prepare_error, touchpoint.getClass().getName()), e));
109 }
110 }
111
112 if (status.matches(IStatus.ERROR)) {
113 MultiStatus result = new MultiStatus(EngineActivator.ID, IStatus.ERROR, NLS.bind(Messages.session_prepare_error, profile.getProfileId()), null);
114 result.merge(status);
115 return result;
116 }
117 return status;
118 }
119
120 IStatus commit(IProgressMonitor monitor) {
121 monitor.subTask(Messages.committing);
122 MultiStatus status = new MultiStatus(EngineActivator.ID, IStatus.OK, null, null);
123 phaseActionRecordsPairs.clear();
124 for (Touchpoint touchpoint : touchpoints) {
125 try {
126 IStatus result = touchpoint.commit(profile);
127 if (!result.isOK())
128 status.add(result);
129 } catch (RuntimeException e) {
130 // "touchpoint.commit" calls user code and might throw an unchecked exception
131 // we catch the error here to gather information on where the problem occurred.
132 status.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.touchpoint_commit_error, touchpoint.getClass().getName()), e));
133 } catch (LinkageError e) {
134 // Catch linkage errors as these are generally recoverable but let other Errors propagate (see bug 222001)
135 status.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.touchpoint_commit_error, touchpoint.getClass().getName()), e));
136 }
137 }
138
139 if (status.matches(IStatus.ERROR)) {
140 MultiStatus result = new MultiStatus(EngineActivator.ID, IStatus.ERROR, NLS.bind(Messages.session_commit_error, profile.getProfileId()), null);
141 result.merge(status);
142 return result;
143 }
144 return status;
145 }
146
147 IStatus rollback(IProgressMonitor monitor, int severity) {
148 if (severity == IStatus.CANCEL)
149 monitor.subTask(Messages.rollingback_cancel);
150
151 if (severity == IStatus.ERROR)
152 monitor.subTask(Messages.rollingback_error);
153
154 MultiStatus status = new MultiStatus(EngineActivator.ID, IStatus.OK, null, null);
155 SubMonitor sub = SubMonitor.convert(monitor, 100 * (phaseActionRecordsPairs.size() + (currentPhaseActive ? 1 : 0) + 1 /* for touchpoint */));
156
157 if (currentPhaseActive) {
158 try {
159 IStatus result = rollBackPhase(currentPhase, currentActionRecords, sub.newChild(100));
160 if (!result.isOK())
161 status.add(result);
162 } catch (RuntimeException e) {
163 // "phase.prePerform and phase.postPerform" calls user code and might throw an unchecked exception
164 // we catch the error here to gather information on where the problem occurred.
165 status.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.phase_undo_error, currentPhase.getClass().getName()), e));
166 } catch (LinkageError e) {
167 // Catch linkage errors as these are generally recoverable but let other Errors propagate (see bug 222001)
168 status.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.phase_undo_error, currentPhase.getClass().getName()), e));
169 }
170 currentPhaseActive = false;
171 currentActionRecords = null;
172 currentRecord = null;
173 }
174 currentPhase = null;
175
176 for (ListIterator<Object[]> it = phaseActionRecordsPairs.listIterator(phaseActionRecordsPairs.size()); it.hasPrevious();) {
177 Object[] pair = it.previous();
178 Phase phase = (Phase) pair[0];
179 @SuppressWarnings("unchecked")
180 List<ActionsRecord> actionRecords = (List<ActionsRecord>) pair[1];
181 try {
182 final IStatus result = rollBackPhase(phase, actionRecords, sub.newChild(100));
183 if (!result.isOK())
184 status.add(result);
185 } catch (RuntimeException e) {
186 // "phase.prePerform and phase.postPerform" calls user code and might throw an unchecked exception
187 // we catch the error here to gather information on where the problem occurred.
188 status.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.phase_undo_error, phase.getClass().getName()), e));
189 } catch (LinkageError e) {
190 // Catch linkage errors as these are generally recoverable but let other Errors propagate (see bug 222001)
191 status.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.phase_undo_error, phase.getClass().getName()), e));
192 }
193 }
194
195 phaseActionRecordsPairs.clear();
196 SubMonitor touchpointSub = sub.setWorkRemaining(100).newChild(100).setWorkRemaining(touchpoints.size() + 1);
197 for (Touchpoint touchpoint : touchpoints) {
198 try {
199 IStatus result = touchpoint.rollback(profile);
200 if (!result.isOK())
201 status.add(result);
202 } catch (RuntimeException e) {
203 // "touchpoint.rollback" calls user code and might throw an unchecked exception
204 // we catch the error here to gather information on where the problem occurred.
205 status.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.touchpoint_rollback_error, touchpoint.getClass().getName()), e));
206 } catch (LinkageError e) {
207 // Catch linkage errors as these are generally recoverable but let other Errors propagate (see bug 222001)
208 status.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.touchpoint_rollback_error, touchpoint.getClass().getName()), e));
209 } finally {
210 touchpointSub.worked(1);
211 }
212 }
213
214 try {
215 if (status.matches(IStatus.ERROR)) {
216 MultiStatus result = new MultiStatus(EngineActivator.ID, IStatus.ERROR, NLS.bind(Messages.session_commit_error, profile.getProfileId()), null);
217 result.merge(status);
218 return result;
219 }
220 return status;
221 } finally {
222 touchpointSub.worked(1);
223 }
224 }
225
226 private IStatus rollBackPhase(Phase phase, List<ActionsRecord> actionRecords, IProgressMonitor monitor) {
227 MultiStatus result = new MultiStatus(EngineActivator.ID, IStatus.OK, null, null);
228 SubMonitor sub = SubMonitor.convert(monitor, 10 + 10 + 10 * actionRecords.size());
229 try {
230 phase.actionManager = agent.getService(ActionManager.class);
231
232 if (!currentPhaseActive)
233 phase.prePerform(result, this, sub.newChild(10));
234
235 for (ListIterator<ActionsRecord> it = actionRecords.listIterator(actionRecords.size()); it.hasPrevious();) {
236 ActionsRecord record = it.previous();
237 List<ProvisioningAction> reversedActions = new ArrayList<>(record.actions);
238 Collections.reverse(reversedActions);
239 ProvisioningAction[] actions = reversedActions.toArray(new ProvisioningAction[record.actions.size()]);
240 try {
241 phase.undo(result, this, profile, record.operand, actions, context);
242 } catch (RuntimeException e) {
243 // "phase.undo" calls user code and might throw an unchecked exception
244 // we catch the error here to gather information on where the problem occurred.
245 result.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.phase_undo_operand_error, phase.getClass().getName(), record.operand), e));
246 } catch (LinkageError e) {
247 // Catch linkage errors as these are generally recoverable but let other Errors propagate (see bug 222001)
248 result.add(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.phase_undo_operand_error, phase.getClass().getName(), record.operand), e));
249 } finally {
250 sub.worked(10);
251 }
252 }
253 phase.postPerform(result, this, sub.setWorkRemaining(10).newChild(10));
254 } finally {
255 phase.actionManager = null;
256 }
257 return result;
258 }
259
260 void recordPhaseEnter(Phase phase) {
261 if (phase == null)
262 throw new IllegalArgumentException(Messages.null_phase);
263
264 if (currentPhase != null)
265 throw new IllegalStateException(Messages.phase_started);
266
267 currentPhase = phase;
268
269 if (DebugHelper.DEBUG_ENGINE_SESSION)
270 debugPhaseEnter(phase);
271 }
272
273 void recordPhaseStart(Phase phase) {
274 if (phase == null)
275 throw new IllegalArgumentException(Messages.null_phase);
276
277 if (currentPhase != phase)
278 throw new IllegalArgumentException(Messages.not_current_phase);
279
280 currentPhaseActive = true;
281 currentActionRecords = new ArrayList<>();
282 }
283
284 void recordPhaseEnd(Phase phase) {
285 if (currentPhase == null)
286 throw new IllegalStateException(Messages.phase_not_started);
287
288 if (currentPhase != phase)
289 throw new IllegalArgumentException(Messages.not_current_phase);
290
291 phaseActionRecordsPairs.add(new Object[] {currentPhase, currentActionRecords});
292 currentActionRecords = null;
293 currentPhaseActive = false;
294 }
295
296 void recordPhaseExit(Phase phase) {
297 if (currentPhase == null)
298 throw new IllegalStateException(Messages.phase_not_started);
299
300 if (currentPhase != phase)
301 throw new IllegalArgumentException(Messages.not_current_phase);
302
303 currentPhase = null;
304 if (DebugHelper.DEBUG_ENGINE_SESSION)
305 debugPhaseExit(phase);
306 }
307
308 void recordOperandStart(Operand operand) {
309 if (operand == null)
310 throw new IllegalArgumentException(Messages.null_operand);
311
312 if (currentRecord != null)
313 throw new IllegalStateException(Messages.operand_started);
314
315 currentRecord = new ActionsRecord(operand);
316 currentActionRecords.add(currentRecord);
317
318 if (DebugHelper.DEBUG_ENGINE_SESSION)
319 debugOperandStart(operand);
320 }
321
322 void recordOperandEnd(Operand operand) {
323 if (currentRecord == null)
324 throw new IllegalStateException(Messages.operand_not_started);
325
326 if (currentRecord.operand != operand)
327 throw new IllegalArgumentException(Messages.not_current_operand);
328
329 currentRecord = null;
330
331 if (DebugHelper.DEBUG_ENGINE_SESSION)
332 debugOperandEnd(operand);
333 }
334
335 void recordActionExecute(ProvisioningAction action, Map<String, Object> parameters) {
336 if (action == null)
337 throw new IllegalArgumentException(Messages.null_action);
338
339 currentRecord.actions.add(action);
340
341 Touchpoint touchpoint = action.getTouchpoint();
342 if (touchpoint != null)
343 touchpoints.add(touchpoint);
344
345 if (DebugHelper.DEBUG_ENGINE_SESSION)
346 debugActionExecute(action, parameters);
347 }
348
349 public void recordActionUndo(ProvisioningAction action, Map<String, Object> parameters) {
350 if (DebugHelper.DEBUG_ENGINE_SESSION)
351 debugActionUndo(action, parameters);
352 }
353
354 public String getContextString(Phase phase, Operand operand, ProvisioningAction action) {
355 if (action instanceof ParameterizedProvisioningAction) {
356 ParameterizedProvisioningAction parameterizedAction = (ParameterizedProvisioningAction) action;
357 action = parameterizedAction.getAction();
358 }
359 String message = NLS.bind(Messages.session_context, new Object[] {profile.getProfileId(), phase.getClass().getName(), operand.toString(), getCurrentActionId()});
360 return message;
361 }
362
363 public String getContextString() {
364 String message = NLS.bind(Messages.session_context, new Object[] {profile.getProfileId(), getCurrentPhaseId(), getCurrentOperandId(), getCurrentActionId()});
365 return message;
366 }
367
368 private Object getCurrentActionId() {
369 if (currentRecord == null || currentRecord.actions.isEmpty())
370 return EMPTY_STRING;
371
372 Object currentAction = currentRecord.actions.get(currentRecord.actions.size() - 1);
373 if (currentAction instanceof ParameterizedProvisioningAction) {
374 ParameterizedProvisioningAction parameterizedAction = (ParameterizedProvisioningAction) currentAction;
375 currentAction = parameterizedAction.getAction();
376 }
377 return currentAction.getClass().getName();
378 }
379
380 private String getCurrentPhaseId() {
381 if (currentPhase == null)
382 return EMPTY_STRING;
383 return currentPhase.getClass().getName();
384 }
385
386 private String getCurrentOperandId() {
387 if (currentRecord == null)
388 return EMPTY_STRING;
389 return currentRecord.operand.toString();
390 }
391
392 private static void debugPhaseEnter(Phase phase) {
393 DebugHelper.debug(ENGINE_SESSION, "Entering phase: " + phase.getClass().getName()); //$NON-NLS-1$
394 }
395
396 private static void debugPhaseExit(Phase phase) {
397 DebugHelper.debug(ENGINE_SESSION, "Exiting phase: " + phase.getClass().getName()); //$NON-NLS-1$
398 }
399
400 private static void debugOperandStart(Operand operand) {
401 DebugHelper.debug(ENGINE_SESSION, "Starting processing of operand: " + operand.toString()); //$NON-NLS-1$
402 }
403
404 private static void debugOperandEnd(Operand operand) {
405 DebugHelper.debug(ENGINE_SESSION, "Ending processing of operand: " + operand.toString()); //$NON-NLS-1$
406 }
407
408 private static void debugActionExecute(ProvisioningAction action, Map<String, Object> parameters) {
409 DebugHelper.debug(ENGINE_SESSION, "Executing action: " + DebugHelper.formatAction(action, parameters)); //$NON-NLS-1$
410 }
411
412 private static void debugActionUndo(ProvisioningAction action, Map<String, Object> parameters) {
413 DebugHelper.debug(ENGINE_SESSION, "Undoing action: " + DebugHelper.formatAction(action, parameters)); //$NON-NLS-1$
414 }
415 }