Debian Codegraph @

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

raw
1 /*******************************************************************************
2 * Copyright (c) 2011, 2017 Sonatype, Inc. 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 * Sonatype, Inc. - initial API and implementation
13 * IBM Corporation - Ongoing development
14 *******************************************************************************/
15 package org.eclipse.equinox.p2.operations;
16
17 import java.net.URI;
18 import java.util.*;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.equinox.internal.p2.operations.Constants;
21 import org.eclipse.equinox.internal.p2.operations.Messages;
22 import org.eclipse.equinox.p2.core.IProvisioningAgent;
23 import org.eclipse.equinox.p2.core.ProvisionException;
24 import org.eclipse.equinox.p2.engine.*;
25 import org.eclipse.equinox.p2.engine.query.UserVisibleRootQuery;
26 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
27 import org.eclipse.equinox.p2.metadata.IVersionedId;
28 import org.eclipse.equinox.p2.query.*;
29 import org.eclipse.osgi.util.NLS;
30 import org.osgi.framework.*;
31
32 /**
33 * OperationFactory provides a set of helpers to simplify dealing with the running installation.
34 * Among other things, it simplifies the installation, un-installation and update.
35 * If the system you are trying to modify is not the running one, you need to directly use the various subclass of {@link ProfileChangeOperation}.
36 * @since 2.1
37 */
38 public class OperationFactory {
39
40 private IProvisioningAgent getAgent() {
41 Collection<ServiceReference<IProvisioningAgent>> ref = null;
42 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
43 try {
44 ref = bundleContext.getServiceReferences(IProvisioningAgent.class, '(' + IProvisioningAgent.SERVICE_CURRENT + '=' + Boolean.TRUE.toString() + ')');
45 } catch (InvalidSyntaxException e) {
46 //ignore can't happen since we write the filter ourselves
47 }
48 if (ref == null || ref.size() == 0)
49 throw new IllegalStateException(Messages.OperationFactory_noAgent);
50 IProvisioningAgent agent = bundleContext.getService(ref.iterator().next());
51 bundleContext.ungetService(ref.iterator().next());
52 return agent;
53 }
54
55 //Return a list of IUs from the list of versionedIDs originally provided
56 private Collection<IInstallableUnit> gatherIUs(IQueryable<IInstallableUnit> searchContext, Collection<? extends IVersionedId> ius, boolean checkIUs, IProgressMonitor monitor) throws ProvisionException {
57 Collection<IInstallableUnit> gatheredIUs = new ArrayList<>(ius.size());
58
59 for (IVersionedId versionedId : ius) {
60 if (!checkIUs && versionedId instanceof IInstallableUnit) {
61 gatheredIUs.add((IInstallableUnit) versionedId);
62 continue;
63 }
64
65 IQuery<IInstallableUnit> installableUnits = QueryUtil.createIUQuery(versionedId.getId(), versionedId.getVersion());
66 IQueryResult<IInstallableUnit> matches = searchContext.query(installableUnits, monitor);
67 if (matches.isEmpty())
68 throw new ProvisionException(new Status(IStatus.ERROR, Constants.BUNDLE_ID, NLS.bind(Messages.OperationFactory_noIUFound, versionedId)));
69
70 //Add the first IU
71 Iterator<IInstallableUnit> iuIt = matches.iterator();
72 gatheredIUs.add(iuIt.next());
73 }
74 return gatheredIUs;
75 }
76
77 private ProvisioningContext createProvisioningContext(Collection<URI> repos, IProvisioningAgent agent) {
78 ProvisioningContext ctx = new ProvisioningContext(agent);
79 if (repos != null) {
80 ctx.setMetadataRepositories(repos.toArray(new URI[repos.size()]));
81 ctx.setArtifactRepositories(repos.toArray(new URI[repos.size()]));
82 }
83 return ctx;
84 }
85
86 /**
87 * This factory method creates an {@link InstallOperation} to install all the elements listed from the specified repositories.
88 * @param toInstall the elements to install. This can not be null.
89 * @param repos the repositories to install the elements from. If null is passed, it will use all previously registered repositories.
90 * @param monitor the progress monitor
91 * @return an operation to install
92 */
93 public InstallOperation createInstallOperation(Collection<? extends IVersionedId> toInstall, Collection<URI> repos, IProgressMonitor monitor) throws ProvisionException {
94 Assert.isNotNull(toInstall);
95 IProvisioningAgent agent = getAgent();
96
97 //add the repos
98 ProvisioningContext ctx = createProvisioningContext(repos, agent);
99
100 //find the ius to install and create the operation
101 InstallOperation resultingOperation = new InstallOperation(new ProvisioningSession(agent), gatherIUs(ctx.getMetadata(monitor), toInstall, false, monitor));
102 resultingOperation.setProvisioningContext(ctx);
103 resultingOperation.setProfileId(IProfileRegistry.SELF);
104
105 return resultingOperation;
106 }
107
108 /**
109 * Create an {@link UninstallOperation} that will uninstall the listed elements from the running instance.
110 * @param toUninstall the elements to uninstall. This can not be null.
111 * @param repos the repositories to install the elements from. If null is passed, it will use all previously registered repositories.
112 * @param monitor the progress monitor
113 * @return an operation to uninstall
114 */
115 public UninstallOperation createUninstallOperation(Collection<? extends IVersionedId> toUninstall, Collection<URI> repos, IProgressMonitor monitor) throws ProvisionException {
116 Assert.isNotNull(toUninstall);
117 IProvisioningAgent agent = getAgent();
118 ProvisioningContext ctx = createProvisioningContext(repos, agent);
119
120 //find the ius to uninstall and create the operation
121 UninstallOperation resultingOperation = new UninstallOperation(new ProvisioningSession(agent), gatherIUs(listInstalledElements(false, monitor), toUninstall, true, monitor));
122 resultingOperation.setProvisioningContext(ctx);
123 resultingOperation.setProfileId(IProfileRegistry.SELF);
124
125 return resultingOperation;
126 }
127
128 /**
129 * Returns the {@link IInstallableUnit}s that are installed in the running instance of Eclipse.
130 *
131 * @param rootsOnly set to true to return only the elements that have been explicitly installed (aka roots).
132 * @param monitor the progress monitor
133 * @return the installable units installed, or an empty result if the installation profile of the running system
134 * cannot be accessed
135 */
136 public IQueryResult<IInstallableUnit> listInstalledElements(boolean rootsOnly, IProgressMonitor monitor) {
137 IProfileRegistry registry = getAgent().getService(IProfileRegistry.class);
138 IProfile profile = registry.getProfile(IProfileRegistry.SELF);
139 if (profile == null)
140 return new CollectionResult<>(null);
141 if (rootsOnly)
142 return profile.query(new UserVisibleRootQuery(), monitor);
143 return profile.query(QueryUtil.ALL_UNITS, monitor);
144 }
145
146 /**
147 * Create an {@link UpdateOperation} that will update the elements specified.
148 * @param toUpdate The elements to update.Passing null will result in looking for an update to all the installed. Note that you can pass the results of {@link OperationFactory#listInstalledElements(boolean, IProgressMonitor)} to this
149 * method if you wish to update all elements installed in the running instance of eclipse.
150 * @param repos the repositories to update the elements from. If null is passed, it will use all previously registered repositories.
151 * @param monitor the progress monitor
152 * @return an instance of {@link UpdateOperation}
153 */
154 public UpdateOperation createUpdateOperation(Collection<? extends IVersionedId> toUpdate, Collection<URI> repos, IProgressMonitor monitor) throws ProvisionException {
155 IProvisioningAgent agent = getAgent();
156 ProvisioningContext ctx = createProvisioningContext(repos, agent);
157
158 //find the ius to update and create the operation
159 UpdateOperation resultingOperation = new UpdateOperation(new ProvisioningSession(agent), toUpdate == null ? null : gatherIUs(listInstalledElements(false, monitor), toUpdate, false, monitor));
160 resultingOperation.setProvisioningContext(ctx);
161 resultingOperation.setProfileId(IProfileRegistry.SELF);
162
163 return resultingOperation;
164 }
165
166 /**
167 * This factory method creates an {@link SynchronizeOperation} that will cause the current installation to exclusively contain the elements listed once executed.
168 * @param toInstall the elements to install. This can not be null.
169 * @param repos the repositories to install the elements from. If null is passed, it will use all previously registered repositories.
170 * @param monitor the progress monitor
171 * @return an instance of {@link SynchronizeOperation}.
172 */
173 public SynchronizeOperation createSynchronizeOperation(Collection<? extends IVersionedId> toInstall, Collection<URI> repos, IProgressMonitor monitor) throws ProvisionException {
174 IProvisioningAgent agent = getAgent();
175 ProvisioningContext ctx = createProvisioningContext(repos, agent);
176
177 Collection<IInstallableUnit> iusToInstall;
178 if (toInstall == null)
179 iusToInstall = ctx.getMetadata(monitor).query(QueryUtil.createIUGroupQuery(), monitor).toUnmodifiableSet();
180 else
181 iusToInstall = gatherIUs(ctx.getMetadata(monitor), toInstall, false, monitor);
182
183 SynchronizeOperation resultingOperation = new SynchronizeOperation(new ProvisioningSession(agent), iusToInstall);
184 resultingOperation.setProvisioningContext(ctx);
185 resultingOperation.setProfileId(IProfileRegistry.SELF);
186
187 return resultingOperation;
188 }
189 }