Debian Codegraph @

bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/Engine.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.util.Arrays;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
19 import org.eclipse.equinox.internal.provisional.p2.core.eventbus.IProvisioningEventBus;
20 import org.eclipse.equinox.p2.core.IProvisioningAgent;
21 import org.eclipse.equinox.p2.engine.*;
22
23 /**
24 * Concrete implementation of the {@link IEngine} API.
25 */
26 public class Engine implements IEngine {
27
28 private static final String ENGINE = "engine"; //$NON-NLS-1$
29 private IProvisioningAgent agent;
30
31 public Engine(IProvisioningAgent agent) {
32 this.agent = agent;
33 agent.registerService(ActionManager.SERVICE_NAME, new ActionManager());
34 }
35
36 private void checkArguments(IProfile iprofile, PhaseSet phaseSet, Operand[] operands) {
37 if (iprofile == null)
38 throw new IllegalArgumentException(Messages.null_profile);
39
40 if (phaseSet == null)
41 throw new IllegalArgumentException(Messages.null_phaseset);
42
43 if (operands == null)
44 throw new IllegalArgumentException(Messages.null_operands);
45 }
46
47 @Override
48 public IStatus perform(IProvisioningPlan plan, IPhaseSet phaseSet, IProgressMonitor monitor) {
49 return perform(plan.getProfile(), phaseSet, ((ProvisioningPlan) plan).getOperands(), plan.getContext(), monitor);
50 }
51
52 @Override
53 public IStatus perform(IProvisioningPlan plan, IProgressMonitor monitor) {
54 return perform(plan, PhaseSetFactory.createDefaultPhaseSet(), monitor);
55 }
56
57 public IStatus perform(IProfile iprofile, IPhaseSet phases, Operand[] operands, ProvisioningContext context, IProgressMonitor monitor) {
58 PhaseSet phaseSet = (PhaseSet) phases;
59 checkArguments(iprofile, phaseSet, operands);
60 if (operands.length == 0)
61 return Status.OK_STATUS;
62 SimpleProfileRegistry profileRegistry = (SimpleProfileRegistry) agent.getService(IProfileRegistry.class);
63 IProvisioningEventBus eventBus = agent.getService(IProvisioningEventBus.class);
64
65 if (context == null)
66 context = new ProvisioningContext(agent);
67
68 if (monitor == null)
69 monitor = new NullProgressMonitor();
70
71 Profile profile = profileRegistry.validate(iprofile);
72
73 profileRegistry.lockProfile(profile);
74 SubMonitor subMon = SubMonitor.convert(monitor, 3);
75 try {
76 eventBus.publishEvent(new BeginOperationEvent(profile, phaseSet, operands, this));
77 if (DebugHelper.DEBUG_ENGINE)
78 DebugHelper.debug(ENGINE, "Beginning engine operation for profile=" + profile.getProfileId() + " [" + profile.getTimestamp() + "]:" + DebugHelper.LINE_SEPARATOR + DebugHelper.formatOperation(phaseSet, operands, context)); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
79
80 EngineSession session = new EngineSession(agent, profile, context);
81
82 // If the property is set already in the context, respect that value.
83 String property = context.getProperty(ProvisioningContext.CHECK_AUTHORITIES);
84 if (property == null) {
85 // Allow a system property to force the property.
86 property = EngineActivator.getContext().getProperty(ProvisioningContext.CHECK_AUTHORITIES);
87 if (property == null) {
88 // Otherwise, if we are checking trust, also check the authorities.
89 if (Arrays.asList(phases.getPhaseIds()).contains(PhaseSetFactory.PHASE_CHECK_TRUST)) {
90 property = Boolean.TRUE.toString();
91 }
92 }
93 context.setProperty(ProvisioningContext.CHECK_AUTHORITIES, property);
94 }
95 MultiStatus result = phaseSet.perform(session, operands, subMon.split(1));
96 if (result.isOK() || result.matches(IStatus.INFO | IStatus.WARNING)) {
97 if (DebugHelper.DEBUG_ENGINE)
98 DebugHelper.debug(ENGINE, "Preparing to commit engine operation for profile=" + profile.getProfileId()); //$NON-NLS-1$
99 result.merge(session.prepare(subMon.split(1)));
100 }
101 subMon.setWorkRemaining(1);
102 if (result.matches(IStatus.ERROR | IStatus.CANCEL)) {
103 if (DebugHelper.DEBUG_ENGINE)
104 DebugHelper.debug(ENGINE, "Rolling back engine operation for profile=" + profile.getProfileId() + ". Reason was: " + result.toString()); //$NON-NLS-1$ //$NON-NLS-2$
105 IStatus status = session.rollback(subMon.split(1), result.getSeverity());
106 if (status.matches(IStatus.ERROR))
107 LogHelper.log(status);
108 eventBus.publishEvent(new RollbackOperationEvent(profile, phaseSet, operands, this, result));
109 } else {
110 if (DebugHelper.DEBUG_ENGINE)
111 DebugHelper.debug(ENGINE, "Committing engine operation for profile=" + profile.getProfileId()); //$NON-NLS-1$
112 if (profile.isChanged())
113 profileRegistry.updateProfile(profile);
114 IStatus status = session.commit(subMon.split(1));
115 if (status.matches(IStatus.ERROR))
116 LogHelper.log(status);
117 eventBus.publishEvent(new CommitOperationEvent(profile, phaseSet, operands, this));
118 }
119 //if there is only one child status, return that status instead because it will have more context
120 IStatus[] children = result.getChildren();
121 return children.length == 1 ? children[0] : result;
122 } finally {
123 profileRegistry.unlockProfile(profile);
124 profile.setChanged(false);
125 monitor.done();
126 }
127 }
128
129 protected IStatus validate(IProfile iprofile, PhaseSet phaseSet, Operand[] operands, ProvisioningContext context, IProgressMonitor monitor) {
130 checkArguments(iprofile, phaseSet, operands);
131
132 if (context == null)
133 context = new ProvisioningContext(agent);
134
135 if (monitor == null)
136 monitor = new NullProgressMonitor();
137
138 ActionManager actionManager = agent.getService(ActionManager.class);
139 return phaseSet.validate(actionManager, iprofile, operands, context, monitor);
140 }
141
142 @Override
143 public IProvisioningPlan createPlan(IProfile profile, ProvisioningContext context) {
144 return new ProvisioningPlan(profile, null, context);
145 }
146 }