Debian Codegraph @

bundles/org.eclipse.equinox.p2.operations/src/org/eclipse/equinox/p2/operations/ProvisioningSession.java

raw
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 * Red Hat Inc. - Bug 460967
15 ******************************************************************************/
16
17 package org.eclipse.equinox.p2.operations;
18
19 import java.io.IOException;
20 import java.util.*;
21 import org.eclipse.core.runtime.*;
22 import org.eclipse.core.runtime.jobs.*;
23 import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
24 import org.eclipse.equinox.internal.p2.operations.Messages;
25 import org.eclipse.equinox.internal.provisional.configurator.Configurator;
26 import org.eclipse.equinox.internal.provisional.p2.core.eventbus.IProvisioningEventBus;
27 import org.eclipse.equinox.p2.core.IAgentLocation;
28 import org.eclipse.equinox.p2.core.IProvisioningAgent;
29 import org.eclipse.equinox.p2.engine.*;
30 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
31 import org.eclipse.equinox.p2.planner.IPlanner;
32 import org.eclipse.equinox.p2.query.QueryUtil;
33 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
34 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.FrameworkUtil;
37
38 /**
39 * ProvisioningSession provides the context for a provisioning session, including
40 * the provisioning services that should be used. It also provides utility
41 * methods for commonly performed provisioning tasks.
42 *
43 * @since 2.0
44 * @noextend This class is not intended to be subclassed by clients.
45 */
46 public class ProvisioningSession {
47 private IProvisioningAgent agent;
48
49 Set<Job> scheduledJobs = Collections.synchronizedSet(new HashSet<>());
50
51 /**
52 * Create a provisioning session using the services of the supplied agent.
53 * @param agent the provisioning agent that supplies services. Must not be <code>null</code>.
54 */
55 public ProvisioningSession(IProvisioningAgent agent) {
56 Assert.isNotNull(agent, Messages.ProvisioningSession_AgentNotFound);
57 this.agent = agent;
58 }
59
60 /**
61 * Return the provisioning agent used to retrieve provisioning services.
62 * @return the provisioning agent
63 */
64 public IProvisioningAgent getProvisioningAgent() {
65 return agent;
66 }
67
68 /**
69 * Return the agent location for this session
70 * @return the agent location
71 */
72 IAgentLocation getAgentLocation() {
73 return agent.getService(IAgentLocation.class);
74 }
75
76 /**
77 * Return the artifact repository manager for this session
78 * @return the repository manager
79 */
80 IArtifactRepositoryManager getArtifactRepositoryManager() {
81 return agent.getService(IArtifactRepositoryManager.class);
82 }
83
84 /**
85 * Return the metadata repository manager for this session
86 * @return the repository manager
87 */
88 IMetadataRepositoryManager getMetadataRepositoryManager() {
89 return agent.getService(IMetadataRepositoryManager.class);
90 }
91
92 IProfileRegistry getProfileRegistry() {
93 return agent.getService(IProfileRegistry.class);
94 }
95
96 /**
97 * Return the provisioning engine for this session
98 * @return the provisioning engine
99 */
100 IEngine getEngine() {
101 return agent.getService(IEngine.class);
102 }
103
104 /**
105 * Return the provisioning event bus used for dispatching events.
106 * @return the event bus
107 */
108 IProvisioningEventBus getProvisioningEventBus() {
109 return agent.getService(IProvisioningEventBus.class);
110 }
111
112 /**
113 * Return the planner used for this session
114 * @return the planner
115 */
116 IPlanner getPlanner() {
117 return agent.getService(IPlanner.class);
118 }
119
120 /**
121 * Perform the specified provisioning plan.
122 *
123 * @param plan the provisioning plan to be performed
124 * @param phaseSet the phase set to be used for the plan
125 * @param context the provisioning context to be used during provisioning
126 * @param monitor the progress monitor to use while performing the plan
127 * @return a status describing the result of performing the plan
128 */
129 public IStatus performProvisioningPlan(IProvisioningPlan plan, IPhaseSet phaseSet, ProvisioningContext context, IProgressMonitor monitor) {
130 IPhaseSet set = phaseSet == null ? PhaseSetFactory.createDefaultPhaseSet() : phaseSet;
131
132 // 300 ticks for download, 100 to install handlers, 100 to compute the plan, 100 to install the rest
133 SubMonitor mon = SubMonitor.convert(monitor, 600);
134 int ticksUsed = 0;
135
136 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=272355
137 // The exact profile instance used in the profile change request and passed to the engine must be used for all
138 // of these operations, otherwise we can get profile out of synch errors.
139 IProfile profile = plan.getProfile();
140
141 if (plan.getInstallerPlan() != null) {
142 if (doesPhaseSetIncludeDownload(set)) {
143 // If the phase set calls for download, then we want to download the install plan artifacts
144 // at the same time as the actual install artifacts. This way, we will only install the install handler
145 // after already knowing we have successfully obtained the artifacts that will be installed afterward.
146 IProvisioningPlan downloadPlan = getEngine().createPlan(profile, context);
147 for (IInstallableUnit element : QueryUtil.compoundQueryable(plan.getAdditions(), plan.getInstallerPlan().getAdditions()).query(QueryUtil.createIUAnyQuery(), null)) {
148 downloadPlan.addInstallableUnit(element);
149 }
150 IPhaseSet download = PhaseSetFactory.createPhaseSetIncluding(new String[] {PhaseSetFactory.PHASE_COLLECT});
151 IStatus downloadStatus = getEngine().perform(downloadPlan, download, mon.newChild(300));
152 if (!downloadStatus.isOK()) {
153 mon.done();
154 return downloadStatus;
155 }
156 ticksUsed = 300;
157 }
158 // we pre-downloaded if necessary. Now perform the install plan against the original phase set.
159 IStatus installerPlanStatus = getEngine().perform(plan.getInstallerPlan(), set, mon.newChild(100));
160 if (!installerPlanStatus.isOK()) {
161 mon.done();
162 return installerPlanStatus;
163 }
164 ticksUsed += 100;
165 // Apply the configuration
166 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
167 Configurator configChanger = ServiceHelper.getService(bundleContext, Configurator.class);
168 try {
169 configChanger.applyConfiguration();
170 } catch (IOException e) {
171 mon.done();
172 return Status.error(Messages.ProvisioningSession_InstallPlanConfigurationError, e);
173 }
174 }
175 IStatus status = getEngine().perform(plan, set, mon.newChild(500 - ticksUsed));
176 mon.done();
177 return status;
178 }
179
180 private boolean doesPhaseSetIncludeDownload(IPhaseSet set) {
181 String[] phaseIds = set.getPhaseIds();
182 for (String phaseId : phaseIds)
183 if (phaseId.equals(PhaseSetFactory.PHASE_COLLECT))
184 return true;
185 return false;
186 }
187
188 /**
189 * Return a boolean indicating whether any other provisioning operations are
190 * scheduled for the specified profile.
191 *
192 * @param profileId the id of the profile in question
193 * @return <code>true</code> if there are pending provisioning operations for
194 * this profile, <code>false</code> if there are not.
195 * @see #rememberJob(Job)
196 */
197 public boolean hasScheduledOperationsFor(String profileId) {
198 Job[] jobs = getScheduledJobs();
199 for (Job job : jobs) {
200 if (job instanceof IProfileChangeJob) {
201 String id = ((IProfileChangeJob) job).getProfileId();
202 if (profileId.equals(id))
203 return true;
204 }
205 }
206 return false;
207 }
208
209 private Job[] getScheduledJobs() {
210 synchronized (scheduledJobs) {
211 return scheduledJobs.toArray(new Job[scheduledJobs.size()]);
212 }
213 }
214
215 /**
216 * Remember the specified job. Remembered jobs are
217 * checked when callers want to know what work is scheduled for
218 * a particular profile.
219 *
220 * @param job the job to be remembered
221 * @see #hasScheduledOperationsFor(String)
222 */
223 public void rememberJob(Job job) {
224 scheduledJobs.add(job);
225 job.addJobChangeListener(new JobChangeAdapter() {
226 @Override
227 public void done(IJobChangeEvent event) {
228 scheduledJobs.remove(event.getJob());
229 }
230 });
231 }
232 }