Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 337016 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/equinox/p2/operations/AbsoluteInstallOperation.java (+69 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2011 Sonatype, Inc. and others.
3
 *  All rights reserved. This program and the accompanying materials
4
 *  are made available under the terms of the Eclipse Public License v1.0
5
 *  which accompanies this distribution, and is available at
6
 *  http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 *  Contributors:
9
 *     Sonatype, Inc. - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.equinox.p2.operations;
12
13
import java.util.Collection;
14
import org.eclipse.core.runtime.*;
15
import org.eclipse.equinox.internal.p2.director.ProfileChangeRequest;
16
import org.eclipse.equinox.internal.p2.operations.Messages;
17
import org.eclipse.equinox.p2.engine.IProfile;
18
import org.eclipse.equinox.p2.engine.query.UserVisibleRootQuery;
19
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
20
import org.eclipse.equinox.p2.planner.ProfileInclusionRules;
21
import org.eclipse.equinox.p2.query.QueryUtil;
22
23
/**
24
 * A {@link AbsoluteInstallOperation} describes an operation that will modify the installation to 
25
 * exclusively include the InstallableUnit mentioned. Note that all the Installable Units necessary
26
 *  to satisfy the dependencies of the Installable Units installed will also be installed.  
27
 * 
28
 * The following snippet shows how one might use an AbsoluteInstallOperation to perform a synchronous resolution and
29
 * then kick off an install in the background:
30
 * 
31
 * <pre>
32
 * AbsoluteInstallOperation op = new AbsoluteInstallOperation(session, new IInstallableUnit [] { myIU });
33
 * IStatus result = op.resolveModal(monitor);
34
 * if (result.isOK()) {
35
 *   op.getProvisioningJob(monitor).schedule();
36
 * }
37
 * </pre>
38
 * 
39
 * @since 2.1
40
 * @see ProfileChangeOperation
41
 * @noextend This class is not intended to be subclassed by clients.
42
 */
43
public class AbsoluteInstallOperation extends InstallOperation {
44
45
	public AbsoluteInstallOperation(ProvisioningSession session, Collection<IInstallableUnit> toInstall) {
46
		super(session, toInstall);
47
	}
48
49
	/*
50
	 * (non-Javadoc)
51
	 * @see org.eclipse.equinox.p2.operations.ProfileChangeOperation#computeProfileChangeRequest(org.eclipse.core.runtime.MultiStatus, org.eclipse.core.runtime.IProgressMonitor)
52
	 */
53
	protected void computeProfileChangeRequest(MultiStatus status, IProgressMonitor monitor) {
54
		request = ProfileChangeRequest.createByProfileId(session.getProvisioningAgent(), profileId);
55
		IProfile profile;
56
		SubMonitor sub = SubMonitor.convert(monitor, Messages.InstallOperation_ComputeProfileChangeProgress, toInstall.size());
57
		profile = session.getProfileRegistry().getProfile(profileId);
58
		request.removeAll(profile.query(new UserVisibleRootQuery(), sub).toUnmodifiableSet());
59
		request.addAll(toInstall);
60
		for (IInstallableUnit entryToInstall : toInstall) {
61
			// If the user is installing a patch, we mark it optional.  This allows the patched IU to be updated later by removing the patch.
62
			if (QueryUtil.isPatch(entryToInstall))
63
				request.setInstallableUnitInclusionRules(entryToInstall, ProfileInclusionRules.createOptionalInclusionRule(entryToInstall));
64
65
			sub.worked(1);
66
		}
67
		sub.done();
68
	}
69
}
(-)src/org/eclipse/equinox/p2/operations/InstallOperation.java (-1 / +1 lines)
Lines 46-52 Link Here
46
 */
46
 */
47
public class InstallOperation extends ProfileChangeOperation {
47
public class InstallOperation extends ProfileChangeOperation {
48
48
49
	private Collection<IInstallableUnit> toInstall;
49
	protected Collection<IInstallableUnit> toInstall;
50
50
51
	/**
51
	/**
52
	 * Create an install operation on the specified provisioning session that installs
52
	 * Create an install operation on the specified provisioning session that installs
(-)src/org/eclipse/equinox/p2/operations/OperationHelper.java (+177 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2011 Sonatype, Inc. and others.
3
 *  All rights reserved. This program and the accompanying materials
4
 *  are made available under the terms of the Eclipse Public License v1.0
5
 *  which accompanies this distribution, and is available at
6
 *  http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 *  Contributors:
9
 *     Sonatype, Inc. - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.equinox.p2.operations;
12
13
import java.net.URI;
14
import java.util.*;
15
import org.eclipse.core.runtime.Assert;
16
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.equinox.internal.p2.operations.Activator;
18
import org.eclipse.equinox.p2.core.IProvisioningAgent;
19
import org.eclipse.equinox.p2.engine.*;
20
import org.eclipse.equinox.p2.engine.query.UserVisibleRootQuery;
21
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
22
import org.eclipse.equinox.p2.metadata.IVersionedId;
23
import org.eclipse.equinox.p2.query.*;
24
import org.osgi.framework.InvalidSyntaxException;
25
import org.osgi.framework.ServiceReference;
26
27
/**
28
 * OperationHelper provides a set of helpers to simplify dealing with the running installation.
29
 * Among other things, it simplifies the installation, un-installation and update.
30
 * If the system you are trying to modify is not the running one, you need to directly use the various subclass of {@link ProfileChangeOperation}.
31
 * @since 2.1  
32
 */
33
public class OperationHelper {
34
	private IProvisioningAgent getAgent() {
35
		Collection<ServiceReference<IProvisioningAgent>> ref = null;
36
		try {
37
			ref = Activator.getContext().getServiceReferences(IProvisioningAgent.class, '(' + IProvisioningAgent.SERVICE_CURRENT + '=' + Boolean.TRUE.toString() + ')');
38
		} catch (InvalidSyntaxException e) {
39
			//ignore can't happen since we write the filter ourselves
40
		}
41
		if (ref == null || ref.size() == 0)
42
			throw new IllegalStateException("p2 is not configured properly. No provisioning agent could be found.");
43
		IProvisioningAgent agent = Activator.getContext().getService(ref.iterator().next());
44
		Activator.getContext().ungetService(ref.iterator().next());
45
		return agent;
46
	}
47
48
	private Collection<IInstallableUnit> gatherIUs(ProvisioningContext context, Collection<IVersionedId> ius, IProgressMonitor monitor) {
49
		Collection<IInstallableUnit> unitsToInstall = new ArrayList<IInstallableUnit>(ius.size());
50
51
		for (IVersionedId versionedId : ius) {
52
			if (ius instanceof IInstallableUnit) {
53
				unitsToInstall.add((IInstallableUnit) versionedId);
54
				continue;
55
			}
56
57
			IQuery<IInstallableUnit> installableUnits = QueryUtil.createIUQuery(versionedId.getId(), versionedId.getVersion());
58
			IQueryResult<IInstallableUnit> matches = context.getMetadata(monitor).query(installableUnits, monitor);
59
			//			if (ius.isEmpty())
60
			//				Do something smart. like throw an exception
61
			//Add the first IU
62
			Iterator<IInstallableUnit> iuIt = matches.iterator();
63
			unitsToInstall.add(iuIt.next());
64
65
			if (iuIt.hasNext())
66
				System.out.println("Log the fact that we have a problem");
67
		}
68
		return unitsToInstall;
69
	}
70
71
	private ProvisioningContext createProvisioningContext(Collection<URI> repos, IProvisioningAgent agent) {
72
		ProvisioningContext ctx = new ProvisioningContext(agent);
73
		if (repos != null) {
74
			ctx.setMetadataRepositories(repos.toArray(new URI[repos.size()]));
75
			ctx.setArtifactRepositories(repos.toArray(new URI[repos.size()]));
76
		}
77
		return ctx;
78
	}
79
80
	/**
81
	 * This factory method creates an {@link InstallOperation} to install all the elements listed from the specified repositories. 
82
	 * @param toInstall the elements to install. This can not be null.
83
	 * @param repos the repositories to install the elements from. 
84
	 * @param monitor the progress monitor
85
	 * @return an operation to install
86
	 */
87
	public InstallOperation createInstallOperation(Collection<IVersionedId> toInstall, Collection<URI> repos, IProgressMonitor monitor) {
88
		Assert.isNotNull(toInstall);
89
		IProvisioningAgent agent = getAgent();
90
		//add the repos
91
		ProvisioningContext ctx = createProvisioningContext(repos, agent);
92
93
		//find the ius to install and create the operation
94
		InstallOperation resultingOperation = new InstallOperation(new ProvisioningSession(agent), gatherIUs(ctx, toInstall, monitor));
95
		resultingOperation.setProvisioningContext(ctx);
96
		resultingOperation.setProfileId(IProfileRegistry.SELF);
97
98
		return resultingOperation;
99
	}
100
101
	/**
102
	 * Create an {@link UninstallOperation} that will uninstall the listed elements from the running instance. 
103
	 * @param toUninstall the elements to uninstall. This can not be null.
104
	 * @param repos the repositories to install the elements from. Passing null will 
105
	 * @param monitor the progress monitor
106
	 * @return an operation to uninstall
107
	 */
108
	public UninstallOperation createUninstallOperation(Collection<IVersionedId> toUninstall, Collection<URI> repos, IProgressMonitor monitor) {
109
		Assert.isNotNull(toUninstall);
110
		IProvisioningAgent agent = getAgent();
111
		ProvisioningContext ctx = createProvisioningContext(repos, agent);
112
113
		//find the ius to uninstall and create the operation
114
		UninstallOperation resultingOperation = new UninstallOperation(new ProvisioningSession(agent), gatherIUs(ctx, toUninstall, monitor));
115
		resultingOperation.setProvisioningContext(ctx);
116
		resultingOperation.setProfileId(IProfileRegistry.SELF);
117
118
		return resultingOperation;
119
	}
120
121
	/**
122
	 * Return the {@link IInstallableUnit} that are installed in the running instance of eclipse.
123
	 * @param rootsOnly set to true to return only the elements that have been explicitly installed (aka roots).
124
	 * @param monitor the progress monitor
125
	 * @return the installable units installed
126
	 */
127
	public IQueryResult<IInstallableUnit> listInstalledElements(boolean rootsOnly, IProgressMonitor monitor) {
128
		IProfileRegistry registry = (IProfileRegistry) getAgent().getService(IProfileRegistry.SERVICE_NAME);
129
		IProfile profile = registry.getProfile(IProfileRegistry.SELF);
130
		if (rootsOnly)
131
			return profile.query(new UserVisibleRootQuery(), monitor);
132
		return profile.query(QueryUtil.ALL_UNITS, monitor);
133
	}
134
135
	/**
136
	 * Create an {@link UpdateOperation} that will update the elements specified.
137
	 * @param toUpdate
138
	 * @param repos
139
	 * @param monitor
140
	 * @return an instance of {@link UpdateOperation}
141
	 */
142
	public UpdateOperation createUpdateOperation(Collection<IVersionedId> toUpdate, Collection<URI> repos, IProgressMonitor monitor) {
143
		IProvisioningAgent agent = getAgent();
144
		ProvisioningContext ctx = createProvisioningContext(repos, agent);
145
146
		//find the ius to update and create the operation
147
		UpdateOperation resultingOperation = new UpdateOperation(new ProvisioningSession(agent), toUpdate == null ? null : gatherIUs(ctx, toUpdate, monitor));
148
		resultingOperation.setProvisioningContext(ctx);
149
		resultingOperation.setProfileId(IProfileRegistry.SELF);
150
151
		return resultingOperation;
152
	}
153
154
	/**
155
	 * This factory method creates an {@link AbsolutInstallOperation} that will cause the current installation to exclusively contain the elements listed once executed.
156
	 * @param toInstall the elements to install. This can not be null.
157
	 * @param repos the repositories to install the elements from. 
158
	 * @param monitor the progress monitor
159
	 * @return an instance of {@link InstallOperation}.
160
	 */
161
	public InstallOperation createAbsoluteInstallOperation(Collection<IVersionedId> toInstall, Collection<URI> repos, IProgressMonitor monitor) {
162
		IProvisioningAgent agent = getAgent();
163
		ProvisioningContext ctx = createProvisioningContext(repos, agent);
164
165
		Collection<IInstallableUnit> iusToInstall;
166
		if (toInstall == null)
167
			iusToInstall = ctx.getMetadata(monitor).query(QueryUtil.createIUGroupQuery(), monitor).toUnmodifiableSet();
168
		else
169
			iusToInstall = gatherIUs(ctx, toInstall, monitor);
170
171
		AbsoluteInstallOperation resultingOperation = new AbsoluteInstallOperation(new ProvisioningSession(agent), iusToInstall);
172
		resultingOperation.setProvisioningContext(ctx);
173
		resultingOperation.setProfileId(IProfileRegistry.SELF);
174
175
		return resultingOperation;
176
	}
177
}

Return to bug 337016