Debian Codegraph @

bundles/org.eclipse.equinox.p2.console/src/org/eclipse/equinox/internal/p2/console/ProvisioningHelper.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 * Sonatype, Inc. - ongoing development
14 * Red Hat Inc. - Bug 460967
15 *******************************************************************************/
16 package org.eclipse.equinox.internal.p2.console;
17
18 import java.net.URI;
19 import java.util.*;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
22 import org.eclipse.equinox.internal.provisional.p2.director.PlanExecutionHelper;
23 import org.eclipse.equinox.p2.core.IProvisioningAgent;
24 import org.eclipse.equinox.p2.core.ProvisionException;
25 import org.eclipse.equinox.p2.engine.*;
26 import org.eclipse.equinox.p2.metadata.*;
27 import org.eclipse.equinox.p2.planner.IPlanner;
28 import org.eclipse.equinox.p2.planner.IProfileChangeRequest;
29 import org.eclipse.equinox.p2.query.*;
30 import org.eclipse.equinox.p2.repository.IRepository;
31 import org.eclipse.equinox.p2.repository.IRepositoryManager;
32 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;
33 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
34 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
35 import org.eclipse.osgi.service.environment.EnvironmentInfo;
36
37 public class ProvisioningHelper {
38
39 static IRepository<IInstallableUnit> addMetadataRepository(IProvisioningAgent agent, URI location) {
40 return addRepository(IMetadataRepositoryManager.class, agent, location, "metadata", //$NON-NLS-1$
41 IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY);
42 }
43
44 static IRepository<IInstallableUnit> getMetadataRepository(IProvisioningAgent agent, URI location) {
45 return getRepository(IMetadataRepositoryManager.class, agent, location);
46 }
47
48 static void removeMetadataRepository(IProvisioningAgent agent, URI location) {
49 removeRepository(IMetadataRepositoryManager.class, agent, location);
50 }
51
52 static IRepository<IArtifactKey> addArtifactRepository(IProvisioningAgent agent, URI location) {
53 return addRepository(IArtifactRepositoryManager.class, agent, location, "artifacts", //$NON-NLS-1$
54 IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY);
55 }
56
57 static void removeArtifactRepository(IProvisioningAgent agent, URI location) {
58 removeRepository(IArtifactRepositoryManager.class, agent, location);
59 }
60
61 static IProfile addProfile(IProvisioningAgent agent, String profileId, Map<String, String> properties) throws ProvisionException {
62 IProfileRegistry profileRegistry = agent.getService(IProfileRegistry.class);
63 if (profileRegistry == null)
64 return null;
65 IProfile profile = profileRegistry.getProfile(profileId);
66 if (profile != null)
67 return profile;
68
69 Map<String, String> profileProperties = new HashMap<>(properties);
70 if (profileProperties.get(IProfile.PROP_ENVIRONMENTS) == null) {
71 EnvironmentInfo info = ServiceHelper.getService(Activator.getContext(), EnvironmentInfo.class);
72 if (info != null)
73 profileProperties.put(IProfile.PROP_ENVIRONMENTS, "osgi.os=" + info.getOS() + ",osgi.ws=" + info.getWS() + ",osgi.arch=" + info.getOSArch()); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
74 else
75 profileProperties.put(IProfile.PROP_ENVIRONMENTS, ""); //$NON-NLS-1$
76 }
77
78 return profileRegistry.addProfile(profileId, profileProperties);
79 }
80
81 static void removeProfile(IProvisioningAgent agent, String profileId) {
82 IProfileRegistry profileRegistry = agent.getService(IProfileRegistry.class);
83 if (profileRegistry == null)
84 return;
85 profileRegistry.removeProfile(profileId);
86 }
87
88 static IProfile[] getProfiles(IProvisioningAgent agent) {
89 IProfileRegistry profileRegistry = agent.getService(IProfileRegistry.class);
90 if (profileRegistry == null)
91 return new IProfile[0];
92 return profileRegistry.getProfiles();
93 }
94
95 static IProfile getProfile(IProvisioningAgent agent, String id) {
96 IProfileRegistry profileRegistry = agent.getService(IProfileRegistry.class);
97 if (profileRegistry == null)
98 return null;
99 return profileRegistry.getProfile(id);
100 }
101
102 /**
103 * Returns the installable units that match the given query
104 * in the given metadata repository.
105 *
106 * @param location The location of the metadata repo to search. <code>null</code> indicates
107 * search all known repos.
108 * @param query The query to perform
109 * @param monitor A progress monitor, or <code>null</code>
110 * @return The IUs that match the query
111 */
112 static IQueryResult<IInstallableUnit> getInstallableUnits(IProvisioningAgent agent, URI location, IQuery<IInstallableUnit> query, IProgressMonitor monitor) {
113 IQueryable<IInstallableUnit> queryable = null;
114 if (location == null) {
115 queryable = agent.getService(IMetadataRepositoryManager.class);
116 } else {
117 queryable = getMetadataRepository(agent, location);
118 }
119 if (queryable != null)
120 return queryable.query(query, monitor);
121 return Collector.emptyCollector();
122 }
123
124 static List<URI> getMetadataRepositories(IProvisioningAgent agent) {
125 return getRepositories(IArtifactRepositoryManager.class, agent);
126 }
127
128 /**
129 * Install the described IU
130 */
131 static IStatus install(IProvisioningAgent agent, String unitId, String version, IProfile profile, IProgressMonitor progress) throws ProvisionException {
132 if (profile == null)
133 return null;
134 IQueryResult<IInstallableUnit> units = getInstallableUnits(agent, (URI) null, QueryUtil.createIUQuery(unitId, Version.create(version)), progress);
135 if (units.isEmpty()) {
136 StringBuilder error = new StringBuilder();
137 error.append("Installable unit not found: " + unitId + ' ' + version + '\n'); //$NON-NLS-1$
138 error.append("Repositories searched:\n");//$NON-NLS-1$
139 for (URI repo : getMetadataRepositories(agent))
140 error.append(repo + "\n");//$NON-NLS-1$
141 throw new ProvisionException(error.toString());
142 }
143
144 IPlanner planner = agent.getService(IPlanner.class);
145 if (planner == null)
146 throw new ProvisionException("No planner service found.");//$NON-NLS-1$
147
148 IEngine engine = agent.getService(IEngine.class);
149 if (engine == null)
150 throw new ProvisionException("No director service found."); //$NON-NLS-1$
151 ProvisioningContext context = new ProvisioningContext(agent);
152 IProfileChangeRequest request = planner.createChangeRequest(profile);
153 request.addAll(units.toUnmodifiableSet());
154 IProvisioningPlan result = planner.getProvisioningPlan(request, context, progress);
155 return PlanExecutionHelper.executePlan(result, engine, context, progress);
156 }
157
158 static List<URI> getArtifactRepositories(IProvisioningAgent agent) {
159 return getRepositories(IArtifactRepositoryManager.class, agent);
160 }
161
162 static IArtifactRepository getArtifactRepository(IProvisioningAgent agent, URI repoURL) {
163 return (IArtifactRepository) getRepository(IArtifactRepositoryManager.class, agent, repoURL);
164 }
165
166 static long[] getProfileTimestamps(IProvisioningAgent agent, String profileId) {
167 if (profileId == null) {
168 profileId = IProfileRegistry.SELF;
169 }
170 IProfileRegistry profileRegistry = agent.getService(IProfileRegistry.class);
171 if (profileRegistry == null)
172 return null;
173 return profileRegistry.listProfileTimestamps(profileId);
174 }
175
176 static IStatus revertToPreviousState(IProvisioningAgent agent, IProfile profile, long revertToPreviousState) throws ProvisionException {
177 IEngine engine = agent.getService(IEngine.class);
178 if (engine == null)
179 throw new ProvisionException("No p2 engine found."); //$NON-NLS-1$
180 IPlanner planner = agent.getService(IPlanner.class);
181 if (planner == null)
182 throw new ProvisionException("No planner found."); //$NON-NLS-1$
183 IProfileRegistry profileRegistry = agent.getService(IProfileRegistry.class);
184 if (profileRegistry == null)
185 throw new ProvisionException("profile registry cannot be null"); //$NON-NLS-1$
186 // If given profile is null, then get/use the self profile
187 if (profile == null) {
188 profile = getProfile(agent, IProfileRegistry.SELF);
189 }
190 IProfile targetProfile = null;
191 if (revertToPreviousState == 0) {
192 long[] profiles = profileRegistry.listProfileTimestamps(profile.getProfileId());
193 if (profiles.length == 0)
194 // Nothing to do, as the profile does not have any previous timestamps
195 return Status.OK_STATUS;
196 targetProfile = profileRegistry.getProfile(profile.getProfileId(), profiles[profiles.length - 1]);
197 } else {
198 targetProfile = profileRegistry.getProfile(profile.getProfileId(), revertToPreviousState);
199 }
200 if (targetProfile == null)
201 throw new ProvisionException("target profile with timestamp=" + revertToPreviousState + " not found"); //$NON-NLS-1$//$NON-NLS-2$
202 URI[] artifactRepos = getArtifactRepositories(agent).toArray(URI[]::new);
203 URI[] metadataRepos = getMetadataRepositories(agent).toArray(URI[]::new);
204 IProvisioningPlan plan = planner.getDiffPlan(profile, targetProfile, new NullProgressMonitor());
205 ProvisioningContext context = new ProvisioningContext(agent);
206 context.setMetadataRepositories(metadataRepos);
207 context.setArtifactRepositories(artifactRepos);
208 return PlanExecutionHelper.executePlan(plan, engine, context, new NullProgressMonitor());
209 }
210
211 /**
212 * Install the described IU
213 */
214 static IStatus uninstall(IProvisioningAgent agent, String unitId, String version, IProfile profile, IProgressMonitor progress) throws ProvisionException {
215 if (profile == null)
216 return null;
217 IQueryResult<IInstallableUnit> units = profile.query(QueryUtil.createIUQuery(unitId, Version.create(version)), progress);
218 if (units.isEmpty()) {
219 StringBuilder error = new StringBuilder();
220 error.append("Installable unit not found: " + unitId + ' ' + version + '\n'); //$NON-NLS-1$
221 error.append("Repositories searched:\n"); //$NON-NLS-1$
222 for (URI repo : getMetadataRepositories(agent)) {
223 error.append(repo + "\n"); //$NON-NLS-1$
224 }
225 throw new ProvisionException(error.toString());
226 }
227
228 IPlanner planner = agent.getService(IPlanner.class);
229 if (planner == null)
230 throw new ProvisionException("No planner service found."); //$NON-NLS-1$
231
232 IEngine engine = agent.getService(IEngine.class);
233 if (engine == null)
234 throw new ProvisionException("No engine service found."); //$NON-NLS-1$
235 ProvisioningContext context = new ProvisioningContext(agent);
236 IProfileChangeRequest request = planner.createChangeRequest(profile);
237 request.removeAll(units.toUnmodifiableSet());
238 IProvisioningPlan result = planner.getProvisioningPlan(request, context, progress);
239 return PlanExecutionHelper.executePlan(result, engine, context, progress);
240 }
241
242 private static <T> IRepository<T> addRepository(Class<? extends IRepositoryManager<T>> repositoryManager,
243 IProvisioningAgent agent, URI location, String nameSuffix, String repoType) {
244 IRepositoryManager<T> manager = getRepositoryManager(agent, repositoryManager);
245 try {
246 return manager.loadRepository(location, null);
247 } catch (ProvisionException e) {
248 // fall through and create a new repository
249 boolean createRepo = "file".equals(location.getScheme()); //$NON-NLS-1$
250 if (!createRepo) {
251 return null;
252 }
253 }
254 // could not load a repo at that location so create one as a convenience
255 String repositoryName = location + " - " + nameSuffix; //$NON-NLS-1$
256 try {
257 return manager.createRepository(location, repositoryName, repoType, null);
258 } catch (ProvisionException e) {
259 return null;
260 }
261 }
262
263 private static <T> void removeRepository(Class<? extends IRepositoryManager<T>> repositoryManager,
264 IProvisioningAgent agent, URI location) {
265 IRepositoryManager<T> manager = getRepositoryManager(agent, repositoryManager);
266 manager.removeRepository(location);
267 }
268
269 private static <T> IRepository<T> getRepository(Class<? extends IRepositoryManager<T>> repositoryManager,
270 IProvisioningAgent agent, URI location) {
271 IRepositoryManager<T> manager = getRepositoryManager(agent, repositoryManager);
272 try {
273 return manager.loadRepository(location, null);
274 } catch (ProvisionException e) {
275 // for console, just ignore repositories that can't be read
276 return null;
277 }
278 }
279
280 private static <T> List<URI> getRepositories(Class<? extends IRepositoryManager<T>> repositoryManager,
281 IProvisioningAgent agent) {
282 IRepositoryManager<T> manager = getRepositoryManager(agent, repositoryManager);
283 URI[] repos = manager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
284 return Arrays.asList(repos);
285 }
286
287 private static <T> IRepositoryManager<T> getRepositoryManager(IProvisioningAgent agent,
288 Class<? extends IRepositoryManager<T>> repositoryManager) {
289 IRepositoryManager<T> manager = agent.getService(repositoryManager);
290 if (manager == null) {
291 throw new IllegalStateException("No repository manager found for type " + repositoryManager); //$NON-NLS-1$
292 }
293 return manager;
294 }
295 }